diff options
Diffstat (limited to 'subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/RelationViewFilter.java')
-rw-r--r-- | subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/RelationViewFilter.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/RelationViewFilter.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/RelationViewFilter.java new file mode 100644 index 00000000..221f1b4a --- /dev/null +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/RelationViewFilter.java | |||
@@ -0,0 +1,66 @@ | |||
1 | package tools.refinery.store.query.viatra.internal.update; | ||
2 | |||
3 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey; | ||
4 | import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContextListener; | ||
5 | import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple; | ||
6 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuple; | ||
7 | |||
8 | import java.util.Arrays; | ||
9 | import java.util.Objects; | ||
10 | |||
11 | public final class RelationViewFilter { | ||
12 | private final IInputKey inputKey; | ||
13 | private final Object[] seed; | ||
14 | private final IQueryRuntimeContextListener listener; | ||
15 | |||
16 | public RelationViewFilter(IInputKey inputKey, ITuple seed, IQueryRuntimeContextListener listener) { | ||
17 | this.inputKey = inputKey; | ||
18 | this.seed = seedToArray(seed); | ||
19 | this.listener = listener; | ||
20 | } | ||
21 | |||
22 | public void update(Tuple updateTuple, boolean isInsertion) { | ||
23 | if (isMatching(updateTuple)) { | ||
24 | listener.update(inputKey, updateTuple, isInsertion); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | private boolean isMatching(ITuple tuple) { | ||
29 | if (seed == null) { | ||
30 | return true; | ||
31 | } | ||
32 | int size = seed.length; | ||
33 | for (int i = 0; i < size; i++) { | ||
34 | var filterElement = seed[i]; | ||
35 | if (filterElement != null && !filterElement.equals(tuple.get(i))) { | ||
36 | return false; | ||
37 | } | ||
38 | } | ||
39 | return true; | ||
40 | } | ||
41 | |||
42 | // Use <code>null</code> instead of an empty array to speed up comparisons. | ||
43 | @SuppressWarnings("squid:S1168") | ||
44 | private static Object[] seedToArray(ITuple seed) { | ||
45 | for (var element : seed.getElements()) { | ||
46 | if (element != null) { | ||
47 | return seed.getElements(); | ||
48 | } | ||
49 | } | ||
50 | return null; | ||
51 | } | ||
52 | |||
53 | @Override | ||
54 | public boolean equals(Object obj) { | ||
55 | if (obj == this) return true; | ||
56 | if (obj == null || obj.getClass() != this.getClass()) return false; | ||
57 | var that = (RelationViewFilter) obj; | ||
58 | return Objects.equals(this.inputKey, that.inputKey) && Arrays.equals(this.seed, that.seed) && | ||
59 | Objects.equals(this.listener, that.listener); | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public int hashCode() { | ||
64 | return Objects.hash(inputKey, Arrays.hashCode(seed), listener); | ||
65 | } | ||
66 | } | ||