diff options
2 files changed, 68 insertions, 29 deletions
diff --git a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/model/ModelInitializer.java b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/model/ModelInitializer.java index 93c7c8e5..d2990aff 100644 --- a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/model/ModelInitializer.java +++ b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/model/ModelInitializer.java | |||
@@ -53,10 +53,14 @@ public class ModelInitializer { | |||
53 | 53 | ||
54 | private final Map<Relation, RelationInfo> relationInfoMap = new LinkedHashMap<>(); | 54 | private final Map<Relation, RelationInfo> relationInfoMap = new LinkedHashMap<>(); |
55 | 55 | ||
56 | private final Map<PartialRelation, RelationInfo> partialRelationInfoMap = new LinkedHashMap<>(); | ||
57 | |||
56 | private Map<Relation, PartialRelation> relationTrace; | 58 | private Map<Relation, PartialRelation> relationTrace; |
57 | 59 | ||
58 | private final MetamodelBuilder metamodelBuilder = Metamodel.builder(); | 60 | private final MetamodelBuilder metamodelBuilder = Metamodel.builder(); |
59 | 61 | ||
62 | private Metamodel metamodel; | ||
63 | |||
60 | public int getNodeCount() { | 64 | public int getNodeCount() { |
61 | return nodeTrace.size(); | 65 | return nodeTrace.size(); |
62 | } | 66 | } |
@@ -75,13 +79,15 @@ public class ModelInitializer { | |||
75 | "Problem has no builtin library")); | 79 | "Problem has no builtin library")); |
76 | var nodeInfo = collectPartialRelation(builtinSymbols.node(), 1, TruthValue.TRUE, TruthValue.TRUE); | 80 | var nodeInfo = collectPartialRelation(builtinSymbols.node(), 1, TruthValue.TRUE, TruthValue.TRUE); |
77 | nodeRelation = nodeInfo.partialRelation(); | 81 | nodeRelation = nodeInfo.partialRelation(); |
78 | metamodelBuilder.type(nodeRelation, true); | 82 | metamodelBuilder.type(nodeRelation); |
79 | relationInfoMap.put(builtinSymbols.exists(), new RelationInfo(ReasoningAdapter.EXISTS_SYMBOL, null, | 83 | relationInfoMap.put(builtinSymbols.exists(), new RelationInfo(ReasoningAdapter.EXISTS_SYMBOL, null, |
80 | TruthValue.TRUE)); | 84 | TruthValue.TRUE)); |
81 | relationInfoMap.put(builtinSymbols.equals(), new RelationInfo(ReasoningAdapter.EQUALS_SYMBOL, (TruthValue) null, | 85 | relationInfoMap.put(builtinSymbols.equals(), new RelationInfo(ReasoningAdapter.EQUALS_SYMBOL, |
86 | (TruthValue) null, | ||
82 | null)); | 87 | null)); |
83 | relationInfoMap.put(builtinSymbols.contained(), new RelationInfo(ContainmentHierarchyTranslator.CONTAINED_SYMBOL, | 88 | relationInfoMap.put(builtinSymbols.contained(), |
84 | null, TruthValue.UNKNOWN)); | 89 | new RelationInfo(ContainmentHierarchyTranslator.CONTAINED_SYMBOL, |
90 | null, TruthValue.UNKNOWN)); | ||
85 | relationInfoMap.put(builtinSymbols.contains(), new RelationInfo(ContainmentHierarchyTranslator.CONTAINS_SYMBOL, | 91 | relationInfoMap.put(builtinSymbols.contains(), new RelationInfo(ContainmentHierarchyTranslator.CONTAINS_SYMBOL, |
86 | null, TruthValue.UNKNOWN)); | 92 | null, TruthValue.UNKNOWN)); |
87 | relationInfoMap.put(builtinSymbols.invalidNumberOfContainers(), | 93 | relationInfoMap.put(builtinSymbols.invalidNumberOfContainers(), |
@@ -89,9 +95,9 @@ public class ModelInitializer { | |||
89 | TruthValue.FALSE)); | 95 | TruthValue.FALSE)); |
90 | collectNodes(); | 96 | collectNodes(); |
91 | collectPartialSymbols(); | 97 | collectPartialSymbols(); |
98 | collectMetamodel(); | ||
99 | metamodel = metamodelBuilder.build(); | ||
92 | collectAssertions(); | 100 | collectAssertions(); |
93 | var metamodel = metamodelBuilder.build(); | ||
94 | builder.with(ReasoningAdapter.builder()); | ||
95 | builder.with(new MultiObjectTranslator()); | 101 | builder.with(new MultiObjectTranslator()); |
96 | builder.with(new MetamodelTranslator(metamodel)); | 102 | builder.with(new MetamodelTranslator(metamodel)); |
97 | relationTrace = new LinkedHashMap<>(relationInfoMap.size()); | 103 | relationTrace = new LinkedHashMap<>(relationInfoMap.size()); |
@@ -160,11 +166,18 @@ public class ModelInitializer { | |||
160 | } | 166 | } |
161 | } | 167 | } |
162 | 168 | ||
169 | private void putRelationInfo(Relation relation, RelationInfo info) { | ||
170 | relationInfoMap.put(relation, info); | ||
171 | partialRelationInfoMap.put(info.partialRelation(), info); | ||
172 | } | ||
173 | |||
163 | private RelationInfo collectPartialRelation(Relation relation, int arity, TruthValue value, | 174 | private RelationInfo collectPartialRelation(Relation relation, int arity, TruthValue value, |
164 | TruthValue defaultValue) { | 175 | TruthValue defaultValue) { |
165 | return relationInfoMap.computeIfAbsent(relation, key -> { | 176 | return relationInfoMap.computeIfAbsent(relation, key -> { |
166 | var name = getName(relation); | 177 | var name = getName(relation); |
167 | return new RelationInfo(name, arity, value, defaultValue); | 178 | var info = new RelationInfo(name, arity, value, defaultValue); |
179 | partialRelationInfoMap.put(info.partialRelation(), info); | ||
180 | return info; | ||
168 | }); | 181 | }); |
169 | } | 182 | } |
170 | 183 | ||
@@ -172,23 +185,22 @@ public class ModelInitializer { | |||
172 | return semanticsUtils.getName(relation).orElseGet(() -> "#" + relationInfoMap.size()); | 185 | return semanticsUtils.getName(relation).orElseGet(() -> "#" + relationInfoMap.size()); |
173 | } | 186 | } |
174 | 187 | ||
175 | private void collectAssertions() { | 188 | private void collectMetamodel() { |
176 | for (var statement : problem.getStatements()) { | 189 | for (var statement : problem.getStatements()) { |
177 | if (statement instanceof ClassDeclaration classDeclaration) { | 190 | if (statement instanceof ClassDeclaration classDeclaration) { |
178 | collectClassDeclarationAssertions(classDeclaration); | 191 | collectClassDeclarationMetamodel(classDeclaration); |
179 | } else if (statement instanceof EnumDeclaration enumDeclaration) { | 192 | } else if (statement instanceof EnumDeclaration enumDeclaration) { |
180 | collectEnumAssertions(enumDeclaration); | 193 | collectEnumMetamodel(enumDeclaration); |
181 | } else if (statement instanceof IndividualDeclaration individualDeclaration) { | ||
182 | for (var individual : individualDeclaration.getNodes()) { | ||
183 | collectIndividualAssertions(individual); | ||
184 | } | ||
185 | } else if (statement instanceof Assertion assertion) { | ||
186 | collectAssertion(assertion); | ||
187 | } | 194 | } |
188 | } | 195 | } |
189 | } | 196 | } |
190 | 197 | ||
191 | private void collectClassDeclarationAssertions(ClassDeclaration classDeclaration) { | 198 | private void collectEnumMetamodel(EnumDeclaration enumDeclaration) { |
199 | var info = getRelationInfo(enumDeclaration); | ||
200 | metamodelBuilder.type(info.partialRelation(), nodeRelation); | ||
201 | } | ||
202 | |||
203 | private void collectClassDeclarationMetamodel(ClassDeclaration classDeclaration) { | ||
192 | var superTypes = classDeclaration.getSuperTypes(); | 204 | var superTypes = classDeclaration.getSuperTypes(); |
193 | var partialSuperTypes = new ArrayList<PartialRelation>(superTypes.size() + 1); | 205 | var partialSuperTypes = new ArrayList<PartialRelation>(superTypes.size() + 1); |
194 | partialSuperTypes.add(nodeRelation); | 206 | partialSuperTypes.add(nodeRelation); |
@@ -198,23 +210,17 @@ public class ModelInitializer { | |||
198 | var info = getRelationInfo(classDeclaration); | 210 | var info = getRelationInfo(classDeclaration); |
199 | metamodelBuilder.type(info.partialRelation(), classDeclaration.isAbstract(), | 211 | metamodelBuilder.type(info.partialRelation(), classDeclaration.isAbstract(), |
200 | partialSuperTypes); | 212 | partialSuperTypes); |
201 | var newNode = classDeclaration.getNewNode(); | ||
202 | if (newNode != null) { | ||
203 | var newNodeId = getNodeId(newNode); | ||
204 | collectCardinalityAssertions(newNodeId, TruthValue.UNKNOWN); | ||
205 | mergeValue(classDeclaration, Tuple.of(newNodeId), TruthValue.TRUE); | ||
206 | } | ||
207 | for (var featureDeclaration : classDeclaration.getFeatureDeclarations()) { | 213 | for (var featureDeclaration : classDeclaration.getFeatureDeclarations()) { |
208 | if (featureDeclaration instanceof ReferenceDeclaration referenceDeclaration) { | 214 | if (featureDeclaration instanceof ReferenceDeclaration referenceDeclaration) { |
209 | collectReferenceDeclarationAssertions(classDeclaration, referenceDeclaration); | 215 | collectReferenceDeclarationMetamodel(classDeclaration, referenceDeclaration); |
210 | } else { | 216 | } else { |
211 | throw new IllegalArgumentException("Unknown feature declaration: " + featureDeclaration); | 217 | throw new IllegalArgumentException("Unknown feature declaration: " + featureDeclaration); |
212 | } | 218 | } |
213 | } | 219 | } |
214 | } | 220 | } |
215 | 221 | ||
216 | private void collectReferenceDeclarationAssertions(ClassDeclaration classDeclaration, | 222 | private void collectReferenceDeclarationMetamodel(ClassDeclaration classDeclaration, |
217 | ReferenceDeclaration referenceDeclaration) { | 223 | ReferenceDeclaration referenceDeclaration) { |
218 | var relation = getRelationInfo(referenceDeclaration).partialRelation(); | 224 | var relation = getRelationInfo(referenceDeclaration).partialRelation(); |
219 | var source = getRelationInfo(classDeclaration).partialRelation(); | 225 | var source = getRelationInfo(classDeclaration).partialRelation(); |
220 | var target = getRelationInfo(referenceDeclaration.getReferenceType()).partialRelation(); | 226 | var target = getRelationInfo(referenceDeclaration.getReferenceType()).partialRelation(); |
@@ -249,15 +255,47 @@ public class ModelInitializer { | |||
249 | return ConstrainedMultiplicity.of(interval, constraint); | 255 | return ConstrainedMultiplicity.of(interval, constraint); |
250 | } | 256 | } |
251 | 257 | ||
258 | |||
259 | private void collectAssertions() { | ||
260 | for (var statement : problem.getStatements()) { | ||
261 | if (statement instanceof ClassDeclaration classDeclaration) { | ||
262 | collectClassDeclarationAssertions(classDeclaration); | ||
263 | } else if (statement instanceof EnumDeclaration enumDeclaration) { | ||
264 | collectEnumAssertions(enumDeclaration); | ||
265 | } else if (statement instanceof IndividualDeclaration individualDeclaration) { | ||
266 | for (var individual : individualDeclaration.getNodes()) { | ||
267 | collectIndividualAssertions(individual); | ||
268 | } | ||
269 | } else if (statement instanceof Assertion assertion) { | ||
270 | collectAssertion(assertion); | ||
271 | } | ||
272 | } | ||
273 | } | ||
274 | |||
275 | private void collectClassDeclarationAssertions(ClassDeclaration classDeclaration) { | ||
276 | var newNode = classDeclaration.getNewNode(); | ||
277 | if (newNode == null) { | ||
278 | return; | ||
279 | } | ||
280 | var newNodeId = getNodeId(newNode); | ||
281 | collectCardinalityAssertions(newNodeId, TruthValue.UNKNOWN); | ||
282 | var info = getRelationInfo(classDeclaration); | ||
283 | var tuple = Tuple.of(newNodeId); | ||
284 | mergeValue(classDeclaration, tuple, TruthValue.TRUE); | ||
285 | var typeInfo = metamodel.typeHierarchy().getAnalysisResult(info.partialRelation()); | ||
286 | for (var subType : typeInfo.getDirectSubtypes()) { | ||
287 | partialRelationInfoMap.get(subType).assertions().mergeValue(tuple, TruthValue.FALSE); | ||
288 | } | ||
289 | } | ||
290 | |||
252 | private void collectEnumAssertions(EnumDeclaration enumDeclaration) { | 291 | private void collectEnumAssertions(EnumDeclaration enumDeclaration) { |
253 | var info = getRelationInfo(enumDeclaration); | ||
254 | metamodelBuilder.type(info.partialRelation(), nodeRelation); | ||
255 | var overlay = new DecisionTree(1, null); | 292 | var overlay = new DecisionTree(1, null); |
256 | for (var literal : enumDeclaration.getLiterals()) { | 293 | for (var literal : enumDeclaration.getLiterals()) { |
257 | collectIndividualAssertions(literal); | 294 | collectIndividualAssertions(literal); |
258 | var nodeId = getNodeId(literal); | 295 | var nodeId = getNodeId(literal); |
259 | overlay.mergeValue(Tuple.of(nodeId), TruthValue.TRUE); | 296 | overlay.mergeValue(Tuple.of(nodeId), TruthValue.TRUE); |
260 | } | 297 | } |
298 | var info = getRelationInfo(enumDeclaration); | ||
261 | info.assertions().overwriteValues(overlay); | 299 | info.assertions().overwriteValues(overlay); |
262 | } | 300 | } |
263 | 301 | ||
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsService.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsService.java index 79ca32ff..c828b3d5 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsService.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/SemanticsService.java | |||
@@ -64,7 +64,8 @@ public class SemanticsService extends AbstractCachedService<SemanticsResult> { | |||
64 | } | 64 | } |
65 | var initializer = initializerProvider.get(); | 65 | var initializer = initializerProvider.get(); |
66 | var builder = ModelStore.builder() | 66 | var builder = ModelStore.builder() |
67 | .with(ViatraModelQueryAdapter.builder()); | 67 | .with(ViatraModelQueryAdapter.builder()) |
68 | .with(ReasoningAdapter.builder()); | ||
68 | operationCanceledManager.checkCanceled(cancelIndicator); | 69 | operationCanceledManager.checkCanceled(cancelIndicator); |
69 | try { | 70 | try { |
70 | var modelSeed = initializer.createModel(problem, builder); | 71 | var modelSeed = initializer.createModel(problem, builder); |