aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/RecursiveCommunicationGroup.java
blob: d8260384ee914b9849541e1a6f07ee5a1bdfd268 (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
/*******************************************************************************
 * 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.Collections;
import java.util.EnumMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
import tools.refinery.viatra.runtime.rete.network.Node;
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.communication.PhasedSelector;
import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox;

/**
 * A communication group representing either a single node where the
 * node is a monotonicity aware one or a set of nodes that form an SCC. 
 * 
 * @author Tamas Szabo
 * @since 2.4
 */
public class RecursiveCommunicationGroup extends CommunicationGroup {

    private final Set<Mailbox> antiMonotoneMailboxes;
    private final Set<Mailbox> monotoneMailboxes;
    private final Set<Mailbox> defaultMailboxes;
    private final Set<RederivableNode> rederivables;
    private boolean currentlyDelivering;

    /**
     * @since 1.7
     */
    public RecursiveCommunicationGroup(final CommunicationTracker tracker, final Node representative, final int identifier) {
        super(tracker, representative, identifier);
        this.antiMonotoneMailboxes = CollectionsFactory.createSet();
        this.monotoneMailboxes = CollectionsFactory.createSet();
        this.defaultMailboxes = CollectionsFactory.createSet();
        this.rederivables = new LinkedHashSet<RederivableNode>();
        this.currentlyDelivering = false;
    }

    @Override
    public void deliverMessages() {
        this.currentlyDelivering = true;
        
        // ANTI-MONOTONE PHASE
        while (!this.antiMonotoneMailboxes.isEmpty() || !this.defaultMailboxes.isEmpty()) {
            while (!this.antiMonotoneMailboxes.isEmpty()) {
                final Mailbox mailbox = this.antiMonotoneMailboxes.iterator().next();
                this.antiMonotoneMailboxes.remove(mailbox);
                mailbox.deliverAll(PhasedSelector.ANTI_MONOTONE);
            }
            while (!this.defaultMailboxes.isEmpty()) {
                final Mailbox mailbox = this.defaultMailboxes.iterator().next();
                this.defaultMailboxes.remove(mailbox);
                mailbox.deliverAll(PhasedSelector.DEFAULT);
            }
        }

        // REDERIVE PHASE
        while (!this.rederivables.isEmpty()) {
            // re-derivable nodes take care of their unregistration!!
            final RederivableNode node = this.rederivables.iterator().next();
            node.rederiveOne();
        }

        // MONOTONE PHASE
        while (!this.monotoneMailboxes.isEmpty() || !this.defaultMailboxes.isEmpty()) {
            while (!this.monotoneMailboxes.isEmpty()) {
                final Mailbox mailbox = this.monotoneMailboxes.iterator().next();
                this.monotoneMailboxes.remove(mailbox);
                mailbox.deliverAll(PhasedSelector.MONOTONE);
            }
            while (!this.defaultMailboxes.isEmpty()) {
                final Mailbox mailbox = this.defaultMailboxes.iterator().next();
                this.defaultMailboxes.remove(mailbox);
                mailbox.deliverAll(PhasedSelector.DEFAULT);
            }
        }
        
        this.currentlyDelivering = false;
    }

    @Override
    public boolean isEmpty() {
        return this.rederivables.isEmpty() && this.antiMonotoneMailboxes.isEmpty()
                && this.monotoneMailboxes.isEmpty() && this.defaultMailboxes.isEmpty();
    }

    @Override
    public void notifyHasMessage(final Mailbox mailbox, final MessageSelector kind) {
        final Collection<Mailbox> mailboxes = getMailboxContainer(kind);
        mailboxes.add(mailbox);
        if (!this.isEnqueued && !this.currentlyDelivering) {
            this.tracker.activateUnenqueued(this);
        }
    }

    @Override
    public void notifyLostAllMessages(final Mailbox mailbox, final MessageSelector kind) {
        final Collection<Mailbox> mailboxes = getMailboxContainer(kind);
        mailboxes.remove(mailbox);
        if (isEmpty()) {
            this.tracker.deactivate(this);
        }
    }

    private Collection<Mailbox> getMailboxContainer(final MessageSelector kind) {
        if (kind == PhasedSelector.ANTI_MONOTONE) {
            return this.antiMonotoneMailboxes;
        } else if (kind == PhasedSelector.MONOTONE) {
            return this.monotoneMailboxes;
        } else if (kind == PhasedSelector.DEFAULT) {
            return this.defaultMailboxes;
        } else {
            throw new IllegalArgumentException(UNSUPPORTED_MESSAGE_KIND + kind);
        }
    }

    public void addRederivable(final RederivableNode node) {
        this.rederivables.add(node);
        if (!this.isEnqueued) {
            this.tracker.activateUnenqueued(this);
        }
    }

    public void removeRederivable(final RederivableNode node) {
        this.rederivables.remove(node);
        if (isEmpty()) {
            this.tracker.deactivate(this);
        }
    }

    public Collection<RederivableNode> getRederivables() {
        return this.rederivables;
    }

    @Override
    public Map<MessageSelector, Collection<Mailbox>> getMailboxes() {
        Map<PhasedSelector, Collection<Mailbox>> map = new EnumMap<>(PhasedSelector.class);
        map.put(PhasedSelector.ANTI_MONOTONE, antiMonotoneMailboxes);
        map.put(PhasedSelector.MONOTONE, monotoneMailboxes);
        map.put(PhasedSelector.DEFAULT, defaultMailboxes);
        return Collections.unmodifiableMap(map);
    }
    
    @Override
    public boolean isRecursive() {
        return true;
    }
    
}