aboutsummaryrefslogtreecommitdiffstats
path: root/language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java
diff options
context:
space:
mode:
Diffstat (limited to 'language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java')
-rw-r--r--language-to-store/src/main/java/tools/refinery/language/mapping/PartialModelMapper.java226
1 files changed, 223 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 @@
1package tools.refinery.language.mapping; 1package tools.refinery.language.mapping;
2 2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Optional;
7
8import org.eclipse.emf.common.util.EList;
9
10import tools.refinery.language.ProblemUtil;
11import tools.refinery.language.model.problem.Assertion;
12import tools.refinery.language.model.problem.AssertionArgument;
13import tools.refinery.language.model.problem.ClassDeclaration;
14import tools.refinery.language.model.problem.EnumDeclaration;
15import tools.refinery.language.model.problem.LogicValue;
16import tools.refinery.language.model.problem.Node;
17import tools.refinery.language.model.problem.NodeAssertionArgument;
18import tools.refinery.language.model.problem.PredicateDefinition;
3import tools.refinery.language.model.problem.Problem; 19import tools.refinery.language.model.problem.Problem;
20import tools.refinery.language.model.problem.ReferenceDeclaration;
21import tools.refinery.language.model.problem.Statement;
22import tools.refinery.language.model.problem.UniqueDeclaration;
4import tools.refinery.store.model.Model; 23import tools.refinery.store.model.Model;
24import tools.refinery.store.model.ModelStore;
25import tools.refinery.store.model.ModelStoreImpl;
26import tools.refinery.store.model.Tuple;
27import tools.refinery.store.model.representation.Relation;
28import tools.refinery.store.model.representation.TruthValue;
29
5 30
6public class PartialModelMapper { 31public 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}