diff options
2 files changed, 27 insertions, 7 deletions
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 537d94ca..1bd419d8 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 | |||
@@ -68,7 +68,9 @@ public class SolutionSerializer { | |||
68 | private Model model; | 68 | private Model model; |
69 | private ReasoningAdapter reasoningAdapter; | 69 | private ReasoningAdapter reasoningAdapter; |
70 | private PartialInterpretation<TruthValue, Boolean> existsInterpretation; | 70 | private PartialInterpretation<TruthValue, Boolean> existsInterpretation; |
71 | private Problem originalProblem; | ||
71 | private Problem problem; | 72 | private Problem problem; |
73 | private NodeDeclaration nodeDeclaration; | ||
72 | private final MutableIntObjectMap<Node> nodes = IntObjectMaps.mutable.empty(); | 74 | private final MutableIntObjectMap<Node> nodes = IntObjectMaps.mutable.empty(); |
73 | 75 | ||
74 | @Inject | 76 | @Inject |
@@ -87,14 +89,22 @@ public class SolutionSerializer { | |||
87 | reasoningAdapter = model.getAdapter(ReasoningAdapter.class); | 89 | reasoningAdapter = model.getAdapter(ReasoningAdapter.class); |
88 | existsInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, | 90 | existsInterpretation = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, |
89 | ReasoningAdapter.EXISTS_SYMBOL); | 91 | ReasoningAdapter.EXISTS_SYMBOL); |
90 | var originalProblem = trace.getProblem(); | 92 | originalProblem = trace.getProblem(); |
91 | problem = copyProblem(originalProblem, uri); | 93 | problem = copyProblem(originalProblem, uri); |
94 | problem.setKind(ModuleKind.MODULE); | ||
92 | problem.getStatements().removeIf(SolutionSerializer::shouldRemoveStatement); | 95 | problem.getStatements().removeIf(SolutionSerializer::shouldRemoveStatement); |
93 | problem.getNodes().removeIf(this::shouldRemoveNode); | 96 | problem.getNodes().removeIf(this::shouldRemoveNode); |
97 | nodeDeclaration = ProblemFactory.eINSTANCE.createNodeDeclaration(); | ||
98 | nodeDeclaration.setKind(NodeKind.NODE); | ||
99 | nodeDeclaration.getNodes().addAll(problem.getNodes()); | ||
100 | problem.getStatements().add(nodeDeclaration); | ||
94 | nameProvider.setProblem(problem); | 101 | nameProvider.setProblem(problem); |
95 | addExistsAssertions(); | 102 | addExistsAssertions(); |
96 | addClassAssertions(); | 103 | addClassAssertions(); |
97 | addReferenceAssertions(); | 104 | addReferenceAssertions(); |
105 | if (nodeDeclaration.getNodes().isEmpty()) { | ||
106 | problem.getStatements().remove(nodeDeclaration); | ||
107 | } | ||
98 | return problem; | 108 | return problem; |
99 | } | 109 | } |
100 | 110 | ||
@@ -104,8 +114,8 @@ public class SolutionSerializer { | |||
104 | 114 | ||
105 | private boolean shouldRemoveNode(Node newNode) { | 115 | private boolean shouldRemoveNode(Node newNode) { |
106 | var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(newNode); | 116 | var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(newNode); |
107 | var scope = scopeProvider.getScope(trace.getProblem(), ProblemPackage.Literals.ASSERTION__RELATION); | 117 | var scope = scopeProvider.getScope(originalProblem, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE); |
108 | var originalNode = semanticsUtils.maybeGetElement(problem, scope, qualifiedName, Node.class); | 118 | var originalNode = semanticsUtils.maybeGetElement(originalProblem, scope, qualifiedName, Node.class); |
109 | if (originalNode == null) { | 119 | if (originalNode == null) { |
110 | return false; | 120 | return false; |
111 | } | 121 | } |
@@ -238,7 +248,7 @@ public class SolutionSerializer { | |||
238 | var nodeName = nameProvider.getNextName(typeName); | 248 | var nodeName = nameProvider.getNextName(typeName); |
239 | node = ProblemFactory.eINSTANCE.createNode(); | 249 | node = ProblemFactory.eINSTANCE.createNode(); |
240 | node.setName(nodeName); | 250 | node.setName(nodeName); |
241 | problem.getNodes().add(node); | 251 | nodeDeclaration.getNodes().add(node); |
242 | nodes.put(nodeId, node); | 252 | nodes.put(nodeId, node); |
243 | } | 253 | } |
244 | addAssertion(candidateRelation, LogicValue.TRUE, node); | 254 | addAssertion(candidateRelation, LogicValue.TRUE, node); |
diff --git a/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java b/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java index b682a7d6..949b7047 100644 --- a/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java +++ b/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java | |||
@@ -72,7 +72,7 @@ class SolutionSerializerTest { | |||
72 | solution.eResource().save(outputStream, Map.of()); | 72 | solution.eResource().save(outputStream, Map.of()); |
73 | actualOutput = outputStream.toString(); | 73 | actualOutput = outputStream.toString(); |
74 | } | 74 | } |
75 | assertThat(actualOutput, is(prefix + "\n" + expectedOutput)); | 75 | assertThat(actualOutput, is("module.\n\n" + prefix + "\n" + expectedOutput)); |
76 | } | 76 | } |
77 | 77 | ||
78 | static Stream<Arguments> solutionSerializerTest() { | 78 | static Stream<Arguments> solutionSerializerTest() { |
@@ -81,6 +81,7 @@ class SolutionSerializerTest { | |||
81 | """, """ | 81 | """, """ |
82 | scope Foo = 3. | 82 | scope Foo = 3. |
83 | """, """ | 83 | """, """ |
84 | declare foo1, foo2, foo3. | ||
84 | !exists(Foo::new). | 85 | !exists(Foo::new). |
85 | Foo(foo1). | 86 | Foo(foo1). |
86 | Foo(foo2). | 87 | Foo(foo2). |
@@ -94,6 +95,7 @@ class SolutionSerializerTest { | |||
94 | """, """ | 95 | """, """ |
95 | scope Foo = 1. | 96 | scope Foo = 1. |
96 | """, """ | 97 | """, """ |
98 | declare foo1, bar1, bar2. | ||
97 | !exists(Foo::new). | 99 | !exists(Foo::new). |
98 | !exists(Bar::new). | 100 | !exists(Bar::new). |
99 | Foo(foo1). | 101 | Foo(foo1). |
@@ -112,6 +114,7 @@ class SolutionSerializerTest { | |||
112 | """, """ | 114 | """, """ |
113 | scope Foo = 1, Bar = 2. | 115 | scope Foo = 1, Bar = 2. |
114 | """, """ | 116 | """, """ |
117 | declare foo1, bar1, bar2. | ||
115 | !exists(Foo::new). | 118 | !exists(Foo::new). |
116 | !exists(Bar::new). | 119 | !exists(Bar::new). |
117 | Foo(foo1). | 120 | Foo(foo1). |
@@ -131,6 +134,7 @@ class SolutionSerializerTest { | |||
131 | 134 | ||
132 | scope Person += 0. | 135 | scope Person += 0. |
133 | """, """ | 136 | """, """ |
137 | declare a, b, c. | ||
134 | !exists(Person::new). | 138 | !exists(Person::new). |
135 | Person(a). | 139 | Person(a). |
136 | Person(b). | 140 | Person(b). |
@@ -156,6 +160,7 @@ class SolutionSerializerTest { | |||
156 | 160 | ||
157 | scope Foo += 0. | 161 | scope Foo += 0. |
158 | """, """ | 162 | """, """ |
163 | declare foo. | ||
159 | !exists(Foo::new). | 164 | !exists(Foo::new). |
160 | Foo(foo). | 165 | Foo(foo). |
161 | default !bar(*, *). | 166 | default !bar(*, *). |
@@ -166,6 +171,7 @@ class SolutionSerializerTest { | |||
166 | """, """ | 171 | """, """ |
167 | scope Foo = 1, Bar = 0. | 172 | scope Foo = 1, Bar = 0. |
168 | """, """ | 173 | """, """ |
174 | declare foo1. | ||
169 | !exists(Foo::new). | 175 | !exists(Foo::new). |
170 | !exists(Bar::new). | 176 | !exists(Bar::new). |
171 | Foo(foo1). | 177 | Foo(foo1). |
@@ -180,6 +186,7 @@ class SolutionSerializerTest { | |||
180 | 186 | ||
181 | scope Foo += 0. | 187 | scope Foo += 0. |
182 | """, """ | 188 | """, """ |
189 | declare a. | ||
183 | !exists(Foo::new). | 190 | !exists(Foo::new). |
184 | Foo(a). | 191 | Foo(a). |
185 | default !ref(*, *). | 192 | default !ref(*, *). |
@@ -200,29 +207,32 @@ class SolutionSerializerTest { | |||
200 | !exists(Foo::new). | 207 | !exists(Foo::new). |
201 | scope Foo = 2. | 208 | scope Foo = 2. |
202 | """, """ | 209 | """, """ |
210 | declare foo1, foo2. | ||
203 | !exists(a). | 211 | !exists(a). |
204 | !exists(Foo::new). | 212 | !exists(Foo::new). |
205 | Foo(foo1). | 213 | Foo(foo1). |
206 | Foo(foo2). | 214 | Foo(foo2). |
207 | """), Arguments.of(""" | 215 | """), Arguments.of(""" |
208 | node a. | 216 | declare a. |
209 | class Foo. | 217 | class Foo. |
210 | """, """ | 218 | """, """ |
211 | Foo(a). | 219 | Foo(a). |
212 | ?exists(a). | 220 | ?exists(a). |
213 | scope Foo = 2, Foo += 1. | 221 | scope Foo = 2, Foo += 1. |
214 | """, """ | 222 | """, """ |
223 | declare foo1. | ||
215 | !exists(Foo::new). | 224 | !exists(Foo::new). |
216 | Foo(a). | 225 | Foo(a). |
217 | Foo(foo1). | 226 | Foo(foo1). |
218 | """), Arguments.of(""" | 227 | """), Arguments.of(""" |
219 | node a. | 228 | declare a. |
220 | class Foo. | 229 | class Foo. |
221 | """, """ | 230 | """, """ |
222 | Foo(a). | 231 | Foo(a). |
223 | ?exists(a). | 232 | ?exists(a). |
224 | scope Foo = 1, Foo += 1. | 233 | scope Foo = 1, Foo += 1. |
225 | """, """ | 234 | """, """ |
235 | declare foo1. | ||
226 | !exists(a). | 236 | !exists(a). |
227 | !exists(Foo::new). | 237 | !exists(Foo::new). |
228 | Foo(foo1). | 238 | Foo(foo1). |