followMany(Obj fromObj) {
51 | return Stream.of(fromObj);
52 | }
53 |
54 | @Override
55 | public boolean testRelation(Obj fromObj, Obj toObj) {
56 | return fromObj == toObj;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/fields/src/main/java/network/aika/utils/ApproximateComparisonValueUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.utils;
18 |
19 |
20 | /**
21 | * @author Lukas Molzberger
22 | *
23 | */
24 | public class ApproximateComparisonValueUtil {
25 |
26 | public static final double PRECISION = 1000.0;
27 |
28 | public static int convert(double newSortValue) {
29 | return (int) (PRECISION * newSortValue);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/fields/src/main/java/network/aika/utils/ArrayUtils.java:
--------------------------------------------------------------------------------
1 | package network.aika.utils;
2 |
3 | public class ArrayUtils {
4 |
5 | public static boolean isAllNull(Object[] array) {
6 | for (Object element : array) {
7 | if (element != null) {
8 | return false; // As soon as a non-null element is found, return false
9 | }
10 | }
11 | return true; // If no non-null element is found, return true
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/fields/src/main/java/network/aika/utils/FieldWritable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.utils;
18 |
19 | import java.io.DataInput;
20 | import java.io.DataOutput;
21 | import java.io.IOException;
22 |
23 | /**
24 | *
25 | * @author Lukas Molzberger
26 | */
27 | public interface FieldWritable {
28 |
29 | /**
30 | * Serialize the fields of this object to out
.
31 | *
32 | * @param out DataOuput
to serialize this object into.
33 | * @throws IOException
34 | */
35 | void write(DataOutput out) throws IOException;
36 |
37 | /**
38 | * Deserialize the fields of this object from in
.
39 | *
40 | * For efficiency, implementations should attempt to re-use storage in the
41 | * existing object where possible.
42 | *
43 | * @param in DataInput
to deseriablize this object from.
44 | * @throws Exception
45 | */
46 | void readFields(DataInput in) throws Exception;
47 |
48 | }
--------------------------------------------------------------------------------
/fields/src/main/java/network/aika/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.utils;
18 |
19 | import java.math.RoundingMode;
20 | import java.text.DecimalFormat;
21 | import java.text.DecimalFormatSymbols;
22 | import java.util.Locale;
23 |
24 | import static network.aika.queue.keys.QueueKey.MAX_ROUND;
25 |
26 | /**
27 | *
28 | * @author Lukas Molzberger
29 | */
30 | public class StringUtils {
31 |
32 | public static double TOLERANCE = 0.001;
33 |
34 |
35 | public static String doubleToString(Double x) {
36 | if(x == null)
37 | return "--";
38 | return doubleToString(x, "#.######");
39 | }
40 |
41 | public static String floatToString(Float d, String format) {
42 | if(d == null)
43 | return "--";
44 | DecimalFormat formatter = new DecimalFormat(format, DecimalFormatSymbols.getInstance(Locale.ENGLISH));
45 | formatter.setRoundingMode( RoundingMode.DOWN );
46 | return formatter.format(d);
47 | }
48 |
49 | public static String doubleToString(Double d, String format) {
50 | if(d == null)
51 | return "--";
52 | DecimalFormat formatter = new DecimalFormat(format, DecimalFormatSymbols.getInstance(Locale.ENGLISH));
53 | formatter.setRoundingMode( RoundingMode.DOWN );
54 | return formatter.format(d);
55 | }
56 |
57 | public static String roundToString(int r) {
58 | return r == MAX_ROUND ? "MAX" : "" + r;
59 | }
60 |
61 | public static String depthToSpace(int depth) {
62 | StringBuilder sb = new StringBuilder();
63 | for(int i = 0; i < depth; i++)
64 | sb.append(" ");
65 | return sb.toString();
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/fields/src/main/java/network/aika/utils/ToleranceUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.utils;
18 |
19 | /**
20 | *
21 | * @author Lukas Molzberger
22 | */
23 | public class ToleranceUtils {
24 |
25 | public static double TOLERANCE = 0.001;
26 |
27 |
28 | public static double sum(double[] a) {
29 | double sum = 0;
30 | for(int i = 0; i < a.length; i++)
31 | sum += a[i];
32 | return sum;
33 | }
34 |
35 | public static boolean belowTolerance(Double tolerance, double[] x) {
36 | if(x == null)
37 | return true;
38 |
39 | if(tolerance == null)
40 | return false;
41 |
42 | return Math.abs(sum(x)) < tolerance;
43 | }
44 |
45 | public static boolean belowTolerance(Double tolerance, double x) {
46 | if(x == 0.0)
47 | return true;
48 |
49 | if(tolerance == null)
50 | return false;
51 |
52 | return Math.abs(x) < tolerance;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/fields/src/main/java/network/aika/utils/Writable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.utils;
18 |
19 | import java.io.DataInput;
20 | import java.io.DataOutput;
21 | import java.io.IOException;
22 |
23 |
24 | public interface Writable {
25 |
26 | /**
27 | * Serialize the fields of this object to out
.
28 | *
29 | * @param out DataOuput
to serialize this object into.
30 | * @throws IOException
31 | */
32 | void write(DataOutput out) throws IOException;
33 |
34 | /**
35 | * Deserialize the fields of this object from in
.
36 | *
37 | * For efficiency, implementations should attempt to re-use storage in the
38 | * existing object where possible.
39 | *
40 | * @param in DataInput
to deseriablize this object from.
41 | * @throws Exception
42 | */
43 | void readFields(DataInput in, M m) throws Exception;
44 |
45 | }
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/manyobjects/OneToManyRelationTest.java:
--------------------------------------------------------------------------------
1 | package network.aika.fields.manyobjects;
2 |
3 | import network.aika.fields.defs.FieldDefinition;
4 | import network.aika.type.TypeRegistry;
5 | import network.aika.type.TypeRegistryImpl;
6 | import org.junit.jupiter.api.Assertions;
7 | import org.junit.jupiter.api.BeforeEach;
8 | import org.junit.jupiter.api.Test;
9 |
10 | import static network.aika.fields.InputField.inputField;
11 | import static network.aika.fields.SumField.sum;
12 | import static network.aika.fields.manyobjects.TestObjectMany.linkObjects;
13 | import static network.aika.fields.manyobjects.TestTypeMany.TEST_RELATION_FROM;
14 |
15 |
16 | public class OneToManyRelationTest {
17 |
18 | protected TypeRegistry registry;
19 | protected TestTypeOne typeA;
20 | protected TestTypeMany typeB;
21 |
22 |
23 | @BeforeEach
24 | public void init() {
25 | registry = new TypeRegistryImpl();
26 |
27 | typeA = new TestTypeOne(registry, "A");
28 |
29 | typeB = new TestTypeMany(registry, "B");
30 | }
31 |
32 | @Test
33 | public void testInitFields() {
34 |
35 | FieldDefinition fieldA = inputField(typeA, "a");
36 | FieldDefinition fieldB = inputField(typeA, "b");
37 |
38 | FieldDefinition fieldC = sum(typeB, "b")
39 | .in(TEST_RELATION_FROM, fieldA)
40 | .in(TEST_RELATION_FROM, fieldB);
41 |
42 | registry.flattenTypeHierarchy();
43 |
44 | // Object and Field initialization
45 |
46 | TestObjectOne objA = new TestObjectOne(typeA);
47 | objA.setFieldValue(fieldA, 5.0);
48 | objA.setFieldValue(fieldB, 5.0);
49 |
50 | TestObjectMany objB = new TestObjectMany(typeB);
51 |
52 | linkObjects(objA, objB);
53 | objB.initFields();
54 |
55 | Assertions.assertEquals(
56 | 10.0,
57 | objB.getFieldOutput(fieldC).getValue()
58 | );
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/manyobjects/TestObjectMany.java:
--------------------------------------------------------------------------------
1 | package network.aika.fields.manyobjects;
2 |
3 | import network.aika.type.Obj;
4 | import network.aika.type.ObjImpl;
5 | import network.aika.type.TypeRegistry;
6 | import network.aika.type.relations.Relation;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 | import java.util.stream.Stream;
11 |
12 | public class TestObjectMany extends ObjImpl {
13 |
14 | List relatedTestObjects = new ArrayList<>();
15 |
16 | public TestObjectMany(TestTypeMany type) {
17 | super(type);
18 | }
19 |
20 | public Stream getRelatedTestObjects() {
21 | return relatedTestObjects.stream();
22 | }
23 |
24 | public static void linkObjects(TestObjectOne objA, TestObjectMany objB) {
25 | objA.relatedTestObject = objB;
26 | objB.relatedTestObjects.add(objA);
27 | }
28 |
29 | @Override
30 | public Stream followManyRelation(Relation rel) {
31 | return relatedTestObjects.stream();
32 | }
33 |
34 | @Override
35 | public Obj followSingleRelation(Relation rel) {
36 | return null;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/manyobjects/TestObjectOne.java:
--------------------------------------------------------------------------------
1 | package network.aika.fields.manyobjects;
2 |
3 | import network.aika.type.ObjImpl;
4 | import network.aika.type.TypeRegistry;
5 |
6 |
7 | public class TestObjectOne extends ObjImpl {
8 |
9 | TestObjectMany relatedTestObject;
10 |
11 | public TestObjectOne(TestTypeOne type) {
12 | super(type);
13 | }
14 |
15 | public TestObjectMany getRelatedTestObject() {
16 | return relatedTestObject;
17 | }
18 |
19 | public static void linkObjects(TestObjectMany objA, TestObjectOne objB) {
20 | objA.relatedTestObjects.add(objB);
21 | objB.relatedTestObject = objA;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/manyobjects/TestTypeMany.java:
--------------------------------------------------------------------------------
1 | package network.aika.fields.manyobjects;
2 |
3 | import network.aika.type.Type;
4 | import network.aika.type.TypeRegistry;
5 | import network.aika.type.relations.Relation;
6 | import network.aika.type.relations.RelationMany;
7 | import network.aika.type.relations.RelationOne;
8 |
9 | import java.util.List;
10 |
11 | import static network.aika.fields.manyobjects.TestTypeOne.TEST_RELATION_TO;
12 |
13 |
14 | public class TestTypeMany extends Type {
15 |
16 | public static RelationMany TEST_RELATION_FROM = new RelationMany(0, "TEST_FROM");
17 |
18 | static {
19 | TEST_RELATION_FROM.setReversed(TEST_RELATION_TO);
20 | }
21 |
22 | public TestTypeMany(TypeRegistry registry, String name) {
23 | super(registry, name);
24 | }
25 |
26 | @Override
27 | public Relation[] getRelations() {
28 | return new Relation[] {TEST_RELATION_FROM};
29 | }
30 |
31 | public TestObjectMany instantiate() {
32 | return new TestObjectMany(this);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/manyobjects/TestTypeOne.java:
--------------------------------------------------------------------------------
1 | package network.aika.fields.manyobjects;
2 |
3 |
4 | import network.aika.type.Type;
5 | import network.aika.type.TypeRegistry;
6 | import network.aika.type.relations.Relation;
7 | import network.aika.type.relations.RelationMany;
8 | import network.aika.type.relations.RelationOne;
9 |
10 | import java.util.List;
11 |
12 | import static network.aika.fields.manyobjects.TestTypeMany.TEST_RELATION_FROM;
13 |
14 |
15 | public class TestTypeOne extends Type {
16 |
17 | public static RelationOne TEST_RELATION_TO = new RelationOne(0, "TEST_TO");
18 |
19 | static {
20 | TEST_RELATION_TO.setReversed(TEST_RELATION_FROM);
21 | }
22 |
23 | public TestTypeOne(TypeRegistry registry, String name) {
24 | super(registry, name);
25 | }
26 |
27 | @Override
28 | public Relation[] getRelations() {
29 | return new Relation[] {TEST_RELATION_TO};
30 | }
31 |
32 | public TestObjectOne instantiate() {
33 | return new TestObjectOne(this);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/AbstractTestWithObjects.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.type.TypeRegistry;
20 | import network.aika.type.TypeRegistryImpl;
21 | import network.aika.type.relations.RelationOne;
22 |
23 | /**
24 | *
25 | * @author Lukas Molzberger
26 | */
27 | public abstract class AbstractTestWithObjects {
28 |
29 | protected TypeRegistry registry;
30 |
31 | protected TestType typeA;
32 | protected TestType typeB;
33 |
34 | public void init() {
35 | registry = new TypeRegistryImpl();
36 |
37 | typeA = new TestType(registry, "A");
38 | typeB = new TestType(registry, "B");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/DivisionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.fields.defs.FieldDefinition;
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeEach;
22 | import org.junit.jupiter.params.ParameterizedTest;
23 | import org.junit.jupiter.params.provider.ValueSource;
24 |
25 | import static network.aika.fields.Division.div;
26 | import static network.aika.fields.InputField.inputField;
27 | import static network.aika.fields.oneobject.TestObject.linkObjects;
28 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM;
29 |
30 |
31 | /**
32 | * @author Lukas Molzberger
33 | */
34 | public class DivisionTest extends AbstractTestWithObjects {
35 |
36 |
37 | @BeforeEach
38 | public void init() {
39 | super.init();
40 | }
41 |
42 | @ParameterizedTest
43 | @ValueSource(ints = {0, 1, 2})
44 | public void testDivision(int linkingPos) {
45 |
46 | // Type and Math Model initialization
47 |
48 | FieldDefinition a = inputField(typeA, "a");
49 | FieldDefinition b = inputField(typeA, "b");
50 |
51 | FieldDefinition c = div(typeB, "c")
52 | .in(TEST_RELATION_FROM, a, 0)
53 | .in(TEST_RELATION_FROM, b, 1);
54 |
55 | registry.flattenTypeHierarchy();
56 |
57 | // Object and Field initialization
58 |
59 | TestObject oa = typeA.instantiate();
60 | TestObject ob = typeB.instantiate();
61 |
62 | if(linkingPos == 0) {
63 | linkObjects(oa, ob);
64 | ob.initFields();
65 | }
66 |
67 | oa.setFieldValue(a, 25.0);
68 |
69 | if(linkingPos == 1) {
70 | linkObjects(oa, ob);
71 | ob.initFields();
72 | }
73 |
74 | Assertions.assertEquals(0.0, ob.getFieldValue(c));
75 |
76 | oa.setFieldValue(b, 5.0);
77 |
78 | if(linkingPos == 2) {
79 | linkObjects(oa, ob);
80 | ob.initFields();
81 | }
82 |
83 | Assertions.assertEquals(
84 | 5.0,
85 | ob.getFieldOutput(c).getValue()
86 | );
87 |
88 | oa.setFieldValue(b, 10.0);
89 |
90 | Assertions.assertEquals(
91 | 2.5,
92 | ob.getFieldOutput(c).getValue()
93 | );
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/ExponentialFunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.fields.defs.FieldDefinition;
20 | import network.aika.type.Obj;
21 | import network.aika.type.ObjImpl;
22 | import network.aika.type.TypeRegistry;
23 | import network.aika.type.TypeRegistryImpl;
24 | import org.junit.jupiter.api.Assertions;
25 | import org.junit.jupiter.api.Test;
26 |
27 | import static network.aika.fields.ExponentialFunction.exp;
28 | import static network.aika.fields.InputField.inputField;
29 | import static network.aika.fields.oneobject.TestType.SELF;
30 |
31 |
32 | /**
33 | * @author Lukas Molzberger
34 | */
35 | @SuppressWarnings({"unchecked", "rawtypes"})
36 | public class ExponentialFunctionTest {
37 |
38 | @Test
39 | public void testExponentialFunction() {
40 | TypeRegistry registry = new TypeRegistryImpl();
41 |
42 | TestType type = new TestType(registry, "test");
43 |
44 | FieldDefinition a = inputField(type, "a");
45 | FieldDefinition b = exp(type, "b")
46 | .in(SELF, a, 0);
47 |
48 | registry.flattenTypeHierarchy();
49 |
50 | Obj o = type.instantiate();
51 |
52 | Assertions.assertNull(o.getFieldOutput(a));
53 | Assertions.assertNull(o.getFieldOutput(b));
54 |
55 | o.setFieldValue(a, 5.0);
56 |
57 | Assertions.assertNotNull(o.getFieldOutput(b));
58 |
59 | Assertions.assertEquals(
60 | 148.413159102576603,
61 | o.getFieldOutput(b).getValue()
62 | );
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/FieldInstantiationWithObjectsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.fields.defs.FieldDefinition;
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeEach;
22 | import org.junit.jupiter.api.Test;
23 |
24 | import static network.aika.fields.InputField.inputField;
25 | import static network.aika.fields.SumField.sum;
26 | import static network.aika.fields.oneobject.TestObject.linkObjects;
27 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_TO;
28 |
29 |
30 | /**
31 | * @author Lukas Molzberger
32 | */
33 | public class FieldInstantiationWithObjectsTest extends AbstractTestWithObjects {
34 |
35 |
36 | private FieldDefinition fieldA;
37 | private FieldDefinition fieldB;
38 |
39 |
40 | @BeforeEach
41 | public void init() {
42 | // Type and Math Model initialization
43 | super.init();
44 |
45 | fieldA = inputField(typeA, "a");
46 | fieldB = sum(typeB, "b")
47 | .in(TEST_RELATION_TO, fieldA);
48 |
49 | registry.flattenTypeHierarchy();
50 | }
51 |
52 | @Test
53 | public void testPropagateValue() {
54 | // Object and Field initialization
55 |
56 | TestObject objA = typeA.instantiate();
57 | TestObject objB = typeB.instantiate();
58 | linkObjects(objA, objB);
59 | objB.initFields();
60 |
61 | objA.setFieldValue(fieldA, 5.0);
62 |
63 | Assertions.assertEquals(
64 | 5.0,
65 | objB.getOrCreateFieldInput(fieldB).getValue()
66 | );
67 | }
68 |
69 | @Test
70 | public void testInitFields() {
71 |
72 | // Object and Field initialization
73 |
74 | TestObject objA = new TestObject(typeA);
75 | objA.setFieldValue(fieldA, 5.0);
76 |
77 | TestObject objB = new TestObject(typeB);
78 |
79 | linkObjects(objA, objB);
80 | objB.initFields();
81 |
82 | Assertions.assertEquals(
83 | 5.0,
84 | objB.getFieldOutput(fieldB).getValue()
85 | );
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/MultiplicationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.fields.defs.FieldDefinition;
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeEach;
22 | import org.junit.jupiter.params.ParameterizedTest;
23 | import org.junit.jupiter.params.provider.ValueSource;
24 |
25 | import static network.aika.fields.InputField.inputField;
26 | import static network.aika.fields.Multiplication.mul;
27 | import static network.aika.fields.oneobject.TestObject.linkObjects;
28 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM;
29 |
30 |
31 | /**
32 | * @author Lukas Molzberger
33 | */
34 | public class MultiplicationTest extends AbstractTestWithObjects {
35 |
36 |
37 | @BeforeEach
38 | public void init() {
39 | super.init();
40 | }
41 |
42 | @ParameterizedTest
43 | @ValueSource(ints = {0, 1, 2})
44 | public void testMultiplication(int linkingPos) {
45 |
46 | // Type and Math Model initialization
47 |
48 | FieldDefinition a = inputField(typeA, "a");
49 | FieldDefinition b = inputField(typeA, "b");
50 |
51 | FieldDefinition c = mul(typeB, "c")
52 | .in(TEST_RELATION_FROM, a, 0)
53 | .in(TEST_RELATION_FROM, b, 1);
54 |
55 | registry.flattenTypeHierarchy();
56 |
57 |
58 | // Object and Field initialization
59 |
60 | TestObject oa = typeA.instantiate();
61 | TestObject ob = typeB.instantiate();
62 |
63 | if(linkingPos == 0) {
64 | linkObjects(oa, ob);
65 | ob.initFields();
66 | }
67 |
68 | oa.setFieldValue(a, 5.0);
69 |
70 | if(linkingPos == 1) {
71 | linkObjects(oa, ob);
72 | ob.initFields();
73 | }
74 |
75 | Assertions.assertEquals(0.0, ob.getFieldValue(c));
76 |
77 | oa.setFieldValue(b, 5.0);
78 |
79 | if(linkingPos == 2) {
80 | linkObjects(oa, ob);
81 | ob.initFields();
82 | }
83 |
84 | Assertions.assertEquals(
85 | 25.0,
86 | ob.getFieldOutput(c).getValue()
87 | );
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/ObjectInstantiationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import org.junit.jupiter.api.Assertions;
20 | import org.junit.jupiter.api.BeforeEach;
21 | import org.junit.jupiter.api.Test;
22 |
23 | /**
24 | *
25 | * @author Lukas Molzberger
26 | */
27 | public class ObjectInstantiationTest extends AbstractTestWithObjects {
28 |
29 | @BeforeEach
30 | public void init() {
31 | super.init();
32 | }
33 |
34 | @Test
35 | public void testObjectInstantiation() {
36 | registry.flattenTypeHierarchy();
37 |
38 | TestObject oa = typeA.instantiate();
39 |
40 | Assertions.assertNotNull(oa);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/SubtractionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.fields.defs.FieldDefinition;
20 | import org.junit.jupiter.api.Assertions;
21 | import org.junit.jupiter.api.BeforeEach;
22 | import org.junit.jupiter.params.ParameterizedTest;
23 | import org.junit.jupiter.params.provider.ValueSource;
24 |
25 | import static network.aika.fields.InputField.inputField;
26 | import static network.aika.fields.Subtraction.sub;
27 | import static network.aika.fields.oneobject.TestObject.linkObjects;
28 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM;
29 |
30 |
31 | /**
32 | * @author Lukas Molzberger
33 | */
34 | public class SubtractionTest extends AbstractTestWithObjects {
35 |
36 |
37 | @BeforeEach
38 | public void init() {
39 | super.init();
40 | }
41 |
42 |
43 | @ParameterizedTest
44 | @ValueSource(ints = {0, 1, 2})
45 | public void testSubtraction(int linkingPos) {
46 |
47 | // Type and Math Model initialization
48 |
49 | FieldDefinition a = inputField(typeA, "a");
50 | FieldDefinition b = inputField(typeA, "b");
51 |
52 | FieldDefinition c = sub(typeB, "c")
53 | .in(TEST_RELATION_FROM, a, 0)
54 | .in(TEST_RELATION_FROM, b, 1);
55 |
56 | registry.flattenTypeHierarchy();
57 |
58 | // Object and Field initialization
59 |
60 | TestObject oa = typeA.instantiate();
61 | TestObject ob = typeB.instantiate();
62 |
63 | if(linkingPos == 0) {
64 | linkObjects(oa, ob);
65 | ob.initFields();
66 | }
67 |
68 | oa.setFieldValue(a, 50.0);
69 |
70 | if(linkingPos == 1) {
71 | linkObjects(oa, ob);
72 | ob.initFields();
73 | }
74 |
75 | oa.setFieldValue(b, 20.0);
76 |
77 | if(linkingPos == 2) {
78 | linkObjects(oa, ob);
79 | ob.initFields();
80 | }
81 |
82 | Assertions.assertEquals(
83 | 30.0,
84 | ob.getFieldOutput(c).getValue()
85 | );
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/TestObject.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.type.Obj;
20 | import network.aika.type.ObjImpl;
21 | import network.aika.type.TypeRegistry;
22 | import network.aika.type.relations.Relation;
23 |
24 | import java.util.stream.Stream;
25 |
26 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_FROM;
27 | import static network.aika.fields.oneobject.TestType.TEST_RELATION_TO;
28 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK;
29 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM;
30 |
31 | /**
32 | *
33 | * @author Lukas Molzberger
34 | */
35 | public class TestObject extends ObjImpl {
36 |
37 | TestObject relatedTestObject;
38 |
39 | public TestObject(TestType type) {
40 | super(type);
41 | }
42 |
43 | @Override
44 | public Obj followSingleRelation(Relation rel) {
45 | if(rel == TEST_RELATION_FROM)
46 | return getRelatedTestObject();
47 | else if(rel == TEST_RELATION_TO)
48 | return getRelatedTestObject();
49 | else
50 | throw new RuntimeException("Invalid Relation");
51 | }
52 |
53 | public TestObject getRelatedTestObject() {
54 | return relatedTestObject;
55 | }
56 |
57 | public static void linkObjects(TestObject objA, TestObject objB) {
58 | objA.relatedTestObject = objB;
59 | objB.relatedTestObject = objA;
60 | }
61 |
62 | @Override
63 | public Stream followManyRelation(Relation rel) {
64 | return Stream.of(relatedTestObject);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/oneobject/TestType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.oneobject;
18 |
19 | import network.aika.type.Type;
20 | import network.aika.type.TypeRegistry;
21 | import network.aika.type.relations.RelationOne;
22 | import network.aika.type.relations.RelationSelf;
23 |
24 | import java.util.List;
25 |
26 | /**
27 | *
28 | * @author Lukas Molzberger
29 | */
30 | public class TestType extends Type {
31 |
32 | public static final RelationSelf SELF = new RelationSelf(0, "TEST_SELF");
33 |
34 | public static final RelationOne TEST_RELATION_FROM = new RelationOne(1, "TEST_FROM");
35 | public static final RelationOne TEST_RELATION_TO = new RelationOne(2, "TEST_TO");
36 |
37 | static {
38 | TEST_RELATION_TO.setReversed(TEST_RELATION_FROM);
39 | TEST_RELATION_FROM.setReversed(TEST_RELATION_TO);
40 | }
41 |
42 | public TestType(TypeRegistry registry, String name) {
43 | super(registry, name);
44 |
45 | relations.addAll(List.of(SELF, TEST_RELATION_FROM, TEST_RELATION_TO));
46 | }
47 |
48 | public TestObject instantiate() {
49 | return new TestObject(this);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/softmax/SoftmaxInputObj.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.softmax;
18 |
19 | import network.aika.type.Obj;
20 | import network.aika.type.ObjImpl;
21 | import network.aika.type.TypeRegistry;
22 | import network.aika.type.relations.Relation;
23 |
24 | import java.util.stream.Stream;
25 |
26 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK;
27 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM;
28 |
29 | /**
30 | *
31 | * @author Lukas Molzberger
32 | */
33 | public class SoftmaxInputObj extends ObjImpl {
34 |
35 | SoftmaxNormObj normObject;
36 | Integer bsId;
37 |
38 | public SoftmaxInputObj(SoftmaxInputType type, Integer bsId) {
39 | super(type);
40 | this.bsId = bsId;
41 | }
42 |
43 | @Override
44 | public Obj followSingleRelation(Relation rel) {
45 | if(rel == INPUT_TO_NORM)
46 | return getNormObject();
47 | else if(rel == CORRESPONDING_OUTPUT_LINK)
48 | return getCorrespondingOutputLink();
49 | else
50 | throw new RuntimeException("Invalid Relation");
51 | }
52 |
53 | public SoftmaxNormObj getNormObject() {
54 | return normObject;
55 | }
56 |
57 | public void setNormObject(SoftmaxNormObj normObject) {
58 | this.normObject = normObject;
59 | }
60 |
61 | public SoftmaxOutputObj getCorrespondingOutputLink() {
62 | if(normObject == null || normObject.outputs.isEmpty())
63 | return null;
64 |
65 | return normObject.getOutput(bsId);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/softmax/SoftmaxInputType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.softmax;
18 |
19 |
20 | import network.aika.type.Type;
21 | import network.aika.type.TypeRegistry;
22 | import network.aika.type.relations.Relation;
23 | import network.aika.type.relations.RelationMany;
24 | import network.aika.type.relations.RelationOne;
25 |
26 | import java.util.List;
27 |
28 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_INPUT;
29 | import static network.aika.fields.softmax.SoftmaxOutputType.CORRESPONDING_INPUT_LINK;
30 |
31 | /**
32 | *
33 | * @author Lukas Molzberger
34 | */
35 | public class SoftmaxInputType extends Type {
36 |
37 | public static RelationOne INPUT_TO_NORM = new RelationOne(0, "INPUT_TO_NORM");
38 | public static RelationOne CORRESPONDING_OUTPUT_LINK = new RelationOne(1, "CORRESPONDING_OUTPUT_LINK");
39 |
40 | static {
41 | INPUT_TO_NORM.setReversed(NORM_TO_INPUT);
42 | CORRESPONDING_OUTPUT_LINK.setReversed(CORRESPONDING_INPUT_LINK);
43 | }
44 |
45 | public SoftmaxInputType(TypeRegistry registry, String name) {
46 | super(registry, name);
47 | }
48 |
49 | @Override
50 | public Relation[] getRelations() {
51 | return new Relation[] {INPUT_TO_NORM, CORRESPONDING_OUTPUT_LINK};
52 | }
53 |
54 | public SoftmaxInputObj instantiate(int bsId) {
55 | return new SoftmaxInputObj(this, bsId);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/softmax/SoftmaxNormObj.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.softmax;
18 |
19 | import network.aika.type.Obj;
20 | import network.aika.type.ObjImpl;
21 | import network.aika.type.TypeRegistry;
22 | import network.aika.type.relations.Relation;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 | import java.util.stream.Stream;
27 |
28 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK;
29 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM;
30 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_INPUT;
31 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_OUTPUT;
32 |
33 | /**
34 | *
35 | * @author Lukas Molzberger
36 | */
37 | public class SoftmaxNormObj extends ObjImpl {
38 |
39 | List inputs = new ArrayList<>();
40 | List outputs = new ArrayList<>();
41 |
42 | public SoftmaxNormObj(SoftmaxNormType type) {
43 | super(type);
44 | }
45 |
46 | public static void linkObjects(SoftmaxInputObj[] inputsObjs, SoftmaxNormObj normObj) {
47 | for (int i = 0; i < inputsObjs.length; i++) {
48 | SoftmaxInputObj inputObj = inputsObjs[i];
49 | inputObj.normObject = normObj;
50 | normObj.inputs.add(inputObj);
51 | }
52 | }
53 |
54 | @Override
55 | public Stream followManyRelation(Relation rel) {
56 | if(rel == NORM_TO_INPUT)
57 | return getInputs().map(o -> o) ;
58 | else if(rel == NORM_TO_OUTPUT)
59 | return getOutputs().map(o -> o) ;
60 | else
61 | throw new RuntimeException("Invalid Relation");
62 | }
63 |
64 | public SoftmaxInputObj getInput(int bsId) {
65 | return inputs.get(bsId);
66 | }
67 |
68 | public Stream getInputs() {
69 | return inputs.stream();
70 | }
71 |
72 | public SoftmaxOutputObj getOutput(int bsId) {
73 | return outputs.get(bsId);
74 | }
75 |
76 | public Stream getOutputs() {
77 | return outputs.stream();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/softmax/SoftmaxNormType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.softmax;
18 |
19 | import network.aika.type.Type;
20 | import network.aika.type.TypeRegistry;
21 | import network.aika.type.relations.Relation;
22 | import network.aika.type.relations.RelationMany;
23 | import network.aika.type.relations.RelationOne;
24 |
25 | import java.util.List;
26 |
27 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM;
28 | import static network.aika.fields.softmax.SoftmaxOutputType.OUTPUT_TO_NORM;
29 |
30 | /**
31 | *
32 | * @author Lukas Molzberger
33 | */
34 | public class SoftmaxNormType extends Type {
35 |
36 | public static RelationMany NORM_TO_INPUT = new RelationMany( 0, "NORM_TO_INPUT");
37 | public static RelationMany NORM_TO_OUTPUT = new RelationMany( 1, "NORM_TO_OUTPUT");
38 |
39 | static {
40 | NORM_TO_INPUT.setReversed(INPUT_TO_NORM);
41 | NORM_TO_OUTPUT.setReversed(OUTPUT_TO_NORM);
42 | }
43 |
44 | public SoftmaxNormType(TypeRegistry registry, String name) {
45 | super(registry, name);
46 | }
47 |
48 | public SoftmaxNormObj instantiate() {
49 | return new SoftmaxNormObj(this);
50 | }
51 |
52 | @Override
53 | public Relation[] getRelations() {
54 | return new Relation[] {NORM_TO_INPUT, NORM_TO_OUTPUT};
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/softmax/SoftmaxOutputObj.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.softmax;
18 |
19 | import network.aika.type.Obj;
20 | import network.aika.type.ObjImpl;
21 | import network.aika.type.TypeRegistry;
22 | import network.aika.type.relations.Relation;
23 |
24 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK;
25 | import static network.aika.fields.softmax.SoftmaxInputType.INPUT_TO_NORM;
26 | import static network.aika.fields.softmax.SoftmaxOutputType.CORRESPONDING_INPUT_LINK;
27 | import static network.aika.fields.softmax.SoftmaxOutputType.OUTPUT_TO_NORM;
28 |
29 | /**
30 | *
31 | * @author Lukas Molzberger
32 | */
33 | public class SoftmaxOutputObj extends ObjImpl {
34 |
35 | SoftmaxNormObj normObj;
36 | Integer bsId;
37 |
38 | public SoftmaxOutputObj(SoftmaxOutputType type, Integer bsId) {
39 | super(type);
40 | this.bsId = bsId;
41 | }
42 |
43 | @Override
44 | public Obj followSingleRelation(Relation rel) {
45 | if(rel == OUTPUT_TO_NORM)
46 | return getNormObj();
47 | else if(rel == CORRESPONDING_INPUT_LINK)
48 | return getCorrespondingInputLink();
49 | else
50 | throw new RuntimeException("Invalid Relation");
51 | }
52 |
53 | public SoftmaxNormObj getNormObj() {
54 | return normObj;
55 | }
56 |
57 | public static void linkObjects(SoftmaxNormObj normObj, SoftmaxOutputObj[] outputObjs) {
58 | for(int i = 0; i < outputObjs.length; i++) {
59 | SoftmaxOutputObj outputObj = outputObjs[i];
60 | normObj.outputs.add(outputObj);
61 | outputObj.normObj = normObj;
62 | }
63 | }
64 |
65 | public SoftmaxInputObj getCorrespondingInputLink() {
66 | if(normObj == null)
67 | return null;
68 |
69 | return normObj.getInput(bsId);
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/fields/src/test/java/network/aika/fields/softmax/SoftmaxOutputType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package network.aika.fields.softmax;
18 |
19 |
20 | import network.aika.type.Type;
21 | import network.aika.type.TypeRegistry;
22 | import network.aika.type.relations.Relation;
23 | import network.aika.type.relations.RelationOne;
24 |
25 | import java.util.List;
26 |
27 | import static network.aika.fields.softmax.SoftmaxInputType.CORRESPONDING_OUTPUT_LINK;
28 | import static network.aika.fields.softmax.SoftmaxNormType.NORM_TO_OUTPUT;
29 |
30 | /**
31 | *
32 | * @author Lukas Molzberger
33 | */
34 | public class SoftmaxOutputType extends Type {
35 |
36 | public static RelationOne OUTPUT_TO_NORM = new RelationOne(0, "OUTPUT_TO_NORM");
37 | public static RelationOne CORRESPONDING_INPUT_LINK = new RelationOne(1, "CORRESPONDING_INPUT_LINK");
38 |
39 | static {
40 | OUTPUT_TO_NORM.setReversed(NORM_TO_OUTPUT);
41 | CORRESPONDING_INPUT_LINK.setReversed(CORRESPONDING_OUTPUT_LINK);
42 | }
43 |
44 | public SoftmaxOutputType(TypeRegistry registry, String name) {
45 | super(registry, name);
46 | }
47 |
48 | public SoftmaxOutputObj instantiate(int bsId) {
49 | return new SoftmaxOutputObj(this, bsId);
50 | }
51 |
52 | @Override
53 | public Relation[] getRelations() {
54 | return new Relation[] {OUTPUT_TO_NORM, CORRESPONDING_INPUT_LINK};
55 | }
56 | }
57 |
--------------------------------------------------------------------------------