diff options
author | 2021-10-05 00:36:47 +0200 | |
---|---|---|
committer | 2021-10-05 00:36:47 +0200 | |
commit | c3e27396c62f191b4343df151e5a86bfa63a32f3 (patch) | |
tree | 4f698c9ba0320a5c740c53877c3f75c00240dca4 /store/src/main/java/org/eclipse/viatra/solver/data/query/internal/PredicateTranslator.java | |
parent | fix(web): improve accessibility (diff) | |
download | refinery-c3e27396c62f191b4343df151e5a86bfa63a32f3.tar.gz refinery-c3e27396c62f191b4343df151e5a86bfa63a32f3.tar.zst refinery-c3e27396c62f191b4343df151e5a86bfa63a32f3.zip |
chore: change package name
Diffstat (limited to 'store/src/main/java/org/eclipse/viatra/solver/data/query/internal/PredicateTranslator.java')
-rw-r--r-- | store/src/main/java/org/eclipse/viatra/solver/data/query/internal/PredicateTranslator.java | 209 |
1 files changed, 0 insertions, 209 deletions
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/PredicateTranslator.java b/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/PredicateTranslator.java deleted file mode 100644 index 54cb4bab..00000000 --- a/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/PredicateTranslator.java +++ /dev/null | |||
@@ -1,209 +0,0 @@ | |||
1 | package org.eclipse.viatra.solver.data.query.internal; | ||
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.ViatraQueryEngine; | ||
14 | import org.eclipse.viatra.query.runtime.api.scope.QueryScope; | ||
15 | import org.eclipse.viatra.query.runtime.matchers.backend.QueryEvaluationHint; | ||
16 | import org.eclipse.viatra.query.runtime.matchers.psystem.PBody; | ||
17 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable; | ||
18 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality; | ||
19 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter; | ||
20 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality; | ||
21 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall; | ||
22 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryReflexiveTransitiveClosure; | ||
23 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure; | ||
24 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall; | ||
25 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint; | ||
26 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.BasePQuery; | ||
27 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter; | ||
28 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery; | ||
29 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PVisibility; | ||
30 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | ||
31 | import org.eclipse.viatra.solver.data.query.RelationalScope; | ||
32 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
33 | |||
34 | public class PredicateTranslator extends BasePQuery { | ||
35 | |||
36 | private final Map<String, PParameter> parameters = new HashMap<String, PParameter>(); | ||
37 | private String fullyQualifiedName; | ||
38 | private LinkedList<PBody> bodies = new LinkedList<PBody>(); | ||
39 | private List<ExportedParameter> symbolicParameters; | ||
40 | |||
41 | public PredicateTranslator(String fullyQualifiedName) { | ||
42 | super(PVisibility.PUBLIC); | ||
43 | this.fullyQualifiedName = fullyQualifiedName; | ||
44 | PBody body = new PBody(this); | ||
45 | bodies.add(body); | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public String getFullyQualifiedName() { | ||
50 | return fullyQualifiedName; | ||
51 | } | ||
52 | |||
53 | public PredicateTranslator addParameter(String name, RelationView<?> type) { | ||
54 | PParameter parameter = new PParameter(name); | ||
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> PredicateTranslator 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); | ||
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 PredicateTranslator 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 PredicateTranslator 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 PredicateTranslator 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 PredicateTranslator 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 PredicateTranslator 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 PredicateTranslator 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 PredicateTranslator 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 | public GenericQuerySpecification<GenericPatternMatcher> build() { | ||
190 | return new GenericQuerySpecification<GenericPatternMatcher>(this) { | ||
191 | |||
192 | @Override | ||
193 | public Class<? extends QueryScope> getPreferredScopeClass() { | ||
194 | return RelationalScope.class; | ||
195 | } | ||
196 | |||
197 | @Override | ||
198 | protected GenericPatternMatcher instantiate(ViatraQueryEngine engine) { | ||
199 | return defaultInstantiate(engine); | ||
200 | } | ||
201 | |||
202 | @Override | ||
203 | public GenericPatternMatcher instantiate() { | ||
204 | return new GenericPatternMatcher(this); | ||
205 | } | ||
206 | |||
207 | }; | ||
208 | } | ||
209 | } \ No newline at end of file | ||