aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/scoping/ProblemScopeProvider.java
blob: fc4034ae4c7edd0317962ea1389253725ed088f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * generated by Xtext 2.25.0
 */
package org.eclipse.viatra.solver.language.scoping;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.viatra.solver.language.ProblemUtil;
import org.eclipse.viatra.solver.language.model.problem.Argument;
import org.eclipse.viatra.solver.language.model.problem.ClassDeclaration;
import org.eclipse.viatra.solver.language.model.problem.ExistentialQuantifier;
import org.eclipse.viatra.solver.language.model.problem.PredicateDefinition;
import org.eclipse.viatra.solver.language.model.problem.ProblemPackage;
import org.eclipse.viatra.solver.language.model.problem.ReferenceDeclaration;
import org.eclipse.viatra.solver.language.model.problem.Relation;
import org.eclipse.viatra.solver.language.model.problem.Variable;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;

/**
 * This class contains custom scoping description.
 * 
 * See
 * https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping
 * on how and when to use it.
 */
public class ProblemScopeProvider extends AbstractProblemScopeProvider {

	@Override
	public IScope getScope(EObject context, EReference reference) {
		IScope scope = super.getScope(context, reference);
		if (reference == ProblemPackage.Literals.ARGUMENT__VARIABLE_OR_NODE) {
			return getVariableScope(context, scope);
		} else if (reference == ProblemPackage.Literals.REFERENCE_DECLARATION__OPPOSITE) {
			return getOppositeScope(context, scope);
		}
		return scope;
	}

	protected IScope getVariableScope(EObject context, IScope delegateScope) {
		List<Variable> variables = new ArrayList<>();
		EObject currentContext = context;
		if (context instanceof Argument) {
			Argument argument = (Argument) context;
			Variable singletonVariable = argument.getSingletonVariable();
			if (singletonVariable != null) {
				variables.add(singletonVariable);
			}
		}
		while (currentContext != null && !(currentContext instanceof PredicateDefinition)) {
			if (currentContext instanceof ExistentialQuantifier) {
				ExistentialQuantifier quantifier = (ExistentialQuantifier) currentContext;
				variables.addAll(quantifier.getImplicitVariables());
			}
			currentContext = currentContext.eContainer();
		}
		if (currentContext instanceof PredicateDefinition) {
			PredicateDefinition definition = (PredicateDefinition) currentContext;
			variables.addAll(definition.getParameters());
		}
		return Scopes.scopeFor(variables, delegateScope);
	}

	protected IScope getOppositeScope(EObject context, IScope delegateScope) {
		ReferenceDeclaration referenceDeclaration = EcoreUtil2.getContainerOfType(context, ReferenceDeclaration.class);
		if (referenceDeclaration == null) {
			return delegateScope;
		}
		Relation relation = referenceDeclaration.getReferenceType();
		if (!(relation instanceof ClassDeclaration)) {
			return delegateScope;
		}
		ClassDeclaration classDeclaration = (ClassDeclaration) relation;
		Collection<ReferenceDeclaration> referenceDeclarations = ProblemUtil
				.getAllReferenceDeclarations(classDeclaration);
		return Scopes.scopeFor(referenceDeclarations, delegateScope);
	}
}