diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-06-27 14:22:40 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-06-27 14:22:40 +0200 |
commit | 07719e927f9d398765e661c84fd8778cefb39083 (patch) | |
tree | 9652080f7e580f09a0763a3e258348dea6e02684 /org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java | |
parent | Add enum support (diff) | |
download | refinery-07719e927f9d398765e661c84fd8778cefb39083.tar.gz refinery-07719e927f9d398765e661c84fd8778cefb39083.tar.zst refinery-07719e927f9d398765e661c84fd8778cefb39083.zip |
Simplify project layout
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 | 299 |
1 files changed, 0 insertions, 299 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 deleted file mode 100644 index b885ce0e..00000000 --- a/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemDerivedStateComputer.java +++ /dev/null | |||
@@ -1,299 +0,0 @@ | |||
1 | package org.eclipse.viatra.solver.language.resource; | ||
2 | |||
3 | import java.util.HashSet; | ||
4 | import java.util.List; | ||
5 | import java.util.Set; | ||
6 | import java.util.function.Predicate; | ||
7 | import java.util.regex.Pattern; | ||
8 | |||
9 | import org.eclipse.emf.ecore.EObject; | ||
10 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
11 | import org.eclipse.viatra.solver.language.ProblemUtil; | ||
12 | import org.eclipse.viatra.solver.language.model.problem.Argument; | ||
13 | import org.eclipse.viatra.solver.language.model.problem.Assertion; | ||
14 | import org.eclipse.viatra.solver.language.model.problem.Atom; | ||
15 | import org.eclipse.viatra.solver.language.model.problem.ClassDeclaration; | ||
16 | import org.eclipse.viatra.solver.language.model.problem.Conjunction; | ||
17 | import org.eclipse.viatra.solver.language.model.problem.ExistentialQuantifier; | ||
18 | import org.eclipse.viatra.solver.language.model.problem.ImplicitVariable; | ||
19 | import org.eclipse.viatra.solver.language.model.problem.Literal; | ||
20 | import org.eclipse.viatra.solver.language.model.problem.NegativeLiteral; | ||
21 | import org.eclipse.viatra.solver.language.model.problem.Node; | ||
22 | import org.eclipse.viatra.solver.language.model.problem.Parameter; | ||
23 | import org.eclipse.viatra.solver.language.model.problem.PredicateDefinition; | ||
24 | import org.eclipse.viatra.solver.language.model.problem.Problem; | ||
25 | import org.eclipse.viatra.solver.language.model.problem.ProblemFactory; | ||
26 | import org.eclipse.viatra.solver.language.model.problem.ProblemPackage; | ||
27 | import org.eclipse.viatra.solver.language.model.problem.Statement; | ||
28 | import org.eclipse.xtext.linking.impl.LinkingHelper; | ||
29 | import org.eclipse.xtext.naming.IQualifiedNameConverter; | ||
30 | import org.eclipse.xtext.naming.QualifiedName; | ||
31 | import org.eclipse.xtext.nodemodel.INode; | ||
32 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils; | ||
33 | import org.eclipse.xtext.resource.DerivedStateAwareResource; | ||
34 | import org.eclipse.xtext.resource.IDerivedStateComputer; | ||
35 | import org.eclipse.xtext.scoping.IScope; | ||
36 | import org.eclipse.xtext.scoping.IScopeProvider; | ||
37 | import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider; | ||
38 | |||
39 | import com.google.inject.Inject; | ||
40 | import com.google.inject.Singleton; | ||
41 | import com.google.inject.name.Named; | ||
42 | |||
43 | @Singleton | ||
44 | public class ProblemDerivedStateComputer implements IDerivedStateComputer { | ||
45 | public static final String NEW_NODE = "new"; | ||
46 | |||
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("'(\\\\.|[^\\'])*'"); | ||
50 | |||
51 | @Inject | ||
52 | private LinkingHelper linkingHelper; | ||
53 | |||
54 | @Inject | ||
55 | private IQualifiedNameConverter qualifiedNameConverter; | ||
56 | |||
57 | @Inject | ||
58 | @Named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE) | ||
59 | private IScopeProvider scopeProvider; | ||
60 | |||
61 | @Override | ||
62 | public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) { | ||
63 | for (EObject object : resource.getContents()) { | ||
64 | if (object instanceof Problem) { | ||
65 | installDerivedProblemState((Problem) object, preLinkingPhase); | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | protected void installDerivedProblemState(Problem problem, boolean preLinkingPhase) { | ||
71 | installNewNodes(problem); | ||
72 | if (!preLinkingPhase) { | ||
73 | installDerivedNodes(problem); | ||
74 | for (Statement statement : problem.getStatements()) { | ||
75 | if (statement instanceof PredicateDefinition) { | ||
76 | PredicateDefinition definition = (PredicateDefinition) statement; | ||
77 | installDerivedPredicateDefinitionState(definition); | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | protected void installNewNodes(Problem problem) { | ||
84 | for (Statement statement : problem.getStatements()) { | ||
85 | if (statement instanceof ClassDeclaration) { | ||
86 | ClassDeclaration declaration = (ClassDeclaration) statement; | ||
87 | if (!declaration.isAbstract()) { | ||
88 | Node newNode = createNode(NEW_NODE); | ||
89 | declaration.setNewNode(newNode); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | protected void installDerivedNodes(Problem problem) { | ||
96 | IScope nodeScope = scopeProvider.getScope(problem, ProblemPackage.Literals.ASSERTION__ARGUMENTS); | ||
97 | Set<String> nodeNames = new HashSet<>(); | ||
98 | for (Statement statement : problem.getStatements()) { | ||
99 | if (statement instanceof Assertion) { | ||
100 | addNodeNames(nodeNames, nodeScope, statement, ProblemPackage.Literals.ASSERTION__ARGUMENTS, | ||
101 | ProblemDerivedStateComputer::validNodeName); | ||
102 | } else if (statement instanceof PredicateDefinition) { | ||
103 | PredicateDefinition predicateDefinition = (PredicateDefinition) statement; | ||
104 | for (Conjunction body : predicateDefinition.getBodies()) { | ||
105 | for (Literal literal : body.getLiterals()) { | ||
106 | Atom atom = null; | ||
107 | if (literal instanceof Atom) { | ||
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); | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | List<Node> grapNodes = problem.getNodes(); | ||
126 | for (String nodeName : nodeNames) { | ||
127 | Node graphNode = createNode(nodeName); | ||
128 | grapNodes.add(graphNode); | ||
129 | } | ||
130 | } | ||
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 | |||
147 | protected Node createNode(String name) { | ||
148 | Node node = ProblemFactory.eINSTANCE.createNode(); | ||
149 | node.setName(name); | ||
150 | return node; | ||
151 | } | ||
152 | |||
153 | protected void installDerivedPredicateDefinitionState(PredicateDefinition definition) { | ||
154 | Set<String> parameterNames = new HashSet<>(); | ||
155 | for (Parameter parameter : definition.getParameters()) { | ||
156 | String name = parameter.getName(); | ||
157 | if (name != null) { | ||
158 | parameterNames.add(name); | ||
159 | } | ||
160 | } | ||
161 | for (Conjunction body : definition.getBodies()) { | ||
162 | installDeriveConjunctionState(body, parameterNames); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | protected void installDeriveConjunctionState(Conjunction conjunction, Set<String> knownVariables) { | ||
167 | Set<String> newVariables = new HashSet<>(); | ||
168 | for (Literal literal : conjunction.getLiterals()) { | ||
169 | if (literal instanceof Atom) { | ||
170 | Atom atom = (Atom) literal; | ||
171 | createSigletonVariablesAndCollectVariables(atom, knownVariables, newVariables); | ||
172 | } | ||
173 | } | ||
174 | createVariables(conjunction, newVariables); | ||
175 | newVariables.addAll(knownVariables); | ||
176 | for (Literal literal : conjunction.getLiterals()) { | ||
177 | if (literal instanceof NegativeLiteral) { | ||
178 | NegativeLiteral negativeLiteral = (NegativeLiteral) literal; | ||
179 | installDeriveNegativeLiteralState(negativeLiteral, newVariables); | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | |||
184 | protected void installDeriveNegativeLiteralState(NegativeLiteral negativeLiteral, Set<String> knownVariables) { | ||
185 | Set<String> newVariables = new HashSet<>(); | ||
186 | createSigletonVariablesAndCollectVariables(negativeLiteral.getAtom(), knownVariables, newVariables); | ||
187 | createVariables(negativeLiteral, newVariables); | ||
188 | } | ||
189 | |||
190 | protected void createSigletonVariablesAndCollectVariables(Atom atom, Set<String> knownVariables, | ||
191 | Set<String> newVariables) { | ||
192 | IScope scope = scopeProvider.getScope(atom, ProblemPackage.Literals.ARGUMENT__VARIABLE_OR_NODE); | ||
193 | List<INode> nodes = NodeModelUtils.findNodesForFeature(atom, ProblemPackage.Literals.ATOM__ARGUMENTS); | ||
194 | int nodesSize = nodes.size(); | ||
195 | List<Argument> arguments = atom.getArguments(); | ||
196 | int argumentsSize = arguments.size(); | ||
197 | for (int i = 0; i < nodesSize; i++) { | ||
198 | INode node = nodes.get(i); | ||
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 | } | ||
207 | if (ProblemUtil.isSingletonVariableName(variableName)) { | ||
208 | if (i < argumentsSize) { | ||
209 | createSingletonVariable(arguments.get(i), variableName); | ||
210 | } | ||
211 | continue; | ||
212 | } | ||
213 | if (!knownVariables.contains(variableName)) { | ||
214 | newVariables.add(variableName); | ||
215 | } | ||
216 | } | ||
217 | } | ||
218 | |||
219 | protected void createVariables(ExistentialQuantifier quantifier, Set<String> newVariables) { | ||
220 | for (String variableName : newVariables) { | ||
221 | createVariable(quantifier, variableName); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | protected void createVariable(ExistentialQuantifier quantifier, String variableName) { | ||
226 | if (validId(variableName)) { | ||
227 | ImplicitVariable variable = createNamedVariable(variableName); | ||
228 | quantifier.getImplicitVariables().add(variable); | ||
229 | } | ||
230 | } | ||
231 | |||
232 | protected void createSingletonVariable(Argument argument, String variableName) { | ||
233 | if (validId(variableName)) { | ||
234 | ImplicitVariable variable = createNamedVariable(variableName); | ||
235 | argument.setSingletonVariable(variable); | ||
236 | } | ||
237 | } | ||
238 | |||
239 | protected ImplicitVariable createNamedVariable(String variableName) { | ||
240 | ImplicitVariable variable = ProblemFactory.eINSTANCE.createImplicitVariable(); | ||
241 | variable.setName(variableName); | ||
242 | return variable; | ||
243 | } | ||
244 | |||
245 | @Override | ||
246 | public void discardDerivedState(DerivedStateAwareResource resource) { | ||
247 | for (EObject object : resource.getContents()) { | ||
248 | if (object instanceof Problem) { | ||
249 | discardDerivedProblemState((Problem) object); | ||
250 | } | ||
251 | } | ||
252 | } | ||
253 | |||
254 | protected void discardDerivedProblemState(Problem problem) { | ||
255 | problem.getNodes().clear(); | ||
256 | for (Statement statement : problem.getStatements()) { | ||
257 | if (statement instanceof ClassDeclaration) { | ||
258 | ClassDeclaration classDeclaration = (ClassDeclaration) statement; | ||
259 | classDeclaration.setNewNode(null); | ||
260 | } else if (statement instanceof PredicateDefinition) { | ||
261 | PredicateDefinition definition = (PredicateDefinition) statement; | ||
262 | for (Conjunction body : definition.getBodies()) { | ||
263 | body.getImplicitVariables().clear(); | ||
264 | for (Literal literal : body.getLiterals()) { | ||
265 | if (literal instanceof Atom) { | ||
266 | discardDerivedAtomState((Atom) literal); | ||
267 | } | ||
268 | if (literal instanceof NegativeLiteral) { | ||
269 | NegativeLiteral negativeLiteral = (NegativeLiteral) literal; | ||
270 | negativeLiteral.getImplicitVariables().clear(); | ||
271 | discardDerivedAtomState(negativeLiteral.getAtom()); | ||
272 | } | ||
273 | } | ||
274 | } | ||
275 | } | ||
276 | } | ||
277 | } | ||
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 | |||
288 | protected static boolean validId(String name) { | ||
289 | return name != null && ID_REGEX.matcher(name).matches(); | ||
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 | } | ||
299 | } | ||