diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendPositivePatternCall.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendPositivePatternCall.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendPositivePatternCall.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendPositivePatternCall.java new file mode 100644 index 00000000..690a3241 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendPositivePatternCall.java | |||
@@ -0,0 +1,118 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Grill Balázs, IncQueryLabs | ||
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.extend; | ||
10 | |||
11 | import java.util.Iterator; | ||
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.ExtendOperationExecutor; | ||
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.Tuple; | ||
23 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
24 | import tools.refinery.viatra.runtime.matchers.tuple.VolatileModifiableMaskedTuple; | ||
25 | |||
26 | /** | ||
27 | * @author Grill Balázs | ||
28 | * @since 1.4 | ||
29 | * | ||
30 | */ | ||
31 | public class ExtendPositivePatternCall implements ISearchOperation, IPatternMatcherOperation { | ||
32 | |||
33 | private class Executor extends ExtendOperationExecutor<Tuple> { | ||
34 | private final VolatileModifiableMaskedTuple maskedTuple; | ||
35 | |||
36 | public Executor() { | ||
37 | maskedTuple = new VolatileModifiableMaskedTuple(information.getThinFrameMask()); | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | protected Iterator<? extends Tuple> getIterator(MatchingFrame frame, ISearchContext context) { | ||
42 | maskedTuple.updateTuple(frame); | ||
43 | IQueryResultProvider matcher = context.getMatcher(information.getCallWithAdornment()); | ||
44 | return matcher.getAllMatches(information.getParameterMask(), maskedTuple).iterator(); | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * @since 2.0 | ||
49 | */ | ||
50 | @Override | ||
51 | protected boolean fillInValue(Tuple result, MatchingFrame frame, ISearchContext context) { | ||
52 | TupleMask mask = information.getFullFrameMask(); | ||
53 | // The first loop clears out the elements from a possible previous iteration | ||
54 | for(int i : information.getFreeParameterIndices()) { | ||
55 | mask.set(frame, i, null); | ||
56 | } | ||
57 | for(int i : information.getFreeParameterIndices()) { | ||
58 | Object oldValue = mask.getValue(frame, i); | ||
59 | Object valueToFill = result.get(i); | ||
60 | if (oldValue != null && !oldValue.equals(valueToFill)){ | ||
61 | // If the inverse map contains more than one values for the same key, it means that these arguments are unified by the caller. | ||
62 | // In this case if the callee assigns different values the frame shall be dropped | ||
63 | return false; | ||
64 | } | ||
65 | mask.set(frame, i, valueToFill); | ||
66 | } | ||
67 | return true; | ||
68 | } | ||
69 | |||
70 | @Override | ||
71 | protected void cleanup(MatchingFrame frame, ISearchContext context) { | ||
72 | TupleMask mask = information.getFullFrameMask(); | ||
73 | for(int i : information.getFreeParameterIndices()){ | ||
74 | mask.set(frame, i, null); | ||
75 | } | ||
76 | |||
77 | } | ||
78 | |||
79 | @Override | ||
80 | public ISearchOperation getOperation() { | ||
81 | return ExtendPositivePatternCall.this; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | private final CallInformation information; | ||
86 | |||
87 | /** | ||
88 | * @since 1.7 | ||
89 | */ | ||
90 | public ExtendPositivePatternCall(CallInformation information) { | ||
91 | this.information = information; | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public ISearchOperationExecutor createExecutor() { | ||
96 | return new Executor(); | ||
97 | } | ||
98 | |||
99 | @Override | ||
100 | public List<Integer> getVariablePositions() { | ||
101 | return information.getVariablePositions(); | ||
102 | } | ||
103 | |||
104 | @Override | ||
105 | public String toString() { | ||
106 | return toString(Object::toString); | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public String toString(Function<Integer, String> variableMapping) { | ||
111 | return "extend find " + information.toString(variableMapping); | ||
112 | } | ||
113 | |||
114 | @Override | ||
115 | public CallInformation getCallInformation() { | ||
116 | return information; | ||
117 | } | ||
118 | } | ||