aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/TimelessCommunicationTracker.java
blob: 1c18c1cd8cd5f80bd0a317198d183f5ac68c93c3 (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
/*******************************************************************************
 * Copyright (c) 2010-2019, Tamas Szabo, 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.network.communication.timeless;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Map.Entry;

import tools.refinery.viatra.runtime.rete.index.DualInputNode;
import tools.refinery.viatra.runtime.rete.index.Indexer;
import tools.refinery.viatra.runtime.rete.index.IndexerListener;
import tools.refinery.viatra.runtime.rete.index.IterableIndexer;
import tools.refinery.viatra.runtime.rete.network.Node;
import tools.refinery.viatra.runtime.rete.network.Receiver;
import tools.refinery.viatra.runtime.rete.network.RederivableNode;
import tools.refinery.viatra.runtime.rete.network.communication.CommunicationGroup;
import tools.refinery.viatra.runtime.rete.network.communication.CommunicationTracker;
import tools.refinery.viatra.runtime.rete.network.communication.MessageSelector;
import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox;
import tools.refinery.viatra.runtime.rete.network.mailbox.timeless.BehaviorChangingMailbox;

/**
 * Timeless implementation of the communication tracker.
 * 
 * @author Tamas Szabo
 * @since 2.2
 */
public class TimelessCommunicationTracker extends CommunicationTracker {

    @Override
    protected CommunicationGroup createGroup(Node representative, int index) {
        final boolean isSingleton = this.sccInformationProvider.sccs.getPartition(representative).size() == 1;
        final boolean isReceiver = representative instanceof Receiver;
        final boolean isPosetIndifferent = isReceiver
                && ((Receiver) representative).getMailbox() instanceof BehaviorChangingMailbox;
        final boolean isSingletonInDRedMode = isSingleton && (representative instanceof RederivableNode)
                && ((RederivableNode) representative).isInDRedMode();

        CommunicationGroup group = null;
        // we can only use a singleton group iff
        // (1) the SCC has one node AND
        // (2) either we have a poset-indifferent mailbox OR the node is not even a receiver AND
        // (3) the node does not run in DRed mode in a singleton group
        if (isSingleton && (isPosetIndifferent || !isReceiver) && !isSingletonInDRedMode) {
            group = new SingletonCommunicationGroup(this, representative, index);
        } else {
            group = new RecursiveCommunicationGroup(this, representative, index);
        }

        return group;
    }

    @Override
    protected void reconstructQueueContents(final Set<CommunicationGroup> oldActiveGroups) {
        for (final CommunicationGroup oldGroup : oldActiveGroups) {
            for (final Entry<MessageSelector, Collection<Mailbox>> entry : oldGroup.getMailboxes().entrySet()) {
                for (final Mailbox mailbox : entry.getValue()) {
                    final CommunicationGroup newGroup = this.groupMap.get(mailbox.getReceiver());
                    newGroup.notifyHasMessage(mailbox, entry.getKey());
                }
            }

            if (oldGroup instanceof RecursiveCommunicationGroup) {
                for (final RederivableNode node : ((RecursiveCommunicationGroup) oldGroup).getRederivables()) {
                    final CommunicationGroup newGroup = this.groupMap.get(node);
                    if (!(newGroup instanceof RecursiveCommunicationGroup)) {
                        throw new IllegalStateException("The new group must also be recursive! " + newGroup);
                    }
                    ((RecursiveCommunicationGroup) newGroup).addRederivable(node);
                }
            }
        }
    }

    @Override
    public Mailbox proxifyMailbox(final Node requester, final Mailbox original) {
        return original;
    }

    @Override
    public IndexerListener proxifyIndexerListener(final Node requester, final IndexerListener original) {
        return original;
    }

    @Override
    protected void postProcessNode(final Node node) {
        if (node instanceof Receiver) {
            final Mailbox mailbox = ((Receiver) node).getMailbox();
            if (mailbox instanceof BehaviorChangingMailbox) {
                final CommunicationGroup group = this.groupMap.get(node);
                final Set<Node> sccNodes = this.sccInformationProvider.sccs.getPartition(node);
                // a default mailbox must split its messages iff
                // (1) its receiver is in a recursive group and
                final boolean c1 = group.isRecursive();
                // (2) its receiver is at the SCC boundary of that group
                final boolean c2 = isAtSCCBoundary(node);
                // (3) its group consists of more than one node
                final boolean c3 = sccNodes.size() > 1;
                ((BehaviorChangingMailbox) mailbox).setSplitFlag(c1 && c2 && c3);
            }
        }
    }
    
    @Override
    protected void postProcessGroup(final CommunicationGroup group) {
        
    }

    /**
     * @since 2.0
     */
    private boolean isAtSCCBoundary(final Node node) {
        final CommunicationGroup ownGroup = this.groupMap.get(node);
        assert ownGroup != null;
        for (final Node source : this.dependencyGraph.getSourceNodes(node).distinctValues()) {
            final Set<Node> sourcesToCheck = new HashSet<Node>();
            sourcesToCheck.add(source);
            // DualInputNodes must be checked additionally because they do not use a mailbox directly.
            // It can happen that their indexers actually belong to other SCCs.
            if (source instanceof DualInputNode) {
                final DualInputNode dualInput = (DualInputNode) source;
                final IterableIndexer primarySlot = dualInput.getPrimarySlot();
                if (primarySlot != null) {
                    sourcesToCheck.add(primarySlot.getActiveNode());
                }
                final Indexer secondarySlot = dualInput.getSecondarySlot();
                if (secondarySlot != null) {
                    sourcesToCheck.add(secondarySlot.getActiveNode());
                }
            }
            for (final Node current : sourcesToCheck) {
                final CommunicationGroup otherGroup = this.groupMap.get(current);
                assert otherGroup != null;
                if (!ownGroup.equals(otherGroup)) {
                    return true;
                }
            }
        }
        return false;
    }

}