aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/aggregation/timely/FaithfulSequentialTimelyColumnAggregatorNode.java
blob: 72e52f7e1787e729c0ced7a765c9185372473e9e (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
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
/*******************************************************************************
 * 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.interpreter.rete.aggregation.timely;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.TreeMap;

import tools.refinery.interpreter.matchers.psystem.aggregations.IMultisetAggregationOperator;
import tools.refinery.interpreter.matchers.tuple.Tuple;
import tools.refinery.interpreter.matchers.tuple.TupleMask;
import tools.refinery.interpreter.matchers.util.CollectionsFactory;
import tools.refinery.interpreter.matchers.util.Direction;
import tools.refinery.interpreter.matchers.util.IDeltaBag;
import tools.refinery.interpreter.matchers.util.Preconditions;
import tools.refinery.interpreter.matchers.util.Signed;
import tools.refinery.interpreter.matchers.util.timeline.Diff;
import tools.refinery.interpreter.rete.aggregation.timely.FaithfulSequentialTimelyColumnAggregatorNode.CumulativeAggregate;
import tools.refinery.interpreter.rete.aggregation.timely.FaithfulSequentialTimelyColumnAggregatorNode.FoldingState;
import tools.refinery.interpreter.rete.network.ReteContainer;
import tools.refinery.interpreter.rete.network.communication.Timestamp;
import tools.refinery.interpreter.rete.network.communication.timely.ResumableNode;

/**
 * Faithful column aggregator with sequential aggregation architecture.
 *
 * @author Tamas Szabo
 * @since 2.4
 *
 */
public class FaithfulSequentialTimelyColumnAggregatorNode<Domain, Accumulator, AggregateResult> extends
        FaithfulTimelyColumnAggregatorNode<Domain, Accumulator, AggregateResult, CumulativeAggregate<Domain, Accumulator, AggregateResult>, FoldingState<Domain, AggregateResult>>
        implements ResumableNode {

    protected boolean isRecursiveAggregation;

    public FaithfulSequentialTimelyColumnAggregatorNode(final ReteContainer reteContainer,
            final IMultisetAggregationOperator<Domain, Accumulator, AggregateResult> operator,
            final TupleMask groupMask, final TupleMask columnMask) {
        super(reteContainer, operator, groupMask, columnMask);
        this.isRecursiveAggregation = false;
    }

    @Override
    public void networkStructureChanged() {
        super.networkStructureChanged();
        this.isRecursiveAggregation = this.reteContainer.getCommunicationTracker().isInRecursiveGroup(this);
    }

    @Override
    protected Map<AggregateResult, Diff<Timestamp>> doFoldingStep(final Tuple group,
            final FoldingState<Domain, AggregateResult> state, final Timestamp timestamp) {
        final CumulativeAggregate<Domain, Accumulator, AggregateResult> aggregate = getAggregate(group, timestamp);
        if (state.delta.isEmpty() && Objects.equals(state.oldResult, state.newResult)) {
            gcAggregates(aggregate, group, timestamp);
            return Collections.emptyMap();
        } else {
            final Map<AggregateResult, Diff<Timestamp>> diffMap = CollectionsFactory.createMap();
            final Timestamp nextTimestamp = this.aggregates.get(group).higherKey(timestamp);

            final AggregateResult previousOldResult = state.oldResult;
            final AggregateResult previousNewResult = state.newResult;

            final AggregateResult currentOldResult = previousOldResult == null
                    ? operator.getAggregate(aggregate.positive)
                    : operator.combine(previousOldResult, aggregate.positive);

            for (final Entry<Domain, Integer> entry : state.delta.entriesWithMultiplicities()) {
                final boolean isInsertion = entry.getValue() > 0;
                final Domain aggregand = entry.getKey();
                if (isInsertion) {
                    for (int i = 0; i < entry.getValue(); i++) {
                        if (isRecursiveAggregation) {
                            final boolean contains = aggregate.negative.containsNonZero(aggregand);
                            if (contains) {
                                aggregate.negative.addOne(aggregand);
                            } else {
                                aggregate.positive = operator.update(aggregate.positive, aggregand, true);
                            }
                        } else {
                            aggregate.positive = operator.update(aggregate.positive, aggregand, true);
                        }
                    }
                } else {
                    for (int i = 0; i < -entry.getValue(); i++) {
                        if (isRecursiveAggregation) {
                            final boolean contains = operator.contains(aggregand, aggregate.positive);
                            if (contains) {
                                aggregate.positive = operator.update(aggregate.positive, aggregand, false);
                            } else {
                                aggregate.negative.removeOne(aggregand);
                            }
                        } else {
                            aggregate.positive = operator.update(aggregate.positive, aggregand, false);
                        }
                    }
                }
            }

            final AggregateResult currentNewResult = previousNewResult == null
                    ? operator.getAggregate(aggregate.positive)
                    : operator.combine(previousNewResult, aggregate.positive);

            aggregate.cachedResult = currentNewResult;

            final boolean sameResult = Objects.equals(currentOldResult, currentNewResult);
            if (!sameResult) {
                // current old result disappears here
                appendDiff(currentOldResult, new Signed<>(Direction.DELETE, timestamp), diffMap);
                if (nextTimestamp != null) {
                    appendDiff(currentOldResult, new Signed<>(Direction.INSERT, nextTimestamp), diffMap);
                }

                // current new result appears here
                appendDiff(currentNewResult, new Signed<>(Direction.INSERT, timestamp), diffMap);
                if (nextTimestamp != null) {
                    appendDiff(currentNewResult, new Signed<>(Direction.DELETE, nextTimestamp), diffMap);
                }
            }

            gcAggregates(aggregate, group, timestamp);
            updateTimeline(group, diffMap);

            // prepare folding state for next timestamp
            if (nextTimestamp != null && !sameResult) {
                final FoldingState<Domain, AggregateResult> newState = new FoldingState<>();
                // DO NOT push forward the delta in the folding state!!! that one only affects the input timestamp
                newState.oldResult = currentOldResult;
                newState.newResult = currentNewResult;
                addFoldingState(group, newState, nextTimestamp);
            }

            return diffMap;
        }
    }

    @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 FoldingState<Domain, AggregateResult> state = new FoldingState<Domain, AggregateResult>();
        if (isInsertion) {
            state.delta.addOne(aggregand);
        } else {
            state.delta.removeOne(aggregand);
        }
        state.oldResult = previousResult;
        state.newResult = previousResult;

        // it is acceptable if both oldResult and newResult are null at this point
        // in that case we did not have a previous entry at a lower timestamp

        addFoldingState(group, state, timestamp);
    }

    protected AggregateResult getResultRaw(final Tuple group, final Timestamp timestamp, final boolean lower) {
        final TreeMap<Timestamp, CumulativeAggregate<Domain, Accumulator, AggregateResult>> entryMap = this.aggregates
                .get(group);
        if (entryMap == null) {
            return null;
        } else {
            CumulativeAggregate<Domain, Accumulator, AggregateResult> aggregate = null;
            if (lower) {
                final Entry<Timestamp, CumulativeAggregate<Domain, Accumulator, AggregateResult>> lowerEntry = entryMap
                        .lowerEntry(timestamp);
                if (lowerEntry != null) {
                    aggregate = lowerEntry.getValue();
                }
            } else {
                aggregate = entryMap.get(timestamp);
            }
            if (aggregate == null) {
                return null;
            } else {
                return aggregate.cachedResult;
            }
        }
    }

    @Override
    protected void gcAggregates(final CumulativeAggregate<Domain, Accumulator, AggregateResult> aggregate,
            final Tuple group, final Timestamp timestamp) {
        if (operator.isNeutral(aggregate.positive) && aggregate.negative.isEmpty()) {
            final TreeMap<Timestamp, CumulativeAggregate<Domain, Accumulator, AggregateResult>> groupAggregates = this.aggregates
                    .get(group);
            groupAggregates.remove(timestamp);
            if (groupAggregates.isEmpty()) {
                this.aggregates.remove(group);
            }
        }
    }

    @Override
    protected CumulativeAggregate<Domain, Accumulator, AggregateResult> getAggregate(final Tuple group,
            final Timestamp timestamp) {
        final TreeMap<Timestamp, CumulativeAggregate<Domain, Accumulator, AggregateResult>> groupAggregates = this.aggregates
                .computeIfAbsent(group, k -> CollectionsFactory.createTreeMap());
        return groupAggregates.computeIfAbsent(timestamp, k -> {
            final CumulativeAggregate<Domain, Accumulator, AggregateResult> aggregate = new CumulativeAggregate<>();
            aggregate.positive = operator.createNeutral();
            return aggregate;
        });
    }

    @Override
    public AggregateResult getAggregateResult(final Tuple group) {
        final TreeMap<Timestamp, CumulativeAggregate<Domain, Accumulator, AggregateResult>> groupAggregates = this.aggregates
                .get(group);
        if (groupAggregates != null) {
            final Entry<Timestamp, CumulativeAggregate<Domain, Accumulator, AggregateResult>> lastEntry = groupAggregates
                    .lastEntry();
            return lastEntry.getValue().cachedResult;
        } else {
            return NEUTRAL;
        }
    }

    protected static class CumulativeAggregate<Domain, Accumulator, AggregateResult> {
        protected Accumulator positive;
        protected IDeltaBag<Domain> negative;
        protected AggregateResult cachedResult;

        protected CumulativeAggregate() {
            this.negative = CollectionsFactory.createDeltaBag();
        }

        @Override
        public String toString() {
            return "positive=" + positive + " negative=" + negative + " cachedResult=" + cachedResult;
        }
    }

    protected static class FoldingState<Domain, AggregateResult>
            implements MergeableFoldingState<FoldingState<Domain, AggregateResult>> {
        protected IDeltaBag<Domain> delta;
        protected AggregateResult oldResult;
        protected AggregateResult newResult;

        protected FoldingState() {
            this.delta = CollectionsFactory.createDeltaBag();
        }

        @Override
        public String toString() {
            return "delta=" + delta + " oldResult=" + oldResult + " newResult=" + newResult;
        }

        /**
         * The returned result will never be null, even if the resulting delta set is empty.
         */
        @Override
        public FoldingState<Domain, AggregateResult> merge(final FoldingState<Domain, AggregateResult> that) {
            Preconditions.checkArgument(that != null);
            // 'this' was the previously registered folding state
            // 'that' is the new folding state being pushed upwards
            final FoldingState<Domain, AggregateResult> result = new FoldingState<Domain, AggregateResult>();
            this.delta.forEachEntryWithMultiplicities((d, m) -> result.delta.addSigned(d, m));
            that.delta.forEachEntryWithMultiplicities((d, m) -> result.delta.addSigned(d, m));
            result.oldResult = this.oldResult;
            result.newResult = that.newResult;
            return result;
        }

    }

}