aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/eval/OutputCachingEvaluatorNode.java
blob: 40a20c4e65ad463f8e67305cdb6021ff2f2f065a (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
/*******************************************************************************
 * Copyright (c) 2010-2013, Bergmann Gabor, Istvan Rath and Daniel Varro
 * 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.eval;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
import tools.refinery.viatra.runtime.matchers.util.Clearable;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
import tools.refinery.viatra.runtime.matchers.util.Direction;
import tools.refinery.viatra.runtime.matchers.util.Signed;
import tools.refinery.viatra.runtime.matchers.util.TimelyMemory;
import tools.refinery.viatra.runtime.matchers.util.timeline.Diff;
import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline;
import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration.TimelineRepresentation;
import tools.refinery.viatra.runtime.rete.network.ReteContainer;
import tools.refinery.viatra.runtime.rete.network.communication.CommunicationGroup;
import tools.refinery.viatra.runtime.rete.network.communication.Timestamp;
import tools.refinery.viatra.runtime.rete.network.communication.timely.ResumableNode;

/**
 * An evaluator node that caches the evaluation result. This node is also capable of caching the timestamps associated
 * with the result tuples if it is used in recursive differential dataflow evaluation.
 * 
 * @author Bergmann Gabor
 * @author Tamas Szabo
 */
public class OutputCachingEvaluatorNode extends AbstractEvaluatorNode implements Clearable, ResumableNode {

    /**
     * @since 2.3
     */
    protected NetworkStructureChangeSensitiveLogic logic;

    /**
     * @since 2.4
     */
    protected Map<Tuple, Iterable<Tuple>> outputCache;

    /**
     * Maps input tuples to timestamps. It is wrong to map evaluation result to timestamps because the different input
     * tuples may yield the same evaluation result. This field is null as long as this node is in a non-recursive group.
     * 
     * @since 2.4
     */
    protected TimelyMemory<Timestamp> memory;

    /**
     * @since 2.4
     */
    protected CommunicationGroup group;

    /**
     * @since 1.5
     */
    public OutputCachingEvaluatorNode(final ReteContainer reteContainer, final EvaluatorCore core) {
        super(reteContainer, core);
        reteContainer.registerClearable(this);
        this.outputCache = CollectionsFactory.createMap();
        this.logic = createLogic();
    }

    @Override
    public CommunicationGroup getCurrentGroup() {
        return this.group;
    }

    @Override
    public void setCurrentGroup(final CommunicationGroup group) {
        this.group = group;
    }

    @Override
    public void networkStructureChanged() {
        super.networkStructureChanged();
        this.logic = createLogic();
    }

    @Override
    public void clear() {
        this.outputCache.clear();
        if (this.memory != null) {
            this.memory.clear();
        }
    }

    /**
     * @since 2.3
     */
    protected NetworkStructureChangeSensitiveLogic createLogic() {
        if (this.reteContainer.isTimelyEvaluation()
                && this.reteContainer.getCommunicationTracker().isInRecursiveGroup(this)) {
            if (this.memory == null) {
                this.memory = new TimelyMemory<Timestamp>(reteContainer.isTimelyEvaluation() && reteContainer
                        .getTimelyConfiguration().getTimelineRepresentation() == TimelineRepresentation.FAITHFUL);
            }
            return TIMELY;
        } else {
            return TIMELESS;
        }
    }

    @Override
    public void pullInto(final Collection<Tuple> collector, final boolean flush) {
        this.logic.pullInto(collector, flush);
    }

    @Override
    public void pullIntoWithTimeline(final Map<Tuple, Timeline<Timestamp>> collector, final boolean flush) {
        this.logic.pullIntoWithTimeline(collector, flush);
    }

    @Override
    public void update(final Direction direction, final Tuple input, final Timestamp timestamp) {
        this.logic.update(direction, input, timestamp);
    }

    /**
     * @since 2.4
     */
    @Override
    public Timestamp getResumableTimestamp() {
        if (this.memory == null) {
            return null;
        } else {
            return this.memory.getResumableTimestamp();
        }
    }

    /**
     * @since 2.4
     */
    @Override
    public void resumeAt(final Timestamp timestamp) {
        this.logic.resumeAt(timestamp);
    }

    /**
     * @since 2.3
     */
    protected static abstract class NetworkStructureChangeSensitiveLogic {

        /**
         * @since 2.4
         */
        public abstract void update(final Direction direction, final Tuple input, final Timestamp timestamp);

        public abstract void pullInto(final Collection<Tuple> collector, final boolean flush);

        /**
         * @since 2.4
         */
        public abstract void pullIntoWithTimeline(final Map<Tuple, Timeline<Timestamp>> collector, final boolean flush);

        /**
         * @since 2.4
         */
        public abstract void resumeAt(final Timestamp timestamp);

    }

    private final NetworkStructureChangeSensitiveLogic TIMELESS = new NetworkStructureChangeSensitiveLogic() {

        @Override
        public void resumeAt(final Timestamp timestamp) {
            // there is nothing to resume in the timeless case because we do not even care about timestamps
        }

        @Override
        public void pullIntoWithTimeline(final Map<Tuple, Timeline<Timestamp>> collector, final boolean flush) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void pullInto(final Collection<Tuple> collector, final boolean flush) {
            for (final Iterable<Tuple> output : outputCache.values()) {
                if (output != NORESULT) {
                    final Iterator<Tuple> itr = output.iterator();
                    while (itr.hasNext()) {
                        collector.add(itr.next());
                    }
                }
            }
        }

        @Override
        public void update(final Direction direction, final Tuple input, final Timestamp timestamp) {
            if (direction == Direction.INSERT) {
                final Iterable<Tuple> output = core.performEvaluation(input);
                if (output != null) {
                    final Iterable<Tuple> previous = outputCache.put(input, output);
                    if (previous != null) {
                        throw new IllegalStateException(
                                String.format("Duplicate insertion of tuple %s into node %s", input, this));
                    }
                    propagateIterableUpdate(direction, output, timestamp);
                }
            } else {
                final Iterable<Tuple> output = outputCache.remove(input);
                if (output != null) {
                    // may be null if no result was yielded
                    propagateIterableUpdate(direction, output, timestamp);
                }
            }
        }
    };

    private final NetworkStructureChangeSensitiveLogic TIMELY = new NetworkStructureChangeSensitiveLogic() {

        @Override
        public void resumeAt(final Timestamp timestamp) {
            final Map<Tuple, Diff<Timestamp>> diffMap = memory.resumeAt(timestamp);

            for (final Entry<Tuple, Diff<Timestamp>> entry : diffMap.entrySet()) {
                final Tuple input = entry.getKey();
                final Iterable<Tuple> output = outputCache.get(input);
                if (output != NORESULT) {
                    for (final Signed<Timestamp> signed : entry.getValue()) {
                        propagateIterableUpdate(signed.getDirection(), output, signed.getPayload());
                    }
                }

                if (memory.get(input) == null) {
                    outputCache.remove(input);
                }
            }

            final Timestamp nextTimestamp = memory.getResumableTimestamp();
            if (nextTimestamp != null) {
                group.notifyHasMessage(mailbox, nextTimestamp);
            }
        }

        @Override
        public void pullIntoWithTimeline(final Map<Tuple, Timeline<Timestamp>> collector, final boolean flush) {
            for (final Entry<Tuple, Timeline<Timestamp>> entry : memory.asMap().entrySet()) {
                final Tuple input = entry.getKey();
                final Iterable<Tuple> output = outputCache.get(input);
                if (output != NORESULT) {
                    final Timeline<Timestamp> timestamp = entry.getValue();
                    final Iterator<Tuple> itr = output.iterator();
                    while (itr.hasNext()) {
                        collector.put(itr.next(), timestamp);
                    }
                }
            }
        }

        @Override
        public void pullInto(final Collection<Tuple> collector, final boolean flush) {
            TIMELESS.pullInto(collector, flush);
        }

        @Override
        public void update(final Direction direction, final Tuple input, final Timestamp timestamp) {
            if (direction == Direction.INSERT) {
                Iterable<Tuple> output = outputCache.get(input);
                if (output == null) {
                    output = core.performEvaluation(input);
                    if (output == null) {
                        // the evaluation result is really null
                        output = NORESULT;
                    }
                    outputCache.put(input, output);
                }
                final Diff<Timestamp> diff = memory.put(input, timestamp);
                if (output != NORESULT) {
                    for (final Signed<Timestamp> signed : diff) {
                        propagateIterableUpdate(signed.getDirection(), output, signed.getPayload());
                    }
                }
            } else {
                final Iterable<Tuple> output = outputCache.get(input);
                final Diff<Timestamp> diff = memory.remove(input, timestamp);
                if (memory.get(input) == null) {
                    outputCache.remove(input);
                }
                if (output != NORESULT) {
                    for (final Signed<Timestamp> signed : diff) {
                        propagateIterableUpdate(signed.getDirection(), output, signed.getPayload());
                    }
                }
            }
        }
    };

    /**
     * This field is used to represent the "null" evaluation result. This is an optimization used in the timely case
     * where the same tuple may be inserted multiple times with different timestamps. This way, we can also cache if
     * something evaluated to null (instead of just forgetting about the previously computed result), thus avoiding the
     * need to re-run a potentially expensive evaluation.
     */
    private static final Iterable<Tuple> NORESULT = Collections
            .singleton(Tuples.staticArityFlatTupleOf(NoResult.INSTANCE));

    private enum NoResult {
        INSTANCE
    }

}