diff options
author | 2021-06-25 17:12:45 +0200 | |
---|---|---|
committer | 2021-06-25 17:12:45 +0200 | |
commit | baba88758a0ea5f671ab4a2aeacec374bed938c4 (patch) | |
tree | 1be30230a0959ce8d22986d91b1ed2460a328148 /org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java | |
parent | Initial commit (diff) | |
download | refinery-baba88758a0ea5f671ab4a2aeacec374bed938c4.tar.gz refinery-baba88758a0ea5f671ab4a2aeacec374bed938c4.tar.zst refinery-baba88758a0ea5f671ab4a2aeacec374bed938c4.zip |
Add initial prototype
Diffstat (limited to 'org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java')
-rw-r--r-- | org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java new file mode 100644 index 00000000..254284a8 --- /dev/null +++ b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java | |||
@@ -0,0 +1,74 @@ | |||
1 | /* | ||
2 | * generated by Xtext 2.25.0 | ||
3 | */ | ||
4 | package org.eclipse.viatra.solver.language.scoping; | ||
5 | |||
6 | import java.util.ArrayList; | ||
7 | import java.util.List; | ||
8 | |||
9 | import org.eclipse.emf.ecore.EObject; | ||
10 | import org.eclipse.emf.ecore.EReference; | ||
11 | import org.eclipse.viatra.solver.language.model.problem.ClassDeclaration; | ||
12 | import org.eclipse.viatra.solver.language.model.problem.ExistentialQuantifier; | ||
13 | import org.eclipse.viatra.solver.language.model.problem.PredicateDefinition; | ||
14 | import org.eclipse.viatra.solver.language.model.problem.Problem; | ||
15 | import org.eclipse.viatra.solver.language.model.problem.ProblemPackage; | ||
16 | import org.eclipse.viatra.solver.language.model.problem.ReferenceDeclaration; | ||
17 | import org.eclipse.viatra.solver.language.model.problem.Statement; | ||
18 | import org.eclipse.viatra.solver.language.model.problem.Variable; | ||
19 | import org.eclipse.xtext.EcoreUtil2; | ||
20 | import org.eclipse.xtext.naming.QualifiedName; | ||
21 | import org.eclipse.xtext.scoping.IScope; | ||
22 | import org.eclipse.xtext.scoping.Scopes; | ||
23 | |||
24 | import com.google.common.collect.ImmutableList; | ||
25 | |||
26 | /** | ||
27 | * This class contains custom scoping description. | ||
28 | * | ||
29 | * See | ||
30 | * https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping | ||
31 | * on how and when to use it. | ||
32 | */ | ||
33 | public class ProblemScopeProvider extends AbstractProblemScopeProvider { | ||
34 | |||
35 | @Override | ||
36 | public IScope getScope(EObject context, EReference reference) { | ||
37 | IScope scope = super.getScope(context, reference); | ||
38 | if (reference == ProblemPackage.Literals.ATOM__ARGUMENTS) { | ||
39 | return getVariableScope(context, scope); | ||
40 | } else if (EcoreUtil2.isAssignableFrom(reference.getEReferenceType(), | ||
41 | ProblemPackage.Literals.REFERENCE_DECLARATION)) { | ||
42 | Problem problem = EcoreUtil2.getContainerOfType(context, Problem.class); | ||
43 | if (problem == null) { | ||
44 | return scope; | ||
45 | } | ||
46 | ImmutableList.Builder<ReferenceDeclaration> builder = ImmutableList.builder(); | ||
47 | for (Statement statement : problem.getStatements()) { | ||
48 | if (statement instanceof ClassDeclaration) { | ||
49 | ClassDeclaration classDeclaration = (ClassDeclaration) statement; | ||
50 | builder.addAll(classDeclaration.getReferenceDeclarations()); | ||
51 | } | ||
52 | } | ||
53 | return Scopes.scopeFor(builder.build(), scope); | ||
54 | } | ||
55 | return scope; | ||
56 | } | ||
57 | |||
58 | protected IScope getVariableScope(EObject context, IScope delegateScope) { | ||
59 | List<Variable> variables = new ArrayList<>(); | ||
60 | EObject currentContext = context; | ||
61 | while (currentContext != null && !(currentContext instanceof PredicateDefinition)) { | ||
62 | if (currentContext instanceof ExistentialQuantifier) { | ||
63 | ExistentialQuantifier quantifier = (ExistentialQuantifier) currentContext; | ||
64 | variables.addAll(quantifier.getImplicitVariables()); | ||
65 | } | ||
66 | currentContext = currentContext.eContainer(); | ||
67 | } | ||
68 | if (currentContext instanceof PredicateDefinition) { | ||
69 | PredicateDefinition definition = (PredicateDefinition) currentContext; | ||
70 | variables.addAll(definition.getParameters()); | ||
71 | } | ||
72 | return Scopes.scopeFor(variables); | ||
73 | } | ||
74 | } | ||