diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/TimelessCommunicationTracker.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/TimelessCommunicationTracker.java | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/TimelessCommunicationTracker.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/TimelessCommunicationTracker.java new file mode 100644 index 00000000..1c18c1cd --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/TimelessCommunicationTracker.java | |||
@@ -0,0 +1,149 @@ | |||
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.communication.timeless; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.HashSet; | ||
13 | import java.util.Set; | ||
14 | import java.util.Map.Entry; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.rete.index.DualInputNode; | ||
17 | import tools.refinery.viatra.runtime.rete.index.Indexer; | ||
18 | import tools.refinery.viatra.runtime.rete.index.IndexerListener; | ||
19 | import tools.refinery.viatra.runtime.rete.index.IterableIndexer; | ||
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.RederivableNode; | ||
23 | import tools.refinery.viatra.runtime.rete.network.communication.CommunicationGroup; | ||
24 | import tools.refinery.viatra.runtime.rete.network.communication.CommunicationTracker; | ||
25 | import tools.refinery.viatra.runtime.rete.network.communication.MessageSelector; | ||
26 | import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox; | ||
27 | import tools.refinery.viatra.runtime.rete.network.mailbox.timeless.BehaviorChangingMailbox; | ||
28 | |||
29 | /** | ||
30 | * Timeless implementation of the communication tracker. | ||
31 | * | ||
32 | * @author Tamas Szabo | ||
33 | * @since 2.2 | ||
34 | */ | ||
35 | public class TimelessCommunicationTracker extends CommunicationTracker { | ||
36 | |||
37 | @Override | ||
38 | protected CommunicationGroup createGroup(Node representative, int index) { | ||
39 | final boolean isSingleton = this.sccInformationProvider.sccs.getPartition(representative).size() == 1; | ||
40 | final boolean isReceiver = representative instanceof Receiver; | ||
41 | final boolean isPosetIndifferent = isReceiver | ||
42 | && ((Receiver) representative).getMailbox() instanceof BehaviorChangingMailbox; | ||
43 | final boolean isSingletonInDRedMode = isSingleton && (representative instanceof RederivableNode) | ||
44 | && ((RederivableNode) representative).isInDRedMode(); | ||
45 | |||
46 | CommunicationGroup group = null; | ||
47 | // we can only use a singleton group iff | ||
48 | // (1) the SCC has one node AND | ||
49 | // (2) either we have a poset-indifferent mailbox OR the node is not even a receiver AND | ||
50 | // (3) the node does not run in DRed mode in a singleton group | ||
51 | if (isSingleton && (isPosetIndifferent || !isReceiver) && !isSingletonInDRedMode) { | ||
52 | group = new SingletonCommunicationGroup(this, representative, index); | ||
53 | } else { | ||
54 | group = new RecursiveCommunicationGroup(this, representative, index); | ||
55 | } | ||
56 | |||
57 | return group; | ||
58 | } | ||
59 | |||
60 | @Override | ||
61 | protected void reconstructQueueContents(final Set<CommunicationGroup> oldActiveGroups) { | ||
62 | for (final CommunicationGroup oldGroup : oldActiveGroups) { | ||
63 | for (final Entry<MessageSelector, Collection<Mailbox>> entry : oldGroup.getMailboxes().entrySet()) { | ||
64 | for (final Mailbox mailbox : entry.getValue()) { | ||
65 | final CommunicationGroup newGroup = this.groupMap.get(mailbox.getReceiver()); | ||
66 | newGroup.notifyHasMessage(mailbox, entry.getKey()); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | if (oldGroup instanceof RecursiveCommunicationGroup) { | ||
71 | for (final RederivableNode node : ((RecursiveCommunicationGroup) oldGroup).getRederivables()) { | ||
72 | final CommunicationGroup newGroup = this.groupMap.get(node); | ||
73 | if (!(newGroup instanceof RecursiveCommunicationGroup)) { | ||
74 | throw new IllegalStateException("The new group must also be recursive! " + newGroup); | ||
75 | } | ||
76 | ((RecursiveCommunicationGroup) newGroup).addRederivable(node); | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | @Override | ||
83 | public Mailbox proxifyMailbox(final Node requester, final Mailbox original) { | ||
84 | return original; | ||
85 | } | ||
86 | |||
87 | @Override | ||
88 | public IndexerListener proxifyIndexerListener(final Node requester, final IndexerListener original) { | ||
89 | return original; | ||
90 | } | ||
91 | |||
92 | @Override | ||
93 | protected void postProcessNode(final Node node) { | ||
94 | if (node instanceof Receiver) { | ||
95 | final Mailbox mailbox = ((Receiver) node).getMailbox(); | ||
96 | if (mailbox instanceof BehaviorChangingMailbox) { | ||
97 | final CommunicationGroup group = this.groupMap.get(node); | ||
98 | final Set<Node> sccNodes = this.sccInformationProvider.sccs.getPartition(node); | ||
99 | // a default mailbox must split its messages iff | ||
100 | // (1) its receiver is in a recursive group and | ||
101 | final boolean c1 = group.isRecursive(); | ||
102 | // (2) its receiver is at the SCC boundary of that group | ||
103 | final boolean c2 = isAtSCCBoundary(node); | ||
104 | // (3) its group consists of more than one node | ||
105 | final boolean c3 = sccNodes.size() > 1; | ||
106 | ((BehaviorChangingMailbox) mailbox).setSplitFlag(c1 && c2 && c3); | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | |||
111 | @Override | ||
112 | protected void postProcessGroup(final CommunicationGroup group) { | ||
113 | |||
114 | } | ||
115 | |||
116 | /** | ||
117 | * @since 2.0 | ||
118 | */ | ||
119 | private boolean isAtSCCBoundary(final Node node) { | ||
120 | final CommunicationGroup ownGroup = this.groupMap.get(node); | ||
121 | assert ownGroup != null; | ||
122 | for (final Node source : this.dependencyGraph.getSourceNodes(node).distinctValues()) { | ||
123 | final Set<Node> sourcesToCheck = new HashSet<Node>(); | ||
124 | sourcesToCheck.add(source); | ||
125 | // DualInputNodes must be checked additionally because they do not use a mailbox directly. | ||
126 | // It can happen that their indexers actually belong to other SCCs. | ||
127 | if (source instanceof DualInputNode) { | ||
128 | final DualInputNode dualInput = (DualInputNode) source; | ||
129 | final IterableIndexer primarySlot = dualInput.getPrimarySlot(); | ||
130 | if (primarySlot != null) { | ||
131 | sourcesToCheck.add(primarySlot.getActiveNode()); | ||
132 | } | ||
133 | final Indexer secondarySlot = dualInput.getSecondarySlot(); | ||
134 | if (secondarySlot != null) { | ||
135 | sourcesToCheck.add(secondarySlot.getActiveNode()); | ||
136 | } | ||
137 | } | ||
138 | for (final Node current : sourcesToCheck) { | ||
139 | final CommunicationGroup otherGroup = this.groupMap.get(current); | ||
140 | assert otherGroup != null; | ||
141 | if (!ownGroup.equals(otherGroup)) { | ||
142 | return true; | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | return false; | ||
147 | } | ||
148 | |||
149 | } | ||