diff options
author | 2023-12-22 02:19:38 +0100 | |
---|---|---|
committer | 2023-12-24 17:32:54 +0100 | |
commit | c65275be9fce0fbba70094c754690a8bd9228ab4 (patch) | |
tree | 8ba97d3668229482f8e25272ebfe62c2d27cca12 /subprojects/language/src | |
parent | refactor(language): use file extension provider (diff) | |
download | refinery-c65275be9fce0fbba70094c754690a8bd9228ab4.tar.gz refinery-c65275be9fce0fbba70094c754690a8bd9228ab4.tar.zst refinery-c65275be9fce0fbba70094c754690a8bd9228ab4.zip |
feat: solution serializer
Diffstat (limited to 'subprojects/language/src')
3 files changed, 28 insertions, 26 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/formatting2/ProblemFormatter.java b/subprojects/language/src/main/java/tools/refinery/language/formatting2/ProblemFormatter.java index 55a5ac20..770a1a19 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/formatting2/ProblemFormatter.java +++ b/subprojects/language/src/main/java/tools/refinery/language/formatting2/ProblemFormatter.java | |||
@@ -75,6 +75,16 @@ public class ProblemFormatter extends AbstractJavaFormatter { | |||
75 | } | 75 | } |
76 | } | 76 | } |
77 | 77 | ||
78 | protected void format(EnumDeclaration enumDeclaration, IFormattableDocument doc) { | ||
79 | surroundNewLines(doc, enumDeclaration, this::twoNewLines); | ||
80 | var region = regionFor(enumDeclaration); | ||
81 | doc.append(region.keyword("enum"), this::oneSpace); | ||
82 | doc.prepend(region.keyword("{"), this::oneSpace); | ||
83 | doc.append(region.keyword("{"), it -> it.setNewLines(1, 1, 2)); | ||
84 | doc.prepend(region.keyword("}"), it -> it.setNewLines(1, 1, 2)); | ||
85 | doc.prepend(region.keyword("."), this::noSpace); | ||
86 | } | ||
87 | |||
78 | protected void format(PredicateDefinition predicateDefinition, IFormattableDocument doc) { | 88 | protected void format(PredicateDefinition predicateDefinition, IFormattableDocument doc) { |
79 | surroundNewLines(doc, predicateDefinition, this::twoNewLines); | 89 | surroundNewLines(doc, predicateDefinition, this::twoNewLines); |
80 | var region = regionFor(predicateDefinition); | 90 | var region = regionFor(predicateDefinition); |
@@ -151,14 +161,14 @@ public class ProblemFormatter extends AbstractJavaFormatter { | |||
151 | } | 161 | } |
152 | 162 | ||
153 | protected void surroundNewLines(IFormattableDocument doc, EObject eObject, | 163 | protected void surroundNewLines(IFormattableDocument doc, EObject eObject, |
154 | Procedure1<? super IHiddenRegionFormatter> init) { | 164 | Procedure1<? super IHiddenRegionFormatter> init) { |
155 | var region = doc.getRequest().getTextRegionAccess().regionForEObject(eObject); | 165 | var region = doc.getRequest().getTextRegionAccess().regionForEObject(eObject); |
156 | preprendNewLines(doc, region, init); | 166 | preprendNewLines(doc, region, init); |
157 | appendNewLines(doc, region, init); | 167 | appendNewLines(doc, region, init); |
158 | } | 168 | } |
159 | 169 | ||
160 | protected void preprendNewLines(IFormattableDocument doc, ISequentialRegion region, | 170 | protected void preprendNewLines(IFormattableDocument doc, ISequentialRegion region, |
161 | Procedure1<? super IHiddenRegionFormatter> init) { | 171 | Procedure1<? super IHiddenRegionFormatter> init) { |
162 | if (region == null) { | 172 | if (region == null) { |
163 | return; | 173 | return; |
164 | } | 174 | } |
@@ -174,7 +184,7 @@ public class ProblemFormatter extends AbstractJavaFormatter { | |||
174 | } | 184 | } |
175 | 185 | ||
176 | protected void appendNewLines(IFormattableDocument doc, ISequentialRegion region, | 186 | protected void appendNewLines(IFormattableDocument doc, ISequentialRegion region, |
177 | Procedure1<? super IHiddenRegionFormatter> init) { | 187 | Procedure1<? super IHiddenRegionFormatter> init) { |
178 | if (region == null) { | 188 | if (region == null) { |
179 | return; | 189 | return; |
180 | } | 190 | } |
diff --git a/subprojects/language/src/main/java/tools/refinery/language/serializer/PreferShortAssertionsProblemSemanticSequencer.java b/subprojects/language/src/main/java/tools/refinery/language/serializer/PreferShortAssertionsProblemSemanticSequencer.java index b9cafbc2..3432b2d8 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/serializer/PreferShortAssertionsProblemSemanticSequencer.java +++ b/subprojects/language/src/main/java/tools/refinery/language/serializer/PreferShortAssertionsProblemSemanticSequencer.java | |||
@@ -21,8 +21,7 @@ public class PreferShortAssertionsProblemSemanticSequencer extends ProblemSemant | |||
21 | 21 | ||
22 | @Override | 22 | @Override |
23 | protected void sequence_Assertion(ISerializationContext context, Assertion semanticObject) { | 23 | protected void sequence_Assertion(ISerializationContext context, Assertion semanticObject) { |
24 | if (semanticObject.isDefault() || | 24 | if (!(semanticObject.getValue() instanceof LogicConstant logicConstant) || |
25 | !(semanticObject.getValue() instanceof LogicConstant logicConstant) || | ||
26 | logicConstant.getLogicValue() == LogicValue.ERROR) { | 25 | logicConstant.getLogicValue() == LogicValue.ERROR) { |
27 | super.sequence_Assertion(context, semanticObject); | 26 | super.sequence_Assertion(context, semanticObject); |
28 | return; | 27 | return; |
@@ -39,6 +38,9 @@ public class PreferShortAssertionsProblemSemanticSequencer extends ProblemSemant | |||
39 | } | 38 | } |
40 | var feeder = createSequencerFeeder(context, semanticObject); | 39 | var feeder = createSequencerFeeder(context, semanticObject); |
41 | var access = grammarAccess.getAssertionAccess(); | 40 | var access = grammarAccess.getAssertionAccess(); |
41 | if (semanticObject.isDefault()) { | ||
42 | feeder.accept(access.getDefaultDefaultKeyword_0_0()); | ||
43 | } | ||
42 | feeder.accept(access.getValueShortLogicConstantParserRuleCall_1_1_0_0(), logicConstant); | 44 | feeder.accept(access.getValueShortLogicConstantParserRuleCall_1_1_0_0(), logicConstant); |
43 | feeder.accept(access.getRelationRelationQualifiedNameParserRuleCall_1_1_1_0_1(), semanticObject.getRelation()); | 45 | feeder.accept(access.getRelationRelationQualifiedNameParserRuleCall_1_1_1_0_1(), semanticObject.getRelation()); |
44 | var iterator = semanticObject.getArguments().iterator(); | 46 | var iterator = semanticObject.getArguments().iterator(); |
diff --git a/subprojects/language/src/test/java/tools/refinery/language/tests/serializer/ProblemSerializerTest.java b/subprojects/language/src/test/java/tools/refinery/language/tests/serializer/ProblemSerializerTest.java index 9f4f2bbf..4a3a9ac2 100644 --- a/subprojects/language/src/test/java/tools/refinery/language/tests/serializer/ProblemSerializerTest.java +++ b/subprojects/language/src/test/java/tools/refinery/language/tests/serializer/ProblemSerializerTest.java | |||
@@ -74,8 +74,8 @@ class ProblemSerializerTest { | |||
74 | } | 74 | } |
75 | 75 | ||
76 | @ParameterizedTest | 76 | @ParameterizedTest |
77 | @MethodSource | 77 | @MethodSource("assertionTest") |
78 | void defaultAssertionTest(LogicValue value, String valueAsString) { | 78 | void defaultAssertionTest(LogicValue value, String serializedAssertion) { |
79 | var pred = createPred(); | 79 | var pred = createPred(); |
80 | var node = ProblemFactory.eINSTANCE.createNode(); | 80 | var node = ProblemFactory.eINSTANCE.createNode(); |
81 | node.setName("a"); | 81 | node.setName("a"); |
@@ -88,12 +88,7 @@ class ProblemSerializerTest { | |||
88 | pred foo(node p). | 88 | pred foo(node p). |
89 | 89 | ||
90 | indiv a. | 90 | indiv a. |
91 | default foo(a):\040""" + valueAsString + ".\n"); | 91 | default\040""" + serializedAssertion + "\n"); |
92 | } | ||
93 | |||
94 | static Stream<Arguments> defaultAssertionTest() { | ||
95 | return Stream.of(Arguments.of(LogicValue.TRUE, "true"), Arguments.of(LogicValue.FALSE, "false"), | ||
96 | Arguments.of(LogicValue.UNKNOWN, "unknown"), Arguments.of(LogicValue.ERROR, "error")); | ||
97 | } | 92 | } |
98 | 93 | ||
99 | @Test | 94 | @Test |
@@ -233,19 +228,14 @@ class ProblemSerializerTest { | |||
233 | } | 228 | } |
234 | 229 | ||
235 | private void assertSerializedResult(String expected) { | 230 | private void assertSerializedResult(String expected) { |
236 | var outputStream = new ByteArrayOutputStream(); | 231 | String problemString; |
237 | try { | 232 | try (var outputStream = new ByteArrayOutputStream()) { |
238 | resource.save(outputStream, Map.of()); | 233 | resource.save(outputStream, Map.of()); |
239 | } catch (IOException e) { | 234 | problemString = outputStream.toString(); |
240 | throw new AssertionError("Failed to serialize problem", e); | 235 | } catch (IOException e) { |
241 | } finally { | 236 | throw new AssertionError("Failed to serialize problem", e); |
242 | try { | 237 | } |
243 | outputStream.close(); | 238 | // Nothing to handle in a test. |
244 | } catch (IOException e) { | ||
245 | // Nothing to handle in a test. | ||
246 | } | ||
247 | } | ||
248 | var problemString = outputStream.toString(); | ||
249 | 239 | ||
250 | assertThat(problemString.replace("\r\n", "\n"), equalTo(expected.replace("\r\n", "\n"))); | 240 | assertThat(problemString.replace("\r\n", "\n"), equalTo(expected.replace("\r\n", "\n"))); |
251 | } | 241 | } |