aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/TimelyUniquenessEnforcerNode.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/TimelyUniquenessEnforcerNode.java')
-rw-r--r--subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/TimelyUniquenessEnforcerNode.java161
1 files changed, 161 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/TimelyUniquenessEnforcerNode.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/TimelyUniquenessEnforcerNode.java
new file mode 100644
index 00000000..4c4b4fc0
--- /dev/null
+++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/single/TimelyUniquenessEnforcerNode.java
@@ -0,0 +1,161 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2019, Tamas Szabo, Istvan Rath and Daniel Varro
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 *******************************************************************************/
9package tools.refinery.viatra.runtime.rete.single;
10
11import java.util.Collection;
12import java.util.Map;
13import java.util.Map.Entry;
14import java.util.Set;
15
16import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
17import tools.refinery.viatra.runtime.matchers.util.Direction;
18import tools.refinery.viatra.runtime.matchers.util.Signed;
19import tools.refinery.viatra.runtime.matchers.util.TimelyMemory;
20import tools.refinery.viatra.runtime.matchers.util.timeline.Diff;
21import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline;
22import tools.refinery.viatra.runtime.rete.index.ProjectionIndexer;
23import tools.refinery.viatra.runtime.rete.index.timely.TimelyMemoryIdentityIndexer;
24import tools.refinery.viatra.runtime.rete.index.timely.TimelyMemoryNullIndexer;
25import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration.TimelineRepresentation;
26import tools.refinery.viatra.runtime.rete.network.ReteContainer;
27import tools.refinery.viatra.runtime.rete.network.communication.CommunicationGroup;
28import tools.refinery.viatra.runtime.rete.network.communication.Timestamp;
29import tools.refinery.viatra.runtime.rete.network.communication.timely.ResumableNode;
30import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox;
31import tools.refinery.viatra.runtime.rete.network.mailbox.timely.TimelyMailbox;
32
33/**
34 * Timely uniqueness enforcer node implementation.
35 *
36 * @author Tamas Szabo
37 * @noinstantiate This class is not intended to be instantiated by clients.
38 * @noextend This class is not intended to be subclassed by clients.
39 * @since 2.4
40 */
41public class TimelyUniquenessEnforcerNode extends AbstractUniquenessEnforcerNode implements ResumableNode {
42
43 protected final TimelyMemory<Timestamp> memory;
44 /**
45 * @since 2.4
46 */
47 protected CommunicationGroup group;
48
49 public TimelyUniquenessEnforcerNode(final ReteContainer container, final int tupleWidth) {
50 super(container, tupleWidth);
51 this.memory = new TimelyMemory<Timestamp>(
52 container.getTimelyConfiguration().getTimelineRepresentation() == TimelineRepresentation.FAITHFUL);
53 container.registerClearable(this.memory);
54 this.mailbox = instantiateMailbox();
55 container.registerClearable(this.mailbox);
56 }
57
58 protected Mailbox instantiateMailbox() {
59 return new TimelyMailbox(this, this.reteContainer);
60 }
61
62 @Override
63 public void pullInto(final Collection<Tuple> collector, final boolean flush) {
64 for (final Tuple tuple : this.memory.getTuplesAtInfinity()) {
65 collector.add(tuple);
66 }
67 }
68
69 @Override
70 public CommunicationGroup getCurrentGroup() {
71 return this.group;
72 }
73
74 @Override
75 public void setCurrentGroup(final CommunicationGroup group) {
76 this.group = group;
77 }
78
79 @Override
80 public Set<Tuple> getTuples() {
81 return this.memory.getTuplesAtInfinity();
82 }
83
84 /**
85 * @since 2.4
86 */
87 @Override
88 public Timestamp getResumableTimestamp() {
89 return this.memory.getResumableTimestamp();
90 }
91
92 /**
93 * @since 2.4
94 */
95 @Override
96 public void resumeAt(final Timestamp timestamp) {
97 final Map<Tuple, Diff<Timestamp>> diffMap = this.memory.resumeAt(timestamp);
98 for (final Entry<Tuple, Diff<Timestamp>> entry : diffMap.entrySet()) {
99 for (final Signed<Timestamp> signed : entry.getValue()) {
100 propagate(signed.getDirection(), entry.getKey(), signed.getPayload());
101 }
102 }
103 final Timestamp nextTimestamp = this.memory.getResumableTimestamp();
104 if (nextTimestamp != null) {
105 this.group.notifyHasMessage(this.mailbox, nextTimestamp);
106 }
107 }
108
109 @Override
110 public void update(final Direction direction, final Tuple update, final Timestamp timestamp) {
111 Diff<Timestamp> resultDiff = null;
112 if (direction == Direction.INSERT) {
113 resultDiff = this.memory.put(update, timestamp);
114 } else {
115 try {
116 resultDiff = this.memory.remove(update, timestamp);
117 } catch (final IllegalStateException e) {
118 issueError("[INTERNAL ERROR] Duplicate deletion of " + update + " was detected in "
119 + this.getClass().getName() + " " + this + " for pattern(s) "
120 + getTraceInfoPatternsEnumerated(), e);
121 // diff will remain unset in case of the exception, it is time to return
122 return;
123 }
124 }
125
126 for (final Signed<Timestamp> signed : resultDiff) {
127 propagate(signed.getDirection(), update, signed.getPayload());
128 }
129 }
130
131 @Override
132 public void pullIntoWithTimeline(final Map<Tuple, Timeline<Timestamp>> collector, final boolean flush) {
133 collector.putAll(this.memory.asMap());
134 }
135
136 @Override
137 public ProjectionIndexer getNullIndexer() {
138 if (this.memoryNullIndexer == null) {
139 this.memoryNullIndexer = new TimelyMemoryNullIndexer(this.reteContainer, this.tupleWidth, this.memory, this,
140 this, this.specializedListeners);
141 this.getCommunicationTracker().registerDependency(this, this.memoryNullIndexer);
142 }
143 return this.memoryNullIndexer;
144 }
145
146 @Override
147 public ProjectionIndexer getIdentityIndexer() {
148 if (this.memoryIdentityIndexer == null) {
149 this.memoryIdentityIndexer = new TimelyMemoryIdentityIndexer(this.reteContainer, this.tupleWidth,
150 this.memory, this, this, this.specializedListeners);
151 this.getCommunicationTracker().registerDependency(this, this.memoryIdentityIndexer);
152 }
153 return this.memoryIdentityIndexer;
154 }
155
156 @Override
157 public void networkStructureChanged() {
158 super.networkStructureChanged();
159 }
160
161} \ No newline at end of file