aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/aggregation/timely/FirstOnlySequentialTimelyColumnAggregatorNode.java
blob: 79197aacadefd464ff3ce86c10ecf3b5e3fb3f2d (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2010-2019, Tamas Szabo, itemis AG, Gabor Bergmann, IncQuery Labs Ltd.
 * 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.aggregation.timely;

import java.util.Objects;
import java.util.TreeMap;

import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
import tools.refinery.viatra.runtime.matchers.util.Direction;
import tools.refinery.viatra.runtime.rete.network.ReteContainer;
import tools.refinery.viatra.runtime.rete.network.communication.Timestamp;

/**
 * First-only column aggregator with sequential aggregation architecture. 
 * 
 * @author Tamas Szabo
 * @since 2.4
 */
public class FirstOnlySequentialTimelyColumnAggregatorNode<Domain, Accumulator, AggregateResult>
        extends FirstOnlyTimelyColumnAggregatorNode<Domain, Accumulator, AggregateResult> {

    public FirstOnlySequentialTimelyColumnAggregatorNode(final ReteContainer reteContainer,
            final IMultisetAggregationOperator<Domain, Accumulator, AggregateResult> operator,
            final TupleMask groupMask, final TupleMask columnMask) {
        super(reteContainer, operator, groupMask, columnMask);
    }

    /**
     * Accumulator gets modified only at the timestamp where the update happened. During the folding, accumulators are
     * never changed at higher timestamps. Aggregate results at higher timestamps may change due to the change at the
     * input timestamp. Uniqueness enforcement may require from aggregate results to jump up/down on demand during the
     * folding.
     */
    @Override
    public void update(final Direction direction, final Tuple update, final Timestamp timestamp) {
        final Tuple group = groupMask.transform(update);
        final Tuple value = columnMask.transform(update);
        @SuppressWarnings("unchecked")
        final Domain aggregand = (Domain) runtimeContext.unwrapElement(value.get(0));
        final boolean isInsertion = direction == Direction.INSERT;

        final AggregateResult previousResult = getResultRaw(group, timestamp, true);

        final Accumulator oldAccumulator = getAccumulator(group, timestamp);
        final AggregateResult oldResult = previousResult == null ? operator.getAggregate(oldAccumulator)
                : operator.combine(previousResult, oldAccumulator);

        final Accumulator newAccumulator = operator.update(oldAccumulator, aggregand, isInsertion);
        final AggregateResult newResult = previousResult == null ? operator.getAggregate(newAccumulator)
                : operator.combine(previousResult, newAccumulator);

        storeIfNotNeutral(group, newAccumulator, newResult, timestamp);

        propagateWithChecks(group, timestamp, previousResult, previousResult, oldResult, newResult);

        // fold up the state towards higher timestamps
        if (!Objects.equals(oldResult, newResult)) {
            AggregateResult previousOldResult = oldResult;
            AggregateResult previousNewResult = newResult;
            AggregateResult currentOldResult = null;
            AggregateResult currentNewResult = null;
            final TreeMap<Timestamp, CumulativeAggregate<Accumulator, AggregateResult>> groupEntries = this.memory
                    .get(group);

            Timestamp currentTimestamp = groupEntries == null ? null : groupEntries.higherKey(timestamp);

            while (currentTimestamp != null) {
                // they cannot be the same, otherwise we would not even be here
                assert !Objects.equals(previousOldResult, previousNewResult);

                final Accumulator accumulator = getAccumulator(group, currentTimestamp);
                currentOldResult = groupEntries.get(currentTimestamp).result;
                currentNewResult = operator.combine(previousNewResult, accumulator);

                // otherwise we would not be iterating over this timestamp
                assert !operator.isNeutral(accumulator);

                propagateWithChecks(group, currentTimestamp, previousOldResult, previousNewResult, currentOldResult,
                        currentNewResult);

                if (!Objects.equals(currentOldResult, currentNewResult)) {
                    storeIfNotNeutral(group, accumulator, currentNewResult, currentTimestamp);
                    previousOldResult = currentOldResult;
                    previousNewResult = currentNewResult;
                    currentTimestamp = groupEntries.higherKey(currentTimestamp);
                } else {
                    // we can stop the folding from here
                    break;
                }
            }
        }
    }

    @Override
    protected Accumulator getAccumulator(final Tuple group, final Timestamp timestamp) {
        final TreeMap<Timestamp, CumulativeAggregate<Accumulator, AggregateResult>> entryMap = this.memory.get(group);
        if (entryMap == null) {
            return operator.createNeutral();
        } else {
            final CumulativeAggregate<Accumulator, AggregateResult> entry = entryMap.get(timestamp);
            if (entry == null) {
                return operator.createNeutral();
            } else {
                return entry.accumulator;
            }
        }
    }

}