aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/communication/timeless/SingletonCommunicationGroup.java
blob: c51c7dbfdf29b4934ac424cd5cdd23e1900dfe97 (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
/*******************************************************************************
 * 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.Map;

import tools.refinery.viatra.runtime.rete.network.Node;
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 containing only a single node with a single default
 * mailbox.
 * 
 * @author Tamas Szabo
 * @since 1.6
 */
public class SingletonCommunicationGroup extends CommunicationGroup {
    
    private Mailbox mailbox;

    /**
     * @since 1.7
     */
    public SingletonCommunicationGroup(final CommunicationTracker tracker, final Node representative, final int identifier) {
        super(tracker, representative, identifier);
    }

    @Override
    public void deliverMessages() {
        this.mailbox.deliverAll(PhasedSelector.DEFAULT);
    }

    @Override
    public boolean isEmpty() {
        return this.mailbox == null;
    }

    @Override
    public void notifyHasMessage(final Mailbox mailbox, final MessageSelector kind) {
        if (kind == PhasedSelector.DEFAULT) {
            this.mailbox = mailbox;
            if (!this.isEnqueued) {
                this.tracker.activateUnenqueued(this);
            }
        } else {
            throw new IllegalArgumentException(UNSUPPORTED_MESSAGE_KIND + kind);
        }
    }

    @Override
    public void notifyLostAllMessages(final Mailbox mailbox, final MessageSelector kind) {
        if (kind == PhasedSelector.DEFAULT) {
            this.mailbox = null;
            this.tracker.deactivate(this);
        } else {
            throw new IllegalArgumentException(UNSUPPORTED_MESSAGE_KIND + kind);
        }
    }

    @Override
    public Map<MessageSelector, Collection<Mailbox>> getMailboxes() {
        if (mailbox != null) {
            return Collections.singletonMap(PhasedSelector.DEFAULT, Collections.singleton(mailbox));
        } else {
            return Collections.emptyMap();
        }
    }

    @Override
    public boolean isRecursive() {
        return false;
    }
    
}