aboutsummaryrefslogtreecommitdiffstats
path: root/language/src/main/java/org/eclipse/viatra/solver/language/resource/NodeNameCollector.java
blob: 4dee54347393151e12327c1ad830a10a055d5837 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.eclipse.viatra.solver.language.resource;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.viatra.solver.language.model.problem.Argument;
import org.eclipse.viatra.solver.language.model.problem.Assertion;
import org.eclipse.viatra.solver.language.model.problem.AssertionArgument;
import org.eclipse.viatra.solver.language.model.problem.Atom;
import org.eclipse.viatra.solver.language.model.problem.Conjunction;
import org.eclipse.viatra.solver.language.model.problem.EnumDeclaration;
import org.eclipse.viatra.solver.language.model.problem.Literal;
import org.eclipse.viatra.solver.language.model.problem.NegativeLiteral;
import org.eclipse.viatra.solver.language.model.problem.Node;
import org.eclipse.viatra.solver.language.model.problem.NodeAssertionArgument;
import org.eclipse.viatra.solver.language.model.problem.NodeValueAssertion;
import org.eclipse.viatra.solver.language.model.problem.PredicateDefinition;
import org.eclipse.viatra.solver.language.model.problem.Problem;
import org.eclipse.viatra.solver.language.model.problem.ProblemPackage;
import org.eclipse.viatra.solver.language.model.problem.Statement;
import org.eclipse.viatra.solver.language.model.problem.VariableOrNodeArgument;
import org.eclipse.xtext.linking.impl.LinkingHelper;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.IScopeProvider;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class NodeNameCollector {
	@Inject
	private LinkingHelper linkingHelper;

	@Inject
	private IQualifiedNameConverter qualifiedNameConverter;

	@Inject
	@Named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)
	private IScopeProvider scopeProvider;

	private final Set<String> nodeNames = new HashSet<>();
	private final Set<String> existingNodeNames = new HashSet<>();

	private IScope nodeScope;

	public Set<String> getNodeNames() {
		return nodeNames;
	}

	public void collectNodeNames(Problem problem) {
		nodeScope = scopeProvider.getScope(problem, ProblemPackage.Literals.ASSERTION__ARGUMENTS);
		collectEnumLiteralNames(problem);
		for (Statement statement : problem.getStatements()) {
			collectStatementNodeNames(statement);
		}
	}

	protected void collectEnumLiteralNames(Problem problem) {
		for (Statement statement : problem.getStatements()) {
			if (statement instanceof EnumDeclaration) {
				EnumDeclaration enumDeclaration = (EnumDeclaration) statement;
				for (Node literal : enumDeclaration.getLiterals()) {
					String name = literal.getName();
					if (ProblemDerivedStateComputer.validId(name)) {
						existingNodeNames.add(name);
					}
				}
			}
		}
	}

	protected void collectStatementNodeNames(Statement statement) {
		if (statement instanceof Assertion) {
			collectAssertionNodeNames((Assertion) statement);
		} else if (statement instanceof NodeValueAssertion) {
			collectNodeValueAssertionNodeNames((NodeValueAssertion) statement);
		} else if (statement instanceof PredicateDefinition) {
			collectPredicateDefinitionNodeNames((PredicateDefinition) statement);
		}
	}

	protected void collectAssertionNodeNames(Assertion assertion) {
		for (AssertionArgument argument : assertion.getArguments()) {
			if (argument instanceof NodeAssertionArgument) {
				addNodeNames(argument, ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE,
						ProblemDerivedStateComputer::validNodeName);
			}
		}
	}

	protected void collectNodeValueAssertionNodeNames(NodeValueAssertion nodeValueAssertion) {
		addNodeNames(nodeValueAssertion, ProblemPackage.Literals.NODE_VALUE_ASSERTION__NODE,
				ProblemDerivedStateComputer::validNodeName);
	}

	protected void collectPredicateDefinitionNodeNames(PredicateDefinition predicateDefinition) {
		for (Conjunction body : predicateDefinition.getBodies()) {
			for (Literal literal : body.getLiterals()) {
				Atom atom = null;
				if (literal instanceof Atom) {
					atom = (Atom) literal;
				} else if (literal instanceof NegativeLiteral) {
					NegativeLiteral negativeLiteral = (NegativeLiteral) literal;
					atom = negativeLiteral.getAtom();
				}
				if (atom == null) {
					continue;
				}
				for (Argument argument : atom.getArguments()) {
					if (argument instanceof VariableOrNodeArgument) {
						addNodeNames(argument, ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__VARIABLE_OR_NODE,
								ProblemDerivedStateComputer::validQuotedId);
					}
				}
			}
		}
	}

	private void addNodeNames(EObject eObject, EStructuralFeature feature, Predicate<String> condition) {
		List<INode> nodes = NodeModelUtils.findNodesForFeature(eObject, feature);
		for (INode node : nodes) {
			String nodeName = linkingHelper.getCrossRefNodeAsString(node, true);
			if (!condition.test(nodeName)) {
				continue;
			}
			QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(nodeName);
			if (!existingNodeNames.contains(nodeName) && nodeScope.getSingleElement(qualifiedName) == null) {
				nodeNames.add(nodeName);
			}
		}
	}
}