diff options
Diffstat (limited to 'language-to-store/src/main/java/tools/refinery')
2 files changed, 277 insertions, 3 deletions
diff --git a/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java index a2cab671..5dacc8cb 100644 --- a/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java +++ b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java | |||
@@ -1,11 +1,231 @@ | |||
1 | package tools.refinery.language.mapping; | 1 | package tools.refinery.language.mapping; |
2 | 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.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.LogicValue; | ||
16 | import tools.refinery.language.model.problem.Node; | ||
17 | import tools.refinery.language.model.problem.NodeAssertionArgument; | ||
18 | import tools.refinery.language.model.problem.PredicateDefinition; | ||
3 | import tools.refinery.language.model.problem.Problem; | 19 | import tools.refinery.language.model.problem.Problem; |
20 | import tools.refinery.language.model.problem.ReferenceDeclaration; | ||
21 | import tools.refinery.language.model.problem.Statement; | ||
22 | import tools.refinery.language.model.problem.UniqueDeclaration; | ||
4 | import tools.refinery.store.model.Model; | 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 | |||
5 | 30 | ||
6 | public class PartialModelMapper { | 31 | public class PartialModelMapper { |
7 | public Model transformProblem(Problem problem) { | 32 | |
8 | // TODO @Marci Implement this | 33 | private int nodeIter; |
9 | throw new UnsupportedOperationException(); | 34 | public PartialModelMapper() { |
35 | this.nodeIter = 0; | ||
36 | } | ||
37 | |||
38 | public PartialModelMapperDTO transformProblem(Problem problem) throws Exception { | ||
39 | PartialModelMapperDTO pmmDTO = initTransform(problem); | ||
40 | |||
41 | Optional<Problem> builtinProblem = ProblemUtil.getBuiltInLibrary(problem); | ||
42 | if (builtinProblem.isEmpty()) throw new Exception("builtin.problem not found"); | ||
43 | PartialModelMapperDTO builtinProblemDTO = initTransform(builtinProblem.get()); | ||
44 | pmmDTO.getRelationMap().putAll(builtinProblemDTO.getRelationMap()); | ||
45 | pmmDTO.getNodeMap().putAll(builtinProblemDTO.getNodeMap()); | ||
46 | pmmDTO.getEnumNodeMap().putAll(builtinProblemDTO.getEnumNodeMap()); //így most valami nem stimmel | ||
47 | pmmDTO.getNewNodeMap().putAll(builtinProblemDTO.getNewNodeMap()); | ||
48 | pmmDTO.getUniqueNodeMap().putAll(builtinProblemDTO.getUniqueNodeMap()); | ||
49 | |||
50 | //Definition of store and model | ||
51 | ModelStore store = new ModelStoreImpl(new HashSet<>(pmmDTO.getRelationMap().values())); | ||
52 | Model model = store.createModel(); | ||
53 | pmmDTO.setModel(model); | ||
54 | |||
55 | Map<Node,Integer> allNodesMap = mergeNodeMaps(pmmDTO.getEnumNodeMap(), | ||
56 | pmmDTO.getUniqueNodeMap(), | ||
57 | pmmDTO.getNewNodeMap(), | ||
58 | pmmDTO.getNodeMap()); | ||
59 | |||
60 | //Filling up the relations with unknown truth values | ||
61 | for (tools.refinery.language.model.problem.Relation relation : pmmDTO.getRelationMap().keySet()) { | ||
62 | if(!(relation instanceof PredicateDefinition pd && pd.isError())) { | ||
63 | Relation<TruthValue> r = pmmDTO.getRelationMap().get(relation); | ||
64 | if(r.getArity() == 1) | ||
65 | for(Integer i : allNodesMap.values()) { | ||
66 | pmmDTO.getModel().put(r, Tuple.of(i), TruthValue.UNKNOWN); | ||
67 | } | ||
68 | else if(r.getArity() == 2) | ||
69 | for(Integer i : allNodesMap.values()) { | ||
70 | for (Integer j : allNodesMap.values()) { | ||
71 | pmmDTO.getModel().put(r, Tuple.of(i,j), TruthValue.UNKNOWN); | ||
72 | } | ||
73 | } | ||
74 | else throw new Exception("Relation with arity above 2 is not supported"); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | //Filling up the exists | ||
79 | tools.refinery.language.model.problem.Relation existsRelation = null; | ||
80 | for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { | ||
81 | if (r.getName().equals("exists")) existsRelation = r; | ||
82 | } | ||
83 | if(existsRelation.equals(null)) throw new Exception("exists not found"); | ||
84 | for (Node n : allNodesMap.keySet()) { | ||
85 | if(pmmDTO.getNewNodeMap().containsKey(n)) { | ||
86 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | ||
87 | Tuple.of(allNodesMap.get(n)), | ||
88 | TruthValue.UNKNOWN); | ||
89 | } | ||
90 | else { | ||
91 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(existsRelation), | ||
92 | Tuple.of(allNodesMap.get(n)), | ||
93 | TruthValue.TRUE); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | //Filling up the equals | ||
98 | tools.refinery.language.model.problem.Relation equalsRelation = null; | ||
99 | for (tools.refinery.language.model.problem.Relation r : builtinProblemDTO.getRelationMap().keySet()) { | ||
100 | if (r.getName().equals("equals")) equalsRelation = r; | ||
101 | } | ||
102 | if(equalsRelation.equals(null)) throw new Exception("equals not found"); | ||
103 | for (Node n1 : allNodesMap.keySet()) { | ||
104 | for(Node n2 : allNodesMap.keySet()) { | ||
105 | if(n1.equals(n2)) { | ||
106 | if(pmmDTO.getNewNodeMap().containsKey(n1)) { | ||
107 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
108 | Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), | ||
109 | TruthValue.UNKNOWN); | ||
110 | } | ||
111 | else { | ||
112 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
113 | Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), | ||
114 | TruthValue.TRUE); | ||
115 | } | ||
116 | } | ||
117 | else { | ||
118 | pmmDTO.getModel().put(builtinProblemDTO.getRelationMap().get(equalsRelation), | ||
119 | Tuple.of(allNodesMap.get(n1),allNodesMap.get(n2)), | ||
120 | TruthValue.FALSE); | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | //Transforming the assertions | ||
126 | processAssertions(problem, pmmDTO, allNodesMap); | ||
127 | processAssertions(builtinProblem.get(), pmmDTO, allNodesMap); | ||
128 | |||
129 | //throw new UnsupportedOperationException(); | ||
130 | return pmmDTO; | ||
131 | } | ||
132 | |||
133 | private void processAssertions(Problem problem, PartialModelMapperDTO pmmDTO, Map<Node, Integer> allNodesMap) { | ||
134 | for(Statement s : problem.getStatements()) { | ||
135 | if(s instanceof Assertion assertion) { | ||
136 | Relation<TruthValue> r1 = pmmDTO.getRelationMap().get(assertion.getRelation()); | ||
137 | int i = 0; | ||
138 | int[] integers = new int[assertion.getArguments().size()]; | ||
139 | for (AssertionArgument aa : assertion.getArguments()) { | ||
140 | if (aa instanceof NodeAssertionArgument nas) { | ||
141 | integers[i] = allNodesMap.get(nas.getNode()); | ||
142 | i++; | ||
143 | } | ||
144 | } | ||
145 | pmmDTO.getModel().put(r1, Tuple.of(integers), logicValueToTruthValue(assertion.getValue())); | ||
146 | } | ||
147 | else if (s instanceof ClassDeclaration cd) { | ||
148 | if(!cd.isAbstract()) | ||
149 | pmmDTO.getModel().put(pmmDTO.getRelationMap().get(cd), | ||
150 | Tuple.of(pmmDTO.getNewNodeMap().get(cd.getNewNode())), | ||
151 | TruthValue.TRUE); | ||
152 | } | ||
153 | else if (s instanceof EnumDeclaration ed) { | ||
154 | for (Node n : ed.getLiterals()) { | ||
155 | pmmDTO.getModel().put(pmmDTO.getRelationMap().get(ed), | ||
156 | Tuple.of(pmmDTO.getEnumNodeMap().get(n)), | ||
157 | TruthValue.TRUE); | ||
158 | } | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | |||
163 | public PartialModelMapperDTO initTransform(Problem problem) { | ||
164 | //Defining needed Maps | ||
165 | Map<tools.refinery.language.model.problem.Relation, Relation<TruthValue>> relationMap = new HashMap<>(); | ||
166 | Map<Node, Integer> enumNodeMap = new HashMap<>(); | ||
167 | Map<Node, Integer> uniqueNodeMap = new HashMap<>(); | ||
168 | Map<Node, Integer> newNodeMap = new HashMap<>(); | ||
169 | |||
170 | //Definition of Relations, filling up the enumNodeMap, uniqueNodeMap, newNodeMap | ||
171 | EList<Statement> statements = problem.getStatements(); | ||
172 | for (Statement s : statements) { | ||
173 | if (s instanceof ClassDeclaration cd) { | ||
174 | Relation<TruthValue> r1 = new Relation<>(cd.getName(), 1, TruthValue.FALSE); | ||
175 | relationMap.put(cd, r1); | ||
176 | if(!cd.isAbstract()) newNodeMap.put(cd.getNewNode(), this.nodeIter++); | ||
177 | EList<ReferenceDeclaration> refDeclList = cd.getReferenceDeclarations(); | ||
178 | for (ReferenceDeclaration refDec : refDeclList) { | ||
179 | Relation<TruthValue> r2 = new Relation<>(refDec.getName(), 2, TruthValue.FALSE); | ||
180 | relationMap.put(refDec, r2); | ||
181 | } | ||
182 | } | ||
183 | else if (s instanceof EnumDeclaration ed) { | ||
184 | Relation<TruthValue> r = new Relation<>(ed.getName(), 1, TruthValue.FALSE); | ||
185 | relationMap.put(ed, r); | ||
186 | EList<Node> nodeList = ed.getLiterals(); | ||
187 | for (Node n : ed.getLiterals()) { | ||
188 | enumNodeMap.put(n, nodeIter++); | ||
189 | } | ||
190 | } | ||
191 | else if (s instanceof UniqueDeclaration ud) { | ||
192 | for (Node n : ud.getNodes()) { | ||
193 | uniqueNodeMap.put(n, this.nodeIter++); | ||
194 | } | ||
195 | } | ||
196 | else if (s instanceof PredicateDefinition pd) { | ||
197 | Relation<TruthValue> r = new Relation<>(pd.getName(), 1, TruthValue.FALSE); | ||
198 | relationMap.put(pd, r); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | |||
203 | |||
204 | |||
205 | //Filling the nodeMap up | ||
206 | Map<Node, Integer> nodeMap = new HashMap<>(); | ||
207 | for(Node n : problem.getNodes()) { | ||
208 | nodeMap.put(n, this.nodeIter++); | ||
209 | } | ||
210 | |||
211 | return new PartialModelMapperDTO(null,relationMap,nodeMap,enumNodeMap,uniqueNodeMap,newNodeMap); | ||
212 | } | ||
213 | |||
214 | private Map<Node, Integer> mergeNodeMaps(Map<Node, Integer> enumNodeMap, | ||
215 | Map<Node, Integer> uniqueNodeMap, | ||
216 | Map<Node, Integer> newNodeMap, | ||
217 | Map<Node, Integer> nodeMap) { | ||
218 | Map<Node, Integer> out = new HashMap<>(enumNodeMap); | ||
219 | for (Node n : uniqueNodeMap.keySet()) out.put(n, uniqueNodeMap.get(n)); | ||
220 | for (Node n : newNodeMap.keySet()) out.put(n, newNodeMap.get(n)); | ||
221 | for (Node n : nodeMap.keySet()) out.put(n, nodeMap.get(n)); | ||
222 | return out; | ||
223 | } | ||
224 | |||
225 | private TruthValue logicValueToTruthValue(LogicValue value) { | ||
226 | if(value.equals(LogicValue.TRUE)) return TruthValue.TRUE; | ||
227 | else if(value.equals(LogicValue.FALSE)) return TruthValue.FALSE; | ||
228 | else if(value.equals(LogicValue.UNKNOWN)) return TruthValue.UNKNOWN; | ||
229 | else return TruthValue.ERROR; | ||
10 | } | 230 | } |
11 | } | 231 | } |
diff --git a/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java b/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapperDTO.java new file mode 100644 index 00000000..3397b4bd --- /dev/null +++ b/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 | } | ||