diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/CountCheck.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/CountCheck.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/CountCheck.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/CountCheck.java new file mode 100644 index 00000000..d74c1c6f --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/CountCheck.java | |||
@@ -0,0 +1,92 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2013, 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 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.localsearch.operations.check; | ||
10 | |||
11 | import java.util.Collections; | ||
12 | import java.util.List; | ||
13 | import java.util.function.Function; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
16 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
17 | import tools.refinery.viatra.runtime.localsearch.operations.CheckOperationExecutor; | ||
18 | import tools.refinery.viatra.runtime.localsearch.operations.IPatternMatcherOperation; | ||
19 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
20 | import tools.refinery.viatra.runtime.localsearch.operations.util.CallInformation; | ||
21 | import tools.refinery.viatra.runtime.matchers.backend.IQueryResultProvider; | ||
22 | import tools.refinery.viatra.runtime.matchers.tuple.VolatileModifiableMaskedTuple; | ||
23 | |||
24 | /** | ||
25 | * Calculates the count of matches for a called matcher | ||
26 | * | ||
27 | * @author Zoltan Ujhelyi | ||
28 | * @noextend This class is not intended to be subclassed by clients. | ||
29 | */ | ||
30 | public class CountCheck implements ISearchOperation, IPatternMatcherOperation { | ||
31 | |||
32 | private class Executor extends CheckOperationExecutor { | ||
33 | |||
34 | private final VolatileModifiableMaskedTuple maskedTuple; | ||
35 | private IQueryResultProvider matcher; | ||
36 | |||
37 | public Executor() { | ||
38 | maskedTuple = new VolatileModifiableMaskedTuple(information.getThinFrameMask()); | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public void onInitialize(MatchingFrame frame, ISearchContext context) { | ||
43 | super.onInitialize(frame, context); | ||
44 | maskedTuple.updateTuple(frame); | ||
45 | matcher = context.getMatcher(information.getCallWithAdornment()); | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | protected boolean check(MatchingFrame frame, ISearchContext context) { | ||
50 | int count = matcher.countMatches(information.getParameterMask(), maskedTuple); | ||
51 | return ((Integer)frame.getValue(position)) == count; | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public ISearchOperation getOperation() { | ||
56 | return CountCheck.this; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | private final int position; | ||
61 | private final CallInformation information; | ||
62 | |||
63 | /** | ||
64 | * @since 1.7 | ||
65 | */ | ||
66 | public CountCheck(CallInformation information, int position) { | ||
67 | super(); | ||
68 | this.information = information; | ||
69 | this.position = position; | ||
70 | } | ||
71 | |||
72 | |||
73 | @Override | ||
74 | public ISearchOperationExecutor createExecutor() { | ||
75 | return new Executor(); | ||
76 | } | ||
77 | |||
78 | @Override | ||
79 | public List<Integer> getVariablePositions() { | ||
80 | return Collections.singletonList(position); | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public String toString(Function<Integer, String> variableMapping) { | ||
85 | return "check "+variableMapping.apply(position)+" = count find "+ information.toString(variableMapping); | ||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public CallInformation getCallInformation() { | ||
90 | return information; | ||
91 | } | ||
92 | } | ||