diff options
Diffstat (limited to 'org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemLocationInFileProvider.java')
-rw-r--r-- | org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemLocationInFileProvider.java | 130 |
1 files changed, 130 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/resource/ProblemLocationInFileProvider.java b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemLocationInFileProvider.java new file mode 100644 index 00000000..dfffc7ef --- /dev/null +++ b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/resource/ProblemLocationInFileProvider.java | |||
@@ -0,0 +1,130 @@ | |||
1 | package org.eclipse.viatra.solver.language.resource; | ||
2 | |||
3 | import java.util.Iterator; | ||
4 | |||
5 | import org.eclipse.emf.ecore.EObject; | ||
6 | import org.eclipse.viatra.solver.language.model.problem.Assertion; | ||
7 | import org.eclipse.viatra.solver.language.model.problem.Atom; | ||
8 | import org.eclipse.viatra.solver.language.model.problem.Conjunction; | ||
9 | import org.eclipse.viatra.solver.language.model.problem.ImplicitVariable; | ||
10 | import org.eclipse.viatra.solver.language.model.problem.Literal; | ||
11 | import org.eclipse.viatra.solver.language.model.problem.NegativeLiteral; | ||
12 | import org.eclipse.viatra.solver.language.model.problem.Node; | ||
13 | import org.eclipse.viatra.solver.language.model.problem.Problem; | ||
14 | import org.eclipse.viatra.solver.language.model.problem.ProblemPackage; | ||
15 | import org.eclipse.viatra.solver.language.model.problem.Statement; | ||
16 | import org.eclipse.xtext.EcoreUtil2; | ||
17 | import org.eclipse.xtext.naming.IQualifiedNameProvider; | ||
18 | import org.eclipse.xtext.naming.QualifiedName; | ||
19 | import org.eclipse.xtext.resource.DefaultLocationInFileProvider; | ||
20 | import org.eclipse.xtext.resource.IEObjectDescription; | ||
21 | import org.eclipse.xtext.scoping.IScope; | ||
22 | import org.eclipse.xtext.scoping.IScopeProvider; | ||
23 | import org.eclipse.xtext.util.ITextRegion; | ||
24 | |||
25 | import com.google.inject.Inject; | ||
26 | |||
27 | public class ProblemLocationInFileProvider extends DefaultLocationInFileProvider { | ||
28 | @Inject | ||
29 | private IQualifiedNameProvider qualifiedNameProvider; | ||
30 | |||
31 | @Inject | ||
32 | private IScopeProvider scopeProvider; | ||
33 | |||
34 | @Override | ||
35 | protected ITextRegion doGetTextRegion(EObject obj, RegionDescription query) { | ||
36 | if (obj instanceof Node) { | ||
37 | return getNodeTextRegion((Node) obj, query); | ||
38 | } | ||
39 | if (obj instanceof ImplicitVariable) { | ||
40 | return getVariableTextRegion((ImplicitVariable) obj, query); | ||
41 | } | ||
42 | return super.doGetTextRegion(obj, query); | ||
43 | } | ||
44 | |||
45 | protected ITextRegion getNodeTextRegion(Node node, RegionDescription query) { | ||
46 | ITextRegion newNodeRegion = getNewNodeTextRegion(node, query); | ||
47 | if (newNodeRegion != null) { | ||
48 | return newNodeRegion; | ||
49 | } | ||
50 | return getNodeFirstReferenceTextRegion(node, query); | ||
51 | } | ||
52 | |||
53 | protected ITextRegion getNewNodeTextRegion(Node node, RegionDescription query) { | ||
54 | QualifiedName nodeName = qualifiedNameProvider.getFullyQualifiedName(node); | ||
55 | if (nodeName == null || nodeName.getSegmentCount() <= 1) { | ||
56 | return null; | ||
57 | } | ||
58 | if (ProblemDerivedStateComputer.NEW_NODE.equals(nodeName.getLastSegment())) { | ||
59 | QualifiedName className = nodeName.skipLast(1); | ||
60 | IScope classScope = scopeProvider.getScope(node, ProblemPackage.Literals.ASSERTION__RELATION); | ||
61 | IEObjectDescription description = classScope.getSingleElement(className); | ||
62 | if (description == null) { | ||
63 | return null; | ||
64 | } | ||
65 | EObject classDeclaration = description.getEObjectOrProxy(); | ||
66 | if (!classDeclaration.eIsProxy()) { | ||
67 | return doGetTextRegion(classDeclaration, query); | ||
68 | } | ||
69 | } | ||
70 | return null; | ||
71 | } | ||
72 | |||
73 | protected ITextRegion getNodeFirstReferenceTextRegion(Node node, RegionDescription query) { | ||
74 | Problem problem = EcoreUtil2.getContainerOfType(node, Problem.class); | ||
75 | if (problem == null) { | ||
76 | return ITextRegion.EMPTY_REGION; | ||
77 | } | ||
78 | for (Statement statement : problem.getStatements()) { | ||
79 | if (statement instanceof Assertion) { | ||
80 | Assertion assertion = (Assertion) statement; | ||
81 | int index = assertion.getArguments().indexOf(node); | ||
82 | if (index >= 0) { | ||
83 | return doGetLocationOfFeature(assertion, ProblemPackage.Literals.ASSERTION__ARGUMENTS, index, | ||
84 | query); | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | return ITextRegion.EMPTY_REGION; | ||
89 | } | ||
90 | |||
91 | protected ITextRegion getVariableTextRegion(ImplicitVariable variable, RegionDescription query) { | ||
92 | EObject container = variable.eContainer(); | ||
93 | if (container instanceof Conjunction) { | ||
94 | return getFirstReferenceToVariableInConjunction(variable, (Conjunction) container, query); | ||
95 | } | ||
96 | if (container instanceof NegativeLiteral) { | ||
97 | return getFirstReferenceToVariableInNegativeLiteral(variable, (NegativeLiteral) container, query); | ||
98 | } | ||
99 | return ITextRegion.EMPTY_REGION; | ||
100 | } | ||
101 | |||
102 | protected ITextRegion getFirstReferenceToVariableInConjunction(ImplicitVariable variable, Conjunction conjunction, | ||
103 | RegionDescription query) { | ||
104 | Iterator<Literal> iterator = conjunction.getLiterals().iterator(); | ||
105 | ITextRegion found = ITextRegion.EMPTY_REGION; | ||
106 | while (found == ITextRegion.EMPTY_REGION && iterator.hasNext()) { | ||
107 | Literal literal = iterator.next(); | ||
108 | if (literal instanceof Atom) { | ||
109 | found = getFirstReferenceToVariableInAtom(variable, (Atom) literal, query); | ||
110 | } else if (literal instanceof NegativeLiteral) { | ||
111 | found = getFirstReferenceToVariableInNegativeLiteral(variable, (NegativeLiteral) literal, query); | ||
112 | } | ||
113 | } | ||
114 | return found; | ||
115 | } | ||
116 | |||
117 | protected ITextRegion getFirstReferenceToVariableInNegativeLiteral(ImplicitVariable variable, | ||
118 | NegativeLiteral literal, RegionDescription query) { | ||
119 | return getFirstReferenceToVariableInAtom(variable, literal.getAtom(), query); | ||
120 | } | ||
121 | |||
122 | protected ITextRegion getFirstReferenceToVariableInAtom(ImplicitVariable variable, Atom atom, | ||
123 | RegionDescription query) { | ||
124 | int index = atom.getArguments().indexOf(variable); | ||
125 | if (index >= 0) { | ||
126 | return doGetLocationOfFeature(atom, ProblemPackage.Literals.ATOM__ARGUMENTS, index, query); | ||
127 | } | ||
128 | return ITextRegion.EMPTY_REGION; | ||
129 | } | ||
130 | } | ||