aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/util/TimelyMemory.java
blob: 4aebfa7a5bf4f90cdda435aec2f9bed93f30f168 (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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*******************************************************************************
 * 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.matchers.util;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;

import tools.refinery.interpreter.matchers.tuple.ITuple;
import tools.refinery.interpreter.matchers.tuple.Tuple;
import tools.refinery.interpreter.matchers.util.resumable.Resumable;
import tools.refinery.interpreter.matchers.util.resumable.UnmaskedResumable;
import tools.refinery.interpreter.matchers.util.timeline.Diff;
import tools.refinery.interpreter.matchers.util.timeline.Timeline;
import tools.refinery.interpreter.matchers.util.timeline.Timelines;

/**
 * A timely memory implementation that incrementally maintains the {@link Timeline}s of tuples. The memory is capable of
 * lazy folding (see {@link Resumable}).
 *
 * @author Tamas Szabo
 * @since 2.3
 */
public class TimelyMemory<Timestamp extends Comparable<Timestamp>> implements Clearable, UnmaskedResumable<Timestamp> {

    protected final Map<Tuple, TreeMap<Timestamp, CumulativeCounter>> counters;
    protected final Map<Tuple, Timeline<Timestamp>> timelines;
    public final TreeMap<Timestamp, Map<Tuple, FoldingState>> foldingState;
    protected final Set<Tuple> presentAtInfinity;
    protected final boolean isLazy;
    protected final Diff<Timestamp> EMPTY_DIFF = new Diff<Timestamp>();

    public TimelyMemory() {
        this(false);
    }

    public TimelyMemory(final boolean isLazy) {
        this.counters = CollectionsFactory.createMap();
        this.timelines = CollectionsFactory.createMap();
        this.presentAtInfinity = CollectionsFactory.createSet();
        this.isLazy = isLazy;
        if (isLazy) {
            this.foldingState = CollectionsFactory.createTreeMap();
        } else {
            this.foldingState = null;
        }
    }

    @Override
    public Set<Tuple> getResumableTuples() {
        if (this.foldingState == null || this.foldingState.isEmpty()) {
            return Collections.emptySet();
        } else {
            return this.foldingState.firstEntry().getValue().keySet();
        }
    }

    @Override
    public Timestamp getResumableTimestamp() {
        if (this.foldingState == null || this.foldingState.isEmpty()) {
            return null;
        } else {
            return this.foldingState.firstKey();
        }
    }

    /**
     * Registers the given folding state for the specified timestamp and tuple. If there is already a state stored, the
     * two states will be merged together.
     */
    protected void addFoldingState(final Tuple tuple, final FoldingState state, final Timestamp timestamp) {
        assert state.diff != 0;
        final Map<Tuple, FoldingState> tupleMap = this.foldingState.computeIfAbsent(timestamp,
                k -> CollectionsFactory.createMap());
        tupleMap.compute(tuple, (k, v) -> {
            return v == null ? state : v.merge(state);
        });
    }

    @Override
    public Map<Tuple, Diff<Timestamp>> resumeAt(final Timestamp timestamp) {
        Timestamp current = this.getResumableTimestamp();
        if (current == null) {
            throw new IllegalStateException("There is othing to fold!");
        } else if (current.compareTo(timestamp) != 0) {
            // It can happen that already registered folding states end up having zero diffs,
            // and we are instructed to continue folding at a timestamp that is higher
            // than the lowest timestamp with a folding state.
            // However, we only do garbage collection in doFoldingState, so now it is time to
            // first clean up those states with zero diffs.
            while (current != null && current.compareTo(timestamp) < 0) {
                final Map<Tuple, FoldingState> tupleMap = this.foldingState.remove(current);
                for (final Entry<Tuple, FoldingState> entry : tupleMap.entrySet()) {
                    final Tuple key = entry.getKey();
                    final FoldingState value = entry.getValue();
                    if (value.diff != 0) {
                        throw new IllegalStateException("Expected zero diff during garbage collection at " + current
                                + ", but the diff was " + value.diff + "!");
                    }
                    doFoldingStep(key, value, current);
                }
                current = this.getResumableTimestamp();
            }
            if (current == null || current.compareTo(timestamp) != 0) {
                throw new IllegalStateException("Expected to continue folding at " + timestamp + "!");
            }
        }

        final Map<Tuple, Diff<Timestamp>> diffMap = CollectionsFactory.createMap();
        final Map<Tuple, FoldingState> tupleMap = this.foldingState.remove(timestamp);
        for (final Entry<Tuple, FoldingState> entry : tupleMap.entrySet()) {
            final Tuple key = entry.getKey();
            final FoldingState value = entry.getValue();
            diffMap.put(key, doFoldingStep(key, value, timestamp));
        }

        if (this.foldingState.get(timestamp) != null) {
            throw new IllegalStateException(
                    "Folding at " + timestamp + " produced more folding work at the same timestamp!");
        }

        return diffMap;
    }

    protected Diff<Timestamp> doFoldingStep(final Tuple tuple, final FoldingState state, final Timestamp timestamp) {
        final CumulativeCounter counter = getCounter(tuple, timestamp);
        if (state.diff == 0) {
            gcCounters(counter, tuple, timestamp);
            return EMPTY_DIFF;
        } else {
            final Diff<Timestamp> resultDiff = new Diff<>();
            final Timestamp nextTimestamp = this.counters.get(tuple).higherKey(timestamp);

            final int oldCumulative = counter.cumulative;

            counter.cumulative += state.diff;

            computeDiffsLazy(state.diff < 0 ? Direction.DELETE : Direction.INSERT, oldCumulative, counter.cumulative,
                    timestamp, nextTimestamp, resultDiff);

            gcCounters(counter, tuple, timestamp);
            updateTimeline(tuple, resultDiff);

            // prepare folding state for next timestamp
            if (nextTimestamp != null) {
                // propagate the incoming diff, not the diff stored in counter
                addFoldingState(tuple, new FoldingState(state.diff), nextTimestamp);
            }

            return resultDiff;
        }
    }

    /**
     * On-demand initializes and returns the counter for the given tuple and timestamp.
     */
    protected CumulativeCounter getCounter(final Tuple tuple, final Timestamp timestamp) {
        final TreeMap<Timestamp, CumulativeCounter> counterTimeline = this.counters.computeIfAbsent(tuple,
                k -> CollectionsFactory.createTreeMap());

        final CumulativeCounter counter = counterTimeline.computeIfAbsent(timestamp, k -> {
            final Entry<Timestamp, CumulativeCounter> previousCounter = counterTimeline.lowerEntry(k);
            final int previousCumulative = previousCounter == null ? 0 : previousCounter.getValue().cumulative;
            return new CumulativeCounter(0, previousCumulative);
        });

        return counter;
    }

    /**
     * Garbage collects the counter of the given tuple and timestamp if the new diff is zero.
     */
    protected void gcCounters(final CumulativeCounter counter, final Tuple tuple, final Timestamp timestamp) {
        if (counter.diff == 0) {
            final TreeMap<Timestamp, CumulativeCounter> counterMap = this.counters.get(tuple);
            counterMap.remove(timestamp);
            if (counterMap.isEmpty()) {
                this.counters.remove(tuple);
            }
        }
    }

    /**
     * Utility method that computes the timeline diffs in case of lazy memories. The diffs will be inserted into the
     * input parameter. This method computes diffs for entire plateaus that spans from timestamp to nextTimestamp.
     *
     * Compared to the eager version of this method, the lazy version makes use of both the old and the new cumulative
     * values because it can happen that the cumulative is incremented by a value that is larger than 1 (as folding
     * states are merged together). This means that we cant decide whether the cumulative became positive by comparing
     * the new value to 1.
     */
    protected void computeDiffsLazy(final Direction direction, final int oldCumulative, final int newCumulative,
            final Timestamp timestamp, final Timestamp nextTimestamp, final Diff<Timestamp> diffs) {
        if (direction == Direction.INSERT) {
            if (newCumulative == 0) {
                throw new IllegalStateException("Cumulative count can never be negative!");
            } else {
                if (oldCumulative == 0 /* current became positive */) {
                    // (1) either we sent out a DELETE before and now we need to cancel it,
                    // (2) or we just INSERT this for the first time
                    diffs.add(new Signed<>(Direction.INSERT, timestamp));
                    if (nextTimestamp != null) {
                        diffs.add(new Signed<>(Direction.DELETE, nextTimestamp));
                    }
                } else /* current stays positive */ {
                    // nothing to do
                }
            }
        } else {
            if (newCumulative < 0) {
                throw new IllegalStateException("Cumulative count can never be negative!");
            } else {
                if (newCumulative == 0 /* current became zero */) {
                    diffs.add(new Signed<>(Direction.DELETE, timestamp));
                    if (nextTimestamp != null) {
                        diffs.add(new Signed<>(Direction.INSERT, nextTimestamp));
                    }
                } else /* current stays positive */ {
                    // nothing to do
                }
            }
        }
    }

    /**
     * Utility method that computes the timeline diffs in case of eager memories. The diffs will be inserted into the
     * input parameter. This method computes diffs that describe momentary changes instead of plateaus. Returns a
     * {@link SignChange} that describes how the sign has changed at the given timestamp.
     */
    protected SignChange computeDiffsEager(final Direction direction, final CumulativeCounter counter,
            final SignChange signChangeAtPrevious, final Timestamp timestamp, final Diff<Timestamp> diffs) {
        if (direction == Direction.INSERT) {
            if (counter.cumulative == 0) {
                throw new IllegalStateException("Cumulative count can never be negative!");
            } else {
                if (counter.cumulative == 1 /* current became positive */) {
                    if (signChangeAtPrevious != SignChange.BECAME_POSITIVE) {
                        // (1) either we sent out a DELETE before and now we need to cancel it,
                        // (2) or we just INSERT this for the first time
                        diffs.add(new Signed<>(Direction.INSERT, timestamp));
                    } else {
                        // we have already emitted this at the previous timestamp
                        // both previous and current became positive
                        throw new IllegalStateException(
                                "This would mean that the diff at current is 0 " + counter.diff);
                    }

                    // remember for next timestamp
                    return SignChange.BECAME_POSITIVE;
                } else /* current stays positive */ {
                    if (signChangeAtPrevious == SignChange.BECAME_POSITIVE) {
                        // we sent out an INSERT before and now the timeline is positive already starting at previous
                        // we need to cancel the effect of this with a DELETE
                        diffs.add(new Signed<>(Direction.DELETE, timestamp));
                    } else {
                        // this is normal, both previous and current was positive and stays positive
                    }

                    // remember for next timestamp
                    return SignChange.IRRELEVANT;
                }
            }
        } else {
            if (counter.cumulative < 0) {
                throw new IllegalStateException("Cumulative count can never be negative!");
            } else {
                if (counter.cumulative == 0 /* current became zero */) {
                    if (signChangeAtPrevious != SignChange.BECAME_ZERO) {
                        // (1) either we sent out a INSERT before and now we need to cancel it,
                        // (2) or we just DELETE this for the first time
                        diffs.add(new Signed<>(Direction.DELETE, timestamp));
                    } else {
                        // we have already emitted this at the previous timestamp
                        // both previous and current became zero
                        throw new IllegalStateException(
                                "This would mean that the diff at current is 0 " + counter.diff);
                    }

                    // remember for next timestamp
                    return SignChange.BECAME_ZERO;
                } else /* current stays positive */ {
                    if (signChangeAtPrevious == SignChange.BECAME_ZERO) {
                        // we sent out a DELETE before and now the timeline is zero already starting at previous
                        // we need to cancel the effect of this with a INSERT
                        diffs.add(new Signed<>(Direction.INSERT, timestamp));
                    } else {
                        // this is normal, both previous and current was positive and stays positive
                    }

                    // remember for next timestamp
                    return SignChange.IRRELEVANT;
                }
            }
        }
    }

    public Diff<Timestamp> put(final Tuple tuple, final Timestamp timestamp) {
        if (this.isLazy) {
            return putLazy(tuple, timestamp);
        } else {
            return putEager(tuple, timestamp);
        }
    }

    public Diff<Timestamp> remove(final Tuple tuple, final Timestamp timestamp) {
        if (this.isLazy) {
            return removeLazy(tuple, timestamp);
        } else {
            return removeEager(tuple, timestamp);
        }
    }

    protected Diff<Timestamp> putEager(final Tuple tuple, final Timestamp timestamp) {
        final Diff<Timestamp> resultDiff = new Diff<>();
        final CumulativeCounter counter = getCounter(tuple, timestamp);
        ++counter.diff;

        // before the INSERT timestamp, no change at all
        // it cannot happen that those became positive in this round
        SignChange signChangeAtPrevious = SignChange.IRRELEVANT;

        final NavigableMap<Timestamp, CumulativeCounter> nextCounters = this.counters.get(tuple).tailMap(timestamp,
                true);
        for (final Entry<Timestamp, CumulativeCounter> currentEntry : nextCounters.entrySet()) {
            final Timestamp currentTimestamp = currentEntry.getKey();
            final CumulativeCounter currentCounter = currentEntry.getValue();
            ++currentCounter.cumulative;
            signChangeAtPrevious = computeDiffsEager(Direction.INSERT, currentCounter, signChangeAtPrevious,
                    currentTimestamp, resultDiff);
        }

        gcCounters(counter, tuple, timestamp);
        updateTimeline(tuple, resultDiff);

        return resultDiff;
    }

    protected Diff<Timestamp> putLazy(final Tuple tuple, final Timestamp timestamp) {
        final CumulativeCounter counter = getCounter(tuple, timestamp);
        counter.diff += 1;
        // before the INSERT timestamp, no change at all
        // it cannot happen that those became positive in this round
        addFoldingState(tuple, new FoldingState(+1), timestamp);
        return EMPTY_DIFF;
    }

    protected Diff<Timestamp> removeEager(final Tuple tuple, final Timestamp timestamp) {
        final Diff<Timestamp> resultDiff = new Diff<>();
        final CumulativeCounter counter = getCounter(tuple, timestamp);
        --counter.diff;

        // before the DELETE timestamp, no change at all
        // it cannot happen that those became zero in this round
        SignChange signChangeAtPrevious = SignChange.IRRELEVANT;

        final NavigableMap<Timestamp, CumulativeCounter> nextCounters = this.counters.get(tuple).tailMap(timestamp,
                true);
        for (final Entry<Timestamp, CumulativeCounter> currentEntry : nextCounters.entrySet()) {
            final Timestamp currentTimestamp = currentEntry.getKey();
            final CumulativeCounter currentCounter = currentEntry.getValue();
            --currentCounter.cumulative;
            signChangeAtPrevious = computeDiffsEager(Direction.DELETE, currentCounter, signChangeAtPrevious,
                    currentTimestamp, resultDiff);
        }

        gcCounters(counter, tuple, timestamp);
        updateTimeline(tuple, resultDiff);

        return resultDiff;
    }

    protected Diff<Timestamp> removeLazy(final Tuple tuple, final Timestamp timestamp) {
        final CumulativeCounter counter = getCounter(tuple, timestamp);
        counter.diff -= 1;
        // before the DELETE timestamp, no change at all
        // it cannot happen that those became zero in this round
        addFoldingState(tuple, new FoldingState(-1), timestamp);
        return EMPTY_DIFF;
    }

    /**
     * Updates and garbage collects the timeline of the given tuple based on the given timeline diff.
     */
    protected void updateTimeline(final Tuple tuple, final Diff<Timestamp> diff) {
        if (!diff.isEmpty()) {
            this.timelines.compute(tuple, (k, oldTimeline) -> {
                this.presentAtInfinity.remove(tuple);
                final Timeline<Timestamp> timeline = oldTimeline == null ? Timelines.createFrom(diff)
                        : oldTimeline.mergeAdditive(diff);
                if (timeline.isPresentAtInfinity()) {
                    this.presentAtInfinity.add(tuple);
                }
                if (timeline.isEmpty()) {
                    return null;
                } else {
                    return timeline;
                }
            });
        }
    }

    /**
     * @since 2.8
     */
    public Set<Tuple> getTuplesAtInfinity() {
        return this.presentAtInfinity;
    }

    /**
     * Returns the number of tuples that are present at the moment 'infinity'.
     */
    public int getCountAtInfinity() {
        return this.presentAtInfinity.size();
    }

    /**
     * Returns true if the given tuple is present at the moment 'infinity'.
     */
    public boolean isPresentAtInfinity(final Tuple tuple) {
        final Timeline<Timestamp> timeline = this.timelines.get(tuple);
        if (timeline == null) {
            return false;
        } else {
            return timeline.isPresentAtInfinity();
        }
    }

    public boolean isEmpty() {
        return this.counters.isEmpty();
    }

    public int size() {
        return this.counters.size();
    }

    public Set<Tuple> keySet() {
        return this.counters.keySet();
    }

    public Map<Tuple, Timeline<Timestamp>> asMap() {
        return this.timelines;
    }

    public Timeline<Timestamp> get(final ITuple tuple) {
        return this.timelines.get(tuple);
    }

    @Override
    public void clear() {
        this.counters.clear();
        this.timelines.clear();
        if (this.foldingState != null) {
            this.foldingState.clear();
        }
    }

    public boolean containsKey(final ITuple tuple) {
        return this.counters.containsKey(tuple);
    }

    @Override
    public String toString() {
        return this.counters + "\n" + this.timelines + "\n" + this.foldingState + "\n";
    }

    protected static final class CumulativeCounter {
        protected int diff;
        protected int cumulative;

        protected CumulativeCounter(final int diff, final int cumulative) {
            this.diff = diff;
            this.cumulative = cumulative;
        }

        @Override
        public String toString() {
            return "{diff=" + this.diff + ", cumulative=" + this.cumulative + "}";
        }

    }

    protected static final class FoldingState {
        protected final int diff;

        protected FoldingState(final int diff) {
            this.diff = diff;
        }

        @Override
        public String toString() {
            return "{diff=" + this.diff + "}";
        }

        /**
         * The returned result will never be null, even if the resulting diff is zero.
         */
        public FoldingState merge(final FoldingState that) {
            Preconditions.checkArgument(that != null);
            return new FoldingState(this.diff + that.diff);
        }

    }

    protected enum SignChange {
        BECAME_POSITIVE, BECAME_ZERO, IRRELEVANT;
    }

}