diff options
Diffstat (limited to 'subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/RawPatternMatcher.java')
-rw-r--r-- | subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/RawPatternMatcher.java | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/RawPatternMatcher.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/RawPatternMatcher.java deleted file mode 100644 index 5924ff15..00000000 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/RawPatternMatcher.java +++ /dev/null | |||
@@ -1,93 +0,0 @@ | |||
1 | package tools.refinery.store.query.viatra.internal.pquery; | ||
2 | |||
3 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatcher; | ||
4 | import org.eclipse.viatra.query.runtime.api.GenericQuerySpecification; | ||
5 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | ||
6 | import org.eclipse.viatra.query.runtime.matchers.backend.IMatcherCapability; | ||
7 | import org.eclipse.viatra.query.runtime.matchers.backend.IQueryResultProvider; | ||
8 | import org.eclipse.viatra.query.runtime.matchers.tuple.TupleMask; | ||
9 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | ||
10 | import org.eclipse.viatra.query.runtime.rete.index.Indexer; | ||
11 | import org.eclipse.viatra.query.runtime.rete.matcher.RetePatternMatcher; | ||
12 | import tools.refinery.store.map.Cursor; | ||
13 | import tools.refinery.store.map.Cursors; | ||
14 | import tools.refinery.store.query.ResultSet; | ||
15 | import tools.refinery.store.query.viatra.ViatraTupleLike; | ||
16 | import tools.refinery.store.tuple.Tuple; | ||
17 | import tools.refinery.store.tuple.TupleLike; | ||
18 | |||
19 | /** | ||
20 | * Directly access the tuples inside a VIATRA pattern matcher.<p> | ||
21 | * This class neglects calling | ||
22 | * {@link org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext#wrapTuple(org.eclipse.viatra.query.runtime.matchers.tuple.Tuple)} | ||
23 | * and | ||
24 | * {@link org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext#unwrapTuple(org.eclipse.viatra.query.runtime.matchers.tuple.Tuple)}, | ||
25 | * because {@link tools.refinery.store.query.viatra.internal.context.RelationalRuntimeContext} provides a trivial | ||
26 | * implementation for these methods. | ||
27 | * Using this class with any other runtime context may lead to undefined behavior. | ||
28 | */ | ||
29 | public class RawPatternMatcher extends GenericPatternMatcher implements ResultSet { | ||
30 | private final Object[] empty; | ||
31 | private final TupleMask identityMask; | ||
32 | private Indexer emptyMaskIndexer; | ||
33 | |||
34 | public RawPatternMatcher(GenericQuerySpecification<? extends GenericPatternMatcher> specification) { | ||
35 | super(specification); | ||
36 | var arity = specification.getParameterNames().size(); | ||
37 | empty = new Object[arity]; | ||
38 | identityMask = TupleMask.identity(arity); | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | protected void setBackend(ViatraQueryEngine engine, IQueryResultProvider resultProvider, | ||
43 | IMatcherCapability capabilities) { | ||
44 | super.setBackend(engine, resultProvider, capabilities); | ||
45 | if (resultProvider instanceof RetePatternMatcher reteBackend) { | ||
46 | emptyMaskIndexer = IndexerUtils.getIndexer(reteBackend, TupleMask.empty(identityMask.sourceWidth)); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | @Override | ||
51 | public boolean hasResult(TupleLike parameters) { | ||
52 | org.eclipse.viatra.query.runtime.matchers.tuple.Tuple tuple; | ||
53 | if (parameters instanceof ViatraTupleLike viatraTupleLike) { | ||
54 | tuple = viatraTupleLike.wrappedTuple().toImmutable(); | ||
55 | } else { | ||
56 | var parametersArray = toParametersArray(parameters); | ||
57 | tuple = Tuples.flatTupleOf(parametersArray); | ||
58 | } | ||
59 | if (emptyMaskIndexer == null) { | ||
60 | return backend.hasMatch(identityMask, tuple); | ||
61 | } | ||
62 | var matches = emptyMaskIndexer.get(Tuples.staticArityFlatTupleOf()); | ||
63 | return matches != null && matches.contains(tuple); | ||
64 | } | ||
65 | |||
66 | @Override | ||
67 | public Cursor<TupleLike, Boolean> allResults() { | ||
68 | if (emptyMaskIndexer == null) { | ||
69 | return new ResultSetCursor(backend.getAllMatches(empty).iterator()); | ||
70 | } | ||
71 | var matches = emptyMaskIndexer.get(Tuples.staticArityFlatTupleOf()); | ||
72 | return matches == null ? Cursors.empty() : new ResultSetCursor(matches.stream().iterator()); | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public int countResults() { | ||
77 | if (emptyMaskIndexer == null) { | ||
78 | return backend.countMatches(empty); | ||
79 | } | ||
80 | var matches = emptyMaskIndexer.get(Tuples.staticArityFlatTupleOf()); | ||
81 | return matches == null ? 0 : matches.size(); | ||
82 | } | ||
83 | |||
84 | private Object[] toParametersArray(TupleLike tuple) { | ||
85 | int size = tuple.getSize(); | ||
86 | var array = new Object[size]; | ||
87 | for (int i = 0; i < size; i++) { | ||
88 | var value = tuple.get(i); | ||
89 | array[i] = Tuple.of(value); | ||
90 | } | ||
91 | return array; | ||
92 | } | ||
93 | } | ||