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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/*******************************************************************************
* Copyright (c) 2010-2014, Bergmann Gabor, Istvan Rath and Daniel Varro
* Copyright (c) 2023 The Refinery Authors <https://refinery.tools>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-v20.html.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package tools.refinery.viatra.runtime.rete.network;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EMap;
import tools.refinery.viatra.runtime.base.itc.alg.representative.RepresentativeElectionAlgorithm;
import tools.refinery.viatra.runtime.base.itc.alg.representative.StronglyConnectedComponentAlgorithm;
import tools.refinery.viatra.runtime.base.itc.alg.representative.WeaklyConnectedComponentAlgorithm;
import tools.refinery.viatra.runtime.matchers.context.IPosetComparator;
import tools.refinery.viatra.runtime.matchers.psystem.IExpressionEvaluator;
import tools.refinery.viatra.runtime.matchers.psystem.IRelationEvaluator;
import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
import tools.refinery.viatra.runtime.rete.aggregation.ColumnAggregatorNode;
import tools.refinery.viatra.runtime.rete.aggregation.CountNode;
import tools.refinery.viatra.runtime.rete.aggregation.IAggregatorNode;
import tools.refinery.viatra.runtime.rete.aggregation.timely.FaithfulParallelTimelyColumnAggregatorNode;
import tools.refinery.viatra.runtime.rete.aggregation.timely.FaithfulSequentialTimelyColumnAggregatorNode;
import tools.refinery.viatra.runtime.rete.aggregation.timely.FirstOnlyParallelTimelyColumnAggregatorNode;
import tools.refinery.viatra.runtime.rete.aggregation.timely.FirstOnlySequentialTimelyColumnAggregatorNode;
import tools.refinery.viatra.runtime.rete.boundary.ExternalInputEnumeratorNode;
import tools.refinery.viatra.runtime.rete.boundary.ExternalInputStatelessFilterNode;
import tools.refinery.viatra.runtime.rete.eval.EvaluatorCore;
import tools.refinery.viatra.runtime.rete.eval.MemorylessEvaluatorNode;
import tools.refinery.viatra.runtime.rete.eval.OutputCachingEvaluatorNode;
import tools.refinery.viatra.runtime.rete.eval.RelationEvaluatorNode;
import tools.refinery.viatra.runtime.rete.index.ExistenceNode;
import tools.refinery.viatra.runtime.rete.index.Indexer;
import tools.refinery.viatra.runtime.rete.index.JoinNode;
import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration;
import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration.AggregatorArchitecture;
import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration.TimelineRepresentation;
import tools.refinery.viatra.runtime.rete.misc.ConstantNode;
import tools.refinery.viatra.runtime.rete.recipes.*;
import tools.refinery.viatra.runtime.rete.single.*;
import tools.refinery.viatra.runtime.rete.traceability.TraceInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Factory for instantiating Rete nodes. The created nodes are not connected to the network yet.
*
* @author Bergmann Gabor
*
*/
class NodeFactory {
Logger logger;
public NodeFactory(Logger logger) {
super();
this.logger = logger;
}
/**
* PRE: parent node must already be created
*/
public Indexer createIndexer(ReteContainer reteContainer, IndexerRecipe recipe, Supplier parentNode,
TraceInfo... traces) {
if (recipe instanceof ProjectionIndexerRecipe) {
return parentNode.constructIndex(toMask(recipe.getMask()), traces);
// already traced
} else if (recipe instanceof AggregatorIndexerRecipe) {
int indexOfAggregateResult = recipe.getParent().getArity();
int resultPosition = recipe.getMask().getSourceIndices().lastIndexOf(indexOfAggregateResult);
IAggregatorNode aggregatorNode = (IAggregatorNode) parentNode;
final Indexer result = (resultPosition == -1) ? aggregatorNode.getAggregatorOuterIndexer()
: aggregatorNode.getAggregatorOuterIdentityIndexer(resultPosition);
for (TraceInfo traceInfo : traces)
result.assignTraceInfo(traceInfo);
return result;
} else
throw new IllegalArgumentException("Unkown Indexer recipe: " + recipe);
}
/**
* PRE: recipe is not an indexer recipe.
*/
public Supplier createNode(ReteContainer reteContainer, ReteNodeRecipe recipe, TraceInfo... traces) {
if (recipe instanceof IndexerRecipe)
throw new IllegalArgumentException("Indexers are not created by NodeFactory: " + recipe);
Supplier result = instantiateNodeDispatch(reteContainer, recipe);
for (TraceInfo traceInfo : traces)
result.assignTraceInfo(traceInfo);
return result;
}
private Supplier instantiateNodeDispatch(ReteContainer reteContainer, ReteNodeRecipe recipe) {
// Parentless
if (recipe instanceof ConstantRecipe)
return instantiateNode(reteContainer, (ConstantRecipe) recipe);
if (recipe instanceof InputRecipe)
return instantiateNode(reteContainer, (InputRecipe) recipe);
// SingleParentNodeRecipe
// if (recipe instanceof ProjectionIndexer)
// return instantiateNode((ProjectionIndexer)recipe);
if (recipe instanceof InputFilterRecipe)
return instantiateNode(reteContainer, (InputFilterRecipe) recipe);
if (recipe instanceof InequalityFilterRecipe)
return instantiateNode(reteContainer, (InequalityFilterRecipe) recipe);
if (recipe instanceof EqualityFilterRecipe)
return instantiateNode(reteContainer, (EqualityFilterRecipe) recipe);
if (recipe instanceof TransparentRecipe)
return instantiateNode(reteContainer, (TransparentRecipe) recipe);
if (recipe instanceof TrimmerRecipe)
return instantiateNode(reteContainer, (TrimmerRecipe) recipe);
if (recipe instanceof TransitiveClosureRecipe)
return instantiateNode(reteContainer, (TransitiveClosureRecipe) recipe);
if (recipe instanceof RepresentativeElectionRecipe)
return instantiateNode(reteContainer, (RepresentativeElectionRecipe) recipe);
if (recipe instanceof RelationEvaluationRecipe)
return instantiateNode(reteContainer, (RelationEvaluationRecipe) recipe);
if (recipe instanceof ExpressionEnforcerRecipe)
return instantiateNode(reteContainer, (ExpressionEnforcerRecipe) recipe);
if (recipe instanceof CountAggregatorRecipe)
return instantiateNode(reteContainer, (CountAggregatorRecipe) recipe);
if (recipe instanceof SingleColumnAggregatorRecipe)
return instantiateNode(reteContainer, (SingleColumnAggregatorRecipe) recipe);
if (recipe instanceof DiscriminatorDispatcherRecipe)
return instantiateNode(reteContainer, (DiscriminatorDispatcherRecipe) recipe);
if (recipe instanceof DiscriminatorBucketRecipe)
return instantiateNode(reteContainer, (DiscriminatorBucketRecipe) recipe);
// MultiParentNodeRecipe
if (recipe instanceof UniquenessEnforcerRecipe)
return instantiateNode(reteContainer, (UniquenessEnforcerRecipe) recipe);
if (recipe instanceof ProductionRecipe)
return instantiateNode(reteContainer, (ProductionRecipe) recipe);
// BetaNodeRecipe
if (recipe instanceof JoinRecipe)
return instantiateNode(reteContainer, (JoinRecipe) recipe);
if (recipe instanceof SemiJoinRecipe)
return instantiateNode(reteContainer, (SemiJoinRecipe) recipe);
if (recipe instanceof AntiJoinRecipe)
return instantiateNode(reteContainer, (AntiJoinRecipe) recipe);
// ... else
throw new IllegalArgumentException("Unsupported recipe type: " + recipe);
}
// INSTANTIATION for recipe types
private Supplier instantiateNode(ReteContainer reteContainer, InputRecipe recipe) {
return new ExternalInputEnumeratorNode(reteContainer);
}
private Supplier instantiateNode(ReteContainer reteContainer, InputFilterRecipe recipe) {
return new ExternalInputStatelessFilterNode(reteContainer, toMaskOrNull(recipe.getMask()));
}
private Supplier instantiateNode(ReteContainer reteContainer, CountAggregatorRecipe recipe) {
return new CountNode(reteContainer);
}
private Supplier instantiateNode(ReteContainer reteContainer, TransparentRecipe recipe) {
return new TransparentNode(reteContainer);
}
private Supplier instantiateNode(ReteContainer reteContainer, ExpressionEnforcerRecipe recipe) {
final IExpressionEvaluator evaluator = toIExpressionEvaluator(recipe.getExpression());
final Map<String, Integer> posMapping = toStringIndexMap(recipe.getMappedIndices());
final int sourceTupleWidth = recipe.getParent().getArity();
EvaluatorCore core = null;
if (recipe instanceof CheckRecipe) {
core = new EvaluatorCore.PredicateEvaluatorCore(logger, evaluator, posMapping, sourceTupleWidth);
} else if (recipe instanceof EvalRecipe) {
final boolean isUnwinding = ((EvalRecipe) recipe).isUnwinding();
core = new EvaluatorCore.FunctionEvaluatorCore(logger, evaluator, posMapping, sourceTupleWidth, isUnwinding);
} else {
throw new IllegalArgumentException("Unhandled expression enforcer recipe: " + recipe.getClass() + "!");
}
if (recipe.isCacheOutput()) {
return new OutputCachingEvaluatorNode(reteContainer, core);
} else {
return new MemorylessEvaluatorNode(reteContainer, core);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private Supplier instantiateNode(ReteContainer reteContainer, SingleColumnAggregatorRecipe recipe) {
final IMultisetAggregationOperator operator = recipe.getMultisetAggregationOperator();
TupleMask coreMask = null;
if (recipe.getOptionalMonotonicityInfo() != null) {
coreMask = toMask(recipe.getOptionalMonotonicityInfo().getCoreMask());
} else {
coreMask = toMask(recipe.getGroupByMask());
}
if (reteContainer.isTimelyEvaluation()) {
final TimelyConfiguration timelyConfiguration = reteContainer.getTimelyConfiguration();
final AggregatorArchitecture aggregatorArchitecture = timelyConfiguration.getAggregatorArchitecture();
final TimelineRepresentation timelineRepresentation = timelyConfiguration.getTimelineRepresentation();
TupleMask posetMask = null;
if (recipe.getOptionalMonotonicityInfo() != null) {
posetMask = toMask(recipe.getOptionalMonotonicityInfo().getPosetMask());
} else {
final int aggregatedColumn = recipe.getAggregableIndex();
posetMask = TupleMask.selectSingle(aggregatedColumn, coreMask.sourceWidth);
}
if (timelineRepresentation == TimelineRepresentation.FIRST_ONLY
&& aggregatorArchitecture == AggregatorArchitecture.SEQUENTIAL) {
return new FirstOnlySequentialTimelyColumnAggregatorNode(reteContainer, operator, coreMask, posetMask);
} else if (timelineRepresentation == TimelineRepresentation.FIRST_ONLY
&& aggregatorArchitecture == AggregatorArchitecture.PARALLEL) {
return new FirstOnlyParallelTimelyColumnAggregatorNode(reteContainer, operator, coreMask, posetMask);
} else if (timelineRepresentation == TimelineRepresentation.FAITHFUL
&& aggregatorArchitecture == AggregatorArchitecture.SEQUENTIAL) {
return new FaithfulSequentialTimelyColumnAggregatorNode(reteContainer, operator, coreMask, posetMask);
} else if (timelineRepresentation == TimelineRepresentation.FAITHFUL
&& aggregatorArchitecture == AggregatorArchitecture.PARALLEL) {
return new FaithfulParallelTimelyColumnAggregatorNode(reteContainer, operator, coreMask, posetMask);
} else {
throw new IllegalArgumentException("Unsupported timely configuration!");
}
} else if (recipe.isDeleteRederiveEvaluation() && recipe.getOptionalMonotonicityInfo() != null) {
final TupleMask posetMask = toMask(recipe.getOptionalMonotonicityInfo().getPosetMask());
final IPosetComparator posetComparator = (IPosetComparator) recipe.getOptionalMonotonicityInfo()
.getPosetComparator();
return new ColumnAggregatorNode(reteContainer, operator, recipe.isDeleteRederiveEvaluation(), coreMask,
posetMask, posetComparator);
} else {
final int aggregatedColumn = recipe.getAggregableIndex();
return new ColumnAggregatorNode(reteContainer, operator, coreMask, aggregatedColumn);
}
}
private Supplier instantiateNode(ReteContainer reteContainer, TransitiveClosureRecipe recipe) {
return new TransitiveClosureNode(reteContainer);
}
private Supplier instantiateNode(ReteContainer reteContainer, RepresentativeElectionRecipe recipe) {
RepresentativeElectionAlgorithm.Factory algorithmFactory = switch (recipe.getConnectivity()) {
case STRONG -> StronglyConnectedComponentAlgorithm::new;
case WEAK -> WeaklyConnectedComponentAlgorithm::new;
};
return new RepresentativeElectionNode(reteContainer, algorithmFactory);
}
private Supplier instantiateNode(ReteContainer reteContainer, RelationEvaluationRecipe recipe) {
return new RelationEvaluatorNode(reteContainer, toIRelationEvaluator(recipe.getEvaluator()));
}
private Supplier instantiateNode(ReteContainer reteContainer, ProductionRecipe recipe) {
if (reteContainer.isTimelyEvaluation()) {
return new TimelyProductionNode(reteContainer, toStringIndexMap(recipe.getMappedIndices()));
} else if (recipe.isDeleteRederiveEvaluation() && recipe.getOptionalMonotonicityInfo() != null) {
TupleMask coreMask = toMask(recipe.getOptionalMonotonicityInfo().getCoreMask());
TupleMask posetMask = toMask(recipe.getOptionalMonotonicityInfo().getPosetMask());
IPosetComparator posetComparator = (IPosetComparator) recipe.getOptionalMonotonicityInfo()
.getPosetComparator();
return new DefaultProductionNode(reteContainer, toStringIndexMap(recipe.getMappedIndices()),
recipe.isDeleteRederiveEvaluation(), coreMask, posetMask, posetComparator);
} else {
return new DefaultProductionNode(reteContainer, toStringIndexMap(recipe.getMappedIndices()),
recipe.isDeleteRederiveEvaluation());
}
}
private Supplier instantiateNode(ReteContainer reteContainer, UniquenessEnforcerRecipe recipe) {
if (reteContainer.isTimelyEvaluation()) {
return new TimelyUniquenessEnforcerNode(reteContainer, recipe.getArity());
} else if (recipe.isDeleteRederiveEvaluation() && recipe.getOptionalMonotonicityInfo() != null) {
TupleMask coreMask = toMask(recipe.getOptionalMonotonicityInfo().getCoreMask());
TupleMask posetMask = toMask(recipe.getOptionalMonotonicityInfo().getPosetMask());
IPosetComparator posetComparator = (IPosetComparator) recipe.getOptionalMonotonicityInfo()
.getPosetComparator();
return new UniquenessEnforcerNode(reteContainer, recipe.getArity(), recipe.isDeleteRederiveEvaluation(),
coreMask, posetMask, posetComparator);
} else {
return new UniquenessEnforcerNode(reteContainer, recipe.getArity(), recipe.isDeleteRederiveEvaluation());
}
}
private Supplier instantiateNode(ReteContainer reteContainer, ConstantRecipe recipe) {
final List<Object> constantValues = recipe.getConstantValues();
final Object[] constantArray = constantValues.toArray(new Object[constantValues.size()]);
return new ConstantNode(reteContainer, Tuples.flatTupleOf(constantArray));
}
private Supplier instantiateNode(ReteContainer reteContainer, DiscriminatorBucketRecipe recipe) {
return new DiscriminatorBucketNode(reteContainer, recipe.getBucketKey());
}
private Supplier instantiateNode(ReteContainer reteContainer, DiscriminatorDispatcherRecipe recipe) {
return new DiscriminatorDispatcherNode(reteContainer, recipe.getDiscriminationColumnIndex());
}
private Supplier instantiateNode(ReteContainer reteContainer, TrimmerRecipe recipe) {
return new TrimmerNode(reteContainer, toMask(recipe.getMask()));
}
private Supplier instantiateNode(ReteContainer reteContainer, InequalityFilterRecipe recipe) {
Tunnel result = new InequalityFilterNode(reteContainer, recipe.getSubject(),
TupleMask.fromSelectedIndices(recipe.getParent().getArity(), recipe.getInequals()));
return result;
}
private Supplier instantiateNode(ReteContainer reteContainer, EqualityFilterRecipe recipe) {
final int[] equalIndices = TupleMask.integersToIntArray(recipe.getIndices());
return new EqualityFilterNode(reteContainer, equalIndices);
}
private Supplier instantiateNode(ReteContainer reteContainer, AntiJoinRecipe recipe) {
return new ExistenceNode(reteContainer, true);
}
private Supplier instantiateNode(ReteContainer reteContainer, SemiJoinRecipe recipe) {
return new ExistenceNode(reteContainer, false);
}
private Supplier instantiateNode(ReteContainer reteContainer, JoinRecipe recipe) {
return new JoinNode(reteContainer, toMask(recipe.getRightParentComplementaryMask()));
}
// HELPERS
private IExpressionEvaluator toIExpressionEvaluator(ExpressionDefinition expressionDefinition) {
final Object evaluator = expressionDefinition.getEvaluator();
if (evaluator instanceof IExpressionEvaluator) {
return (IExpressionEvaluator) evaluator;
}
throw new IllegalArgumentException("No runtime support for expression evaluator: " + evaluator);
}
private IRelationEvaluator toIRelationEvaluator(ExpressionDefinition expressionDefinition) {
final Object evaluator = expressionDefinition.getEvaluator();
if (evaluator instanceof IRelationEvaluator) {
return (IRelationEvaluator) evaluator;
}
throw new IllegalArgumentException("No runtime support for relation evaluator: " + evaluator);
}
private Map<String, Integer> toStringIndexMap(final EMap<String, Integer> mappedIndices) {
final HashMap<String, Integer> result = new HashMap<String, Integer>();
for (java.util.Map.Entry<String, Integer> entry : mappedIndices) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
/** Mask can be null */
private TupleMask toMaskOrNull(Mask mask) {
if (mask == null)
return null;
else
return toMask(mask);
}
/** Mask is non-null. */
private TupleMask toMask(Mask mask) {
return TupleMask.fromSelectedIndices(mask.getSourceArity(), mask.getSourceIndices());
}
}
|