diff options
author | 2021-06-30 02:34:51 +0200 | |
---|---|---|
committer | 2021-06-30 02:39:54 +0200 | |
commit | 5c515bbe92e32c9cea7fd968ec9436a49af9ebf9 (patch) | |
tree | a4a3602e6fa729ecccbccdc316a1f186fcfdc8b5 /language/src | |
parent | Node scoping fixes (diff) | |
download | refinery-5c515bbe92e32c9cea7fd968ec9436a49af9ebf9.tar.gz refinery-5c515bbe92e32c9cea7fd968ec9436a49af9ebf9.tar.zst refinery-5c515bbe92e32c9cea7fd968ec9436a49af9ebf9.zip |
Fix enum literal scopes
Diffstat (limited to 'language/src')
2 files changed, 51 insertions, 27 deletions
diff --git a/language/src/main/java/org/eclipse/viatra/solver/language/resource/NodeNameCollector.java b/language/src/main/java/org/eclipse/viatra/solver/language/resource/NodeNameCollector.java index 4dee5434..79d7ffbb 100644 --- a/language/src/main/java/org/eclipse/viatra/solver/language/resource/NodeNameCollector.java +++ b/language/src/main/java/org/eclipse/viatra/solver/language/resource/NodeNameCollector.java | |||
@@ -12,10 +12,8 @@ import org.eclipse.viatra.solver.language.model.problem.Assertion; | |||
12 | import org.eclipse.viatra.solver.language.model.problem.AssertionArgument; | 12 | import org.eclipse.viatra.solver.language.model.problem.AssertionArgument; |
13 | import org.eclipse.viatra.solver.language.model.problem.Atom; | 13 | import org.eclipse.viatra.solver.language.model.problem.Atom; |
14 | import org.eclipse.viatra.solver.language.model.problem.Conjunction; | 14 | import org.eclipse.viatra.solver.language.model.problem.Conjunction; |
15 | import org.eclipse.viatra.solver.language.model.problem.EnumDeclaration; | ||
16 | import org.eclipse.viatra.solver.language.model.problem.Literal; | 15 | import org.eclipse.viatra.solver.language.model.problem.Literal; |
17 | import org.eclipse.viatra.solver.language.model.problem.NegativeLiteral; | 16 | import org.eclipse.viatra.solver.language.model.problem.NegativeLiteral; |
18 | import org.eclipse.viatra.solver.language.model.problem.Node; | ||
19 | import org.eclipse.viatra.solver.language.model.problem.NodeAssertionArgument; | 17 | import org.eclipse.viatra.solver.language.model.problem.NodeAssertionArgument; |
20 | import org.eclipse.viatra.solver.language.model.problem.NodeValueAssertion; | 18 | import org.eclipse.viatra.solver.language.model.problem.NodeValueAssertion; |
21 | import org.eclipse.viatra.solver.language.model.problem.PredicateDefinition; | 19 | import org.eclipse.viatra.solver.language.model.problem.PredicateDefinition; |
@@ -47,7 +45,6 @@ public class NodeNameCollector { | |||
47 | private IScopeProvider scopeProvider; | 45 | private IScopeProvider scopeProvider; |
48 | 46 | ||
49 | private final Set<String> nodeNames = new HashSet<>(); | 47 | private final Set<String> nodeNames = new HashSet<>(); |
50 | private final Set<String> existingNodeNames = new HashSet<>(); | ||
51 | 48 | ||
52 | private IScope nodeScope; | 49 | private IScope nodeScope; |
53 | 50 | ||
@@ -56,27 +53,12 @@ public class NodeNameCollector { | |||
56 | } | 53 | } |
57 | 54 | ||
58 | public void collectNodeNames(Problem problem) { | 55 | public void collectNodeNames(Problem problem) { |
59 | nodeScope = scopeProvider.getScope(problem, ProblemPackage.Literals.ASSERTION__ARGUMENTS); | 56 | nodeScope = scopeProvider.getScope(problem, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE); |
60 | collectEnumLiteralNames(problem); | ||
61 | for (Statement statement : problem.getStatements()) { | 57 | for (Statement statement : problem.getStatements()) { |
62 | collectStatementNodeNames(statement); | 58 | collectStatementNodeNames(statement); |
63 | } | 59 | } |
64 | } | 60 | } |
65 | 61 | ||
66 | protected void collectEnumLiteralNames(Problem problem) { | ||
67 | for (Statement statement : problem.getStatements()) { | ||
68 | if (statement instanceof EnumDeclaration) { | ||
69 | EnumDeclaration enumDeclaration = (EnumDeclaration) statement; | ||
70 | for (Node literal : enumDeclaration.getLiterals()) { | ||
71 | String name = literal.getName(); | ||
72 | if (ProblemDerivedStateComputer.validId(name)) { | ||
73 | existingNodeNames.add(name); | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | |||
80 | protected void collectStatementNodeNames(Statement statement) { | 62 | protected void collectStatementNodeNames(Statement statement) { |
81 | if (statement instanceof Assertion) { | 63 | if (statement instanceof Assertion) { |
82 | collectAssertionNodeNames((Assertion) statement); | 64 | collectAssertionNodeNames((Assertion) statement); |
@@ -90,14 +72,14 @@ public class NodeNameCollector { | |||
90 | protected void collectAssertionNodeNames(Assertion assertion) { | 72 | protected void collectAssertionNodeNames(Assertion assertion) { |
91 | for (AssertionArgument argument : assertion.getArguments()) { | 73 | for (AssertionArgument argument : assertion.getArguments()) { |
92 | if (argument instanceof NodeAssertionArgument) { | 74 | if (argument instanceof NodeAssertionArgument) { |
93 | addNodeNames(argument, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE, | 75 | collectNodeNames(argument, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE, |
94 | ProblemDerivedStateComputer::validNodeName); | 76 | ProblemDerivedStateComputer::validNodeName); |
95 | } | 77 | } |
96 | } | 78 | } |
97 | } | 79 | } |
98 | 80 | ||
99 | protected void collectNodeValueAssertionNodeNames(NodeValueAssertion nodeValueAssertion) { | 81 | protected void collectNodeValueAssertionNodeNames(NodeValueAssertion nodeValueAssertion) { |
100 | addNodeNames(nodeValueAssertion, ProblemPackage.Literals.NODE_VALUE_ASSERTION__NODE, | 82 | collectNodeNames(nodeValueAssertion, ProblemPackage.Literals.NODE_VALUE_ASSERTION__NODE, |
101 | ProblemDerivedStateComputer::validNodeName); | 83 | ProblemDerivedStateComputer::validNodeName); |
102 | } | 84 | } |
103 | 85 | ||
@@ -116,7 +98,7 @@ public class NodeNameCollector { | |||
116 | } | 98 | } |
117 | for (Argument argument : atom.getArguments()) { | 99 | for (Argument argument : atom.getArguments()) { |
118 | if (argument instanceof VariableOrNodeArgument) { | 100 | if (argument instanceof VariableOrNodeArgument) { |
119 | addNodeNames(argument, ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__VARIABLE_OR_NODE, | 101 | collectNodeNames(argument, ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__VARIABLE_OR_NODE, |
120 | ProblemDerivedStateComputer::validQuotedId); | 102 | ProblemDerivedStateComputer::validQuotedId); |
121 | } | 103 | } |
122 | } | 104 | } |
@@ -124,7 +106,7 @@ public class NodeNameCollector { | |||
124 | } | 106 | } |
125 | } | 107 | } |
126 | 108 | ||
127 | private void addNodeNames(EObject eObject, EStructuralFeature feature, Predicate<String> condition) { | 109 | private void collectNodeNames(EObject eObject, EStructuralFeature feature, Predicate<String> condition) { |
128 | List<INode> nodes = NodeModelUtils.findNodesForFeature(eObject, feature); | 110 | List<INode> nodes = NodeModelUtils.findNodesForFeature(eObject, feature); |
129 | for (INode node : nodes) { | 111 | for (INode node : nodes) { |
130 | String nodeName = linkingHelper.getCrossRefNodeAsString(node, true); | 112 | String nodeName = linkingHelper.getCrossRefNodeAsString(node, true); |
@@ -132,7 +114,7 @@ public class NodeNameCollector { | |||
132 | continue; | 114 | continue; |
133 | } | 115 | } |
134 | QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(nodeName); | 116 | QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(nodeName); |
135 | if (!existingNodeNames.contains(nodeName) && nodeScope.getSingleElement(qualifiedName) == null) { | 117 | if (nodeScope.getSingleElement(qualifiedName) == null) { |
136 | nodeNames.add(nodeName); | 118 | nodeNames.add(nodeName); |
137 | } | 119 | } |
138 | } | 120 | } |
diff --git a/language/src/test/java/org/eclipse/viatra/solver/language/tests/scoping/NodeScopingTest.xtend b/language/src/test/java/org/eclipse/viatra/solver/language/tests/scoping/NodeScopingTest.xtend index c92ad4c2..91a9e8a4 100644 --- a/language/src/test/java/org/eclipse/viatra/solver/language/tests/scoping/NodeScopingTest.xtend +++ b/language/src/test/java/org/eclipse/viatra/solver/language/tests/scoping/NodeScopingTest.xtend | |||
@@ -219,7 +219,7 @@ class NodeScopingTest { | |||
219 | predicate(«qualifiedName»). | 219 | predicate(«qualifiedName»). |
220 | ''') | 220 | ''') |
221 | assertThat(errors, empty) | 221 | assertThat(errors, empty) |
222 | assertThat(nodeNames, empty) | 222 | assertThat(nodes, empty) |
223 | assertThat(assertion(0).arg(0).node, equalTo(findEnum("Foo").literal("alpha"))) | 223 | assertThat(assertion(0).arg(0).node, equalTo(findEnum("Foo").literal("alpha"))) |
224 | } | 224 | } |
225 | 225 | ||
@@ -232,7 +232,7 @@ class NodeScopingTest { | |||
232 | «qualifiedName»: 16. | 232 | «qualifiedName»: 16. |
233 | ''') | 233 | ''') |
234 | assertThat(errors, empty) | 234 | assertThat(errors, empty) |
235 | assertThat(nodeNames, empty) | 235 | assertThat(nodes, empty) |
236 | assertThat(nodeValueAssertion(0).node, equalTo(findEnum("Foo").literal("alpha"))) | 236 | assertThat(nodeValueAssertion(0).node, equalTo(findEnum("Foo").literal("alpha"))) |
237 | } | 237 | } |
238 | 238 | ||
@@ -245,7 +245,7 @@ class NodeScopingTest { | |||
245 | pred predicate(Foo a) :- node(«qualifiedName»). | 245 | pred predicate(Foo a) :- node(«qualifiedName»). |
246 | ''') | 246 | ''') |
247 | assertThat(errors, empty) | 247 | assertThat(errors, empty) |
248 | assertThat(nodeNames, empty) | 248 | assertThat(nodes, empty) |
249 | assertThat(pred("predicate").conj(0).lit(0).arg(0).node, equalTo(findEnum("Foo").literal("alpha"))) | 249 | assertThat(pred("predicate").conj(0).lit(0).arg(0).node, equalTo(findEnum("Foo").literal("alpha"))) |
250 | } | 250 | } |
251 | 251 | ||
@@ -259,4 +259,46 @@ class NodeScopingTest { | |||
259 | Arguments.of("test::Foo::alpha", true) | 259 | Arguments.of("test::Foo::alpha", true) |
260 | ) | 260 | ) |
261 | } | 261 | } |
262 | |||
263 | @ParameterizedTest | ||
264 | @MethodSource("builtInEnumLiteralReferencesSource") | ||
265 | def void builtInEnumLiteralTest(String qualifiedName) { | ||
266 | val it = parseHelper.parse(''' | ||
267 | pred predicate(node a) :- node(a). | ||
268 | predicate(«qualifiedName»). | ||
269 | ''') | ||
270 | assertThat(errors, empty) | ||
271 | assertThat(nodes, empty) | ||
272 | assertThat(assertion(0).arg(0).node, equalTo(builtin.findEnum("bool").literal("true"))) | ||
273 | } | ||
274 | |||
275 | @ParameterizedTest | ||
276 | @MethodSource("builtInEnumLiteralReferencesSource") | ||
277 | def void builtInEnumLiteralInNodeValueAssertionTest(String qualifiedName) { | ||
278 | val it = parseHelper.parse(''' | ||
279 | «qualifiedName»: 16. | ||
280 | ''') | ||
281 | assertThat(errors, empty) | ||
282 | assertThat(nodes, empty) | ||
283 | assertThat(nodeValueAssertion(0).node, equalTo(builtin.findEnum("bool").literal("true"))) | ||
284 | } | ||
285 | |||
286 | @ParameterizedTest | ||
287 | @MethodSource("builtInEnumLiteralReferencesSource") | ||
288 | def void bultInEnumLiteralInPredicateTest(String qualifiedName) { | ||
289 | val it = parseHelper.parse(''' | ||
290 | pred predicate() :- node(«qualifiedName»). | ||
291 | ''') | ||
292 | assertThat(errors, empty) | ||
293 | assertThat(pred("predicate").conj(0).lit(0).arg(0).node, equalTo(builtin.findEnum("bool").literal("true"))) | ||
294 | } | ||
295 | |||
296 | static def builtInEnumLiteralReferencesSource() { | ||
297 | Stream.of( | ||
298 | Arguments.of("true"), | ||
299 | Arguments.of("bool::true"), | ||
300 | Arguments.of("builtin::true"), | ||
301 | Arguments.of("builtin::bool::true") | ||
302 | ) | ||
303 | } | ||
262 | } | 304 | } |