aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/language-semantics/src/main/java/tools/refinery/language/semantics')
-rw-r--r--subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java41
-rw-r--r--subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java106
2 files changed, 106 insertions, 41 deletions
diff --git a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java
index 110295b2..9c40e6df 100644
--- a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java
+++ b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java
@@ -1,5 +1,5 @@
1/* 1/*
2 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> 2 * SPDX-FileCopyrightText: 2023-2024 The Refinery Authors <https://refinery.tools/>
3 * 3 *
4 * SPDX-License-Identifier: EPL-2.0 4 * SPDX-License-Identifier: EPL-2.0
5 */ 5 */
@@ -8,11 +8,14 @@ package tools.refinery.language.semantics;
8import com.google.inject.Inject; 8import com.google.inject.Inject;
9import com.google.inject.Singleton; 9import com.google.inject.Singleton;
10import com.google.inject.name.Named; 10import com.google.inject.name.Named;
11import org.eclipse.emf.ecore.EClass;
11import org.eclipse.emf.ecore.EObject; 12import org.eclipse.emf.ecore.EObject;
12import org.eclipse.emf.ecore.util.EcoreUtil; 13import org.eclipse.emf.ecore.util.EcoreUtil;
13import org.eclipse.xtext.naming.IQualifiedNameConverter; 14import org.eclipse.xtext.naming.IQualifiedNameConverter;
14import org.eclipse.xtext.naming.IQualifiedNameProvider; 15import org.eclipse.xtext.naming.IQualifiedNameProvider;
15import org.eclipse.xtext.naming.QualifiedName; 16import org.eclipse.xtext.naming.QualifiedName;
17import org.eclipse.xtext.resource.IEObjectDescription;
18import org.eclipse.xtext.resource.IResourceDescriptionsProvider;
16import org.eclipse.xtext.scoping.IScope; 19import org.eclipse.xtext.scoping.IScope;
17import org.jetbrains.annotations.NotNull; 20import org.jetbrains.annotations.NotNull;
18import org.jetbrains.annotations.Nullable; 21import org.jetbrains.annotations.Nullable;
@@ -33,6 +36,9 @@ public class SemanticsUtils {
33 @Inject 36 @Inject
34 private IQualifiedNameConverter qualifiedNameConverter; 37 private IQualifiedNameConverter qualifiedNameConverter;
35 38
39 @Inject
40 private IResourceDescriptionsProvider resourceDescriptionsProvider;
41
36 public Optional<String> getNameWithoutRootPrefix(EObject eObject) { 42 public Optional<String> getNameWithoutRootPrefix(EObject eObject) {
37 var qualifiedName = delegateQualifiedNameProvider.getFullyQualifiedName(eObject); 43 var qualifiedName = delegateQualifiedNameProvider.getFullyQualifiedName(eObject);
38 if (qualifiedName == null) { 44 if (qualifiedName == null) {
@@ -42,11 +48,31 @@ public class SemanticsUtils {
42 } 48 }
43 49
44 @Nullable 50 @Nullable
51 public <T> T maybeGetLocalElement(Problem problem, QualifiedName qualifiedName, Class<T> type, EClass eClass) {
52 var resource = problem.eResource();
53 var resourceSet = resource.getResourceSet();
54 var resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(resourceSet);
55 var resourceDescription = resourceDescriptions.getResourceDescription(resource.getURI());
56 if (resourceDescription == null) {
57 return null;
58 }
59 var eObjectDescriptions = resourceDescription.getExportedObjects(eClass, qualifiedName, false);
60 return maybeGet(problem, eObjectDescriptions, qualifiedName, type);
61 }
62
63 @Nullable
45 public <T> T maybeGetElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) { 64 public <T> T maybeGetElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) {
46 if (qualifiedName == null) { 65 if (qualifiedName == null) {
47 throw new IllegalArgumentException("Element name must not be null"); 66 throw new IllegalArgumentException("Element name must not be null");
48 } 67 }
49 var iterator = scope.getElements(qualifiedName).iterator(); 68 var eObjectDescriptions = scope.getElements(qualifiedName);
69 return maybeGet(problem, eObjectDescriptions, qualifiedName, type);
70 }
71
72 @Nullable
73 private <T> T maybeGet(Problem problem, Iterable<IEObjectDescription> eObjectDescriptions,
74 QualifiedName qualifiedName, Class<T> type) {
75 var iterator = eObjectDescriptions.iterator();
50 if (!iterator.hasNext()) { 76 if (!iterator.hasNext()) {
51 return null; 77 return null;
52 } 78 }
@@ -66,8 +92,19 @@ public class SemanticsUtils {
66 } 92 }
67 93
68 @NotNull 94 @NotNull
95 public <T> T getLocalElement(Problem problem, QualifiedName qualifiedName, Class<T> type, EClass eClass) {
96 var element = maybeGetLocalElement(problem, qualifiedName, type, eClass);
97 return getOrThrow(element, qualifiedName, type);
98 }
99
100 @NotNull
69 public <T> T getElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) { 101 public <T> T getElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) {
70 var element = maybeGetElement(problem, scope, qualifiedName, type); 102 var element = maybeGetElement(problem, scope, qualifiedName, type);
103 return getOrThrow(element, qualifiedName, type);
104 }
105
106 @NotNull
107 private <T> T getOrThrow(@Nullable T element, QualifiedName qualifiedName, Class<T> type) {
71 if (element == null) { 108 if (element == null) {
72 var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); 109 var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName);
73 throw new IllegalArgumentException("No such %s: %s" 110 throw new IllegalArgumentException("No such %s: %s"
diff --git a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java
index 2fb0a49d..377a66f3 100644
--- a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java
+++ b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java
@@ -10,6 +10,8 @@ import com.google.inject.Provider;
10import org.eclipse.collections.api.factory.primitive.IntObjectMaps; 10import org.eclipse.collections.api.factory.primitive.IntObjectMaps;
11import org.eclipse.collections.api.map.primitive.MutableIntObjectMap; 11import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
12import org.eclipse.emf.common.util.URI; 12import org.eclipse.emf.common.util.URI;
13import org.eclipse.emf.ecore.EObject;
14import org.eclipse.emf.ecore.resource.Resource;
13import org.eclipse.emf.ecore.util.EcoreUtil; 15import org.eclipse.emf.ecore.util.EcoreUtil;
14import org.eclipse.xtext.naming.IQualifiedNameProvider; 16import org.eclipse.xtext.naming.IQualifiedNameProvider;
15import org.eclipse.xtext.naming.QualifiedName; 17import org.eclipse.xtext.naming.QualifiedName;
@@ -18,7 +20,7 @@ import org.eclipse.xtext.resource.XtextResource;
18import org.eclipse.xtext.resource.XtextResourceSet; 20import org.eclipse.xtext.resource.XtextResourceSet;
19import org.eclipse.xtext.scoping.IScopeProvider; 21import org.eclipse.xtext.scoping.IScopeProvider;
20import tools.refinery.language.model.problem.*; 22import tools.refinery.language.model.problem.*;
21import tools.refinery.language.scoping.imports.ImportAdapter; 23import tools.refinery.language.naming.NamingUtil;
22import tools.refinery.language.utils.ProblemDesugarer; 24import tools.refinery.language.utils.ProblemDesugarer;
23import tools.refinery.language.utils.ProblemUtil; 25import tools.refinery.language.utils.ProblemUtil;
24import tools.refinery.store.model.Model; 26import tools.refinery.store.model.Model;
@@ -34,9 +36,7 @@ import tools.refinery.store.tuple.Tuple;
34import java.io.ByteArrayInputStream; 36import java.io.ByteArrayInputStream;
35import java.io.ByteArrayOutputStream; 37import java.io.ByteArrayOutputStream;
36import java.io.IOException; 38import java.io.IOException;
37import java.util.Map; 39import java.util.*;
38import java.util.TreeMap;
39import java.util.TreeSet;
40import java.util.function.Function; 40import java.util.function.Function;
41import java.util.stream.Collectors; 41import java.util.stream.Collectors;
42 42
@@ -66,13 +66,17 @@ public class SolutionSerializer {
66 private Model model; 66 private Model model;
67 private ReasoningAdapter reasoningAdapter; 67 private ReasoningAdapter reasoningAdapter;
68 private PartialInterpretation<TruthValue, Boolean> existsInterpretation; 68 private PartialInterpretation<TruthValue, Boolean> existsInterpretation;
69 private Resource originalResource;
69 private Problem originalProblem; 70 private Problem originalProblem;
71 private QualifiedName originalProblemName;
70 private Problem problem; 72 private Problem problem;
73 private QualifiedName newProblemName;
71 private NodeDeclaration nodeDeclaration; 74 private NodeDeclaration nodeDeclaration;
72 private final MutableIntObjectMap<Node> nodes = IntObjectMaps.mutable.empty(); 75 private final MutableIntObjectMap<Node> nodes = IntObjectMaps.mutable.empty();
73 76
74 public Problem serializeSolution(ProblemTrace trace, Model model) { 77 public Problem serializeSolution(ProblemTrace trace, Model model) {
75 var uri = URI.createURI("__synthetic." + ProblemUtil.MODULE_EXTENSION); 78 var uri = URI.createURI("__solution_%s.%s".formatted(UUID.randomUUID().toString().replace('-', '_'),
79 ProblemUtil.MODULE_EXTENSION));
76 return serializeSolution(trace, model, uri); 80 return serializeSolution(trace, model, uri);
77 } 81 }
78 82
@@ -83,10 +87,14 @@ public class SolutionSerializer {
83 existsInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, 87 existsInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE,
84 ReasoningAdapter.EXISTS_SYMBOL); 88 ReasoningAdapter.EXISTS_SYMBOL);
85 originalProblem = trace.getProblem(); 89 originalProblem = trace.getProblem();
90 originalProblemName = qualifiedNameProvider.getFullyQualifiedName(originalProblem);
86 problem = copyProblem(originalProblem, uri); 91 problem = copyProblem(originalProblem, uri);
87 problem.setKind(ModuleKind.MODULE); 92 problem.setKind(ProblemUtil.getDefaultModuleKind(uri));
93 problem.setExplicitKind(false);
94 problem.setName(null);
95 newProblemName = qualifiedNameProvider.getFullyQualifiedName(originalProblem);
88 problem.getStatements().removeIf(SolutionSerializer::shouldRemoveStatement); 96 problem.getStatements().removeIf(SolutionSerializer::shouldRemoveStatement);
89 problem.getNodes().removeIf(this::shouldRemoveNode); 97 removeNonExistentImplicitNodes();
90 nodeDeclaration = ProblemFactory.eINSTANCE.createNodeDeclaration(); 98 nodeDeclaration = ProblemFactory.eINSTANCE.createNodeDeclaration();
91 nodeDeclaration.setKind(NodeKind.NODE); 99 nodeDeclaration.setKind(NodeKind.NODE);
92 nodeDeclaration.getNodes().addAll(problem.getNodes()); 100 nodeDeclaration.getNodes().addAll(problem.getNodes());
@@ -105,15 +113,28 @@ public class SolutionSerializer {
105 return statement instanceof Assertion || statement instanceof ScopeDeclaration; 113 return statement instanceof Assertion || statement instanceof ScopeDeclaration;
106 } 114 }
107 115
108 private boolean shouldRemoveNode(Node newNode) { 116 private void removeNonExistentImplicitNodes() {
109 var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(newNode); 117 var originalImplicitNodes = originalProblem.getNodes();
110 var scope = scopeProvider.getScope(originalProblem, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE); 118 var newImplicitNodes = problem.getNodes();
111 var originalNode = semanticsUtils.maybeGetElement(originalProblem, scope, qualifiedName, Node.class); 119 if (newImplicitNodes.size() != originalImplicitNodes.size()) {
112 if (originalNode == null) { 120 throw new IllegalStateException("Expected %d implicit nodes in problem, but got %d after copying"
113 return false; 121 .formatted(originalImplicitNodes.size(), newImplicitNodes.size()));
122 }
123 var iterator = newImplicitNodes.iterator();
124 for (var originalNode : originalImplicitNodes) {
125 if (!iterator.hasNext()) {
126 throw new AssertionError("Unexpected end of copied implicit node list");
127 }
128 var newNode = iterator.next();
129 if (!Objects.equals(originalNode.getName(), newNode.getName())) {
130 throw new IllegalStateException("Expected copy of '%s' to have the same name, got '%s' instead"
131 .formatted(originalNode.getName(), newNode.getName()));
132 }
133 int nodeId = trace.getNodeId(originalNode);
134 if (!isExistingNode(nodeId)) {
135 iterator.remove();
136 }
114 } 137 }
115 int nodeId = trace.getNodeId(originalNode);
116 return !isExistingNode(nodeId);
117 } 138 }
118 139
119 private boolean isExistingNode(int nodeId) { 140 private boolean isExistingNode(int nodeId) {
@@ -125,14 +146,10 @@ public class SolutionSerializer {
125 } 146 }
126 147
127 private Problem copyProblem(Problem originalProblem, URI uri) { 148 private Problem copyProblem(Problem originalProblem, URI uri) {
128 var newResourceSet = resourceSetProvider.get(); 149 originalResource = originalProblem.eResource();
129 ImportAdapter.copySettings(originalProblem, newResourceSet); 150 var resourceSet = originalResource.getResourceSet();
130 if (!ProblemUtil.MODULE_EXTENSION.equals(uri.fileExtension())) {
131 uri = uri.appendFileExtension(ProblemUtil.MODULE_EXTENSION);
132 }
133 var newResource = resourceFactory.createResource(uri); 151 var newResource = resourceFactory.createResource(uri);
134 newResourceSet.getResources().add(newResource); 152 resourceSet.getResources().add(newResource);
135 var originalResource = originalProblem.eResource();
136 if (originalResource instanceof XtextResource) { 153 if (originalResource instanceof XtextResource) {
137 byte[] bytes; 154 byte[] bytes;
138 try { 155 try {
@@ -147,7 +164,7 @@ public class SolutionSerializer {
147 throw new IllegalStateException("Failed to copy problem", e); 164 throw new IllegalStateException("Failed to copy problem", e);
148 } 165 }
149 var contents = newResource.getContents(); 166 var contents = newResource.getContents();
150 EcoreUtil.resolveAll(newResourceSet); 167 EcoreUtil.resolveAll(newResource);
151 if (!contents.isEmpty() && contents.getFirst() instanceof Problem newProblem) { 168 if (!contents.isEmpty() && contents.getFirst() instanceof Problem newProblem) {
152 return newProblem; 169 return newProblem;
153 } 170 }
@@ -159,10 +176,24 @@ public class SolutionSerializer {
159 } 176 }
160 } 177 }
161 178
179 private QualifiedName getConvertedName(EObject original) {
180 var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(original);
181 if (originalProblemName != null && qualifiedName.startsWith(originalProblemName)) {
182 qualifiedName = qualifiedName.skipFirst(originalProblemName.getSegmentCount());
183 }
184 if (newProblemName != null) {
185 qualifiedName = newProblemName.append(qualifiedName);
186 }
187 return NamingUtil.addRootPrefix(qualifiedName);
188 }
189
162 private Relation findRelation(Relation originalRelation) { 190 private Relation findRelation(Relation originalRelation) {
163 var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(originalRelation); 191 if (originalRelation.eResource() != originalResource) {
164 var scope = scopeProvider.getScope(problem, ProblemPackage.Literals.ASSERTION__RELATION); 192 return originalRelation;
165 return semanticsUtils.getElement(problem, scope, qualifiedName, Relation.class); 193 }
194 var qualifiedName = getConvertedName(originalRelation);
195 return semanticsUtils.getLocalElement(problem, qualifiedName, Relation.class,
196 ProblemPackage.Literals.RELATION);
166 } 197 }
167 198
168 private Relation findPartialRelation(PartialRelation partialRelation) { 199 private Relation findPartialRelation(PartialRelation partialRelation) {
@@ -170,13 +201,11 @@ public class SolutionSerializer {
170 } 201 }
171 202
172 private Node findNode(Node originalNode) { 203 private Node findNode(Node originalNode) {
173 var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(originalNode); 204 if (originalNode.eResource() != originalResource) {
174 return findNode(qualifiedName); 205 return originalNode;
175 } 206 }
176 207 var qualifiedName = getConvertedName(originalNode);
177 private Node findNode(QualifiedName qualifiedName) { 208 return semanticsUtils.maybeGetLocalElement(problem, qualifiedName, Node.class, ProblemPackage.Literals.NODE);
178 var scope = scopeProvider.getScope(problem, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE);
179 return semanticsUtils.maybeGetElement(problem, scope, qualifiedName, Node.class);
180 } 209 }
181 210
182 private void addAssertion(Relation relation, LogicValue value, Node... arguments) { 211 private void addAssertion(Relation relation, LogicValue value, Node... arguments) {
@@ -194,8 +223,8 @@ public class SolutionSerializer {
194 } 223 }
195 224
196 private void addExistsAssertions() { 225 private void addExistsAssertions() {
197 var builtinSymbols = desugarer.getBuiltinSymbols(problem) 226 var builtinSymbols = desugarer.getBuiltinSymbols(problem).orElseThrow(() -> new IllegalStateException("No " +
198 .orElseThrow(() -> new IllegalStateException("No builtin library in copied problem")); 227 "builtin library in copied problem"));
199 // Make sure to output exists assertions in a deterministic order. 228 // Make sure to output exists assertions in a deterministic order.
200 var sortedNewNodes = new TreeMap<Integer, Node>(); 229 var sortedNewNodes = new TreeMap<Integer, Node>();
201 for (var pair : trace.getNodeTrace().keyValuesView()) { 230 for (var pair : trace.getNodeTrace().keyValuesView()) {
@@ -204,8 +233,7 @@ public class SolutionSerializer {
204 var newNode = findNode(originalNode); 233 var newNode = findNode(originalNode);
205 // Since all implicit nodes that do not exist has already been removed in serializeSolution, 234 // Since all implicit nodes that do not exist has already been removed in serializeSolution,
206 // we only need to add !exists assertions to ::new nodes and explicitly declared nodes that do not exist. 235 // we only need to add !exists assertions to ::new nodes and explicitly declared nodes that do not exist.
207 if (ProblemUtil.isMultiNode(originalNode) || 236 if (ProblemUtil.isMultiNode(originalNode) || (ProblemUtil.isDeclaredNode(originalNode) && !isExistingNode(nodeId))) {
208 (ProblemUtil.isDeclaredNode(originalNode) && !isExistingNode(nodeId))) {
209 sortedNewNodes.put(nodeId, newNode); 237 sortedNewNodes.put(nodeId, newNode);
210 } else { 238 } else {
211 nodes.put(nodeId, newNode); 239 nodes.put(nodeId, newNode);
@@ -218,8 +246,8 @@ public class SolutionSerializer {
218 } 246 }
219 247
220 private void addClassAssertions() { 248 private void addClassAssertions() {
221 var types = trace.getMetamodel().typeHierarchy().getPreservedTypes().keySet().stream() 249 var types =
222 .collect(Collectors.toMap(Function.identity(), this::findPartialRelation)); 250 trace.getMetamodel().typeHierarchy().getPreservedTypes().keySet().stream().collect(Collectors.toMap(Function.identity(), this::findPartialRelation));
223 var cursor = model.getInterpretation(TypeHierarchyTranslator.TYPE_SYMBOL).getAll(); 251 var cursor = model.getInterpretation(TypeHierarchyTranslator.TYPE_SYMBOL).getAll();
224 while (cursor.move()) { 252 while (cursor.move()) {
225 var key = cursor.getKey(); 253 var key = cursor.getKey();