aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/mailbox/timeless/BehaviorChangingMailbox.java
blob: fe822d7ce632bf7b708166ffb0fbc0e1a52ac645 (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
/*******************************************************************************
 * Copyright (c) 2010-2016, 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.mailbox.timeless;

import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.util.Direction;
import tools.refinery.viatra.runtime.rete.network.Node;
import tools.refinery.viatra.runtime.rete.network.Receiver;
import tools.refinery.viatra.runtime.rete.network.ReteContainer;
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.Timestamp;
import tools.refinery.viatra.runtime.rete.network.communication.timeless.TimelessCommunicationTracker;
import tools.refinery.viatra.runtime.rete.network.mailbox.AdaptableMailbox;
import tools.refinery.viatra.runtime.rete.network.mailbox.FallThroughCapableMailbox;

/**
 * This mailbox changes its behavior based on the position of its {@link Receiver} in the network topology. 
 * It either behaves as a {@link DefaultMailbox} or as an {@link UpdateSplittingMailbox}. The decision is made by the
 * {@link CommunicationTracker}, see {@link TimelessCommunicationTracker#postProcessNode(Node)} for more details.
 * 
 * @author Tamas Szabo
 */
public class BehaviorChangingMailbox implements FallThroughCapableMailbox {

    protected boolean fallThrough;
    protected boolean split;
    protected AdaptableMailbox wrapped;
    protected final Receiver receiver;
    protected final ReteContainer container;
    protected CommunicationGroup group;

    public BehaviorChangingMailbox(final Receiver receiver, final ReteContainer container) {
        this.fallThrough = false;
        this.split = false;
        this.receiver = receiver;
        this.container = container;
        this.wrapped = new DefaultMailbox(receiver, container);
        this.wrapped.setAdapter(this);
    }

    @Override
    public void postMessage(final Direction direction, final Tuple update, final Timestamp timestamp) {
        if (this.fallThrough && !this.container.isExecutingDelayedCommands()) {
            // disable fall through while we are in the middle of executing delayed construction commands
            this.receiver.update(direction, update, timestamp);
        } else {
            this.wrapped.postMessage(direction, update, timestamp);
        }
    }

    @Override
    public void deliverAll(final MessageSelector kind) {
        this.wrapped.deliverAll(kind);
    }

    @Override
    public String toString() {
        return "A_MBOX -> " + this.wrapped;
    }

    public void setSplitFlag(final boolean splitValue) {
        if (this.split != splitValue) {
            assert isEmpty();
            if (splitValue) {
                this.wrapped = new UpdateSplittingMailbox(this.receiver, this.container);
            } else {
                this.wrapped = new DefaultMailbox(this.receiver, this.container);
            }
            this.wrapped.setAdapter(this);
            this.split = splitValue;
        }
    }

    @Override
    public boolean isEmpty() {
        return this.wrapped.isEmpty();
    }

    @Override
    public void clear() {
        this.wrapped.clear();
    }

    @Override
    public Receiver getReceiver() {
        return this.receiver;
    }
    
    @Override
    public CommunicationGroup getCurrentGroup() {
        return this.group;
    }

    @Override
    public void setCurrentGroup(final CommunicationGroup group) {
        this.group = group;
    }

    @Override
    public boolean isFallThrough() {
        return this.fallThrough;
    }
    
    @Override
    public void setFallThrough(final boolean fallThrough) {
        this.fallThrough = fallThrough;
    }
    
}