diff options
Diffstat (limited to 'subprojects/language-to-store/src/main/java/tools')
2 files changed, 293 insertions, 0 deletions
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 | } | ||