diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/generic/GenericTypeCheck.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/generic/GenericTypeCheck.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/generic/GenericTypeCheck.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/generic/GenericTypeCheck.java new file mode 100644 index 00000000..2b189c57 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/generic/GenericTypeCheck.java | |||
@@ -0,0 +1,96 @@ | |||
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 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.localsearch.operations.generic; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Collections; | ||
13 | import java.util.List; | ||
14 | import java.util.function.Function; | ||
15 | import java.util.stream.Collectors; | ||
16 | |||
17 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
18 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
19 | import tools.refinery.viatra.runtime.localsearch.operations.CheckOperationExecutor; | ||
20 | import tools.refinery.viatra.runtime.localsearch.operations.IIteratingSearchOperation; | ||
21 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
22 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
23 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
24 | import tools.refinery.viatra.runtime.matchers.tuple.VolatileMaskedTuple; | ||
25 | import tools.refinery.viatra.runtime.matchers.util.Preconditions; | ||
26 | |||
27 | /** | ||
28 | * @author Zoltan Ujhelyi | ||
29 | * @since 1.7 | ||
30 | * @noextend This class is not intended to be subclassed by clients. | ||
31 | */ | ||
32 | public class GenericTypeCheck implements ISearchOperation, IIteratingSearchOperation { | ||
33 | |||
34 | private class Executor extends CheckOperationExecutor { | ||
35 | private VolatileMaskedTuple maskedTuple; | ||
36 | |||
37 | private Executor() { | ||
38 | this.maskedTuple = new VolatileMaskedTuple(callMask); | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | protected boolean check(MatchingFrame frame, ISearchContext context) { | ||
43 | maskedTuple.updateTuple(frame); | ||
44 | return context.getRuntimeContext().containsTuple(type, maskedTuple); | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public ISearchOperation getOperation() { | ||
49 | return GenericTypeCheck.this; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | private final IInputKey type; | ||
54 | private final List<Integer> positionList; | ||
55 | private final TupleMask callMask; | ||
56 | |||
57 | public GenericTypeCheck(IInputKey type, int[] positions, TupleMask callMask) { | ||
58 | this.callMask = callMask; | ||
59 | Preconditions.checkArgument(positions.length == type.getArity(), | ||
60 | "The type %s requires %d parameters, but %d positions are provided", type.getPrettyPrintableName(), | ||
61 | type.getArity(), positions.length); | ||
62 | List<Integer> modifiablePositionList = new ArrayList<>(); | ||
63 | for (int position : positions) { | ||
64 | modifiablePositionList.add(position); | ||
65 | } | ||
66 | this.positionList = Collections.unmodifiableList(modifiablePositionList); | ||
67 | this.type = type; | ||
68 | } | ||
69 | |||
70 | @Override | ||
71 | public List<Integer> getVariablePositions() { | ||
72 | return positionList; | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public ISearchOperationExecutor createExecutor() { | ||
77 | return new Executor(); | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public String toString() { | ||
82 | return toString(Object::toString); | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public String toString(Function<Integer, String> variableMapping) { | ||
87 | return "check " + type.getPrettyPrintableName() + "(" | ||
88 | + positionList.stream().map(input -> String.format("+%s", variableMapping.apply(input))).collect(Collectors.joining(", ")) | ||
89 | + ")"; | ||
90 | } | ||
91 | |||
92 | @Override | ||
93 | public IInputKey getIteratedInputKey() { | ||
94 | return type; | ||
95 | } | ||
96 | } | ||