diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/AbstractUniquenessEnforcerNode.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/AbstractUniquenessEnforcerNode.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/AbstractUniquenessEnforcerNode.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/AbstractUniquenessEnforcerNode.java new file mode 100644 index 00000000..e92ce63f --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/AbstractUniquenessEnforcerNode.java | |||
@@ -0,0 +1,138 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2019, Tamas Szabo, itemis AG, Gabor Bergmann, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.rete.single; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Collection; | ||
13 | import java.util.List; | ||
14 | import java.util.Set; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
17 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
18 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
19 | import tools.refinery.viatra.runtime.rete.index.ProjectionIndexer; | ||
20 | import tools.refinery.viatra.runtime.rete.index.SpecializedProjectionIndexer.ListenerSubscription; | ||
21 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
22 | import tools.refinery.viatra.runtime.rete.network.StandardNode; | ||
23 | import tools.refinery.viatra.runtime.rete.network.Supplier; | ||
24 | import tools.refinery.viatra.runtime.rete.network.Tunnel; | ||
25 | import tools.refinery.viatra.runtime.rete.network.communication.Timestamp; | ||
26 | import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox; | ||
27 | import tools.refinery.viatra.runtime.rete.traceability.TraceInfo; | ||
28 | import tools.refinery.viatra.runtime.rete.util.Options; | ||
29 | |||
30 | /** | ||
31 | * Ensures that no identical copies get to the output. Only one replica of each pattern substitution may traverse this | ||
32 | * node. There are both timeless and timely implementations. | ||
33 | * | ||
34 | * @author Gabor Bergmann | ||
35 | * @author Tamas Szabo | ||
36 | * @noinstantiate This class is not intended to be instantiated by clients. | ||
37 | * @noextend This class is not intended to be subclassed by clients. | ||
38 | * @since 2.2 | ||
39 | */ | ||
40 | public abstract class AbstractUniquenessEnforcerNode extends StandardNode implements Tunnel { | ||
41 | |||
42 | protected final Collection<Supplier> parents; | ||
43 | protected ProjectionIndexer memoryNullIndexer; | ||
44 | protected ProjectionIndexer memoryIdentityIndexer; | ||
45 | protected final int tupleWidth; | ||
46 | // MUST BE INSTANTIATED IN THE CONCRETE SUBCLASSES AFTER ALL FIELDS ARE SET | ||
47 | protected Mailbox mailbox; | ||
48 | protected final TupleMask nullMask; | ||
49 | protected final TupleMask identityMask; | ||
50 | protected final List<ListenerSubscription> specializedListeners; | ||
51 | |||
52 | public AbstractUniquenessEnforcerNode(final ReteContainer reteContainer, final int tupleWidth) { | ||
53 | super(reteContainer); | ||
54 | this.parents = new ArrayList<Supplier>(); | ||
55 | this.specializedListeners = new ArrayList<ListenerSubscription>(); | ||
56 | this.tupleWidth = tupleWidth; | ||
57 | this.nullMask = TupleMask.linear(0, tupleWidth); | ||
58 | this.identityMask = TupleMask.identity(tupleWidth); | ||
59 | } | ||
60 | |||
61 | protected abstract Mailbox instantiateMailbox(); | ||
62 | |||
63 | @Override | ||
64 | public Mailbox getMailbox() { | ||
65 | return this.mailbox; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * @since 2.8 | ||
70 | */ | ||
71 | public abstract Set<Tuple> getTuples(); | ||
72 | |||
73 | /** | ||
74 | * @since 2.4 | ||
75 | */ | ||
76 | protected void propagate(final Direction direction, final Tuple update, final Timestamp timestamp) { | ||
77 | // See Bug 518434 | ||
78 | // trivial (non-active) indexers must be updated before other listeners | ||
79 | // so that if they are joined against each other, trivial indexers lookups | ||
80 | // will be consistent with their notifications; | ||
81 | // also, their subscriptions must share a single order | ||
82 | for (final ListenerSubscription subscription : specializedListeners) { | ||
83 | subscription.propagate(direction, update, timestamp); | ||
84 | } | ||
85 | propagateUpdate(direction, update, timestamp); | ||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public ProjectionIndexer constructIndex(final TupleMask mask, final TraceInfo... traces) { | ||
90 | if (Options.employTrivialIndexers) { | ||
91 | if (nullMask.equals(mask)) { | ||
92 | final ProjectionIndexer indexer = getNullIndexer(); | ||
93 | for (final TraceInfo traceInfo : traces) { | ||
94 | indexer.assignTraceInfo(traceInfo); | ||
95 | } | ||
96 | return indexer; | ||
97 | } | ||
98 | if (identityMask.equals(mask)) { | ||
99 | final ProjectionIndexer indexer = getIdentityIndexer(); | ||
100 | for (final TraceInfo traceInfo : traces) { | ||
101 | indexer.assignTraceInfo(traceInfo); | ||
102 | } | ||
103 | return indexer; | ||
104 | } | ||
105 | } | ||
106 | return super.constructIndex(mask, traces); | ||
107 | } | ||
108 | |||
109 | public abstract ProjectionIndexer getNullIndexer(); | ||
110 | |||
111 | public abstract ProjectionIndexer getIdentityIndexer(); | ||
112 | |||
113 | @Override | ||
114 | public void appendParent(final Supplier supplier) { | ||
115 | parents.add(supplier); | ||
116 | } | ||
117 | |||
118 | @Override | ||
119 | public void removeParent(final Supplier supplier) { | ||
120 | parents.remove(supplier); | ||
121 | } | ||
122 | |||
123 | @Override | ||
124 | public Collection<Supplier> getParents() { | ||
125 | return parents; | ||
126 | } | ||
127 | |||
128 | @Override | ||
129 | public void assignTraceInfo(final TraceInfo traceInfo) { | ||
130 | super.assignTraceInfo(traceInfo); | ||
131 | if (traceInfo.propagateFromStandardNodeToSupplierParent()) { | ||
132 | for (final Supplier parent : parents) { | ||
133 | parent.acceptPropagatedTraceInfo(traceInfo); | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | |||
138 | } \ No newline at end of file | ||