diff options
author | 2021-06-27 01:43:59 +0200 | |
---|---|---|
committer | 2021-06-27 02:01:19 +0200 | |
commit | b88c842ba625753ea185e6166cd967e694160798 (patch) | |
tree | 51931f892dff12d253f7697179e137bdfe705155 /org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java | |
parent | Opposite reference scope (diff) | |
download | refinery-b88c842ba625753ea185e6166cd967e694160798.tar.gz refinery-b88c842ba625753ea185e6166cd967e694160798.tar.zst refinery-b88c842ba625753ea185e6166cd967e694160798.zip |
Add enum support
Diffstat (limited to 'org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java')
-rw-r--r-- | org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java | 100 |
1 files changed, 80 insertions, 20 deletions
diff --git a/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java index c161de22..b885ce0e 100644 --- a/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java +++ b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java | |||
@@ -3,9 +3,11 @@ package org.eclipse.viatra.solver.language.resource; | |||
3 | import java.util.HashSet; | 3 | import java.util.HashSet; |
4 | import java.util.List; | 4 | import java.util.List; |
5 | import java.util.Set; | 5 | import java.util.Set; |
6 | import java.util.function.Predicate; | ||
6 | import java.util.regex.Pattern; | 7 | import java.util.regex.Pattern; |
7 | 8 | ||
8 | import org.eclipse.emf.ecore.EObject; | 9 | import org.eclipse.emf.ecore.EObject; |
10 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
9 | import org.eclipse.viatra.solver.language.ProblemUtil; | 11 | import org.eclipse.viatra.solver.language.ProblemUtil; |
10 | import org.eclipse.viatra.solver.language.model.problem.Argument; | 12 | import org.eclipse.viatra.solver.language.model.problem.Argument; |
11 | import org.eclipse.viatra.solver.language.model.problem.Assertion; | 13 | import org.eclipse.viatra.solver.language.model.problem.Assertion; |
@@ -30,18 +32,21 @@ import org.eclipse.xtext.nodemodel.INode; | |||
30 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils; | 32 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils; |
31 | import org.eclipse.xtext.resource.DerivedStateAwareResource; | 33 | import org.eclipse.xtext.resource.DerivedStateAwareResource; |
32 | import org.eclipse.xtext.resource.IDerivedStateComputer; | 34 | import org.eclipse.xtext.resource.IDerivedStateComputer; |
33 | import org.eclipse.xtext.scoping.IGlobalScopeProvider; | ||
34 | import org.eclipse.xtext.scoping.IScope; | 35 | import org.eclipse.xtext.scoping.IScope; |
36 | import org.eclipse.xtext.scoping.IScopeProvider; | ||
37 | import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider; | ||
35 | 38 | ||
36 | import com.google.common.base.Predicates; | ||
37 | import com.google.inject.Inject; | 39 | import com.google.inject.Inject; |
38 | import com.google.inject.Singleton; | 40 | import com.google.inject.Singleton; |
41 | import com.google.inject.name.Named; | ||
39 | 42 | ||
40 | @Singleton | 43 | @Singleton |
41 | public class ProblemDerivedStateComputer implements IDerivedStateComputer { | 44 | public class ProblemDerivedStateComputer implements IDerivedStateComputer { |
42 | public static final String NEW_NODE = "new"; | 45 | public static final String NEW_NODE = "new"; |
43 | 46 | ||
44 | private static final Pattern ID_REGEX = Pattern.compile("[_a-zA-Z][_0-9a-zA-Z]*|'(\\\\.|[^\\'])*'"); | 47 | private static final Pattern ID_REGEX = Pattern.compile("[_a-zA-Z][_0-9a-zA-Z]*"); |
48 | |||
49 | private static final Pattern QUOTED_ID_REGEX = Pattern.compile("'(\\\\.|[^\\'])*'"); | ||
45 | 50 | ||
46 | @Inject | 51 | @Inject |
47 | private LinkingHelper linkingHelper; | 52 | private LinkingHelper linkingHelper; |
@@ -50,7 +55,8 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
50 | private IQualifiedNameConverter qualifiedNameConverter; | 55 | private IQualifiedNameConverter qualifiedNameConverter; |
51 | 56 | ||
52 | @Inject | 57 | @Inject |
53 | private IGlobalScopeProvider scopeProvider; | 58 | @Named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE) |
59 | private IScopeProvider scopeProvider; | ||
54 | 60 | ||
55 | @Override | 61 | @Override |
56 | public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) { | 62 | public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) { |
@@ -65,11 +71,11 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
65 | installNewNodes(problem); | 71 | installNewNodes(problem); |
66 | if (!preLinkingPhase) { | 72 | if (!preLinkingPhase) { |
67 | installDerivedNodes(problem); | 73 | installDerivedNodes(problem); |
68 | } | 74 | for (Statement statement : problem.getStatements()) { |
69 | for (Statement statement : problem.getStatements()) { | 75 | if (statement instanceof PredicateDefinition) { |
70 | if (statement instanceof PredicateDefinition) { | 76 | PredicateDefinition definition = (PredicateDefinition) statement; |
71 | PredicateDefinition definition = (PredicateDefinition) statement; | 77 | installDerivedPredicateDefinitionState(definition); |
72 | installDerivedPredicateDefinitionState(definition); | 78 | } |
73 | } | 79 | } |
74 | } | 80 | } |
75 | } | 81 | } |
@@ -87,20 +93,30 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
87 | } | 93 | } |
88 | 94 | ||
89 | protected void installDerivedNodes(Problem problem) { | 95 | protected void installDerivedNodes(Problem problem) { |
90 | IScope nodeScope = scopeProvider.getScope(problem.eResource(), ProblemPackage.Literals.ASSERTION__ARGUMENTS, | 96 | IScope nodeScope = scopeProvider.getScope(problem, ProblemPackage.Literals.ASSERTION__ARGUMENTS); |
91 | Predicates.alwaysTrue()); | ||
92 | Set<String> nodeNames = new HashSet<>(); | 97 | Set<String> nodeNames = new HashSet<>(); |
93 | for (Statement statement : problem.getStatements()) { | 98 | for (Statement statement : problem.getStatements()) { |
94 | if (statement instanceof Assertion) { | 99 | if (statement instanceof Assertion) { |
95 | Assertion assertion = (Assertion) statement; | 100 | addNodeNames(nodeNames, nodeScope, statement, ProblemPackage.Literals.ASSERTION__ARGUMENTS, |
96 | List<INode> nodes = NodeModelUtils.findNodesForFeature(assertion, | 101 | ProblemDerivedStateComputer::validNodeName); |
97 | ProblemPackage.Literals.ASSERTION__ARGUMENTS); | 102 | } else if (statement instanceof PredicateDefinition) { |
98 | for (INode node : nodes) { | 103 | PredicateDefinition predicateDefinition = (PredicateDefinition) statement; |
99 | String nodeName = linkingHelper.getCrossRefNodeAsString(node, true); | 104 | for (Conjunction body : predicateDefinition.getBodies()) { |
100 | if (validId(nodeName)) { | 105 | for (Literal literal : body.getLiterals()) { |
101 | QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(nodeName); | 106 | Atom atom = null; |
102 | if (!nodeNames.contains(nodeName) && nodeScope.getSingleElement(qualifiedName) == null) { | 107 | if (literal instanceof Atom) { |
103 | nodeNames.add(nodeName); | 108 | atom = (Atom) literal; |
109 | } else if (literal instanceof NegativeLiteral) { | ||
110 | NegativeLiteral negativeLiteral = (NegativeLiteral) literal; | ||
111 | atom = negativeLiteral.getAtom(); | ||
112 | } | ||
113 | if (atom == null) { | ||
114 | continue; | ||
115 | } | ||
116 | for (Argument argument : atom.getArguments()) { | ||
117 | addNodeNames(nodeNames, nodeScope, argument, | ||
118 | ProblemPackage.Literals.ARGUMENT__VARIABLE_OR_NODE, | ||
119 | ProblemDerivedStateComputer::validQuotedId); | ||
104 | } | 120 | } |
105 | } | 121 | } |
106 | } | 122 | } |
@@ -113,6 +129,21 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
113 | } | 129 | } |
114 | } | 130 | } |
115 | 131 | ||
132 | private void addNodeNames(Set<String> nodeNames, IScope nodeScope, EObject eObject, EStructuralFeature feature, | ||
133 | Predicate<String> condition) { | ||
134 | List<INode> nodes = NodeModelUtils.findNodesForFeature(eObject, feature); | ||
135 | for (INode node : nodes) { | ||
136 | String nodeName = linkingHelper.getCrossRefNodeAsString(node, true); | ||
137 | if (!condition.test(nodeName)) { | ||
138 | continue; | ||
139 | } | ||
140 | QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(nodeName); | ||
141 | if (!nodeNames.contains(nodeName) && nodeScope.getSingleElement(qualifiedName) == null) { | ||
142 | nodeNames.add(nodeName); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
116 | protected Node createNode(String name) { | 147 | protected Node createNode(String name) { |
117 | Node node = ProblemFactory.eINSTANCE.createNode(); | 148 | Node node = ProblemFactory.eINSTANCE.createNode(); |
118 | node.setName(name); | 149 | node.setName(name); |
@@ -158,6 +189,7 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
158 | 189 | ||
159 | protected void createSigletonVariablesAndCollectVariables(Atom atom, Set<String> knownVariables, | 190 | protected void createSigletonVariablesAndCollectVariables(Atom atom, Set<String> knownVariables, |
160 | Set<String> newVariables) { | 191 | Set<String> newVariables) { |
192 | IScope scope = scopeProvider.getScope(atom, ProblemPackage.Literals.ARGUMENT__VARIABLE_OR_NODE); | ||
161 | List<INode> nodes = NodeModelUtils.findNodesForFeature(atom, ProblemPackage.Literals.ATOM__ARGUMENTS); | 193 | List<INode> nodes = NodeModelUtils.findNodesForFeature(atom, ProblemPackage.Literals.ATOM__ARGUMENTS); |
162 | int nodesSize = nodes.size(); | 194 | int nodesSize = nodes.size(); |
163 | List<Argument> arguments = atom.getArguments(); | 195 | List<Argument> arguments = atom.getArguments(); |
@@ -165,6 +197,13 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
165 | for (int i = 0; i < nodesSize; i++) { | 197 | for (int i = 0; i < nodesSize; i++) { |
166 | INode node = nodes.get(i); | 198 | INode node = nodes.get(i); |
167 | String variableName = linkingHelper.getCrossRefNodeAsString(node, true); | 199 | String variableName = linkingHelper.getCrossRefNodeAsString(node, true); |
200 | if (!validId(variableName)) { | ||
201 | continue; | ||
202 | } | ||
203 | QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(variableName); | ||
204 | if (scope.getSingleElement(qualifiedName) != null) { | ||
205 | continue; | ||
206 | } | ||
168 | if (ProblemUtil.isSingletonVariableName(variableName)) { | 207 | if (ProblemUtil.isSingletonVariableName(variableName)) { |
169 | if (i < argumentsSize) { | 208 | if (i < argumentsSize) { |
170 | createSingletonVariable(arguments.get(i), variableName); | 209 | createSingletonVariable(arguments.get(i), variableName); |
@@ -223,9 +262,13 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
223 | for (Conjunction body : definition.getBodies()) { | 262 | for (Conjunction body : definition.getBodies()) { |
224 | body.getImplicitVariables().clear(); | 263 | body.getImplicitVariables().clear(); |
225 | for (Literal literal : body.getLiterals()) { | 264 | for (Literal literal : body.getLiterals()) { |
265 | if (literal instanceof Atom) { | ||
266 | discardDerivedAtomState((Atom) literal); | ||
267 | } | ||
226 | if (literal instanceof NegativeLiteral) { | 268 | if (literal instanceof NegativeLiteral) { |
227 | NegativeLiteral negativeLiteral = (NegativeLiteral) literal; | 269 | NegativeLiteral negativeLiteral = (NegativeLiteral) literal; |
228 | negativeLiteral.getImplicitVariables().clear(); | 270 | negativeLiteral.getImplicitVariables().clear(); |
271 | discardDerivedAtomState(negativeLiteral.getAtom()); | ||
229 | } | 272 | } |
230 | } | 273 | } |
231 | } | 274 | } |
@@ -233,7 +276,24 @@ public class ProblemDerivedStateComputer implements IDerivedStateComputer { | |||
233 | } | 276 | } |
234 | } | 277 | } |
235 | 278 | ||
279 | protected void discardDerivedAtomState(Atom atom) { | ||
280 | if (atom == null) { | ||
281 | return; | ||
282 | } | ||
283 | for (Argument argument : atom.getArguments()) { | ||
284 | argument.setSingletonVariable(null); | ||
285 | } | ||
286 | } | ||
287 | |||
236 | protected static boolean validId(String name) { | 288 | protected static boolean validId(String name) { |
237 | return name != null && ID_REGEX.matcher(name).matches(); | 289 | return name != null && ID_REGEX.matcher(name).matches(); |
238 | } | 290 | } |
291 | |||
292 | protected static boolean validQuotedId(String name) { | ||
293 | return name != null && QUOTED_ID_REGEX.matcher(name).matches(); | ||
294 | } | ||
295 | |||
296 | protected static boolean validNodeName(String name) { | ||
297 | return validId(name) || validQuotedId(name); | ||
298 | } | ||
239 | } | 299 | } |