From fbd2f59a86d8dbd54dae50ca3c3e27d0642d5806 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 10 Nov 2022 17:25:24 +0100 Subject: feat(store): DataRepresentation reflective type --- .../store/model/representation/AuxiliaryData.java | 5 ++- .../model/representation/DataRepresentation.java | 16 ++++++- .../store/model/representation/Relation.java | 6 +-- .../refinery/store/model/tests/ModelTest.java | 50 ++++++++++------------ 4 files changed, 44 insertions(+), 33 deletions(-) (limited to 'subprojects/store/src') diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/representation/AuxiliaryData.java b/subprojects/store/src/main/java/tools/refinery/store/model/representation/AuxiliaryData.java index 1a968f50..18c38151 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/representation/AuxiliaryData.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/representation/AuxiliaryData.java @@ -5,8 +5,9 @@ import tools.refinery.store.map.ContinousHashProvider; public final class AuxiliaryData extends DataRepresentation { private final ContinousHashProvider hashProvider; - public AuxiliaryData(String name, ContinousHashProvider hashProvider, V defaultValue) { - super(name, defaultValue); + public AuxiliaryData(String name, Class keyType, ContinousHashProvider hashProvider, Class valueType, + V defaultValue) { + super(name, keyType, valueType, defaultValue); this.hashProvider = hashProvider; } diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/representation/DataRepresentation.java b/subprojects/store/src/main/java/tools/refinery/store/model/representation/DataRepresentation.java index 49f74717..61c6291b 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/representation/DataRepresentation.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/representation/DataRepresentation.java @@ -7,9 +7,15 @@ public abstract sealed class DataRepresentation permits Relation, Auxiliar private final V defaultValue; - protected DataRepresentation(String name, V defaultValue) { + private final Class keyType; + + private final Class valueType; + + protected DataRepresentation(String name, Class keyType, Class valueType, V defaultValue) { this.name = name; this.defaultValue = defaultValue; + this.keyType = keyType; + this.valueType = valueType; } public String getName() { @@ -23,4 +29,12 @@ public abstract sealed class DataRepresentation permits Relation, Auxiliar public V getDefaultValue() { return defaultValue; } + + public Class getKeyType() { + return keyType; + } + + public Class getValueType() { + return valueType; + } } diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/representation/Relation.java b/subprojects/store/src/main/java/tools/refinery/store/model/representation/Relation.java index 2e21ea37..cc32257c 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/representation/Relation.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/representation/Relation.java @@ -2,14 +2,14 @@ package tools.refinery.store.model.representation; import tools.refinery.store.map.ContinousHashProvider; import tools.refinery.store.model.RelationLike; -import tools.refinery.store.tuple.Tuple; import tools.refinery.store.model.TupleHashProvider; +import tools.refinery.store.tuple.Tuple; public final class Relation extends DataRepresentation implements RelationLike { private final int arity; - public Relation(String name, int arity, D defaultValue) { - super(name, defaultValue); + public Relation(String name, int arity, Class valueType, D defaultValue) { + super(name, Tuple.class, valueType, defaultValue); this.arity = arity; } diff --git a/subprojects/store/src/test/java/tools/refinery/store/model/tests/ModelTest.java b/subprojects/store/src/test/java/tools/refinery/store/model/tests/ModelTest.java index 61dd7c74..a9c2e3f7 100644 --- a/subprojects/store/src/test/java/tools/refinery/store/model/tests/ModelTest.java +++ b/subprojects/store/src/test/java/tools/refinery/store/model/tests/ModelTest.java @@ -1,26 +1,22 @@ package tools.refinery.store.model.tests; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Set; - -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - import tools.refinery.store.model.Model; import tools.refinery.store.model.ModelStore; import tools.refinery.store.model.ModelStoreImpl; -import tools.refinery.store.tuple.Tuple; import tools.refinery.store.model.representation.Relation; +import tools.refinery.store.tuple.Tuple; + +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; class ModelTest { @Test void modelConstructionTest() { - Relation person = new Relation<>("Person", 1, false); - Relation friend = new Relation<>("friend", 2, false); + Relation person = new Relation<>("Person", 1, Boolean.class, false); + Relation friend = new Relation<>("friend", 2, Boolean.class, false); ModelStore store = new ModelStoreImpl(Set.of(person, friend)); Model model = store.createModel(); @@ -30,15 +26,15 @@ class ModelTest { assertTrue(model.getDataRepresentations().contains(person)); assertTrue(model.getDataRepresentations().contains(friend)); - Relation other = new Relation("other", 2, null); + Relation other = new Relation("other", 2, Integer.class, null); assertFalse(model.getDataRepresentations().contains(other)); } @Test void modelBuildingTest() { - Relation person = new Relation<>("Person", 1, false); - Relation age = new Relation("age", 1, null); - Relation friend = new Relation<>("friend", 2, false); + Relation person = new Relation<>("Person", 1, Boolean.class, false); + Relation age = new Relation("age", 1, Integer.class, null); + Relation friend = new Relation<>("friend", 2, Boolean.class, false); ModelStore store = new ModelStoreImpl(Set.of(person, age, friend)); Model model = store.createModel(); @@ -56,7 +52,7 @@ class ModelTest { assertEquals(3, model.get(age, Tuple.of(0))); assertEquals(1, model.get(age, Tuple.of(1))); - assertEquals(null, model.get(age, Tuple.of(2))); + assertNull(model.get(age, Tuple.of(2))); assertTrue(model.get(friend, Tuple.of(0, 1))); assertFalse(model.get(friend, Tuple.of(0, 5))); @@ -64,32 +60,32 @@ class ModelTest { @Test void modelBuildingArityFailTest() { - Relation person = new Relation<>("Person", 1, false); + Relation person = new Relation<>("Person", 1, Boolean.class, false); ModelStore store = new ModelStoreImpl(Set.of(person)); Model model = store.createModel(); final Tuple tuple3 = Tuple.of(1, 1, 1); - Assertions.assertThrows(IllegalArgumentException.class, () -> model.put(person, tuple3, true)); - Assertions.assertThrows(IllegalArgumentException.class, () -> model.get(person, tuple3)); + assertThrows(IllegalArgumentException.class, () -> model.put(person, tuple3, true)); + assertThrows(IllegalArgumentException.class, () -> model.get(person, tuple3)); } @Test void modelBuildingNullFailTest() { - Relation age = new Relation("age", 1, null); + Relation age = new Relation("age", 1, Integer.class, null); ModelStore store = new ModelStoreImpl(Set.of(age)); Model model = store.createModel(); model.put(age, Tuple.of(1), null); // valid - Assertions.assertThrows(IllegalArgumentException.class, () -> model.put(age, null, 1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> model.get(age, null)); + assertThrows(IllegalArgumentException.class, () -> model.put(age, null, 1)); + assertThrows(IllegalArgumentException.class, () -> model.get(age, null)); } @Test void modelUpdateTest() { - Relation person = new Relation<>("Person", 1, false); - Relation age = new Relation("age", 1, null); - Relation friend = new Relation<>("friend", 2, false); + Relation person = new Relation<>("Person", 1, Boolean.class, false); + Relation age = new Relation("age", 1, Integer.class, null); + Relation friend = new Relation<>("friend", 2, Boolean.class, false); ModelStore store = new ModelStoreImpl(Set.of(person, age, friend)); Model model = store.createModel(); @@ -113,8 +109,8 @@ class ModelTest { @Test void restoreTest() { - Relation person = new Relation("Person", 1, false); - Relation friend = new Relation("friend", 2, false); + Relation person = new Relation("Person", 1, Boolean.class, false); + Relation friend = new Relation("friend", 2, Boolean.class, false); ModelStore store = new ModelStoreImpl(Set.of(person, friend)); Model model = store.createModel(); -- cgit v1.2.3-70-g09d2