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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package org.eclipse.viatra.solver.language.resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.viatra.solver.language.model.problem.Argument;
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.ExistentialQuantifier;
import org.eclipse.viatra.solver.language.model.problem.ImplicitVariable;
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.Parameter;
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.ProblemFactory;
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.viatra.solver.language.naming.NamingUtil;
import org.eclipse.xtext.linking.impl.LinkingHelper;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
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.Singleton;
import com.google.inject.name.Named;
@Singleton
public class DerivedVariableComputer {
@Inject
private LinkingHelper linkingHelper;
@Inject
private IQualifiedNameConverter qualifiedNameConverter;
@Inject
@Named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)
private IScopeProvider scopeProvider;
public void installDerivedVariables(Problem problem, Set<String> nodeNames) {
for (Statement statement : problem.getStatements()) {
if (statement instanceof PredicateDefinition definition) {
installDerivedPredicateDefinitionState(definition, nodeNames);
}
}
}
protected void installDerivedPredicateDefinitionState(PredicateDefinition definition, Set<String> nodeNames) {
Set<String> knownVariables = new HashSet<>();
knownVariables.addAll(nodeNames);
for (Parameter parameter : definition.getParameters()) {
String name = parameter.getName();
if (name != null) {
knownVariables.add(name);
}
}
for (Conjunction body : definition.getBodies()) {
installDeriveConjunctionState(body, knownVariables);
}
}
protected void installDeriveConjunctionState(Conjunction conjunction, Set<String> knownVariables) {
Set<String> newVariables = new HashSet<>();
for (Literal literal : conjunction.getLiterals()) {
if (literal instanceof Atom atom) {
createSigletonVariablesAndCollectVariables(atom, knownVariables, newVariables);
}
}
createVariables(conjunction, newVariables);
newVariables.addAll(knownVariables);
for (Literal literal : conjunction.getLiterals()) {
if (literal instanceof NegativeLiteral negativeLiteral) {
installDeriveNegativeLiteralState(negativeLiteral, newVariables);
}
}
}
protected void installDeriveNegativeLiteralState(NegativeLiteral negativeLiteral, Set<String> knownVariables) {
Set<String> newVariables = new HashSet<>();
createSigletonVariablesAndCollectVariables(negativeLiteral.getAtom(), knownVariables, newVariables);
createVariables(negativeLiteral, newVariables);
}
protected void createSigletonVariablesAndCollectVariables(Atom atom, Set<String> knownVariables,
Set<String> newVariables) {
for (Argument argument : atom.getArguments()) {
if (argument instanceof VariableOrNodeArgument variableOrNodeArgument) {
IScope scope = scopeProvider.getScope(variableOrNodeArgument,
ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__VARIABLE_OR_NODE);
List<INode> nodes = NodeModelUtils.findNodesForFeature(variableOrNodeArgument,
ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__VARIABLE_OR_NODE);
for (INode node : nodes) {
var variableName = linkingHelper.getCrossRefNodeAsString(node, true);
var created = tryCreateVariableForArgument(variableOrNodeArgument, variableName, scope,
knownVariables, newVariables);
if (created) {
break;
}
}
}
}
}
protected boolean tryCreateVariableForArgument(VariableOrNodeArgument variableOrNodeArgument, String variableName,
IScope scope, Set<String> knownVariables, Set<String> newVariables) {
if (!NamingUtil.isValidId(variableName)) {
return false;
}
var qualifiedName = qualifiedNameConverter.toQualifiedName(variableName);
if (scope.getSingleElement(qualifiedName) != null) {
return false;
}
if (NamingUtil.isSingletonVariableName(variableName)) {
createSingletonVariable(variableOrNodeArgument, variableName);
return true;
}
if (!knownVariables.contains(variableName)) {
newVariables.add(variableName);
return true;
}
return false;
}
protected void createVariables(ExistentialQuantifier quantifier, Set<String> newVariables) {
for (String variableName : newVariables) {
createVariable(quantifier, variableName);
}
}
protected void createVariable(ExistentialQuantifier quantifier, String variableName) {
if (NamingUtil.isValidId(variableName)) {
ImplicitVariable variable = createNamedVariable(variableName);
quantifier.getImplicitVariables().add(variable);
}
}
protected void createSingletonVariable(VariableOrNodeArgument argument, String variableName) {
if (NamingUtil.isValidId(variableName)) {
ImplicitVariable variable = createNamedVariable(variableName);
argument.setSingletonVariable(variable);
}
}
protected ImplicitVariable createNamedVariable(String variableName) {
var variable = ProblemFactory.eINSTANCE.createImplicitVariable();
variable.setName(variableName);
return variable;
}
public void discardDerivedVariables(Problem problem) {
for (Statement statement : problem.getStatements()) {
if (statement instanceof PredicateDefinition predicateDefinition) {
discardPredicateDefinitionState(predicateDefinition);
}
}
}
protected void discardPredicateDefinitionState(PredicateDefinition definition) {
for (Conjunction body : definition.getBodies()) {
body.getImplicitVariables().clear();
for (Literal literal : body.getLiterals()) {
if (literal instanceof Atom atom) {
discardDerivedAtomState(atom);
}
if (literal instanceof NegativeLiteral negativeLiteral) {
negativeLiteral.getImplicitVariables().clear();
discardDerivedAtomState(negativeLiteral.getAtom());
}
}
}
}
protected void discardDerivedAtomState(Atom atom) {
if (atom == null) {
return;
}
for (Argument argument : atom.getArguments()) {
if (argument instanceof VariableOrNodeArgument variableOrNodeArgument) {
variableOrNodeArgument.setSingletonVariable(null);
}
}
}
}
|