aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/mailbox/timeless/AbstractUpdateSplittingMailbox.java
blob: 1e1ada71129eb12b17c176e8c25a64bfe8ad91fe (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
/*******************************************************************************
 * Copyright (c) 2010-2018, 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.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.indexer.MessageIndexer;
import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox;
import tools.refinery.viatra.runtime.rete.network.mailbox.MessageIndexerFactory;

/**
 * An abstract mailbox implementation that is capable of splitting update messages based on some form of monotonicity
 * (anti-monotone and monotone). The monotonicity is either defined by the less or equal operator of a poset or, it can
 * be the standard subset ordering among sets of tuples.
 * 
 * @author Tamas Szabo
 * @since 2.0
 *
 */
public abstract class AbstractUpdateSplittingMailbox<IndexerType extends MessageIndexer, ReceiverType extends Receiver> implements Mailbox {

    protected IndexerType monotoneQueue;
    protected IndexerType antiMonotoneQueue;
    protected IndexerType monotoneBuffer;
    protected IndexerType antiMonotoneBuffer;
    protected boolean deliveringMonotone;
    protected boolean deliveringAntiMonotone;
    protected final ReceiverType receiver;
    protected final ReteContainer container;
    protected CommunicationGroup group;

    public AbstractUpdateSplittingMailbox(final ReceiverType receiver, final ReteContainer container,
            final MessageIndexerFactory<IndexerType> factory) {
        this.receiver = receiver;
        this.container = container;
        this.monotoneQueue = factory.create();
        this.antiMonotoneQueue = factory.create();
        this.monotoneBuffer = factory.create();
        this.antiMonotoneBuffer = factory.create();
        this.deliveringMonotone = false;
        this.deliveringAntiMonotone = false;
    }

    protected void swapAndClearMonotone() {
        final IndexerType tmp = this.monotoneQueue;
        this.monotoneQueue = this.monotoneBuffer;
        this.monotoneBuffer = tmp;
        this.monotoneBuffer.clear();
    }

    protected void swapAndClearAntiMonotone() {
        final IndexerType tmp = this.antiMonotoneQueue;
        this.antiMonotoneQueue = this.antiMonotoneBuffer;
        this.antiMonotoneBuffer = tmp;
        this.antiMonotoneBuffer.clear();
    }

    protected IndexerType getActiveMonotoneQueue() {
        if (this.deliveringMonotone) {
            return this.monotoneBuffer;
        } else {
            return this.monotoneQueue;
        }
    }

    protected IndexerType getActiveAntiMonotoneQueue() {
        if (this.deliveringAntiMonotone) {
            return this.antiMonotoneBuffer;
        } else {
            return this.antiMonotoneQueue;
        }
    }

    @Override
    public ReceiverType getReceiver() {
        return this.receiver;
    }

    @Override
    public void clear() {
        this.monotoneQueue.clear();
        this.antiMonotoneQueue.clear();
        this.monotoneBuffer.clear();
        this.antiMonotoneBuffer.clear();
    }

    @Override
    public boolean isEmpty() {
        return this.getActiveMonotoneQueue().isEmpty() && this.getActiveAntiMonotoneQueue().isEmpty();
    }

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

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