blob: d14769206033511cec2df7c4ef83288f0df8e962 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.query.viatra.internal.matcher;
import org.eclipse.viatra.query.runtime.matchers.backend.IQueryResultProvider;
import org.eclipse.viatra.query.runtime.matchers.tuple.TupleMask;
import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples;
import org.eclipse.viatra.query.runtime.rete.index.Indexer;
import org.eclipse.viatra.query.runtime.rete.matcher.RetePatternMatcher;
import tools.refinery.store.map.Cursor;
import tools.refinery.store.map.Cursors;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.ResultSet;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.query.viatra.internal.ViatraModelQueryAdapterImpl;
import tools.refinery.store.tuple.Tuple;
/**
* Directly access the tuples inside a VIATRA pattern matcher.<p>
* This class neglects calling
* {@link org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext#wrapTuple(org.eclipse.viatra.query.runtime.matchers.tuple.Tuple)}
* and
* {@link org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext#unwrapTuple(org.eclipse.viatra.query.runtime.matchers.tuple.Tuple)},
* because {@link tools.refinery.store.query.viatra.internal.context.RelationalRuntimeContext} provides a trivial
* implementation for these methods.
* Using this class with any other runtime context may lead to undefined behavior.
*/
public class RelationalViatraMatcher implements ResultSet<Boolean> {
private final ViatraModelQueryAdapterImpl adapter;
private final RelationalQuery query;
private final TupleMask emptyMask;
private final TupleMask identityMask;
private final IQueryResultProvider backend;
private final Indexer emptyMaskIndexer;
public RelationalViatraMatcher(ViatraModelQueryAdapterImpl adapter, RelationalQuery query,
RawPatternMatcher rawPatternMatcher) {
this.adapter = adapter;
this.query = query;
int arity = query.arity();
emptyMask = TupleMask.empty(arity);
identityMask = TupleMask.identity(arity);
backend = rawPatternMatcher.getBackend();
if (backend instanceof RetePatternMatcher reteBackend) {
emptyMaskIndexer = IndexerUtils.getIndexer(reteBackend, emptyMask);
} else {
emptyMaskIndexer = null;
}
}
@Override
public ModelQueryAdapter getAdapter() {
return adapter;
}
@Override
public Query<Boolean> getQuery() {
return query;
}
@Override
public Boolean get(Tuple parameters) {
var tuple = MatcherUtils.toViatraTuple(parameters);
if (emptyMaskIndexer == null) {
return backend.hasMatch(identityMask, tuple);
}
var matches = emptyMaskIndexer.get(Tuples.staticArityFlatTupleOf());
return matches != null && matches.contains(tuple);
}
@Override
public Cursor<Tuple, Boolean> getAll() {
if (emptyMaskIndexer == null) {
var allMatches = backend.getAllMatches(emptyMask, Tuples.staticArityFlatTupleOf());
return new RelationalCursor(allMatches.iterator());
}
var matches = emptyMaskIndexer.get(Tuples.staticArityFlatTupleOf());
return matches == null ? Cursors.empty() : new RelationalCursor(matches.stream().iterator());
}
@Override
public int size() {
if (emptyMaskIndexer == null) {
return backend.countMatches(emptyMask, Tuples.staticArityFlatTupleOf());
}
var matches = emptyMaskIndexer.get(Tuples.staticArityFlatTupleOf());
return matches == null ? 0 : matches.size();
}
}
|