diff options
Diffstat (limited to 'model-data')
-rw-r--r-- | model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationQuery.java | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationQuery.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationQuery.java new file mode 100644 index 00000000..f84fdeed --- /dev/null +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationQuery.java | |||
@@ -0,0 +1,189 @@ | |||
1 | package org.eclipse.viatra.solver.data.query; | ||
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.HashMap; | ||
5 | import java.util.LinkedHashSet; | ||
6 | import java.util.LinkedList; | ||
7 | import java.util.List; | ||
8 | import java.util.Map; | ||
9 | import java.util.Set; | ||
10 | |||
11 | import org.eclipse.viatra.query.runtime.matchers.backend.QueryEvaluationHint; | ||
12 | import org.eclipse.viatra.query.runtime.matchers.psystem.PBody; | ||
13 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable; | ||
14 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality; | ||
15 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter; | ||
16 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality; | ||
17 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall; | ||
18 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryReflexiveTransitiveClosure; | ||
19 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure; | ||
20 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall; | ||
21 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint; | ||
22 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.BasePQuery; | ||
23 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter; | ||
24 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery; | ||
25 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PVisibility; | ||
26 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | ||
27 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
28 | |||
29 | public class RelationQuery extends BasePQuery { | ||
30 | |||
31 | private final Map<String, PParameter> parameters = new HashMap<String, PParameter>(); | ||
32 | private String fullyQualifiedName; | ||
33 | private LinkedList<PBody> bodies = new LinkedList<PBody>(); | ||
34 | private List<ExportedParameter> symbolicParameters; | ||
35 | |||
36 | public RelationQuery(String fullyQualifiedName) { | ||
37 | super(PVisibility.PUBLIC); | ||
38 | this.fullyQualifiedName = fullyQualifiedName; | ||
39 | PBody body = new PBody(this); | ||
40 | bodies.add(body); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public String getFullyQualifiedName() { | ||
45 | return fullyQualifiedName; | ||
46 | } | ||
47 | |||
48 | public RelationQuery addParameter(String name, String inputKey) { | ||
49 | return addParameter(name, inputKey); | ||
50 | } | ||
51 | |||
52 | // public RelationQuery addParameter(String name, String inputKey, String typeName) { | ||
53 | // PParameter parameter = new PParameter(name, typeName, new StringExactInstancesKey(inputKey), | ||
54 | // PParameterDirection.INOUT); | ||
55 | // parameters.put(name, parameter); | ||
56 | // | ||
57 | // PBody body = bodies.peekLast(); | ||
58 | // List<ExportedParameter> symbolicParameters = new ArrayList<>(); | ||
59 | // parameters.forEach((pName, pParameter) -> { | ||
60 | // PVariable var = body.getOrCreateVariableByName(pName); | ||
61 | // symbolicParameters.add(new ExportedParameter(body, var, pParameter)); | ||
62 | // }); | ||
63 | // body.setSymbolicParameters(symbolicParameters); | ||
64 | // | ||
65 | // return this; | ||
66 | // } | ||
67 | |||
68 | @Override | ||
69 | public List<PParameter> getParameters() { | ||
70 | return new ArrayList<PParameter>(parameters.values()); | ||
71 | } | ||
72 | public <D> RelationQuery addConstraint(RelationView<D> view, String... name) { | ||
73 | if(name.length != view.getArity()) { | ||
74 | throw new IllegalArgumentException("Arity ("+view.getArity()+") does not match parameter numbers ("+name.length+")"); | ||
75 | } | ||
76 | PBody body = bodies.peekLast(); | ||
77 | Object[] variables = new Object[name.length]; | ||
78 | for(int i = 0; i<name.length; i++) { | ||
79 | variables[i] = body.getOrCreateVariableByName(name[i]); | ||
80 | } | ||
81 | new TypeConstraint(body, Tuples.flatTupleOf(variables), view.getKey()); | ||
82 | return this; | ||
83 | } | ||
84 | |||
85 | // // Type constraint | ||
86 | // public RelationQuery addConstraint(String type, String name) { | ||
87 | // PBody body = bodies.peekLast(); | ||
88 | // PVariable var = body.getOrCreateVariableByName(name); | ||
89 | // new TypeConstraint(body, Tuples.flatTupleOf(var), new StringExactInstancesKey(type)); | ||
90 | // return this; | ||
91 | // } | ||
92 | // | ||
93 | // // Relation constraint | ||
94 | // public RelationQuery addConstraint(String type, String sourceName, String targetName) { | ||
95 | // PBody body = bodies.peekLast(); | ||
96 | // PVariable var_source = body.getOrCreateVariableByName(sourceName); | ||
97 | // PVariable var_target = body.getOrCreateVariableByName(targetName); | ||
98 | // new TypeConstraint(body, Tuples.flatTupleOf(var_source, var_target), | ||
99 | // new StringStructuralFeatureInstancesKey(type)); | ||
100 | // return this; | ||
101 | // } | ||
102 | |||
103 | // Create new Body | ||
104 | public RelationQuery or() { | ||
105 | PBody body = new PBody(this); | ||
106 | List<ExportedParameter> symbolicParameters = new ArrayList<>(); | ||
107 | parameters.forEach((name, parameter) -> { | ||
108 | PVariable var = body.getOrCreateVariableByName(name); | ||
109 | symbolicParameters.add(new ExportedParameter(body, var, parameter)); | ||
110 | }); | ||
111 | body.setSymbolicParameters(symbolicParameters); | ||
112 | bodies.add(body); | ||
113 | return this; | ||
114 | } | ||
115 | |||
116 | // Equality constraint | ||
117 | public RelationQuery addEquality(String sourceName, String targetName) { | ||
118 | PBody body = bodies.peekLast(); | ||
119 | PVariable var_source = body.getOrCreateVariableByName(sourceName); | ||
120 | PVariable var_target = body.getOrCreateVariableByName(targetName); | ||
121 | new Equality(body, var_source, var_target); | ||
122 | return this; | ||
123 | } | ||
124 | |||
125 | // Inequality constraint | ||
126 | public RelationQuery addInequality(String sourceName, String targetName) { | ||
127 | PBody body = bodies.peekLast(); | ||
128 | PVariable var_source = body.getOrCreateVariableByName(sourceName); | ||
129 | PVariable var_target = body.getOrCreateVariableByName(targetName); | ||
130 | new Inequality(body, var_source, var_target); | ||
131 | return this; | ||
132 | } | ||
133 | |||
134 | // Positive pattern call | ||
135 | public RelationQuery addPatternCall(PQuery query, String... names) { | ||
136 | PBody body = bodies.peekLast(); | ||
137 | PVariable[] vars = new PVariable[names.length]; | ||
138 | for (int i = 0; i < names.length; i++) { | ||
139 | vars[i] = body.getOrCreateVariableByName(names[i]); | ||
140 | } | ||
141 | new PositivePatternCall(body, Tuples.flatTupleOf(vars), query); | ||
142 | return this; | ||
143 | } | ||
144 | |||
145 | // Negative pattern call | ||
146 | public RelationQuery addNegativePatternCall(PQuery query, String... names) { | ||
147 | PBody body = bodies.peekLast(); | ||
148 | PVariable[] vars = new PVariable[names.length]; | ||
149 | for (int i = 0; i < names.length; i++) { | ||
150 | vars[i] = body.getOrCreateVariableByName(names[i]); | ||
151 | } | ||
152 | new NegativePatternCall(body, Tuples.flatTupleOf(vars), query); | ||
153 | return this; | ||
154 | } | ||
155 | |||
156 | // Binary transitive closure pattern call | ||
157 | public RelationQuery addBinaryTransitiveClosure(PQuery query, String sourceName, String targetName) { | ||
158 | PBody body = bodies.peekLast(); | ||
159 | PVariable var_source = body.getOrCreateVariableByName(sourceName); | ||
160 | PVariable var_target = body.getOrCreateVariableByName(targetName); | ||
161 | new BinaryTransitiveClosure(body, Tuples.flatTupleOf(var_source, var_target), query); | ||
162 | return this; | ||
163 | } | ||
164 | |||
165 | // Binary reflexive transitive closure pattern call | ||
166 | public RelationQuery addBinaryReflexiveTransitiveClosure(PQuery query, String sourceName, String targetName) { | ||
167 | PBody body = bodies.peekLast(); | ||
168 | PVariable var_source = body.getOrCreateVariableByName(sourceName); | ||
169 | PVariable var_target = body.getOrCreateVariableByName(targetName); | ||
170 | new BinaryReflexiveTransitiveClosure(body, Tuples.flatTupleOf(var_source, var_target), query, | ||
171 | query.getParameters().get(0).getDeclaredUnaryType()); | ||
172 | return this; | ||
173 | } | ||
174 | |||
175 | @Override | ||
176 | public Set<PBody> doGetContainedBodies() { | ||
177 | setEvaluationHints(new QueryEvaluationHint(null, QueryEvaluationHint.BackendRequirement.UNSPECIFIED)); | ||
178 | return new LinkedHashSet<PBody>(bodies); | ||
179 | } | ||
180 | |||
181 | public void addSymbolicParameters(ExportedParameter symbolicParameter) { | ||
182 | checkMutability(); | ||
183 | if (symbolicParameters == null) { | ||
184 | symbolicParameters = new ArrayList<>(); | ||
185 | } | ||
186 | symbolicParameters.add(symbolicParameter); | ||
187 | } | ||
188 | |||
189 | } \ No newline at end of file | ||