Skip to content

Commit 945fae0

Browse files
FieldLinkDefinition refactoring
1 parent 33772a0 commit 945fae0

File tree

10 files changed

+38
-20
lines changed

10 files changed

+38
-20
lines changed

fields/src/main/java/network/aika/type/Obj.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public interface Obj<T extends Type<T, O>, O extends Obj<T, O>> {
3333

3434
T getType();
3535

36-
void setFields(Field<T, O>[] fields);
37-
3836
Field<T, O> getOrCreateField(FieldDefinition<T, O> fd);
3937

4038
Field<T, O> getField(FieldDefinition<T, O> fd);

fields/src/main/java/network/aika/type/ObjImpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public class ObjImpl<T extends Type<T, O>, O extends Obj<T, O>, M> implements Ob
3737

3838
private Field<T, O>[] fields;
3939

40+
@SuppressWarnings("unchecked")
4041
public ObjImpl(T type) {
4142
this.type = type;
43+
44+
fields = new Field[type.getFlattenedType().getNumberOfFields()];
4245
}
4346

44-
@SuppressWarnings("unchecked")
4547
@Override
4648
public void initFields() {
4749
for(short i = 0; i < type.getFlattenedType().getNumberOfFields(); i++) {
@@ -62,11 +64,6 @@ public boolean isInstanceOf(T t) {
6264
return type.isInstanceOf(t);
6365
}
6466

65-
@Override
66-
public void setFields(Field<T, O>[] fields) {
67-
this.fields = fields;
68-
}
69-
7067
@Override
7168
public Field<T, O> getField(FieldDefinition<T, O> fd) {
7269
short fieldIndex = type.getFlattenedType().getFieldIndex(fd);

fields/src/main/java/network/aika/type/Type.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ protected O instantiate(List<Class<?>> parameterTypes, List<Object> parameters)
109109
LOG.debug(instance.toString());
110110
}
111111

112-
instance.setFields(new Field[flattenedType.getNumberOfFields()]);
113-
114112
return instance;
115113
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
116114
throw new RuntimeException(e);
@@ -151,6 +149,9 @@ public TypeRegistry getTypeRegistry() {
151149
}
152150

153151
public FlattenedType<T, O> getFlattenedType() {
152+
if(flattenedType == null)
153+
throw new RuntimeException("Type has not been flattened yet. TypeRegistry.flattenTypeHierarchy() needs to be called beforehand.");
154+
154155
return flattenedType;
155156
}
156157

fields/src/test/java/network/aika/fields/oneobject/DivisionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public void testDivision(int linkingPos) {
5252
.in(TEST_RELATION_FROM, a, 0)
5353
.in(TEST_RELATION_FROM, b, 1);
5454

55+
registry.flattenTypeHierarchy();
56+
5557
// Object and Field initialization
5658

5759
TestObject oa = typeA.instantiate();

fields/src/test/java/network/aika/fields/oneobject/ExponentialFunctionTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ public class ExponentialFunctionTest {
3737

3838
@Test
3939
public void testExponentialFunction() {
40-
TypeRegistry reg = new TypeRegistryImpl();
40+
TypeRegistry registry = new TypeRegistryImpl();
4141

42-
TestType type = new TestType(reg, "test");
42+
TestType type = new TestType(registry, "test");
4343

4444
FieldDefinition<TestType, TestObject> a = inputField(type, "a");
4545
FieldDefinition<TestType, TestObject> b = exp(type, "b")
4646
.in(SELF, a, 0);
4747

48+
registry.flattenTypeHierarchy();
49+
4850
Obj o = new ObjImpl(type);
4951

5052
Assertions.assertNull(o.getField(a));

fields/src/test/java/network/aika/fields/oneobject/FieldInstantiationTest.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ public class FieldInstantiationTest {
3535

3636
@Test
3737
public void testFieldInstantiation() {
38-
TypeRegistry reg = new TypeRegistryImpl();
38+
TypeRegistry registry = new TypeRegistryImpl();
3939

40-
TestType type = new TestType(reg, "test");
40+
TestType type = new TestType(registry, "test");
4141

4242
FieldDefinition<TestType, TestObject> a = inputField(type, "a");
4343
FieldDefinition<TestType, TestObject> b = sum(type, "b")
4444
.in(SELF, a);
4545

46+
registry.flattenTypeHierarchy();
47+
48+
// Object and Field initialization
49+
4650
Obj o = new ObjImpl(type);
4751

4852
Assertions.assertNull(o.getField(a));
@@ -60,16 +64,20 @@ public void testFieldInstantiation() {
6064

6165
@Test
6266
public void testFieldInstantiationChain() {
63-
TypeRegistry reg = new TypeRegistryImpl();
67+
TypeRegistry registry = new TypeRegistryImpl();
6468

65-
TestType type = new TestType(reg, "test");
69+
TestType type = new TestType(registry, "test");
6670

6771
FieldDefinition<TestType, TestObject> a = inputField(type, "a");
6872
FieldDefinition<TestType, TestObject> b = sum(type, "b")
6973
.in(SELF, a);
7074
FieldDefinition<TestType, TestObject> c = sum(type, "c")
7175
.in(SELF, b);
7276

77+
registry.flattenTypeHierarchy();
78+
79+
// Object and Field initialization
80+
7381
Obj o = new ObjImpl(type);
7482

7583
o.setFieldValue(a, 5.0);
@@ -82,9 +90,9 @@ public void testFieldInstantiationChain() {
8290

8391
@Test
8492
public void testMultiplication() {
85-
TypeRegistry reg = new TypeRegistryImpl();
93+
TypeRegistry registry = new TypeRegistryImpl();
8694

87-
TestType type = new TestType(reg, "test");
95+
TestType type = new TestType(registry, "test");
8896

8997
FieldDefinition<TestType, TestObject> a = inputField(type, "a");
9098
FieldDefinition<TestType, TestObject> b = inputField(type, "b");
@@ -93,6 +101,10 @@ public void testMultiplication() {
93101
.in(SELF, a, 0)
94102
.in(SELF, b, 1);
95103

104+
registry.flattenTypeHierarchy();
105+
106+
// Object and Field initialization
107+
96108
Obj o = new ObjImpl(type);
97109

98110
o.setFieldValue(a, 5.0);

fields/src/test/java/network/aika/fields/oneobject/FieldInstantiationWithObjectsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public void init() {
4545
fieldA = inputField(typeA, "a");
4646
fieldB = sum(typeB, "b")
4747
.in(TEST_RELATION_TO, fieldA);
48+
49+
registry.flattenTypeHierarchy();
4850
}
4951

5052
@Test

fields/src/test/java/network/aika/fields/oneobject/MultiplicationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public void testMultiplication(int linkingPos) {
5252
.in(TEST_RELATION_FROM, a, 0)
5353
.in(TEST_RELATION_FROM, b, 1);
5454

55-
5655
registry.flattenTypeHierarchy();
5756

57+
5858
// Object and Field initialization
5959

6060
TestObject oa = typeA.instantiate();

fields/src/test/java/network/aika/fields/oneobject/SubtractionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void testSubtraction(int linkingPos) {
5353
.in(TEST_RELATION_FROM, a, 0)
5454
.in(TEST_RELATION_FROM, b, 1);
5555

56+
registry.flattenTypeHierarchy();
57+
5658
// Object and Field initialization
5759

5860
TestObject oa = typeA.instantiate();

fields/src/test/java/network/aika/fields/softmax/SoftmaxTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
public class SoftmaxTest {
2222

23+
protected TypeRegistry registry;
2324
protected SoftmaxInputType inputType;
2425
protected SoftmaxNormType normType;
2526
protected SoftmaxOutputType outputType;
@@ -28,7 +29,7 @@ public class SoftmaxTest {
2829

2930
@BeforeEach
3031
public void init() {
31-
TypeRegistry registry = new TypeRegistryImpl();
32+
registry = new TypeRegistryImpl();
3233

3334
inputType = new SoftmaxInputType(registry, "Input")
3435
.setClazz(SoftmaxInputObj.class);
@@ -55,6 +56,7 @@ public void testSoftmax(int setInputValuesPos) {
5556
CORRESPONDING_INPUT_LINK,
5657
"test softmax"
5758
);
59+
registry.flattenTypeHierarchy();
5860

5961
// Object and Field initialization
6062
SoftmaxInputObj[] inputsObjs = new SoftmaxInputObj[inputValues.length];

0 commit comments

Comments
 (0)