aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java
diff options
context:
space:
mode:
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.java102
1 files changed, 102 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..606048b7
--- /dev/null
+++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/compiler/GenericOperationCompiler.java
@@ -0,0 +1,102 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2017, Zoltan Ujhelyi, IncQuery Labs Ltd.
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.localsearch.planner.compiler;
10
11import java.util.ArrayList;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Map;
15import java.util.Set;
16
17import tools.refinery.viatra.runtime.localsearch.operations.generic.GenericTypeCheck;
18import tools.refinery.viatra.runtime.localsearch.operations.generic.GenericTypeExtend;
19import tools.refinery.viatra.runtime.localsearch.operations.generic.GenericTypeExtendSingleValue;
20import tools.refinery.viatra.runtime.matchers.context.IInputKey;
21import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext;
22import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
23import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint;
24import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.TypeConstraint;
25import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
26import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
27
28/**
29 * @author Zoltan Ujhelyi
30 * @since 1.7
31 *
32 */
33public class GenericOperationCompiler extends AbstractOperationCompiler {
34
35 public GenericOperationCompiler(IQueryRuntimeContext runtimeContext) {
36 super(runtimeContext);
37 }
38
39 @Override
40 protected void createCheck(TypeFilterConstraint typeConstraint, Map<PVariable, Integer> variableMapping) {
41 IInputKey inputKey = typeConstraint.getInputKey();
42 Tuple tuple = typeConstraint.getVariablesTuple();
43 int[] positions = new int[tuple.getSize()];
44 for (int i = 0; i < tuple.getSize(); i++) {
45 PVariable variable = (PVariable) tuple.get(i);
46 positions[i] = variableMapping.get(variable);
47 }
48 operations.add(new GenericTypeCheck(inputKey, positions, TupleMask.fromSelectedIndices(variableMapping.size(), positions)));
49
50 }
51
52 @Override
53 protected void createCheck(TypeConstraint typeConstraint, Map<PVariable, Integer> variableMapping) {
54 IInputKey inputKey = typeConstraint.getSupplierKey();
55 Tuple tuple = typeConstraint.getVariablesTuple();
56 int[] positions = new int[tuple.getSize()];
57 for (int i = 0; i < tuple.getSize(); i++) {
58 PVariable variable = (PVariable) tuple.get(i);
59 positions[i] = variableMapping.get(variable);
60 }
61 operations.add(new GenericTypeCheck(inputKey, positions, TupleMask.fromSelectedIndices(variableMapping.size(), positions)));
62 }
63
64 @Override
65 protected void createUnaryTypeCheck(IInputKey inputKey, int position) {
66 int[] positions = new int[] {position};
67 operations.add(new GenericTypeCheck(inputKey, positions, TupleMask.fromSelectedIndices(1, positions)));
68 }
69
70 @Override
71 protected void createExtend(TypeConstraint typeConstraint, Map<PVariable, Integer> variableMapping) {
72 IInputKey inputKey = typeConstraint.getSupplierKey();
73 Tuple tuple = typeConstraint.getVariablesTuple();
74
75 int[] positions = new int[tuple.getSize()];
76 List<Integer> boundVariableIndices = new ArrayList<>();
77 List<Integer> boundVariables = new ArrayList<>();
78 Set<Integer> unboundVariables = new HashSet<>();
79 for (int i = 0; i < tuple.getSize(); i++) {
80 PVariable variable = (PVariable) tuple.get(i);
81 Integer position = variableMapping.get(variable);
82 positions[i] = position;
83 if (variableBindings.get(typeConstraint).contains(position)) {
84 boundVariableIndices.add(i);
85 boundVariables.add(position);
86 } else {
87 unboundVariables.add(position);
88 }
89 }
90 TupleMask indexerMask = TupleMask.fromSelectedIndices(inputKey.getArity(), boundVariableIndices);
91 TupleMask callMask = TupleMask.fromSelectedIndices(variableMapping.size(), boundVariables);
92 if (unboundVariables.size() == 1) {
93 operations.add(new GenericTypeExtendSingleValue(inputKey, positions, callMask, indexerMask, unboundVariables.iterator().next()));
94 } else {
95 operations.add(new GenericTypeExtend(inputKey, positions, callMask, indexerMask, unboundVariables));
96 }
97
98 }
99
100
101
102}