diff options
Diffstat (limited to 'subprojects/language-to-store')
4 files changed, 741 insertions, 0 deletions
diff --git a/subprojects/language-to-store/build.gradle b/subprojects/language-to-store/build.gradle new file mode 100644 index 00000000..f1c1564d --- /dev/null +++ b/subprojects/language-to-store/build.gradle | |||
@@ -0,0 +1,10 @@ | |||
1 | plugins { | ||
2 | id 'refinery-java-library' | ||
3 | id 'refinery-xtext-conventions' | ||
4 | } | ||
5 | |||
6 | dependencies { | ||
7 | api project(':refinery-language-model') | ||
8 | api project(':refinery-store') | ||
9 | testImplementation testFixtures(project(':refinery-language')) | ||
10 | } | ||
diff --git a/subprojects/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java b/subprojects/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java new file mode 100644 index 00000000..8f90a743 --- /dev/null +++ b/subprojects/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java | |||
@@ -0,0 +1,239 @@ | |||
1 | package tools.refinery.language.mapping; | ||
2 | |||
3 | import java.util.HashMap; | ||
4 | import java.util.HashSet; | ||
5 | import java.util.Map; | ||
6 | import java.util.Optional; | ||
7 | |||
8 | import org.eclipse.emf.common.util.EList; | ||
9 | |||
10 | import tools.refinery.language.model.ProblemUtil; | ||
11 | import tools.refinery.language.model.problem.Assertion; | ||
12 | import tools.refinery.language.model.problem.AssertionArgument; | ||
13 | import tools.refinery.language.model.problem.ClassDeclaration; | ||
14 | import tools.refinery.language.model.problem.EnumDeclaration; | ||
15 | import tools.refinery.language.model.problem.IndividualDeclaration; | ||
16 | import tools.refinery.language.model.problem.LogicValue; | ||
17 | import tools.refinery.language.model.problem.Node; | ||
18 | import tools.refinery.language.model.problem.NodeAssertionArgument; | ||
19 | import tools.refinery.language.model.problem.PredicateDefinition; | ||
20 | import tools.refinery.language.model.problem.Problem; | ||
21 | import tools.refinery.language.model.problem.ReferenceDeclaration; | ||
22 | import tools.refinery.language.model.problem.Statement; | ||
23 | import tools.refinery.store.model.Model; | ||
24 | import tools.refinery.store.model.ModelStore; | ||
25 | import tools.refinery.store.model.ModelStoreImpl; | ||
26 | import tools.refinery.store.model.Tuple; | ||
27 | import tools.refinery.store.model.representation.Relation; | ||
28 | import tools.refinery.store.model.representation.TruthValue; | ||
29 | |||
30 | public class PartialModelMapper { | ||
31 | public PartialModelMapperDTO transformProblem(Problem problem) throws PartialModelMapperException { | ||
32 | // Defining an integer in order to assign different values to all the nodes | ||
33 | int[] nodeIter = new int[] { 0 }; | ||
34 | |||
35 | // Getting the relations and the nodes from the given problem | ||
36 | PartialModelMapperDTO pmmDTO = initTransform(problem, nodeIter); | ||
37 | |||
38 | // Getting the relations and the nodes from the built in problem | ||
39 | Optional<Problem> builtinProblem = ProblemUtil.getBuiltInLibrary(problem); | ||
40 | if (builtinProblem.isEmpty()) | ||
41 | throw new PartialModelMapperException("builtin.problem not found"); | ||
42 | PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get(), nodeIter); | ||
43 | |||
44 | // Merging the relation and the nodes from the given problem and from the built | ||
45 | // in problem | ||
46 | pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); | ||
47 | pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); | ||
48 | pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); | ||
49 | pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); | ||
50 | pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); | ||
51 | |||
52 | // Definition of store and model | ||
53 | ModelStore store = new ModelStoreImpl(new HashSet<>(pmmDTO.getRelationMap().values())); | ||
54 | Model model = store.createModel(); | ||
55 | pmmDTO.setModel(model); | ||
56 | |||
57 | // Collecting all the nodes in one map | ||
58 | Map<Node, Integer> allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), pmmDTO.getUniqueNodeMap(), | ||
59 | pmmDTO.getNewNodeMap(), pmmDTO.getNodeMap()); | ||
60 | |||
61 | // Filling up the relations with unknown truth values | ||
62 | for (tools.refinery.language.model.problem.Relation relation : pmmDTO.getRelationMap().keySet()) { | ||
63 | if (!(relation instanceof PredicateDefinition pd && pd.isError())) { | ||
64 | Relation<TruthValue> r = pmmDTO.getRelationMap().get(relation); | ||
65 | if (r.getArity() == 1) | ||
66 | for (Integer i : allNodesMap.values()) { | ||
67 | pmmDTO.getModel().put(r, Tuple.of(i), TruthValue.UNKNOWN); | ||
68 | } | ||
69 | else if (r.getArity() == 2) | ||
70 | for (Integer i : allNodesMap.values()) { | ||
71 | for (Integer j : allNodesMap.values()) { | ||
72 | pmmDTO.getModel().put(r, Tuple.of(i, j), TruthValue.UNKNOWN); | ||
73 | } | ||
74 | } | ||
75 | else | ||
76 | throw new PartialModelMapperException("Relation with arity above 2 is not supported"); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | // Filling up the exists | ||
81 | tools.refinery.language.model.problem.Relation existsRelation = findingRelationInDTO(builtinProblemDTO, | ||
82 | "exists", "The exists not found in built in problem"); | ||
83 | for (Node n : allNodesMap.keySet()) { | ||
84 | if (pmmDTO.getNewNodeMap().containsKey(n)) { | ||
85 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | ||
86 | Tuple.of(allNodesMap.get(n)), TruthValue.UNKNOWN); | ||
87 | } else { | ||
88 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | ||
89 | Tuple.of(allNodesMap.get(n)), TruthValue.TRUE); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | // Filling up the equals | ||
94 | tools.refinery.language.model.problem.Relation equalsRelation = findingRelationInDTO(builtinProblemDTO, | ||
95 | "equals", "The equals not found in built in problem"); | ||
96 | for (Node n1 : allNodesMap.keySet()) { | ||
97 | for (Node n2 : allNodesMap.keySet()) { | ||
98 | if (n1.equals(n2)) { | ||
99 | if (pmmDTO.getNewNodeMap().containsKey(n1)) { | ||
100 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
101 | Tuple.of(allNodesMap.get(n1), allNodesMap.get(n2)), TruthValue.UNKNOWN); | ||
102 | } else { | ||
103 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
104 | Tuple.of(allNodesMap.get(n1), allNodesMap.get(n2)), TruthValue.TRUE); | ||
105 | } | ||
106 | } else { | ||
107 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
108 | Tuple.of(allNodesMap.get(n1), allNodesMap.get(n2)), TruthValue.FALSE); | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | // Transforming the assertions | ||
114 | processAssertions(problem, pmmDTO, allNodesMap); | ||
115 | processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); | ||
116 | |||
117 | return pmmDTO; | ||
118 | } | ||
119 | |||
120 | // Searches for and gives back a relation in a PartialModelMapperDTO | ||
121 | private tools.refinery.language.model.problem.Relation findingRelationInDTO( | ||
122 | PartialModelMapperDTO partialModelMapperDTO, String searchedRelation, String errorText) | ||
123 | throws PartialModelMapperException { | ||
124 | for (tools.refinery.language.model.problem.Relation r : partialModelMapperDTO.getRelationMap().keySet()) { | ||
125 | if (searchedRelation.equals(r.getName())) | ||
126 | return r; | ||
127 | } | ||
128 | throw new PartialModelMapperException(errorText); | ||
129 | } | ||
130 | |||
131 | // Processing assertions and placing them in the model | ||
132 | private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map<Node, Integer> allNodesMap) { | ||
133 | for (Statement s : problem.getStatements()) { | ||
134 | if (s instanceof Assertion assertion) { | ||
135 | Relation<TruthValue> r1 = pmmDTO.getRelationMap().get(assertion.getRelation()); | ||
136 | int i = 0; | ||
137 | int[] integers = new int[assertion.getArguments().size()]; | ||
138 | for (AssertionArgument aa : assertion.getArguments()) { | ||
139 | if (aa instanceof NodeAssertionArgument nas) { | ||
140 | integers[i] = allNodesMap.get(nas.getNode()); | ||
141 | i++; | ||
142 | } | ||
143 | } | ||
144 | pmmDTO.getModel().put(r1, Tuple.of(integers), logicValueToTruthValue(assertion.getValue())); | ||
145 | } else if (s instanceof ClassDeclaration cd) { | ||
146 | if (!cd.isAbstract()) | ||
147 | pmmDTO.getModel().put(pmmDTO.getRelationMap().get(cd), | ||
148 | Tuple.of(pmmDTO.getNewNodeMap().get(cd.getNewNode())), TruthValue.TRUE); | ||
149 | } else if (s instanceof EnumDeclaration ed) { | ||
150 | for (Node n : ed.getLiterals()) { | ||
151 | pmmDTO.getModel().put(pmmDTO.getRelationMap().get(ed), Tuple.of(pmmDTO.getEnumNodeMap().get(n)), | ||
152 | TruthValue.TRUE); | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | // Getting the relations and nodes from the problem | ||
159 | private PartialModelMapperDTO initTransform(Problem problem, int[] nodeIter) { | ||
160 | // Defining needed Maps | ||
161 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap = new HashMap<>(); | ||
162 | Map<Node, Integer> enumNodeMap = new HashMap<>(); | ||
163 | Map<Node, Integer> uniqueNodeMap = new HashMap<>(); | ||
164 | Map<Node, Integer> newNodeMap = new HashMap<>(); | ||
165 | |||
166 | // Definition of Relations, filling up the enumNodeMap, uniqueNodeMap, | ||
167 | // newNodeMap | ||
168 | EList<Statement> statements = problem.getStatements(); | ||
169 | for (Statement s : statements) { | ||
170 | if (s instanceof ClassDeclaration cd) { | ||
171 | Relation<TruthValue> r1 = new Relation<>(cd.getName(), 1, TruthValue.FALSE); | ||
172 | relationMap.put(cd, r1); | ||
173 | if (!cd.isAbstract()) | ||
174 | newNodeMap.put(cd.getNewNode(), nodeIter[0]++); | ||
175 | EList<ReferenceDeclaration> refDeclList = cd.getReferenceDeclarations(); | ||
176 | for (ReferenceDeclaration refDec : refDeclList) { | ||
177 | Relation<TruthValue> r2 = new Relation<>(refDec.getName(), 2, TruthValue.FALSE); | ||
178 | relationMap.put(refDec, r2); | ||
179 | } | ||
180 | } else if (s instanceof EnumDeclaration ed) { | ||
181 | Relation<TruthValue> r = new Relation<>(ed.getName(), 1, TruthValue.FALSE); | ||
182 | relationMap.put(ed, r); | ||
183 | for (Node n : ed.getLiterals()) { | ||
184 | enumNodeMap.put(n, nodeIter[0]++); | ||
185 | } | ||
186 | } else if (s instanceof IndividualDeclaration ud) { | ||
187 | for (Node n : ud.getNodes()) { | ||
188 | uniqueNodeMap.put(n, nodeIter[0]++); | ||
189 | } | ||
190 | } else if (s instanceof PredicateDefinition pd) { | ||
191 | Relation<TruthValue> r = new Relation<>(pd.getName(), 1, TruthValue.FALSE); | ||
192 | relationMap.put(pd, r); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | // Filling the nodeMap up | ||
197 | Map<Node, Integer> nodeMap = new HashMap<>(); | ||
198 | for (Node n : problem.getNodes()) { | ||
199 | nodeMap.put(n, nodeIter[0]++); | ||
200 | } | ||
201 | |||
202 | return new PartialModelMapperDTO(null, relationMap, nodeMap, enumNodeMap, uniqueNodeMap, newNodeMap); | ||
203 | } | ||
204 | |||
205 | // Merging the maps of nodes into one map | ||
206 | private Map<Node, Integer> mergeNodeMaps(Map<Node, Integer> enumNodeMap, Map<Node, Integer> uniqueNodeMap, | ||
207 | Map<Node, Integer> newNodeMap, Map<Node, Integer> nodeMap) { | ||
208 | Map<Node, Integer> out = new HashMap<>(); | ||
209 | out.putAll(enumNodeMap); | ||
210 | out.putAll(uniqueNodeMap); | ||
211 | out.putAll(newNodeMap); | ||
212 | out.putAll(nodeMap); | ||
213 | return out; | ||
214 | } | ||
215 | |||
216 | // Exchange method from LogicValue to TruthValue | ||
217 | private TruthValue logicValueToTruthValue(LogicValue value) { | ||
218 | if (value.equals(LogicValue.TRUE)) | ||
219 | return TruthValue.TRUE; | ||
220 | else if (value.equals(LogicValue.FALSE)) | ||
221 | return TruthValue.FALSE; | ||
222 | else if (value.equals(LogicValue.UNKNOWN)) | ||
223 | return TruthValue.UNKNOWN; | ||
224 | else | ||
225 | return TruthValue.ERROR; | ||
226 | } | ||
227 | |||
228 | public class PartialModelMapperException extends Exception { | ||
229 | private static final long serialVersionUID = 1L; | ||
230 | |||
231 | public PartialModelMapperException(String errorText) { | ||
232 | super(errorText); | ||
233 | } | ||
234 | |||
235 | public PartialModelMapperException() { | ||
236 | super(); | ||
237 | } | ||
238 | } | ||
239 | } | ||
diff --git a/subprojects/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java b/subprojects/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java new file mode 100644 index 00000000..3397b4bd --- /dev/null +++ b/subprojects/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java | |||
@@ -0,0 +1,54 @@ | |||
1 | package tools.refinery.language.mapping; | ||
2 | |||
3 | import java.util.Map; | ||
4 | |||
5 | import tools.refinery.language.model.problem.Node; | ||
6 | import tools.refinery.store.model.Model; | ||
7 | import tools.refinery.store.model.representation.Relation; | ||
8 | import tools.refinery.store.model.representation.TruthValue; | ||
9 | |||
10 | public class PartialModelMapperDTO { | ||
11 | private Model model; | ||
12 | private Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap; | ||
13 | private Map<Node, Integer> nodeMap; | ||
14 | private Map<Node, Integer> enumNodeMap; | ||
15 | private Map<Node, Integer> uniqueNodeMap; | ||
16 | private Map<Node, Integer> newNodeMap; | ||
17 | |||
18 | public PartialModelMapperDTO(Model model, | ||
19 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap, | ||
20 | Map<Node, Integer> nodeMap, | ||
21 | Map<Node, Integer> enumNodeMap, | ||
22 | Map<Node, Integer> uniqueNodeMap, | ||
23 | Map<Node, Integer> newNodeMap) { | ||
24 | this.model = model; | ||
25 | this.relationMap = relationMap; | ||
26 | this.nodeMap = nodeMap; | ||
27 | this.enumNodeMap = enumNodeMap; | ||
28 | this.uniqueNodeMap = uniqueNodeMap; | ||
29 | this.newNodeMap = newNodeMap; | ||
30 | } | ||
31 | |||
32 | public Model getModel() { | ||
33 | return this.model; | ||
34 | } | ||
35 | public Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> getRelationMap(){ | ||
36 | return this.relationMap; | ||
37 | } | ||
38 | public Map<Node, Integer> getNodeMap() { | ||
39 | return this.nodeMap; | ||
40 | } | ||
41 | public Map<Node, Integer> getEnumNodeMap() { | ||
42 | return this.enumNodeMap; | ||
43 | } | ||
44 | public Map<Node, Integer> getUniqueNodeMap() { | ||
45 | return this.uniqueNodeMap; | ||
46 | } | ||
47 | public Map<Node, Integer> getNewNodeMap() { | ||
48 | return this.newNodeMap; | ||
49 | } | ||
50 | |||
51 | public void setModel(Model model) { | ||
52 | this.model = model; | ||
53 | } | ||
54 | } | ||
diff --git a/subprojects/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend b/subprojects/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend new file mode 100644 index 00000000..b2fcbaa9 --- /dev/null +++ b/subprojects/language-to-store/src/test/java/tools/refinery/language/mapping/tests/PartialModelMapperTest.xtend | |||
@@ -0,0 +1,438 @@ | |||
1 | package tools.refinery.language.mapping.tests | ||
2 | |||
3 | import com.google.inject.Inject | ||
4 | import org.eclipse.emf.ecore.util.EcoreUtil | ||
5 | import org.eclipse.xtext.testing.InjectWith | ||
6 | import org.eclipse.xtext.testing.extensions.InjectionExtension | ||
7 | import org.eclipse.xtext.testing.util.ParseHelper | ||
8 | import org.junit.jupiter.api.BeforeEach | ||
9 | import org.junit.jupiter.api.Test | ||
10 | import org.junit.jupiter.api.^extension.ExtendWith | ||
11 | import tools.refinery.language.mapping.PartialModelMapper | ||
12 | import tools.refinery.language.model.problem.Problem | ||
13 | import tools.refinery.language.model.tests.ProblemTestUtil | ||
14 | import tools.refinery.language.tests.ProblemInjectorProvider | ||
15 | import tools.refinery.store.model.Tuple | ||
16 | import tools.refinery.store.model.representation.TruthValue | ||
17 | |||
18 | import static org.hamcrest.MatcherAssert.assertThat | ||
19 | import static org.hamcrest.Matchers.* | ||
20 | import static org.junit.jupiter.api.Assertions.assertTrue | ||
21 | |||
22 | @ExtendWith(InjectionExtension) | ||
23 | @InjectWith(ProblemInjectorProvider) | ||
24 | class PartialModelMapperTest { | ||
25 | @Inject | ||
26 | ParseHelper<Problem> parseHelper | ||
27 | |||
28 | @Inject | ||
29 | extension ProblemTestUtil | ||
30 | |||
31 | PartialModelMapper mapper | ||
32 | |||
33 | @BeforeEach | ||
34 | def void beforeEach() { | ||
35 | mapper = new PartialModelMapper | ||
36 | } | ||
37 | |||
38 | //Testing the relation | ||
39 | @Test | ||
40 | def void relationTest() { | ||
41 | val problem = parseHelper.parse(''' | ||
42 | class Person { | ||
43 | Person[0..*] friend | ||
44 | } | ||
45 | |||
46 | friend(a, b). | ||
47 | ''') | ||
48 | EcoreUtil.resolveAll(problem) | ||
49 | |||
50 | val modelAndMaps = mapper.transformProblem(problem) | ||
51 | assertThat(modelAndMaps, notNullValue()) | ||
52 | |||
53 | val model = modelAndMaps.model | ||
54 | val relationMap = modelAndMaps.relationMap | ||
55 | val nodeMap = modelAndMaps.nodeMap | ||
56 | |||
57 | val person = problem.findClass("Person") | ||
58 | val friend = problem.findClass("Person").reference("friend") | ||
59 | val a = problem.node("a") | ||
60 | val b = problem.node("b") | ||
61 | |||
62 | assertTrue(model.getDataRepresentations().contains(relationMap.get(person))) | ||
63 | assertTrue(model.getDataRepresentations().contains(relationMap.get(friend))) | ||
64 | assertTrue(model.get(relationMap.get(friend), Tuple.of(nodeMap.get(a),nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
65 | } | ||
66 | |||
67 | //Testing the class | ||
68 | @Test | ||
69 | def void classTest() { | ||
70 | val problem = parseHelper.parse(''' | ||
71 | class Person { | ||
72 | Person[0..*] friend | ||
73 | } | ||
74 | |||
75 | Person(a). | ||
76 | ''') | ||
77 | EcoreUtil.resolveAll(problem) | ||
78 | |||
79 | val modelAndMaps = mapper.transformProblem(problem) | ||
80 | assertThat(modelAndMaps, notNullValue()) | ||
81 | |||
82 | val model = modelAndMaps.model | ||
83 | val relationMap = modelAndMaps.relationMap | ||
84 | val nodeMap = modelAndMaps.nodeMap | ||
85 | |||
86 | val person = problem.findClass("Person") | ||
87 | val friend = problem.findClass("Person").reference("friend") | ||
88 | val a = problem.node("a") | ||
89 | |||
90 | assertTrue(model.getDataRepresentations().contains(relationMap.get(person))) | ||
91 | assertTrue(model.getDataRepresentations().contains(relationMap.get(friend))) | ||
92 | |||
93 | assertTrue(model.get(relationMap.get(person), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
94 | } | ||
95 | |||
96 | //Testing the equals and exists from the built in problem | ||
97 | @Test | ||
98 | def void equalsAndExistTest() { | ||
99 | val problem = parseHelper.parse(''' | ||
100 | node(a). | ||
101 | node(b). | ||
102 | |||
103 | class Person. | ||
104 | ''') | ||
105 | EcoreUtil.resolveAll(problem) | ||
106 | val builtin = problem.builtin | ||
107 | |||
108 | val modelAndMaps = mapper.transformProblem(problem) | ||
109 | assertThat(modelAndMaps, notNullValue()) | ||
110 | |||
111 | val model = modelAndMaps.model | ||
112 | val relationMap = modelAndMaps.relationMap | ||
113 | val nodeMap = modelAndMaps.nodeMap | ||
114 | val newNodeMap = modelAndMaps.newNodeMap | ||
115 | |||
116 | val a = problem.node("a") | ||
117 | val b = problem.node("b") | ||
118 | val Person = problem.findClass("Person") | ||
119 | val PersonNew = problem.findClass("Person").newNode | ||
120 | val exists = builtin.pred("exists") | ||
121 | val equals = builtin.findClass("node").reference("equals") | ||
122 | |||
123 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))) | ||
124 | assertTrue(model.getDataRepresentations().contains(relationMap.get(exists))) | ||
125 | assertTrue(model.getDataRepresentations().contains(relationMap.get(equals))) | ||
126 | |||
127 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
128 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
129 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
130 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
131 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(b))).equals(TruthValue.FALSE)) | ||
132 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(a))).equals(TruthValue.FALSE)) | ||
133 | |||
134 | assertTrue(model.get(relationMap.get(exists), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) | ||
135 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) | ||
136 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(a))).equals(TruthValue.FALSE)) | ||
137 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(b))).equals(TruthValue.FALSE)) | ||
138 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | ||
139 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | ||
140 | } | ||
141 | |||
142 | //Testing the equals and exists from the built in problem with a different example | ||
143 | @Test | ||
144 | def void equalsAndExistTest2() { | ||
145 | val problem = parseHelper.parse(''' | ||
146 | class Person. | ||
147 | |||
148 | Person(a). | ||
149 | Person(b). | ||
150 | ''') | ||
151 | val builtin = problem.builtin | ||
152 | EcoreUtil.resolveAll(problem) | ||
153 | |||
154 | val modelAndMaps = mapper.transformProblem(problem) | ||
155 | assertThat(modelAndMaps, notNullValue()) | ||
156 | |||
157 | val model = modelAndMaps.model | ||
158 | val relationMap = modelAndMaps.relationMap | ||
159 | val nodeMap = modelAndMaps.nodeMap | ||
160 | val newNodeMap = modelAndMaps.newNodeMap | ||
161 | |||
162 | val a = problem.node("a") | ||
163 | val b = problem.node("b") | ||
164 | val Person = problem.findClass("Person") | ||
165 | val PersonNew = problem.findClass("Person").newNode | ||
166 | val exists = builtin.pred("exists") | ||
167 | val equals = builtin.findClass("node").reference("equals") | ||
168 | |||
169 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))) | ||
170 | assertTrue(model.getDataRepresentations().contains(relationMap.get(exists))) | ||
171 | assertTrue(model.getDataRepresentations().contains(relationMap.get(equals))) | ||
172 | |||
173 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
174 | assertTrue(model.get(relationMap.get(exists), Tuple.of(nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
175 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(a))).equals(TruthValue.TRUE)) | ||
176 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(b))).equals(TruthValue.TRUE)) | ||
177 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), nodeMap.get(b))).equals(TruthValue.FALSE)) | ||
178 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), nodeMap.get(a))).equals(TruthValue.FALSE)) | ||
179 | |||
180 | assertTrue(model.get(relationMap.get(exists), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) | ||
181 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), newNodeMap.get(PersonNew))).equals(TruthValue.UNKNOWN)) | ||
182 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(a))).equals(TruthValue.FALSE)) | ||
183 | assertTrue(model.get(relationMap.get(equals), Tuple.of(newNodeMap.get(PersonNew), nodeMap.get(b))).equals(TruthValue.FALSE)) | ||
184 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(a), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | ||
185 | assertTrue(model.get(relationMap.get(equals), Tuple.of(nodeMap.get(b), newNodeMap.get(PersonNew))).equals(TruthValue.FALSE)) | ||
186 | } | ||
187 | |||
188 | //Testing the behavior of the newNodes | ||
189 | @Test | ||
190 | def void newNodeTest(){ | ||
191 | val problem = parseHelper.parse(''' | ||
192 | class Person. | ||
193 | abstract class Family. | ||
194 | ''') | ||
195 | EcoreUtil.resolveAll(problem) | ||
196 | |||
197 | val modelAndMaps = mapper.transformProblem(problem) | ||
198 | assertThat(modelAndMaps, notNullValue()) | ||
199 | |||
200 | val model = modelAndMaps.model | ||
201 | val relationMap = modelAndMaps.relationMap | ||
202 | val newNodeMap = modelAndMaps.newNodeMap | ||
203 | |||
204 | val Person = problem.findClass("Person") | ||
205 | val Family = problem.findClass("Family") | ||
206 | val PersonNew = problem.findClass("Person").newNode | ||
207 | |||
208 | |||
209 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))) | ||
210 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Family))) | ||
211 | |||
212 | assertTrue(newNodeMap.size.equals(4)) //3 from builtin.problem, 1 from Person | ||
213 | assertTrue(model.get(relationMap.get(Person), Tuple.of(newNodeMap.get(PersonNew))).equals(TruthValue.TRUE)) | ||
214 | } | ||
215 | |||
216 | //Testing the behavior of enumerations | ||
217 | @Test | ||
218 | def void enumTest(){ | ||
219 | val problem = parseHelper.parse(''' | ||
220 | enum TaxStatus { | ||
221 | child, student, adult, retired | ||
222 | } | ||
223 | ''') | ||
224 | EcoreUtil.resolveAll(problem) | ||
225 | |||
226 | val modelAndMaps = mapper.transformProblem(problem) | ||
227 | assertThat(modelAndMaps, notNullValue()) | ||
228 | |||
229 | val model = modelAndMaps.model | ||
230 | val relationMap = modelAndMaps.relationMap | ||
231 | val enumNodeMap = modelAndMaps.enumNodeMap | ||
232 | |||
233 | val TaxStatus = problem.findEnum("TaxStatus") | ||
234 | val child = problem.findEnum("TaxStatus").literal("child") | ||
235 | val student = problem.findEnum("TaxStatus").literal("student") | ||
236 | val adult = problem.findEnum("TaxStatus").literal("adult") | ||
237 | val retired = problem.findEnum("TaxStatus").literal("retired") | ||
238 | |||
239 | assertTrue(model.getDataRepresentations().contains(relationMap.get(TaxStatus))) | ||
240 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(child))).equals(TruthValue.TRUE)) | ||
241 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(student))).equals(TruthValue.TRUE)) | ||
242 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(adult))).equals(TruthValue.TRUE)) | ||
243 | assertTrue(model.get(relationMap.get(TaxStatus), Tuple.of(enumNodeMap.get(retired))).equals(TruthValue.TRUE)) | ||
244 | } | ||
245 | |||
246 | //Testing the bool from the built in problem | ||
247 | @Test | ||
248 | def void builtinBoolTest(){ | ||
249 | val problem = parseHelper.parse(''' | ||
250 | class Person. | ||
251 | ''') | ||
252 | EcoreUtil.resolveAll(problem) | ||
253 | val builtin = problem.builtin | ||
254 | |||
255 | val modelAndMaps = mapper.transformProblem(problem) | ||
256 | assertThat(modelAndMaps, notNullValue()) | ||
257 | |||
258 | val model = modelAndMaps.model | ||
259 | val relationMap = modelAndMaps.relationMap | ||
260 | val enumNodeMap = modelAndMaps.enumNodeMap | ||
261 | |||
262 | val bool = builtin.findEnum("bool") | ||
263 | val trueEnum = builtin.findEnum("bool").literal("true") //Emiatt nem sikerül a teszt | ||
264 | val falseEnum = builtin.findEnum("bool").literal("false") | ||
265 | |||
266 | assertTrue(model.getDataRepresentations().contains(relationMap.get(bool))) | ||
267 | assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(trueEnum))).equals(TruthValue.TRUE)) | ||
268 | assertTrue(model.get(relationMap.get(bool), Tuple.of(enumNodeMap.get(falseEnum))).equals(TruthValue.TRUE)) | ||
269 | } | ||
270 | |||
271 | //Testing different aspects of the behavior | ||
272 | @Test | ||
273 | def void compositeTest() { | ||
274 | val problem = parseHelper.parse(''' | ||
275 | class Family { | ||
276 | contains Person[] members | ||
277 | } | ||
278 | |||
279 | class Person { | ||
280 | Person[0..*] children | ||
281 | Person[0..1] parent | ||
282 | TaxStatus taxStatus | ||
283 | } | ||
284 | |||
285 | enum TaxStatus { | ||
286 | child, student, adult, retired | ||
287 | } | ||
288 | |||
289 | % A child cannot have any dependents. | ||
290 | error invalidTaxStatus(Person p) <-> | ||
291 | taxStatus(p, child), children(p, _q). | ||
292 | |||
293 | indiv family. | ||
294 | Family(family). | ||
295 | members(family, anne): true. | ||
296 | members(family, bob). | ||
297 | members(family, ciri). | ||
298 | children(anne, ciri). | ||
299 | ?children(bob, ciri). | ||
300 | taxStatus(anne, adult). | ||
301 | ''') | ||
302 | EcoreUtil.resolveAll(problem) | ||
303 | |||
304 | val modelAndMaps = mapper.transformProblem(problem) | ||
305 | assertThat(modelAndMaps, notNullValue()) | ||
306 | |||
307 | val model = modelAndMaps.model | ||
308 | val relationMap = modelAndMaps.relationMap | ||
309 | val nodeMap = modelAndMaps.nodeMap | ||
310 | val uniqueNodeMap = modelAndMaps.uniqueNodeMap | ||
311 | val enumNodeMap = modelAndMaps.enumNodeMap | ||
312 | |||
313 | val Family = problem.findClass("Family") | ||
314 | val members = problem.findClass("Family").reference("members") | ||
315 | val Person = problem.findClass("Person") | ||
316 | val children = problem.findClass("Person").reference("children") | ||
317 | val parent = problem.findClass("Person").reference("parent") | ||
318 | val taxStatus = problem.findClass("Person").reference("taxStatus") | ||
319 | val TaxStatus = problem.findEnum("TaxStatus") | ||
320 | val invalidTaxStatus = problem.pred("invalidTaxStatus") | ||
321 | |||
322 | val anne = problem.node("anne") | ||
323 | val bob = problem.node("bob") | ||
324 | val ciri = problem.node("ciri") | ||
325 | val family = problem.individualNode("family") | ||
326 | val adult = problem.findEnum("TaxStatus").literal("adult") | ||
327 | |||
328 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Family))) | ||
329 | assertTrue(model.getDataRepresentations().contains(relationMap.get(members))) | ||
330 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Person))) | ||
331 | assertTrue(model.getDataRepresentations().contains(relationMap.get(children))) | ||
332 | assertTrue(model.getDataRepresentations().contains(relationMap.get(parent))) | ||
333 | assertTrue(model.getDataRepresentations().contains(relationMap.get(taxStatus))) | ||
334 | assertTrue(model.getDataRepresentations().contains(relationMap.get(TaxStatus))) | ||
335 | assertTrue(model.getDataRepresentations().contains(relationMap.get(invalidTaxStatus))) | ||
336 | |||
337 | assertTrue(model.get(relationMap.get(Family), Tuple.of(uniqueNodeMap.get(family))).equals(TruthValue.TRUE)) | ||
338 | assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(anne))).equals(TruthValue.TRUE)) | ||
339 | assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(bob))).equals(TruthValue.TRUE)) | ||
340 | assertTrue(model.get(relationMap.get(members), Tuple.of(uniqueNodeMap.get(family),nodeMap.get(ciri))).equals(TruthValue.TRUE)) | ||
341 | assertTrue(model.get(relationMap.get(children), Tuple.of(nodeMap.get(anne),nodeMap.get(ciri))).equals(TruthValue.TRUE)) | ||
342 | assertTrue(model.get(relationMap.get(children), Tuple.of(nodeMap.get(bob),nodeMap.get(ciri))).equals(TruthValue.UNKNOWN)) | ||
343 | assertTrue(model.get(relationMap.get(taxStatus), Tuple.of(nodeMap.get(anne),enumNodeMap.get(adult))).equals(TruthValue.TRUE)) | ||
344 | } | ||
345 | |||
346 | @Test | ||
347 | def void carCaseStudyTest(){ | ||
348 | val problem = parseHelper.parse(''' | ||
349 | abstract class DynamicComponent { | ||
350 | contains StaticComponent[1..1] placedOn | ||
351 | } | ||
352 | abstract class StaticComponent. | ||
353 | class Car extends DynamicComponent. | ||
354 | class Pedestrian extends DynamicComponent. | ||
355 | class Road extends StaticComponent { | ||
356 | contains LaneSegment[0..*] lanes | ||
357 | } | ||
358 | class LaneSegment extends StaticComponent { | ||
359 | Lane[0..*] adjacentLanes | ||
360 | Lane[0..*] sameDirLanes | ||
361 | } | ||
362 | |||
363 | Car(c1). | ||
364 | Car(c2). | ||
365 | Pedestrian(p1). | ||
366 | Road(r1). | ||
367 | LaneSegment(l1). | ||
368 | LaneSegment(l2). | ||
369 | LaneSegment(l3). | ||
370 | placedOn(c1,l1). | ||
371 | placedOn(c2,l2). | ||
372 | placedOn(p1,l3). | ||
373 | lanes(r1,l1). | ||
374 | lanes(r1,l2). | ||
375 | lanes(r1,l3). | ||
376 | adjacentLanes(l1,l2). | ||
377 | adjacentLanes(l2,l1). | ||
378 | sameDirLanes(l1,l3). | ||
379 | sameDirLanes(l3,l1). | ||
380 | ''') | ||
381 | EcoreUtil.resolveAll(problem) | ||
382 | |||
383 | val modelAndMaps = mapper.transformProblem(problem) | ||
384 | assertThat(modelAndMaps, notNullValue()) | ||
385 | |||
386 | val model = modelAndMaps.model | ||
387 | val relationMap = modelAndMaps.relationMap | ||
388 | val nodeMap = modelAndMaps.nodeMap | ||
389 | |||
390 | val DynamicComponent = problem.findClass("DynamicComponent") | ||
391 | val placedOn = problem.findClass("DynamicComponent").reference("placedOn") | ||
392 | val StaticComponent = problem.findClass("StaticComponent") | ||
393 | val Car = problem.findClass("Car") | ||
394 | val Pedestrian = problem.findClass("Pedestrian") | ||
395 | val Road = problem.findClass("Road") | ||
396 | val lanes = problem.findClass("Road").reference("lanes") | ||
397 | val LaneSegment = problem.findClass("LaneSegment") | ||
398 | val adjacentLanes = problem.findClass("LaneSegment").reference("adjacentLanes") | ||
399 | val sameDirLanes = problem.findClass("LaneSegment").reference("sameDirLanes") | ||
400 | |||
401 | val c1 = problem.node("c1") | ||
402 | val c2 = problem.node("c2") | ||
403 | val p1 = problem.node("p1") | ||
404 | val r1 = problem.node("r1") | ||
405 | val l1 = problem.node("l1") | ||
406 | val l2 = problem.node("l2") | ||
407 | val l3 = problem.node("l3") | ||
408 | |||
409 | assertTrue(model.getDataRepresentations().contains(relationMap.get(DynamicComponent))) | ||
410 | assertTrue(model.getDataRepresentations().contains(relationMap.get(placedOn))) | ||
411 | assertTrue(model.getDataRepresentations().contains(relationMap.get(StaticComponent))) | ||
412 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Car))) | ||
413 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Pedestrian))) | ||
414 | assertTrue(model.getDataRepresentations().contains(relationMap.get(Road))) | ||
415 | assertTrue(model.getDataRepresentations().contains(relationMap.get(lanes))) | ||
416 | assertTrue(model.getDataRepresentations().contains(relationMap.get(LaneSegment))) | ||
417 | assertTrue(model.getDataRepresentations().contains(relationMap.get(adjacentLanes))) | ||
418 | assertTrue(model.getDataRepresentations().contains(relationMap.get(sameDirLanes))) | ||
419 | |||
420 | assertTrue(model.get(relationMap.get(Car), Tuple.of(nodeMap.get(c1))).equals(TruthValue.TRUE)) | ||
421 | assertTrue(model.get(relationMap.get(Car), Tuple.of(nodeMap.get(c2))).equals(TruthValue.TRUE)) | ||
422 | assertTrue(model.get(relationMap.get(Pedestrian), Tuple.of(nodeMap.get(p1))).equals(TruthValue.TRUE)) | ||
423 | assertTrue(model.get(relationMap.get(Road), Tuple.of(nodeMap.get(r1))).equals(TruthValue.TRUE)) | ||
424 | assertTrue(model.get(relationMap.get(LaneSegment), Tuple.of(nodeMap.get(l1))).equals(TruthValue.TRUE)) | ||
425 | assertTrue(model.get(relationMap.get(LaneSegment), Tuple.of(nodeMap.get(l2))).equals(TruthValue.TRUE)) | ||
426 | assertTrue(model.get(relationMap.get(LaneSegment), Tuple.of(nodeMap.get(l3))).equals(TruthValue.TRUE)) | ||
427 | assertTrue(model.get(relationMap.get(placedOn), Tuple.of(nodeMap.get(c1),nodeMap.get(l1))).equals(TruthValue.TRUE)) | ||
428 | assertTrue(model.get(relationMap.get(placedOn), Tuple.of(nodeMap.get(c2),nodeMap.get(l2))).equals(TruthValue.TRUE)) | ||
429 | assertTrue(model.get(relationMap.get(placedOn), Tuple.of(nodeMap.get(p1),nodeMap.get(l3))).equals(TruthValue.TRUE)) | ||
430 | assertTrue(model.get(relationMap.get(lanes), Tuple.of(nodeMap.get(r1),nodeMap.get(l1))).equals(TruthValue.TRUE)) | ||
431 | assertTrue(model.get(relationMap.get(lanes), Tuple.of(nodeMap.get(r1),nodeMap.get(l2))).equals(TruthValue.TRUE)) | ||
432 | assertTrue(model.get(relationMap.get(lanes), Tuple.of(nodeMap.get(r1),nodeMap.get(l3))).equals(TruthValue.TRUE)) | ||
433 | assertTrue(model.get(relationMap.get(adjacentLanes), Tuple.of(nodeMap.get(l1),nodeMap.get(l2))).equals(TruthValue.TRUE)) | ||
434 | assertTrue(model.get(relationMap.get(adjacentLanes), Tuple.of(nodeMap.get(l2),nodeMap.get(l1))).equals(TruthValue.TRUE)) | ||
435 | assertTrue(model.get(relationMap.get(sameDirLanes), Tuple.of(nodeMap.get(l1),nodeMap.get(l3))).equals(TruthValue.TRUE)) | ||
436 | assertTrue(model.get(relationMap.get(sameDirLanes), Tuple.of(nodeMap.get(l3),nodeMap.get(l1))).equals(TruthValue.TRUE)) | ||
437 | } | ||
438 | } | ||