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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package tools.refinery.store.query.viatra.internal.context;
import org.eclipse.viatra.query.runtime.matchers.context.*;
import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple;
import org.eclipse.viatra.query.runtime.matchers.tuple.Tuple;
import org.eclipse.viatra.query.runtime.matchers.tuple.TupleMask;
import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples;
import org.eclipse.viatra.query.runtime.matchers.util.Accuracy;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.viatra.internal.pquery.RelationViewWrapper;
import tools.refinery.store.query.viatra.internal.viewupdate.ModelUpdateListener;
import tools.refinery.store.query.view.AnyRelationView;
import tools.refinery.store.query.view.RelationView;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Optional;
import java.util.concurrent.Callable;
import static tools.refinery.store.util.CollectionsUtil.filter;
import static tools.refinery.store.util.CollectionsUtil.map;
public class RelationalRuntimeContext implements IQueryRuntimeContext {
private final RelationalQueryMetaContext metaContext = new RelationalQueryMetaContext();
private final ModelUpdateListener modelUpdateListener;
private final Model model;
public RelationalRuntimeContext(Model model, ModelUpdateListener relationUpdateListener) {
this.model = model;
this.modelUpdateListener = relationUpdateListener;
}
@Override
public IQueryMetaContext getMetaContext() {
return metaContext;
}
@Override
public <V> V coalesceTraversals(Callable<V> callable) throws InvocationTargetException {
try {
return callable.call();
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
@Override
public boolean isCoalescing() {
return true;
}
@Override
public boolean isIndexed(IInputKey key, IndexingService service) {
if (key instanceof RelationView<?> relationalKey) {
return this.modelUpdateListener.containsRelationView(relationalKey);
} else {
return false;
}
}
@Override
public void ensureIndexed(IInputKey key, IndexingService service) {
if (!isIndexed(key, service)) {
throw new IllegalStateException("Engine tries to index a new key " + key);
}
}
AnyRelationView checkKey(IInputKey key) {
if (key instanceof RelationViewWrapper wrappedKey) {
var relationViewKey = wrappedKey.getWrappedKey();
if (modelUpdateListener.containsRelationView(relationViewKey)) {
return relationViewKey;
} else {
throw new IllegalStateException("Query is asking for non-indexed key");
}
} else {
throw new IllegalStateException("Query is asking for non-relational key");
}
}
@Override
public int countTuples(IInputKey key, TupleMask seedMask, ITuple seed) {
var relationViewKey = checkKey(key);
Iterable<Object[]> allObjects = relationViewKey.getAll(model);
Iterable<Object[]> filteredBySeed = filter(allObjects, objectArray -> isMatching(objectArray, seedMask, seed));
Iterator<Object[]> iterator = filteredBySeed.iterator();
int result = 0;
while (iterator.hasNext()) {
iterator.next();
result++;
}
return result;
}
@Override
public Optional<Long> estimateCardinality(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy) {
return Optional.empty();
}
@Override
public Iterable<Tuple> enumerateTuples(IInputKey key, TupleMask seedMask, ITuple seed) {
var relationViewKey = checkKey(key);
Iterable<Object[]> allObjects = relationViewKey.getAll(model);
Iterable<Object[]> filteredBySeed = filter(allObjects, objectArray -> isMatching(objectArray, seedMask, seed));
return map(filteredBySeed, Tuples::flatTupleOf);
}
private boolean isMatching(Object[] tuple, TupleMask seedMask, ITuple seed) {
for (int i = 0; i < seedMask.indices.length; i++) {
final Object seedElement = seed.get(i);
final Object tupleElement = tuple[seedMask.indices[i]];
if (!tupleElement.equals(seedElement)) {
return false;
}
}
return true;
}
@Override
public Iterable<?> enumerateValues(IInputKey key, TupleMask seedMask, ITuple seed) {
return enumerateTuples(key, seedMask, seed);
}
@Override
public boolean containsTuple(IInputKey key, ITuple seed) {
var relationViewKey = checkKey(key);
return relationViewKey.get(model, seed.getElements());
}
@Override
public void addUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) {
var relationViewKey = (RelationView<?>) checkKey(key);
this.modelUpdateListener.addListener(key, relationViewKey, seed, listener);
}
@Override
public void removeUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) {
var relationViewKey = checkKey(key);
this.modelUpdateListener.removeListener(key, relationViewKey, seed, listener);
}
@Override
public Object wrapElement(Object externalElement) {
return externalElement;
}
@Override
public Object unwrapElement(Object internalElement) {
return internalElement;
}
@Override
public Tuple wrapTuple(Tuple externalElements) {
return externalElements;
}
@Override
public Tuple unwrapTuple(Tuple internalElements) {
return internalElements;
}
@Override
public void ensureWildcardIndexing(IndexingService service) {
throw new UnsupportedOperationException();
}
@Override
public void executeAfterTraversal(Runnable runnable) throws InvocationTargetException {
runnable.run();
}
}
|