diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/timely/FirstOnlySequentialTimelyColumnAggregatorNode.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/timely/FirstOnlySequentialTimelyColumnAggregatorNode.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/timely/FirstOnlySequentialTimelyColumnAggregatorNode.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/timely/FirstOnlySequentialTimelyColumnAggregatorNode.java new file mode 100644 index 00000000..79197aac --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/timely/FirstOnlySequentialTimelyColumnAggregatorNode.java | |||
@@ -0,0 +1,117 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2019, Tamas Szabo, itemis AG, Gabor Bergmann, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.rete.aggregation.timely; | ||
10 | |||
11 | import java.util.Objects; | ||
12 | import java.util.TreeMap; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
15 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
16 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
17 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
18 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
19 | import tools.refinery.viatra.runtime.rete.network.communication.Timestamp; | ||
20 | |||
21 | /** | ||
22 | * First-only column aggregator with sequential aggregation architecture. | ||
23 | * | ||
24 | * @author Tamas Szabo | ||
25 | * @since 2.4 | ||
26 | */ | ||
27 | public class FirstOnlySequentialTimelyColumnAggregatorNode<Domain, Accumulator, AggregateResult> | ||
28 | extends FirstOnlyTimelyColumnAggregatorNode<Domain, Accumulator, AggregateResult> { | ||
29 | |||
30 | public FirstOnlySequentialTimelyColumnAggregatorNode(final ReteContainer reteContainer, | ||
31 | final IMultisetAggregationOperator<Domain, Accumulator, AggregateResult> operator, | ||
32 | final TupleMask groupMask, final TupleMask columnMask) { | ||
33 | super(reteContainer, operator, groupMask, columnMask); | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Accumulator gets modified only at the timestamp where the update happened. During the folding, accumulators are | ||
38 | * never changed at higher timestamps. Aggregate results at higher timestamps may change due to the change at the | ||
39 | * input timestamp. Uniqueness enforcement may require from aggregate results to jump up/down on demand during the | ||
40 | * folding. | ||
41 | */ | ||
42 | @Override | ||
43 | public void update(final Direction direction, final Tuple update, final Timestamp timestamp) { | ||
44 | final Tuple group = groupMask.transform(update); | ||
45 | final Tuple value = columnMask.transform(update); | ||
46 | @SuppressWarnings("unchecked") | ||
47 | final Domain aggregand = (Domain) runtimeContext.unwrapElement(value.get(0)); | ||
48 | final boolean isInsertion = direction == Direction.INSERT; | ||
49 | |||
50 | final AggregateResult previousResult = getResultRaw(group, timestamp, true); | ||
51 | |||
52 | final Accumulator oldAccumulator = getAccumulator(group, timestamp); | ||
53 | final AggregateResult oldResult = previousResult == null ? operator.getAggregate(oldAccumulator) | ||
54 | : operator.combine(previousResult, oldAccumulator); | ||
55 | |||
56 | final Accumulator newAccumulator = operator.update(oldAccumulator, aggregand, isInsertion); | ||
57 | final AggregateResult newResult = previousResult == null ? operator.getAggregate(newAccumulator) | ||
58 | : operator.combine(previousResult, newAccumulator); | ||
59 | |||
60 | storeIfNotNeutral(group, newAccumulator, newResult, timestamp); | ||
61 | |||
62 | propagateWithChecks(group, timestamp, previousResult, previousResult, oldResult, newResult); | ||
63 | |||
64 | // fold up the state towards higher timestamps | ||
65 | if (!Objects.equals(oldResult, newResult)) { | ||
66 | AggregateResult previousOldResult = oldResult; | ||
67 | AggregateResult previousNewResult = newResult; | ||
68 | AggregateResult currentOldResult = null; | ||
69 | AggregateResult currentNewResult = null; | ||
70 | final TreeMap<Timestamp, CumulativeAggregate<Accumulator, AggregateResult>> groupEntries = this.memory | ||
71 | .get(group); | ||
72 | |||
73 | Timestamp currentTimestamp = groupEntries == null ? null : groupEntries.higherKey(timestamp); | ||
74 | |||
75 | while (currentTimestamp != null) { | ||
76 | // they cannot be the same, otherwise we would not even be here | ||
77 | assert !Objects.equals(previousOldResult, previousNewResult); | ||
78 | |||
79 | final Accumulator accumulator = getAccumulator(group, currentTimestamp); | ||
80 | currentOldResult = groupEntries.get(currentTimestamp).result; | ||
81 | currentNewResult = operator.combine(previousNewResult, accumulator); | ||
82 | |||
83 | // otherwise we would not be iterating over this timestamp | ||
84 | assert !operator.isNeutral(accumulator); | ||
85 | |||
86 | propagateWithChecks(group, currentTimestamp, previousOldResult, previousNewResult, currentOldResult, | ||
87 | currentNewResult); | ||
88 | |||
89 | if (!Objects.equals(currentOldResult, currentNewResult)) { | ||
90 | storeIfNotNeutral(group, accumulator, currentNewResult, currentTimestamp); | ||
91 | previousOldResult = currentOldResult; | ||
92 | previousNewResult = currentNewResult; | ||
93 | currentTimestamp = groupEntries.higherKey(currentTimestamp); | ||
94 | } else { | ||
95 | // we can stop the folding from here | ||
96 | break; | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | @Override | ||
103 | protected Accumulator getAccumulator(final Tuple group, final Timestamp timestamp) { | ||
104 | final TreeMap<Timestamp, CumulativeAggregate<Accumulator, AggregateResult>> entryMap = this.memory.get(group); | ||
105 | if (entryMap == null) { | ||
106 | return operator.createNeutral(); | ||
107 | } else { | ||
108 | final CumulativeAggregate<Accumulator, AggregateResult> entry = entryMap.get(timestamp); | ||
109 | if (entry == null) { | ||
110 | return operator.createNeutral(); | ||
111 | } else { | ||
112 | return entry.accumulator; | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | |||
117 | } | ||