aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/PBodyCopier.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/PBodyCopier.java')
-rw-r--r--subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/PBodyCopier.java306
1 files changed, 306 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/PBodyCopier.java b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/PBodyCopier.java
new file mode 100644
index 00000000..e66c4eea
--- /dev/null
+++ b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/PBodyCopier.java
@@ -0,0 +1,306 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2014, Marton Bur, Akos Horvath, Zoltan Ujhelyi, Istvan Rath and Daniel Varro
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v. 2.0 which is available at
5 * http://www.eclipse.org/legal/epl-v20.html.
6 *
7 * SPDX-License-Identifier: EPL-2.0
8 *******************************************************************************/
9package tools.refinery.viatra.runtime.matchers.psystem.rewriters;
10
11import tools.refinery.viatra.runtime.matchers.planning.QueryProcessingException;
12import tools.refinery.viatra.runtime.matchers.psystem.EnumerablePConstraint;
13import tools.refinery.viatra.runtime.matchers.psystem.PBody;
14import tools.refinery.viatra.runtime.matchers.psystem.PConstraint;
15import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
16import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.*;
17import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.*;
18import tools.refinery.viatra.runtime.matchers.psystem.queries.PParameter;
19import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery;
20import tools.refinery.viatra.runtime.matchers.psystem.rewriters.IConstraintFilter.AllowAllFilter;
21import tools.refinery.viatra.runtime.matchers.psystem.rewriters.IVariableRenamer.SameName;
22import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
23import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
24
25import java.util.*;
26import java.util.stream.Collectors;
27
28/**
29 * This class can create a new PBody for a PQuery. The result body contains a copy of given variables and constraints.
30 *
31 * @author Marton Bur
32 *
33 */
34public class PBodyCopier extends AbstractRewriterTraceSource {
35
36 /**
37 * The created body
38 */
39 protected PBody body;
40 /**
41 * Mapping between the original and the copied variables
42 */
43 protected Map<PVariable, PVariable> variableMapping = new HashMap<>();
44
45 public Map<PVariable, PVariable> getVariableMapping() {
46 return variableMapping;
47 }
48
49 /**
50 * @since 1.6
51 */
52 public PBodyCopier(PBody body, IRewriterTraceCollector traceCollector) {
53 this.body = new PBody(body.getPattern());
54 setTraceCollector(traceCollector);
55
56 // do the actual copying
57 mergeBody(body);
58 }
59
60 /**
61 * @since 1.6
62 */
63 public PBodyCopier(PQuery query) {
64 this.body = new PBody(query);
65 }
66
67 public void mergeBody(PBody sourceBody) {
68 mergeBody(sourceBody, new SameName(), new AllowAllFilter());
69 }
70
71 /**
72 * Merge all variables and constraints from a source body to a target body. If multiple bodies are merged into a
73 * single one, use the renamer and filter options to avoid collisions.
74 */
75 public void mergeBody(PBody sourceBody, IVariableRenamer namingTool, IConstraintFilter filter) {
76
77 // Copy variables
78 Set<PVariable> allVariables = sourceBody.getAllVariables();
79 for (PVariable pVariable : allVariables) {
80 if (pVariable.isUnique()) {
81 copyVariable(pVariable, namingTool.createVariableName(pVariable, sourceBody.getPattern()));
82 }
83 }
84
85 // Copy exported parameters
86 this.body.setSymbolicParameters(sourceBody.getSymbolicParameters().stream()
87 .map(this::copyExportedParameterConstraint).collect(Collectors.toList()));
88
89 // Copy constraints which are not filtered
90 Set<PConstraint> constraints = sourceBody.getConstraints();
91 for (PConstraint pConstraint : constraints) {
92 if (!(pConstraint instanceof ExportedParameter) && !filter.filter(pConstraint)) {
93 copyConstraint(pConstraint);
94 }
95 }
96
97 // Add trace between original and copied body
98 addTrace(sourceBody, body);
99 }
100
101 protected void copyVariable(PVariable variable, String newName) {
102 PVariable newPVariable = body.getOrCreateVariableByName(newName);
103 variableMapping.put(variable, newPVariable);
104 }
105
106 /**
107 * Returns the body with the copied variables and constraints. The returned body is still uninitialized.
108 */
109 public PBody getCopiedBody() {
110 return body;
111 }
112
113 protected void copyConstraint(PConstraint constraint) {
114 if (constraint instanceof ExportedParameter) {
115 copyExportedParameterConstraint((ExportedParameter) constraint);
116 } else if (constraint instanceof Equality) {
117 copyEqualityConstraint((Equality) constraint);
118 } else if (constraint instanceof Inequality) {
119 copyInequalityConstraint((Inequality) constraint);
120 } else if (constraint instanceof TypeConstraint) {
121 copyTypeConstraint((TypeConstraint) constraint);
122 } else if (constraint instanceof TypeFilterConstraint) {
123 copyTypeFilterConstraint((TypeFilterConstraint) constraint);
124 } else if (constraint instanceof ConstantValue) {
125 copyConstantValueConstraint((ConstantValue) constraint);
126 } else if (constraint instanceof PositivePatternCall) {
127 copyPositivePatternCallConstraint((PositivePatternCall) constraint);
128 } else if (constraint instanceof NegativePatternCall) {
129 copyNegativePatternCallConstraint((NegativePatternCall) constraint);
130 } else if (constraint instanceof BinaryTransitiveClosure) {
131 copyBinaryTransitiveClosureConstraint((BinaryTransitiveClosure) constraint);
132 } else if (constraint instanceof RepresentativeElectionConstraint) {
133 copyRepresentativeElectionConstraint((RepresentativeElectionConstraint) constraint);
134 } else if (constraint instanceof RelationEvaluation) {
135 copyRelationEvaluationConstraint((RelationEvaluation) constraint);
136 } else if (constraint instanceof BinaryReflexiveTransitiveClosure) {
137 copyBinaryReflexiveTransitiveClosureConstraint((BinaryReflexiveTransitiveClosure) constraint);
138 } else if (constraint instanceof PatternMatchCounter) {
139 copyPatternMatchCounterConstraint((PatternMatchCounter) constraint);
140 } else if (constraint instanceof AggregatorConstraint) {
141 copyAggregatorConstraint((AggregatorConstraint) constraint);
142 } else if (constraint instanceof ExpressionEvaluation) {
143 copyExpressionEvaluationConstraint((ExpressionEvaluation) constraint);
144 } else {
145 throw new QueryProcessingException("Unknown PConstraint {0} encountered while copying PBody",
146 new String[] { constraint.getClass().getName() }, "Unknown PConstraint", body.getPattern());
147 }
148 }
149
150 protected ExportedParameter copyExportedParameterConstraint(ExportedParameter exportedParameter) {
151 PVariable mappedPVariable = variableMapping.get(exportedParameter.getParameterVariable());
152 PParameter parameter = exportedParameter.getPatternParameter();
153 ExportedParameter newExportedParameter;
154 newExportedParameter = new ExportedParameter(body, mappedPVariable, parameter);
155 body.getSymbolicParameters().add(newExportedParameter);
156 addTrace(exportedParameter, newExportedParameter);
157 return newExportedParameter;
158 }
159
160 protected void copyEqualityConstraint(Equality equality) {
161 PVariable who = equality.getWho();
162 PVariable withWhom = equality.getWithWhom();
163 addTrace(equality, new Equality(body, variableMapping.get(who), variableMapping.get(withWhom)));
164 }
165
166 protected void copyInequalityConstraint(Inequality inequality) {
167 PVariable who = inequality.getWho();
168 PVariable withWhom = inequality.getWithWhom();
169 addTrace(inequality, new Inequality(body, variableMapping.get(who), variableMapping.get(withWhom)));
170 }
171
172 protected void copyTypeConstraint(TypeConstraint typeConstraint) {
173 PVariable[] mappedVariables = extractMappedVariables(typeConstraint);
174 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
175 addTrace(typeConstraint, new TypeConstraint(body, variablesTuple, typeConstraint.getSupplierKey()));
176 }
177
178 protected void copyTypeFilterConstraint(TypeFilterConstraint typeConstraint) {
179 PVariable[] mappedVariables = extractMappedVariables(typeConstraint);
180 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
181 addTrace(typeConstraint, new TypeFilterConstraint(body, variablesTuple, typeConstraint.getInputKey()));
182 }
183
184 protected void copyConstantValueConstraint(ConstantValue constantValue) {
185 PVariable pVariable = (PVariable) constantValue.getVariablesTuple().getElements()[0];
186 addTrace(constantValue,
187 new ConstantValue(body, variableMapping.get(pVariable), constantValue.getSupplierKey()));
188 }
189
190 protected void copyPositivePatternCallConstraint(PositivePatternCall positivePatternCall) {
191 PVariable[] mappedVariables = extractMappedVariables(positivePatternCall);
192 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
193 addTrace(positivePatternCall,
194 new PositivePatternCall(body, variablesTuple, positivePatternCall.getReferredQuery()));
195 }
196
197 protected void copyNegativePatternCallConstraint(NegativePatternCall negativePatternCall) {
198 PVariable[] mappedVariables = extractMappedVariables(negativePatternCall);
199 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
200 addTrace(negativePatternCall,
201 new NegativePatternCall(body, variablesTuple, negativePatternCall.getReferredQuery()));
202 }
203
204 protected void copyBinaryTransitiveClosureConstraint(BinaryTransitiveClosure binaryTransitiveClosure) {
205 PVariable[] mappedVariables = extractMappedVariables(binaryTransitiveClosure);
206 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
207 addTrace(binaryTransitiveClosure,
208 new BinaryTransitiveClosure(body, variablesTuple, binaryTransitiveClosure.getReferredQuery()));
209 }
210
211 protected void copyRepresentativeElectionConstraint(RepresentativeElectionConstraint constraint) {
212 var mappedVariables = extractMappedVariables(constraint);
213 var variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
214 addTrace(constraint, new RepresentativeElectionConstraint(body, variablesTuple, constraint.getReferredQuery(),
215 constraint.getConnectivity()));
216 }
217
218 /**
219 * @since 2.8
220 */
221 protected void copyRelationEvaluationConstraint(RelationEvaluation relationEvaluation) {
222 PVariable[] mappedVariables = extractMappedVariables(relationEvaluation);
223 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
224 addTrace(relationEvaluation, new RelationEvaluation(body, variablesTuple, relationEvaluation.getReferredQueries(),
225 relationEvaluation.getEvaluator()));
226 }
227
228 /**
229 * @since 2.0
230 */
231 protected void copyBinaryReflexiveTransitiveClosureConstraint(
232 BinaryReflexiveTransitiveClosure binaryReflexiveTransitiveClosure) {
233 PVariable[] mappedVariables = extractMappedVariables(binaryReflexiveTransitiveClosure);
234 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
235 addTrace(binaryReflexiveTransitiveClosure,
236 new BinaryReflexiveTransitiveClosure(body, variablesTuple,
237 binaryReflexiveTransitiveClosure.getReferredQuery(),
238 binaryReflexiveTransitiveClosure.getUniverseType()));
239 }
240
241 protected void copyPatternMatchCounterConstraint(PatternMatchCounter patternMatchCounter) {
242 PVariable[] mappedVariables = extractMappedVariables(patternMatchCounter);
243 PVariable mappedResultVariable = variableMapping.get(patternMatchCounter.getResultVariable());
244 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
245 addTrace(patternMatchCounter, new PatternMatchCounter(body, variablesTuple,
246 patternMatchCounter.getReferredQuery(), mappedResultVariable));
247 }
248
249 /**
250 * @since 1.4
251 */
252 protected void copyAggregatorConstraint(AggregatorConstraint constraint) {
253 PVariable[] mappedVariables = extractMappedVariables(constraint);
254 PVariable mappedResultVariable = variableMapping.get(constraint.getResultVariable());
255 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
256 addTrace(constraint, new AggregatorConstraint(constraint.getAggregator(), body, variablesTuple,
257 constraint.getReferredQuery(), mappedResultVariable, constraint.getAggregatedColumn()));
258 }
259
260 protected void copyExpressionEvaluationConstraint(ExpressionEvaluation expressionEvaluation) {
261 PVariable mappedOutputVariable = variableMapping.get(expressionEvaluation.getOutputVariable());
262 addTrace(expressionEvaluation, new ExpressionEvaluation(body,
263 new VariableMappingExpressionEvaluatorWrapper(expressionEvaluation.getEvaluator(), variableMapping),
264 mappedOutputVariable, expressionEvaluation.isUnwinding()));
265 }
266
267 /**
268 * For positive pattern calls
269 *
270 * @param positivePatternCall
271 * @return the mapped variables to the pattern's parameters
272 */
273 protected PVariable[] extractMappedVariables(EnumerablePConstraint enumerablePConstraint) {
274 Object[] pVariables = enumerablePConstraint.getVariablesTuple().getElements();
275 return mapVariableList(pVariables);
276 }
277
278 /**
279 * For negative and count pattern calls.
280 *
281 * @param patternMatchCounter
282 * @return the mapped variables to the pattern's parameters
283 */
284 private PVariable[] extractMappedVariables(PatternCallBasedDeferred patternCallBasedDeferred) {
285 Object[] pVariables = patternCallBasedDeferred.getActualParametersTuple().getElements();
286 return mapVariableList(pVariables);
287 }
288
289 /**
290 * For type filters.
291 */
292 private PVariable[] extractMappedVariables(TypeFilterConstraint typeFilterConstraint) {
293 Object[] pVariables = typeFilterConstraint.getVariablesTuple().getElements();
294 return mapVariableList(pVariables);
295 }
296
297 private PVariable[] mapVariableList(Object[] pVariables) {
298 List<PVariable> list = new ArrayList<PVariable>();
299 for (int i = 0; i < pVariables.length; i++) {
300 PVariable mappedVariable = variableMapping.get(pVariables[i]);
301 list.add(mappedVariable);
302 }
303 return list.toArray(new PVariable[0]);
304 }
305
306}