diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java new file mode 100644 index 00000000..d86982e9 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java | |||
@@ -0,0 +1,101 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | package tools.refinery.viatra.runtime.localsearch.planner.compiler; | ||
11 | |||
12 | import tools.refinery.viatra.runtime.localsearch.operations.generic.GenericTypeCheck; | ||
13 | import tools.refinery.viatra.runtime.localsearch.operations.generic.GenericTypeExtend; | ||
14 | import tools.refinery.viatra.runtime.localsearch.operations.generic.GenericTypeExtendSingleValue; | ||
15 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
16 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext; | ||
17 | import tools.refinery.viatra.runtime.matchers.psystem.PVariable; | ||
18 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint; | ||
19 | import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.TypeConstraint; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
21 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
22 | |||
23 | import java.util.*; | ||
24 | |||
25 | /** | ||
26 | * @author Zoltan Ujhelyi | ||
27 | * @since 1.7 | ||
28 | * | ||
29 | */ | ||
30 | public class GenericOperationCompiler extends AbstractOperationCompiler { | ||
31 | |||
32 | public GenericOperationCompiler(IQueryRuntimeContext runtimeContext) { | ||
33 | super(runtimeContext); | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | protected void createCheck(TypeFilterConstraint typeConstraint, Map<PVariable, Integer> variableMapping) { | ||
38 | IInputKey inputKey = typeConstraint.getInputKey(); | ||
39 | Tuple tuple = typeConstraint.getVariablesTuple(); | ||
40 | int[] positions = new int[tuple.getSize()]; | ||
41 | for (int i = 0; i < tuple.getSize(); i++) { | ||
42 | PVariable variable = (PVariable) tuple.get(i); | ||
43 | positions[i] = variableMapping.get(variable); | ||
44 | } | ||
45 | operations.add(new GenericTypeCheck(inputKey, positions, TupleMask.fromSelectedIndices(variableMapping.size(), positions))); | ||
46 | |||
47 | } | ||
48 | |||
49 | @Override | ||
50 | protected void createCheck(TypeConstraint typeConstraint, Map<PVariable, Integer> variableMapping) { | ||
51 | IInputKey inputKey = typeConstraint.getSupplierKey(); | ||
52 | Tuple tuple = typeConstraint.getVariablesTuple(); | ||
53 | int[] positions = new int[tuple.getSize()]; | ||
54 | for (int i = 0; i < tuple.getSize(); i++) { | ||
55 | PVariable variable = (PVariable) tuple.get(i); | ||
56 | positions[i] = variableMapping.get(variable); | ||
57 | } | ||
58 | operations.add(new GenericTypeCheck(inputKey, positions, TupleMask.fromSelectedIndices(variableMapping.size(), positions))); | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | protected void createUnaryTypeCheck(IInputKey inputKey, int position) { | ||
63 | int[] positions = new int[] {position}; | ||
64 | operations.add(new GenericTypeCheck(inputKey, positions, TupleMask.fromSelectedIndices(1, positions))); | ||
65 | } | ||
66 | |||
67 | @Override | ||
68 | protected void createExtend(TypeConstraint typeConstraint, Map<PVariable, Integer> variableMapping) { | ||
69 | IInputKey inputKey = typeConstraint.getSupplierKey(); | ||
70 | Tuple tuple = typeConstraint.getVariablesTuple(); | ||
71 | |||
72 | int[] positions = new int[tuple.getSize()]; | ||
73 | List<Integer> boundVariableIndices = new ArrayList<>(); | ||
74 | List<Integer> boundVariables = new ArrayList<>(); | ||
75 | Set<Integer> unboundVariables = new HashSet<>(); | ||
76 | for (int i = 0; i < tuple.getSize(); i++) { | ||
77 | PVariable variable = (PVariable) tuple.get(i); | ||
78 | Integer position = variableMapping.get(variable); | ||
79 | positions[i] = position; | ||
80 | if (variableBindings.get(typeConstraint).contains(position)) { | ||
81 | boundVariableIndices.add(i); | ||
82 | boundVariables.add(position); | ||
83 | } else { | ||
84 | unboundVariables.add(position); | ||
85 | } | ||
86 | } | ||
87 | TupleMask indexerMask = TupleMask.fromSelectedIndices(inputKey.getArity(), boundVariableIndices); | ||
88 | TupleMask callMask = TupleMask.fromSelectedIndices(variableMapping.size(), boundVariables); | ||
89 | // If multiple tuple elements from the indexer should be bound to the same variable, we must use a | ||
90 | // {@link GenericTypeExtend} check whether the tuple elements have the same value. | ||
91 | if (unboundVariables.size() == 1 && indexerMask.getSize() + 1 == indexerMask.getSourceWidth()) { | ||
92 | operations.add(new GenericTypeExtendSingleValue(inputKey, positions, callMask, indexerMask, unboundVariables.iterator().next())); | ||
93 | } else { | ||
94 | operations.add(new GenericTypeExtend(inputKey, positions, callMask, indexerMask, unboundVariables)); | ||
95 | } | ||
96 | |||
97 | } | ||
98 | |||
99 | |||
100 | |||
101 | } | ||