diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/delayed/DelayedCommand.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/delayed/DelayedCommand.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/delayed/DelayedCommand.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/delayed/DelayedCommand.java new file mode 100644 index 00000000..d6312671 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/delayed/DelayedCommand.java | |||
@@ -0,0 +1,81 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2019, Tamas Szabo, Istvan Rath and Daniel Varro | ||
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.network.delayed; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Map; | ||
13 | import java.util.Map.Entry; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
16 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
17 | import tools.refinery.viatra.runtime.matchers.util.Signed; | ||
18 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
19 | import tools.refinery.viatra.runtime.rete.network.Network; | ||
20 | import tools.refinery.viatra.runtime.rete.network.Node; | ||
21 | import tools.refinery.viatra.runtime.rete.network.Receiver; | ||
22 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
23 | import tools.refinery.viatra.runtime.rete.network.Supplier; | ||
24 | import tools.refinery.viatra.runtime.rete.network.communication.CommunicationTracker; | ||
25 | import tools.refinery.viatra.runtime.rete.network.communication.Timestamp; | ||
26 | import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox; | ||
27 | |||
28 | /** | ||
29 | * Instances of this class are responsible for initializing a {@link Receiver} with the contents of a {@link Supplier}. | ||
30 | * However, due to the dynamic nature of the Rete {@link Network} and to the fact that certain {@link Node}s in the | ||
31 | * {@link Network} are sensitive to the shape of the {@link Network}, the commands must be delayed until the | ||
32 | * construction of the {@link Network} has stabilized. | ||
33 | * | ||
34 | * @author Tamas Szabo | ||
35 | * @since 2.3 | ||
36 | */ | ||
37 | public abstract class DelayedCommand implements Runnable { | ||
38 | |||
39 | protected final Supplier supplier; | ||
40 | protected final Receiver receiver; | ||
41 | protected final Direction direction; | ||
42 | protected final ReteContainer container; | ||
43 | |||
44 | public DelayedCommand(final Supplier supplier, final Receiver receiver, final Direction direction, | ||
45 | final ReteContainer container) { | ||
46 | this.supplier = supplier; | ||
47 | this.receiver = receiver; | ||
48 | this.direction = direction; | ||
49 | this.container = container; | ||
50 | } | ||
51 | |||
52 | @Override | ||
53 | public void run() { | ||
54 | final CommunicationTracker tracker = this.container.getCommunicationTracker(); | ||
55 | final Mailbox mailbox = tracker.proxifyMailbox(this.supplier, this.receiver.getMailbox()); | ||
56 | |||
57 | if (this.isTimestampAware()) { | ||
58 | final Map<Tuple, Timeline<Timestamp>> contents = this.container.pullContentsWithTimeline(this.supplier, | ||
59 | false); | ||
60 | for (final Entry<Tuple, Timeline<Timestamp>> entry : contents.entrySet()) { | ||
61 | for (final Signed<Timestamp> change : entry.getValue().asChangeSequence()) { | ||
62 | mailbox.postMessage(change.getDirection().multiply(this.direction), entry.getKey(), | ||
63 | change.getPayload()); | ||
64 | } | ||
65 | } | ||
66 | } else { | ||
67 | final Collection<Tuple> contents = this.container.pullContents(this.supplier, false); | ||
68 | for (final Tuple tuple : contents) { | ||
69 | mailbox.postMessage(this.direction, tuple, Timestamp.ZERO); | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public String toString() { | ||
76 | return this.supplier + " -> " + this.receiver.toString(); | ||
77 | } | ||
78 | |||
79 | protected abstract boolean isTimestampAware(); | ||
80 | |||
81 | } | ||