diff options
Diffstat (limited to 'subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/localsearch/RelationalOperationCompiler.java')
-rw-r--r-- | subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/localsearch/RelationalOperationCompiler.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/localsearch/RelationalOperationCompiler.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/localsearch/RelationalOperationCompiler.java new file mode 100644 index 00000000..b6832b39 --- /dev/null +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/localsearch/RelationalOperationCompiler.java | |||
@@ -0,0 +1,65 @@ | |||
1 | package tools.refinery.store.query.viatra.internal.localsearch; | ||
2 | |||
3 | import org.eclipse.viatra.query.runtime.localsearch.operations.generic.GenericTypeExtendSingleValue; | ||
4 | import org.eclipse.viatra.query.runtime.localsearch.operations.util.CallInformation; | ||
5 | import org.eclipse.viatra.query.runtime.localsearch.planner.compiler.GenericOperationCompiler; | ||
6 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey; | ||
7 | import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext; | ||
8 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable; | ||
9 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall; | ||
10 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint; | ||
11 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuple; | ||
12 | import org.eclipse.viatra.query.runtime.matchers.tuple.TupleMask; | ||
13 | |||
14 | import java.util.*; | ||
15 | |||
16 | public class RelationalOperationCompiler extends GenericOperationCompiler { | ||
17 | public RelationalOperationCompiler(IQueryRuntimeContext runtimeContext) { | ||
18 | super(runtimeContext); | ||
19 | } | ||
20 | |||
21 | @Override | ||
22 | protected void createExtend(TypeConstraint typeConstraint, Map<PVariable, Integer> variableMapping) { | ||
23 | IInputKey inputKey = typeConstraint.getSupplierKey(); | ||
24 | Tuple tuple = typeConstraint.getVariablesTuple(); | ||
25 | |||
26 | int[] positions = new int[tuple.getSize()]; | ||
27 | List<Integer> boundVariableIndices = new ArrayList<>(); | ||
28 | List<Integer> boundVariables = new ArrayList<>(); | ||
29 | Set<Integer> unboundVariables = new HashSet<>(); | ||
30 | for (int i = 0; i < tuple.getSize(); i++) { | ||
31 | PVariable variable = (PVariable) tuple.get(i); | ||
32 | Integer position = variableMapping.get(variable); | ||
33 | positions[i] = position; | ||
34 | if (variableBindings.get(typeConstraint).contains(position)) { | ||
35 | boundVariableIndices.add(i); | ||
36 | boundVariables.add(position); | ||
37 | } else { | ||
38 | unboundVariables.add(position); | ||
39 | } | ||
40 | } | ||
41 | TupleMask indexerMask = TupleMask.fromSelectedIndices(inputKey.getArity(), boundVariableIndices); | ||
42 | TupleMask callMask = TupleMask.fromSelectedIndices(variableMapping.size(), boundVariables); | ||
43 | // If multiple tuple elements from the indexer should be bound to the same variable, we must use a | ||
44 | // {@link GenericTypeExtend} check whether the tuple elements have the same value. | ||
45 | if (unboundVariables.size() == 1 && indexerMask.getSize() + 1 == indexerMask.getSourceWidth()) { | ||
46 | operations.add(new GenericTypeExtendSingleValue(inputKey, positions, callMask, indexerMask, | ||
47 | unboundVariables.iterator().next())); | ||
48 | } else { | ||
49 | // Use a fixed version of | ||
50 | // {@code org.eclipse.viatra.query.runtime.localsearch.operations.generic.GenericTypeExtend} that handles | ||
51 | // failed unification of variables correctly. | ||
52 | operations.add(new GenericTypeExtend(inputKey, positions, callMask, indexerMask, unboundVariables)); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | protected void createExtend(PositivePatternCall pCall, Map<PVariable, Integer> variableMapping) { | ||
58 | CallInformation information = CallInformation.create(pCall, variableMapping, variableBindings.get(pCall)); | ||
59 | // Use a fixed version of | ||
60 | // {@code org.eclipse.viatra.query.runtime.localsearch.operations.extend.ExtendPositivePatternCall} that handles | ||
61 | // failed unification of variables correctly. | ||
62 | operations.add(new ExtendPositivePatternCall(information)); | ||
63 | dependencies.add(information.getCallWithAdornment()); | ||
64 | } | ||
65 | } | ||