diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely')
6 files changed, 773 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyMaskedMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyMaskedMemory.java new file mode 100644 index 00000000..45ce3a4e --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyMaskedMemory.java | |||
@@ -0,0 +1,228 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, 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.matchers.memories.timely; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Iterator; | ||
13 | import java.util.Map; | ||
14 | import java.util.Map.Entry; | ||
15 | import java.util.Objects; | ||
16 | import java.util.Set; | ||
17 | import java.util.TreeMap; | ||
18 | |||
19 | import tools.refinery.viatra.runtime.matchers.memories.MaskedTupleMemory; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
21 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
24 | import tools.refinery.viatra.runtime.matchers.util.Signed; | ||
25 | import tools.refinery.viatra.runtime.matchers.util.TimelyMemory; | ||
26 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
27 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
28 | |||
29 | /** | ||
30 | * Common parts of timely default and timely unary implementations. | ||
31 | * | ||
32 | * @noextend This class is not intended to be subclassed by clients. | ||
33 | * @author Tamas Szabo | ||
34 | * @since 2.3 | ||
35 | */ | ||
36 | abstract class AbstractTimelyMaskedMemory<Timestamp extends Comparable<Timestamp>, KeyType> | ||
37 | extends MaskedTupleMemory<Timestamp> { | ||
38 | |||
39 | protected final TreeMap<Timestamp, Set<KeyType>> foldingStates; | ||
40 | protected final Map<KeyType, TimelyMemory<Timestamp>> memoryMap; | ||
41 | protected final boolean isLazy; | ||
42 | |||
43 | public AbstractTimelyMaskedMemory(final TupleMask mask, final Object owner, final boolean isLazy) { | ||
44 | super(mask, owner); | ||
45 | this.isLazy = isLazy; | ||
46 | this.memoryMap = CollectionsFactory.createMap(); | ||
47 | this.foldingStates = this.isLazy ? CollectionsFactory.createTreeMap() : null; | ||
48 | } | ||
49 | |||
50 | @Override | ||
51 | public void initializeWith(final MaskedTupleMemory<Timestamp> other, final Timestamp defaultValue) { | ||
52 | final Iterable<Tuple> signatures = other.getSignatures(); | ||
53 | for (final Tuple signature : signatures) { | ||
54 | if (other.isTimely()) { | ||
55 | final Map<Tuple, Timeline<Timestamp>> tupleMap = other.getWithTimeline(signature); | ||
56 | for (final Entry<Tuple, Timeline<Timestamp>> entry : tupleMap.entrySet()) { | ||
57 | for (final Signed<Timestamp> signed : entry.getValue().asChangeSequence()) { | ||
58 | if (signed.getDirection() == Direction.DELETE) { | ||
59 | this.removeWithTimestamp(entry.getKey(), signed.getPayload()); | ||
60 | } else { | ||
61 | this.addWithTimestamp(entry.getKey(), signed.getPayload()); | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | } else { | ||
66 | final Collection<Tuple> tuples = other.get(signature); | ||
67 | for (final Tuple tuple : tuples) { | ||
68 | this.addWithTimestamp(tuple, defaultValue); | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | public boolean isPresentAtInfinityInteral(KeyType key) { | ||
75 | final TimelyMemory<Timestamp> values = this.memoryMap.get(key); | ||
76 | if (values == null) { | ||
77 | return false; | ||
78 | } else { | ||
79 | return values.getCountAtInfinity() != 0; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public void clear() { | ||
85 | this.memoryMap.clear(); | ||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public int getKeysetSize() { | ||
90 | return this.memoryMap.keySet().size(); | ||
91 | } | ||
92 | |||
93 | @Override | ||
94 | public int getTotalSize() { | ||
95 | int i = 0; | ||
96 | for (final Entry<KeyType, TimelyMemory<Timestamp>> entry : this.memoryMap.entrySet()) { | ||
97 | i += entry.getValue().size(); | ||
98 | } | ||
99 | return i; | ||
100 | } | ||
101 | |||
102 | @Override | ||
103 | public Iterator<Tuple> iterator() { | ||
104 | return this.memoryMap.values().stream().flatMap(e -> e.keySet().stream()).iterator(); | ||
105 | } | ||
106 | |||
107 | protected Collection<Tuple> getInternal(final KeyType key) { | ||
108 | final TimelyMemory<Timestamp> memory = this.memoryMap.get(key); | ||
109 | if (memory == null) { | ||
110 | return null; | ||
111 | } else { | ||
112 | return memory.getTuplesAtInfinity(); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | public Map<Tuple, Timeline<Timestamp>> getWithTimestampInternal(final KeyType key) { | ||
117 | final TimelyMemory<Timestamp> memory = this.memoryMap.get(key); | ||
118 | if (memory == null) { | ||
119 | return null; | ||
120 | } else { | ||
121 | return memory.asMap(); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | protected Diff<Timestamp> removeInternal(final KeyType key, final Tuple tuple, final Timestamp timestamp) { | ||
126 | Timestamp oldResumableTimestamp = null; | ||
127 | Timestamp newResumableTimestamp = null; | ||
128 | |||
129 | final TimelyMemory<Timestamp> keyMemory = this.memoryMap.get(key); | ||
130 | if (keyMemory == null) { | ||
131 | throw raiseDuplicateDeletion(tuple); | ||
132 | } | ||
133 | |||
134 | if (this.isLazy) { | ||
135 | oldResumableTimestamp = keyMemory.getResumableTimestamp(); | ||
136 | } | ||
137 | |||
138 | Diff<Timestamp> diff = null; | ||
139 | try { | ||
140 | diff = keyMemory.remove(tuple, timestamp); | ||
141 | } catch (final IllegalStateException e) { | ||
142 | throw raiseDuplicateDeletion(tuple); | ||
143 | } | ||
144 | if (keyMemory.isEmpty()) { | ||
145 | this.memoryMap.remove(key); | ||
146 | } | ||
147 | |||
148 | if (this.isLazy) { | ||
149 | newResumableTimestamp = keyMemory.getResumableTimestamp(); | ||
150 | if (!Objects.equals(oldResumableTimestamp, newResumableTimestamp)) { | ||
151 | unregisterFoldingState(oldResumableTimestamp, key); | ||
152 | registerFoldingState(newResumableTimestamp, key); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | return diff; | ||
157 | } | ||
158 | |||
159 | protected Diff<Timestamp> addInternal(final KeyType key, final Tuple tuple, final Timestamp timestamp) { | ||
160 | Timestamp oldResumableTimestamp = null; | ||
161 | Timestamp newResumableTimestamp = null; | ||
162 | |||
163 | final TimelyMemory<Timestamp> keyMemory = this.memoryMap.computeIfAbsent(key, | ||
164 | k -> new TimelyMemory<Timestamp>(this.isLazy)); | ||
165 | |||
166 | if (this.isLazy) { | ||
167 | oldResumableTimestamp = keyMemory.getResumableTimestamp(); | ||
168 | } | ||
169 | |||
170 | final Diff<Timestamp> diff = keyMemory.put(tuple, timestamp); | ||
171 | |||
172 | if (this.isLazy) { | ||
173 | newResumableTimestamp = keyMemory.getResumableTimestamp(); | ||
174 | if (!Objects.equals(oldResumableTimestamp, newResumableTimestamp)) { | ||
175 | unregisterFoldingState(oldResumableTimestamp, key); | ||
176 | registerFoldingState(newResumableTimestamp, key); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | return diff; | ||
181 | } | ||
182 | |||
183 | @Override | ||
184 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Timestamp timestamp) { | ||
185 | return removeWithTimestamp(tuple, null, timestamp); | ||
186 | } | ||
187 | |||
188 | @Override | ||
189 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Timestamp timestamp) { | ||
190 | return addWithTimestamp(tuple, null, timestamp); | ||
191 | } | ||
192 | |||
193 | @Override | ||
194 | public boolean isTimely() { | ||
195 | return true; | ||
196 | } | ||
197 | |||
198 | protected void registerFoldingState(final Timestamp timestamp, final KeyType key) { | ||
199 | if (timestamp != null) { | ||
200 | this.foldingStates.compute(timestamp, (k, v) -> { | ||
201 | if (v == null) { | ||
202 | v = CollectionsFactory.createSet(); | ||
203 | } | ||
204 | v.add(key); | ||
205 | return v; | ||
206 | }); | ||
207 | } | ||
208 | } | ||
209 | |||
210 | protected void unregisterFoldingState(final Timestamp timestamp, final KeyType key) { | ||
211 | if (timestamp != null) { | ||
212 | this.foldingStates.compute(timestamp, (k, v) -> { | ||
213 | v.remove(key); | ||
214 | return v.isEmpty() ? null : v; | ||
215 | }); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | @Override | ||
220 | public Timestamp getResumableTimestamp() { | ||
221 | if (this.foldingStates == null || this.foldingStates.isEmpty()) { | ||
222 | return null; | ||
223 | } else { | ||
224 | return this.foldingStates.firstKey(); | ||
225 | } | ||
226 | } | ||
227 | |||
228 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyTrivialMaskedMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyTrivialMaskedMemory.java new file mode 100644 index 00000000..ca06685a --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/AbstractTimelyTrivialMaskedMemory.java | |||
@@ -0,0 +1,100 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, 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.matchers.memories.timely; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Iterator; | ||
13 | import java.util.Map; | ||
14 | import java.util.Map.Entry; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.memories.MaskedTupleMemory; | ||
17 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
19 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
20 | import tools.refinery.viatra.runtime.matchers.util.Signed; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.TimelyMemory; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
24 | |||
25 | /** | ||
26 | * Common parts of timely nullary and timely identity implementations. | ||
27 | * | ||
28 | * @noextend This class is not intended to be subclassed by clients. | ||
29 | * @author Tamas Szabo | ||
30 | * @since 2.3 | ||
31 | */ | ||
32 | abstract class AbstractTimelyTrivialMaskedMemory<Timestamp extends Comparable<Timestamp>> extends MaskedTupleMemory<Timestamp> { | ||
33 | |||
34 | protected final TimelyMemory<Timestamp> memory; | ||
35 | |||
36 | protected AbstractTimelyTrivialMaskedMemory(final TupleMask mask, final Object owner, final boolean isLazy) { | ||
37 | super(mask, owner); | ||
38 | this.memory = new TimelyMemory<Timestamp>(isLazy); | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public void initializeWith(final MaskedTupleMemory<Timestamp> other, final Timestamp defaultValue) { | ||
43 | final Iterable<Tuple> signatures = other.getSignatures(); | ||
44 | for (final Tuple signature : signatures) { | ||
45 | if (other.isTimely()) { | ||
46 | final Map<Tuple, Timeline<Timestamp>> tupleMap = other.getWithTimeline(signature); | ||
47 | for (final Entry<Tuple, Timeline<Timestamp>> entry : tupleMap.entrySet()) { | ||
48 | for (final Signed<Timestamp> signed : entry.getValue().asChangeSequence()) { | ||
49 | if (signed.getDirection() == Direction.DELETE) { | ||
50 | this.removeWithTimestamp(entry.getKey(), signed.getPayload()); | ||
51 | } else { | ||
52 | this.addWithTimestamp(entry.getKey(), signed.getPayload()); | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | } else { | ||
57 | final Collection<Tuple> tuples = other.get(signature); | ||
58 | for (final Tuple tuple : tuples) { | ||
59 | this.removeWithTimestamp(tuple, defaultValue); | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | @Override | ||
66 | public void clear() { | ||
67 | this.memory.clear(); | ||
68 | } | ||
69 | |||
70 | @Override | ||
71 | public int getTotalSize() { | ||
72 | return this.memory.size(); | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public Iterator<Tuple> iterator() { | ||
77 | return this.memory.keySet().iterator(); | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Timestamp timestamp) { | ||
82 | return removeWithTimestamp(tuple, null, timestamp); | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Timestamp timestamp) { | ||
87 | return addWithTimestamp(tuple, null, timestamp); | ||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public boolean isTimely() { | ||
92 | return true; | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public Timestamp getResumableTimestamp() { | ||
97 | return this.memory.getResumableTimestamp(); | ||
98 | } | ||
99 | |||
100 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyDefaultMaskedTupleMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyDefaultMaskedTupleMemory.java new file mode 100644 index 00000000..623d7399 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyDefaultMaskedTupleMemory.java | |||
@@ -0,0 +1,98 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2008 Gabor Bergmann 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 | *******************************************************************************/ | ||
9 | |||
10 | package tools.refinery.viatra.runtime.matchers.memories.timely; | ||
11 | |||
12 | import java.util.Collection; | ||
13 | import java.util.Collections; | ||
14 | import java.util.Map; | ||
15 | import java.util.Set; | ||
16 | |||
17 | import tools.refinery.viatra.runtime.matchers.tuple.ITuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
19 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
20 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.TimelyMemory; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
24 | |||
25 | /** | ||
26 | * Default timely implementation that covers all cases. | ||
27 | * | ||
28 | * @author Tamas Szabo | ||
29 | * @since 2.3 | ||
30 | */ | ||
31 | public final class TimelyDefaultMaskedTupleMemory<Timestamp extends Comparable<Timestamp>> | ||
32 | extends AbstractTimelyMaskedMemory<Timestamp, Tuple> { | ||
33 | |||
34 | public TimelyDefaultMaskedTupleMemory(final TupleMask mask, final Object owner, final boolean isLazy) { | ||
35 | super(mask, owner, isLazy); | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public Iterable<Tuple> getSignatures() { | ||
40 | return this.memoryMap.keySet(); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Tuple signature, | ||
45 | final Timestamp timestamp) { | ||
46 | final Tuple key = mask.transform(tuple); | ||
47 | return removeInternal(key, tuple, timestamp); | ||
48 | } | ||
49 | |||
50 | @Override | ||
51 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Tuple signature, | ||
52 | final Timestamp timestamp) { | ||
53 | final Tuple key = this.mask.transform(tuple); | ||
54 | return addInternal(key, tuple, timestamp); | ||
55 | } | ||
56 | |||
57 | @Override | ||
58 | public Collection<Tuple> get(final ITuple signature) { | ||
59 | return getInternal(signature.toImmutable()); | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public Map<Tuple, Timeline<Timestamp>> getWithTimeline(final ITuple signature) { | ||
64 | return getWithTimestampInternal(signature.toImmutable()); | ||
65 | } | ||
66 | |||
67 | @Override | ||
68 | public boolean isPresentAtInfinity(final ITuple signature) { | ||
69 | return isPresentAtInfinityInteral(signature.toImmutable()); | ||
70 | } | ||
71 | |||
72 | @Override | ||
73 | public Set<Tuple> getResumableSignatures() { | ||
74 | if (this.foldingStates == null || this.foldingStates.isEmpty()) { | ||
75 | return Collections.emptySet(); | ||
76 | } else { | ||
77 | return this.foldingStates.firstEntry().getValue(); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public Map<Tuple, Map<Tuple, Diff<Timestamp>>> resumeAt(final Timestamp timestamp) { | ||
83 | final Map<Tuple, Map<Tuple, Diff<Timestamp>>> result = CollectionsFactory.createMap(); | ||
84 | final Timestamp resumableTimestamp = this.getResumableTimestamp(); | ||
85 | if (resumableTimestamp == null || resumableTimestamp.compareTo(timestamp) != 0) { | ||
86 | throw new IllegalStateException("Expected to continue folding at " + resumableTimestamp + "!"); | ||
87 | } | ||
88 | final Set<Tuple> signatures = this.foldingStates.remove(timestamp); | ||
89 | for (final Tuple signature : signatures) { | ||
90 | final TimelyMemory<Timestamp> memory = this.memoryMap.get(signature); | ||
91 | final Map<Tuple, Diff<Timestamp>> diffMap = memory.resumeAt(resumableTimestamp); | ||
92 | result.put(signature, diffMap); | ||
93 | registerFoldingState(memory.getResumableTimestamp(), signature); | ||
94 | } | ||
95 | return result; | ||
96 | } | ||
97 | |||
98 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyIdentityMaskedTupleMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyIdentityMaskedTupleMemory.java new file mode 100644 index 00000000..568f274d --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyIdentityMaskedTupleMemory.java | |||
@@ -0,0 +1,106 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, 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.matchers.memories.timely; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | import java.util.Map; | ||
14 | import java.util.Map.Entry; | ||
15 | import java.util.Set; | ||
16 | |||
17 | import tools.refinery.viatra.runtime.matchers.tuple.ITuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
19 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
20 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
23 | |||
24 | /** | ||
25 | * Timely specialization for identity mask. | ||
26 | * | ||
27 | * @author Tamas Szabo | ||
28 | * @since 2.3 | ||
29 | */ | ||
30 | public final class TimelyIdentityMaskedTupleMemory<Timestamp extends Comparable<Timestamp>> | ||
31 | extends AbstractTimelyTrivialMaskedMemory<Timestamp> { | ||
32 | |||
33 | public TimelyIdentityMaskedTupleMemory(final TupleMask mask, final Object owner, final boolean isLazy) { | ||
34 | super(mask, owner, isLazy); | ||
35 | if (!mask.isIdentity()) | ||
36 | throw new IllegalArgumentException(mask.toString()); | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public int getKeysetSize() { | ||
41 | return this.memory.size(); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public Iterable<Tuple> getSignatures() { | ||
46 | return this.memory.keySet(); | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public Collection<Tuple> get(final ITuple signature) { | ||
51 | if (this.memory.getTuplesAtInfinity().contains(signature)) { | ||
52 | return Collections.singleton(signature.toImmutable()); | ||
53 | } else { | ||
54 | return null; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public Map<Tuple, Timeline<Timestamp>> getWithTimeline(final ITuple signature) { | ||
60 | final Timeline<Timestamp> value = this.memory.get(signature); | ||
61 | if (value != null) { | ||
62 | return Collections.singletonMap(signature.toImmutable(), value); | ||
63 | } else { | ||
64 | return null; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
70 | try { | ||
71 | return this.memory.remove(tuple, timestamp); | ||
72 | } catch (final IllegalStateException e) { | ||
73 | throw raiseDuplicateDeletion(tuple); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | @Override | ||
78 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
79 | return this.memory.put(tuple, timestamp); | ||
80 | } | ||
81 | |||
82 | @Override | ||
83 | public boolean isPresentAtInfinity(final ITuple signature) { | ||
84 | return this.memory.isPresentAtInfinity(signature.toImmutable()); | ||
85 | } | ||
86 | |||
87 | @Override | ||
88 | public Set<Tuple> getResumableSignatures() { | ||
89 | if (this.memory.getResumableTimestamp() != null) { | ||
90 | return this.memory.getResumableTuples(); | ||
91 | } else { | ||
92 | return Collections.emptySet(); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public Map<Tuple, Map<Tuple, Diff<Timestamp>>> resumeAt(final Timestamp timestamp) { | ||
98 | final Map<Tuple, Diff<Timestamp>> diffMap = this.memory.resumeAt(timestamp); | ||
99 | final Map<Tuple, Map<Tuple, Diff<Timestamp>>> result = CollectionsFactory.createMap(); | ||
100 | for (final Entry<Tuple, Diff<Timestamp>> entry : diffMap.entrySet()) { | ||
101 | result.put(entry.getKey(), Collections.singletonMap(entry.getKey(), entry.getValue())); | ||
102 | } | ||
103 | return result; | ||
104 | } | ||
105 | |||
106 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyNullaryMaskedTupleMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyNullaryMaskedTupleMemory.java new file mode 100644 index 00000000..75987a89 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyNullaryMaskedTupleMemory.java | |||
@@ -0,0 +1,108 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, 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.matchers.memories.timely; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | import java.util.Map; | ||
14 | import java.util.Set; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.tuple.ITuple; | ||
17 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
19 | import tools.refinery.viatra.runtime.matchers.tuple.Tuples; | ||
20 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
22 | |||
23 | /** | ||
24 | * Timely specialization for nullary mask. | ||
25 | * | ||
26 | * @author Tamas Szabo | ||
27 | * @since 2.3 | ||
28 | */ | ||
29 | public final class TimelyNullaryMaskedTupleMemory<Timestamp extends Comparable<Timestamp>> | ||
30 | extends AbstractTimelyTrivialMaskedMemory<Timestamp> { | ||
31 | |||
32 | protected static final Tuple EMPTY_TUPLE = Tuples.staticArityFlatTupleOf(); | ||
33 | protected static final Set<Tuple> UNIT_RELATION = Collections.singleton(EMPTY_TUPLE); | ||
34 | protected static final Set<Tuple> EMPTY_RELATION = Collections.emptySet(); | ||
35 | |||
36 | public TimelyNullaryMaskedTupleMemory(final TupleMask mask, final Object owner, final boolean isLazy) { | ||
37 | super(mask, owner, isLazy); | ||
38 | if (0 != mask.getSize()) { | ||
39 | throw new IllegalArgumentException(mask.toString()); | ||
40 | } | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public int getKeysetSize() { | ||
45 | return this.memory.isEmpty() ? 0 : 1; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public Iterable<Tuple> getSignatures() { | ||
50 | return this.memory.isEmpty() ? EMPTY_RELATION : UNIT_RELATION; | ||
51 | } | ||
52 | |||
53 | @Override | ||
54 | public Collection<Tuple> get(final ITuple signature) { | ||
55 | if (0 == signature.getSize()) { | ||
56 | return this.memory.getTuplesAtInfinity(); | ||
57 | } else { | ||
58 | return null; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public Map<Tuple, Timeline<Timestamp>> getWithTimeline(final ITuple signature) { | ||
64 | if (0 == signature.getSize()) { | ||
65 | return this.memory.asMap(); | ||
66 | } else { | ||
67 | return null; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | @Override | ||
72 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
73 | try { | ||
74 | return this.memory.remove(tuple, timestamp); | ||
75 | } catch (final IllegalStateException e) { | ||
76 | throw raiseDuplicateDeletion(tuple); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
82 | return this.memory.put(tuple, timestamp); | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public boolean isPresentAtInfinity(final ITuple signature) { | ||
87 | if (0 == signature.getSize()) { | ||
88 | return this.memory.getCountAtInfinity() > 0; | ||
89 | } else { | ||
90 | return false; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public Set<Tuple> getResumableSignatures() { | ||
96 | if (this.memory.getResumableTimestamp() != null) { | ||
97 | return UNIT_RELATION; | ||
98 | } else { | ||
99 | return EMPTY_RELATION; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | @Override | ||
104 | public Map<Tuple, Map<Tuple, Diff<Timestamp>>> resumeAt(final Timestamp timestamp) { | ||
105 | return Collections.singletonMap(EMPTY_TUPLE, this.memory.resumeAt(timestamp)); | ||
106 | } | ||
107 | |||
108 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyUnaryMaskedTupleMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyUnaryMaskedTupleMemory.java new file mode 100644 index 00000000..178193af --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/timely/TimelyUnaryMaskedTupleMemory.java | |||
@@ -0,0 +1,133 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, 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.matchers.memories.timely; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | import java.util.Iterator; | ||
14 | import java.util.Map; | ||
15 | import java.util.Set; | ||
16 | |||
17 | import tools.refinery.viatra.runtime.matchers.tuple.ITuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
19 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.Tuples; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.TimelyMemory; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
24 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
25 | |||
26 | /** | ||
27 | * Timely specialization for unary mask. | ||
28 | * | ||
29 | * @author Tamas Szabo | ||
30 | * @since 2.3 | ||
31 | */ | ||
32 | public final class TimelyUnaryMaskedTupleMemory<Timestamp extends Comparable<Timestamp>> | ||
33 | extends AbstractTimelyMaskedMemory<Timestamp, Object> { | ||
34 | |||
35 | protected final int keyPosition; | ||
36 | |||
37 | public TimelyUnaryMaskedTupleMemory(final TupleMask mask, final Object owner, final boolean isLazy) { | ||
38 | super(mask, owner, isLazy); | ||
39 | if (1 != mask.getSize()) | ||
40 | throw new IllegalArgumentException(mask.toString()); | ||
41 | this.keyPosition = mask.indices[0]; | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public Iterable<Tuple> getSignatures() { | ||
46 | return () -> { | ||
47 | final Iterator<Object> wrapped = this.memoryMap.keySet().iterator(); | ||
48 | return new Iterator<Tuple>() { | ||
49 | @Override | ||
50 | public boolean hasNext() { | ||
51 | return wrapped.hasNext(); | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public Tuple next() { | ||
56 | final Object key = wrapped.next(); | ||
57 | return Tuples.staticArityFlatTupleOf(key); | ||
58 | } | ||
59 | }; | ||
60 | }; | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
65 | final Object key = tuple.get(keyPosition); | ||
66 | return removeInternal(key, tuple, timestamp); | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
71 | final Object key = tuple.get(keyPosition); | ||
72 | return addInternal(key, tuple, timestamp); | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public Collection<Tuple> get(final ITuple signature) { | ||
77 | return getInternal(signature.get(0)); | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public Map<Tuple, Timeline<Timestamp>> getWithTimeline(final ITuple signature) { | ||
82 | return getWithTimestampInternal(signature.get(0)); | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public boolean isPresentAtInfinity(ITuple signature) { | ||
87 | return isPresentAtInfinityInteral(signature.get(0)); | ||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public Iterable<Tuple> getResumableSignatures() { | ||
92 | if (this.foldingStates == null || this.foldingStates.isEmpty()) { | ||
93 | return Collections.emptySet(); | ||
94 | } else { | ||
95 | return () -> { | ||
96 | final Iterator<Object> wrapped = this.foldingStates.firstEntry().getValue().iterator(); | ||
97 | return new Iterator<Tuple>() { | ||
98 | @Override | ||
99 | public boolean hasNext() { | ||
100 | return wrapped.hasNext(); | ||
101 | } | ||
102 | |||
103 | @Override | ||
104 | public Tuple next() { | ||
105 | final Object key = wrapped.next(); | ||
106 | return Tuples.staticArityFlatTupleOf(key); | ||
107 | } | ||
108 | }; | ||
109 | }; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | public Map<Tuple, Map<Tuple, Diff<Timestamp>>> resumeAt(final Timestamp timestamp) { | ||
115 | final Map<Tuple, Map<Tuple, Diff<Timestamp>>> result = CollectionsFactory.createMap(); | ||
116 | final Timestamp resumableTimestamp = this.getResumableTimestamp(); | ||
117 | if (resumableTimestamp == null) { | ||
118 | throw new IllegalStateException("There is nothing to fold!"); | ||
119 | } else if (resumableTimestamp.compareTo(timestamp) != 0) { | ||
120 | throw new IllegalStateException("Expected to continue folding at " + resumableTimestamp + "!"); | ||
121 | } | ||
122 | |||
123 | final Set<Object> signatures = this.foldingStates.remove(timestamp); | ||
124 | for (final Object signature : signatures) { | ||
125 | final TimelyMemory<Timestamp> memory = this.memoryMap.get(signature); | ||
126 | final Map<Tuple, Diff<Timestamp>> diffMap = memory.resumeAt(resumableTimestamp); | ||
127 | result.put(Tuples.staticArityFlatTupleOf(signature), diffMap); | ||
128 | registerFoldingState(memory.getResumableTimestamp(), signature); | ||
129 | } | ||
130 | return result; | ||
131 | } | ||
132 | |||
133 | } | ||