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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
package tools.refinery.store.query.viatra.internal;
import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.GenericQueryGroup;
import org.eclipse.viatra.query.runtime.api.GenericQuerySpecification;
import org.eclipse.viatra.query.runtime.api.IQueryGroup;
import tools.refinery.store.map.Cursor;
import tools.refinery.store.map.DiffCursor;
import tools.refinery.store.model.Model;
import tools.refinery.store.model.ModelDiffCursor;
import tools.refinery.store.model.Tuple;
import tools.refinery.store.model.representation.DataRepresentation;
import tools.refinery.store.model.representation.Relation;
import tools.refinery.store.query.QueryableModel;
import tools.refinery.store.query.QueryableModelStore;
import tools.refinery.store.query.building.DNFPredicate;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
public class ViatraQueryableModel implements QueryableModel {
protected final QueryableModelStore store;
protected final Model model;
protected final Map<DNFPredicate, GenericQuerySpecification<RawPatternMatcher>> predicates2PQuery;
protected RelationalScope scope;
protected AdvancedViatraQueryEngine engine;
protected Map<DNFPredicate, RawPatternMatcher> predicate2Matcher;
public ViatraQueryableModel(QueryableModelStore store, Model model,
Map<DNFPredicate, GenericQuerySpecification<RawPatternMatcher>> predicates2PQuery) {
this.store = store;
this.model = model;
this.predicates2PQuery = predicates2PQuery;
initEngine();
}
private void initEngine() {
this.scope = new RelationalScope(this.model, this.store.getViews());
this.engine = AdvancedViatraQueryEngine.createUnmanagedEngine(this.scope);
this.predicate2Matcher = initMatchers(this.engine, this.predicates2PQuery);
}
private Map<DNFPredicate, RawPatternMatcher> initMatchers(
AdvancedViatraQueryEngine engine,
Map<DNFPredicate, GenericQuerySpecification<RawPatternMatcher>> predicates2pQuery) {
// 1. prepare group
IQueryGroup queryGroup = GenericQueryGroup.of(Set.copyOf(predicates2pQuery.values()));
engine.prepareGroup(queryGroup, null);
// 2. then get all matchers
Map<DNFPredicate, RawPatternMatcher> result = new HashMap<>();
for (var entry : predicates2pQuery.entrySet()) {
var matcher = engine.getMatcher(entry.getValue());
result.put(entry.getKey(), matcher);
}
return result;
}
@Override
public Set<DataRepresentation<?, ?>> getDataRepresentations() {
return model.getDataRepresentations();
}
@Override
public Set<DNFPredicate> getPredicates() {
return store.getPredicates();
}
@Override
public <K, V> V get(DataRepresentation<K, V> representation, K key) {
return model.get(representation, key);
}
@Override
public <K, V> Cursor<K, V> getAll(DataRepresentation<K, V> representation) {
return model.getAll(representation);
}
@SuppressWarnings("unchecked")
@Override
public <K, V> V put(DataRepresentation<K, V> representation, K key, V value) {
V oldValue = this.model.put(representation, key, value);
if (representation instanceof Relation<?> relation) {
this.scope.processUpdate((Relation<V>) relation, (Tuple) key, oldValue, value);
}
return oldValue;
}
@Override
public <K, V> void putAll(DataRepresentation<K, V> representation, Cursor<K, V> cursor) {
if (representation instanceof Relation<?>) {
//noinspection RedundantSuppression
@SuppressWarnings("unchecked")
Relation<V> relation = (Relation<V>) representation;
while (cursor.move()) {
Tuple key = (Tuple) cursor.getKey();
V newValue = cursor.getValue();
V oldValue = this.model.put(relation, key, newValue);
this.scope.processUpdate(relation, key, oldValue, newValue);
}
} else {
this.model.putAll(representation, cursor);
}
}
@Override
public <K, V> long getSize(DataRepresentation<K, V> representation) {
return model.getSize(representation);
}
protected RawPatternMatcher getMatcher(DNFPredicate predicate) {
var result = this.predicate2Matcher.get(predicate);
if (result == null) {
throw new IllegalArgumentException("Model does not contain predicate %s".formatted(predicate.getName()));
} else
return result;
}
protected void validateParameters(DNFPredicate predicate, Object[] parameters) {
int predicateArity = predicate.getVariables().size();
int parameterArity = parameters.length;
if (parameterArity != predicateArity) {
throw new IllegalArgumentException(
"Predicate %s with %d arity called with different number of parameters (%d)"
.formatted(predicate.getName(), predicateArity, parameterArity));
}
}
@Override
public boolean hasResult(DNFPredicate predicate) {
return getMatcher(predicate).hasResult();
}
@Override
public boolean hasResult(DNFPredicate predicate, Object[] parameters) {
validateParameters(predicate, parameters);
return getMatcher(predicate).hasResult(parameters);
}
@Override
public Optional<Object[]> oneResult(DNFPredicate predicate) {
return getMatcher(predicate).oneResult();
}
@Override
public Optional<Object[]> oneResult(DNFPredicate predicate, Object[] parameters) {
validateParameters(predicate, parameters);
return getMatcher(predicate).oneResult(parameters);
}
@Override
public Stream<Object[]> allResults(DNFPredicate predicate) {
return getMatcher(predicate).allResults();
}
@Override
public Stream<Object[]> allResults(DNFPredicate predicate, Object[] parameters) {
validateParameters(predicate, parameters);
return getMatcher(predicate).allResults(parameters);
}
@Override
public int countResults(DNFPredicate predicate) {
return getMatcher(predicate).countResults();
}
@Override
public int countResults(DNFPredicate predicate, Object[] parameters) {
validateParameters(predicate, parameters);
return getMatcher(predicate).countResults(parameters);
}
@Override
public boolean hasChanges() {
return scope.hasChanges();
}
@Override
public void flushChanges() {
this.scope.flush();
}
@Override
public ModelDiffCursor getDiffCursor(long to) {
return model.getDiffCursor(to);
}
@Override
public long commit() {
return this.model.commit();
}
@Override
public void restore(long state) {
restoreWithDiffReplay(state);
}
private void restoreWithDiffReplay(long state) {
var modelDiffCursor = getDiffCursor(state);
for (DataRepresentation<?, ?> dataRepresentation : this.getDataRepresentations()) {
restoreRepresentationWithDiffReplay(modelDiffCursor, dataRepresentation);
}
}
private <K, V> void restoreRepresentationWithDiffReplay(ModelDiffCursor modelDiffCursor,
DataRepresentation<K, V> dataRepresentation) {
DiffCursor<K, V> diffCursor = modelDiffCursor.getCursor(dataRepresentation);
this.putAll(dataRepresentation, diffCursor);
}
}
|