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