diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/MaskedTupleMemory.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/MaskedTupleMemory.java | 385 |
1 files changed, 385 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/MaskedTupleMemory.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/MaskedTupleMemory.java new file mode 100644 index 00000000..62377624 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/memories/MaskedTupleMemory.java | |||
@@ -0,0 +1,385 @@ | |||
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; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | import java.util.Iterator; | ||
14 | import java.util.Map; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.memories.timely.TimelyDefaultMaskedTupleMemory; | ||
17 | import tools.refinery.viatra.runtime.matchers.memories.timely.TimelyIdentityMaskedTupleMemory; | ||
18 | import tools.refinery.viatra.runtime.matchers.memories.timely.TimelyNullaryMaskedTupleMemory; | ||
19 | import tools.refinery.viatra.runtime.matchers.memories.timely.TimelyUnaryMaskedTupleMemory; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.ITuple; | ||
21 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
22 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.Clearable; | ||
24 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory.MemoryType; | ||
25 | import tools.refinery.viatra.runtime.matchers.util.resumable.MaskedResumable; | ||
26 | import tools.refinery.viatra.runtime.matchers.util.timeline.Diff; | ||
27 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
28 | |||
29 | /** | ||
30 | * Indexes a collection of tuples by their signature (i.e. footprint, projection) obtained according to a mask. May | ||
31 | * belong to an "owner" (for documentation / traceability purposes). | ||
32 | * <p> | ||
33 | * There are timeless and timely versions of the different memories. Timely versions associate {@link Timeline}s with | ||
34 | * the stored tuples. | ||
35 | * | ||
36 | * @noextend This class is not intended to be subclassed by clients. | ||
37 | * @author Gabor Bergmann | ||
38 | * @author Tamas Szabo | ||
39 | * @since 2.0 | ||
40 | */ | ||
41 | public abstract class MaskedTupleMemory<Timestamp extends Comparable<Timestamp>> | ||
42 | implements Clearable, MaskedResumable<Timestamp> { | ||
43 | |||
44 | /** | ||
45 | * Creates a new memory for the given owner that indexes tuples according to the given mask. | ||
46 | */ | ||
47 | public static <T extends Comparable<T>> MaskedTupleMemory<T> create(final TupleMask mask, | ||
48 | final MemoryType bucketType, final Object owner) { | ||
49 | return create(mask, bucketType, owner, false); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Creates a new memory for the given owner that indexes tuples according to the given mask. Clients can specify if | ||
54 | * the created memory should be timely or not. <br> | ||
55 | * <br> | ||
56 | * Timely means that tuples are associated with a timeline. | ||
57 | * | ||
58 | * @since 2.3 | ||
59 | */ | ||
60 | public static <T extends Comparable<T>> MaskedTupleMemory<T> create(final TupleMask mask, | ||
61 | final MemoryType bucketType, final Object owner, final boolean isTimely) { | ||
62 | return create(mask, bucketType, owner, isTimely, false); | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Creates a new memory for the given owner that indexes tuples according to the given mask. Clients can specify if | ||
67 | * the created memory should be timely or not. In case of timely memory, clients can also specify if the memory is | ||
68 | * lazy or not. <br> | ||
69 | * <br> | ||
70 | * Timely means that tuples are associated with a timeline. <br> | ||
71 | * <br> | ||
72 | * Lazyness can only be used together with timely memories. It means that the maintenance of the timelines is lazy, | ||
73 | * that is, the memory only updates its internal data structures at the timestamp affected by an update, and can be | ||
74 | * instructed later to resume the maintenance at higher timestamps, as well. | ||
75 | * | ||
76 | * @since 2.4 | ||
77 | */ | ||
78 | public static <T extends Comparable<T>> MaskedTupleMemory<T> create(final TupleMask mask, | ||
79 | final MemoryType bucketType, final Object owner, final boolean isTimely, final boolean isLazy) { | ||
80 | if (isTimely) { | ||
81 | if (bucketType != MemoryType.SETS) { | ||
82 | throw new IllegalArgumentException("Timely memories only support SETS as the bucket type!"); | ||
83 | } | ||
84 | if (mask.isIdentity()) { | ||
85 | return new TimelyIdentityMaskedTupleMemory<T>(mask, owner, isLazy); | ||
86 | } else if (0 == mask.getSize()) { | ||
87 | return new TimelyNullaryMaskedTupleMemory<T>(mask, owner, isLazy); | ||
88 | } else if (1 == mask.getSize()) { | ||
89 | return new TimelyUnaryMaskedTupleMemory<T>(mask, owner, isLazy); | ||
90 | } else { | ||
91 | return new TimelyDefaultMaskedTupleMemory<T>(mask, owner, isLazy); | ||
92 | } | ||
93 | } else { | ||
94 | if (isLazy) { | ||
95 | throw new IllegalArgumentException("Lazy maintenance is only supported by timely memories!"); | ||
96 | } | ||
97 | if (mask.isIdentity()) { | ||
98 | return new IdentityMaskedTupleMemory<T>(mask, bucketType, owner); | ||
99 | } else if (0 == mask.getSize()) { | ||
100 | return new NullaryMaskedTupleMemory<T>(mask, bucketType, owner); | ||
101 | } else if (1 == mask.getSize()) { | ||
102 | return new UnaryMaskedTupleMemory<T>(mask, bucketType, owner); | ||
103 | } else { | ||
104 | return new DefaultMaskedTupleMemory<T>(mask, bucketType, owner); | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | |||
109 | @Override | ||
110 | public Map<Tuple, Map<Tuple, Diff<Timestamp>>> resumeAt(final Timestamp timestamp) { | ||
111 | throw new UnsupportedOperationException("This is only supported by lazy timely memory implementations!"); | ||
112 | } | ||
113 | |||
114 | @Override | ||
115 | public Iterable<Tuple> getResumableSignatures() { | ||
116 | throw new UnsupportedOperationException("This is only supported by lazy timely memory implementations!"); | ||
117 | } | ||
118 | |||
119 | @Override | ||
120 | public Timestamp getResumableTimestamp() { | ||
121 | return null; | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * Initializes the contents of this memory based on the contents of another memory. The default value is associated | ||
126 | * with each tuple in the timely memories. | ||
127 | * | ||
128 | * @since 2.3 | ||
129 | */ | ||
130 | public void initializeWith(final MaskedTupleMemory<Timestamp> other, final Timestamp defaultValue) { | ||
131 | throw new UnsupportedOperationException("This is only supported by timely memory implementations!"); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Returns true if there is any tuple with the given signature that is present at the timestamp +inf, false | ||
136 | * otherwise. | ||
137 | * @since 2.4 | ||
138 | */ | ||
139 | public boolean isPresentAtInfinity(final ITuple signature) { | ||
140 | return get(signature) != null; | ||
141 | } | ||
142 | |||
143 | /** | ||
144 | * Returns true of this memory is timely, false otherwise. | ||
145 | * | ||
146 | * @since 2.3 | ||
147 | */ | ||
148 | public boolean isTimely() { | ||
149 | return false; | ||
150 | } | ||
151 | |||
152 | /** | ||
153 | * The mask by which the tuples are indexed. | ||
154 | */ | ||
155 | protected final TupleMask mask; | ||
156 | |||
157 | /** | ||
158 | * The object "owning" this memory. May be null. | ||
159 | * | ||
160 | * @since 1.7 | ||
161 | */ | ||
162 | protected final Object owner; | ||
163 | |||
164 | /** | ||
165 | * The node owning this memory. May be null. | ||
166 | * | ||
167 | * @since 2.0 | ||
168 | */ | ||
169 | public Object getOwner() { | ||
170 | return owner; | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * The mask according to which tuples are projected and indexed. | ||
175 | * | ||
176 | * @since 2.0 | ||
177 | */ | ||
178 | public TupleMask getMask() { | ||
179 | return mask; | ||
180 | } | ||
181 | |||
182 | /** | ||
183 | * @return the number of distinct signatures of all stored tuples. | ||
184 | */ | ||
185 | public abstract int getKeysetSize(); | ||
186 | |||
187 | /** | ||
188 | * @return the total number of distinct tuples stored. Multiple copies of the same tuple, if allowed, are counted as | ||
189 | * one. | ||
190 | * | ||
191 | * <p> | ||
192 | * This is currently not cached but computed on demand. It is therefore not efficient, and shall only be | ||
193 | * used for debug / profiling purposes. | ||
194 | */ | ||
195 | public abstract int getTotalSize(); | ||
196 | |||
197 | /** | ||
198 | * Iterates over distinct tuples stored in the memory, regardless of their signatures. | ||
199 | */ | ||
200 | public abstract Iterator<Tuple> iterator(); | ||
201 | |||
202 | /** | ||
203 | * Retrieves a read-only view of exactly those signatures for which at least one tuple is stored | ||
204 | * | ||
205 | * @since 2.0 | ||
206 | */ | ||
207 | public abstract Iterable<Tuple> getSignatures(); | ||
208 | |||
209 | /** | ||
210 | * Retrieves tuples that have the specified signature | ||
211 | * | ||
212 | * @return collection of tuples found, null if none | ||
213 | */ | ||
214 | public abstract Collection<Tuple> get(final ITuple signature); | ||
215 | |||
216 | /** | ||
217 | * Retrieves the tuples and their associated timelines that have the specified signature. | ||
218 | * | ||
219 | * @return the mappings from tuples to timelines, null if there is no mapping for the signature | ||
220 | * @since 2.4 | ||
221 | */ | ||
222 | public abstract Map<Tuple, Timeline<Timestamp>> getWithTimeline(final ITuple signature); | ||
223 | |||
224 | /** | ||
225 | * Retrieves tuples that have the specified signature. | ||
226 | * | ||
227 | * @return collection of tuples found, never null | ||
228 | * @since 2.1 | ||
229 | */ | ||
230 | public Collection<Tuple> getOrEmpty(final ITuple signature) { | ||
231 | final Collection<Tuple> result = get(signature); | ||
232 | return result == null ? Collections.emptySet() : result; | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * Retrieves tuples with their associated timelines that have the specified signature. | ||
237 | * | ||
238 | * @return map of tuples and timelines found, never null | ||
239 | * @since 2.4 | ||
240 | */ | ||
241 | public Map<Tuple, Timeline<Timestamp>> getOrEmptyWithTimeline(final ITuple signature) { | ||
242 | final Map<Tuple, Timeline<Timestamp>> result = getWithTimeline(signature); | ||
243 | return result == null ? Collections.emptyMap() : result; | ||
244 | } | ||
245 | |||
246 | /** | ||
247 | * Removes a tuple occurrence from the memory with the given signature. | ||
248 | * | ||
249 | * @param tuple | ||
250 | * the tuple to be removed from the memory | ||
251 | * @param signature | ||
252 | * precomputed footprint of the tuple according to the mask | ||
253 | * | ||
254 | * @return true if this was the the last occurrence of the signature (according to the mask) | ||
255 | */ | ||
256 | public boolean remove(final Tuple tuple, final Tuple signature) { | ||
257 | throw new UnsupportedOperationException("This is only supported by timeless memory implementations!"); | ||
258 | } | ||
259 | |||
260 | /** | ||
261 | * Removes a tuple occurrence from the memory with the given signature and timestamp. | ||
262 | * | ||
263 | * @param tuple | ||
264 | * the tuple to be removed from the memory | ||
265 | * @param signature | ||
266 | * precomputed footprint of the tuple according to the mask | ||
267 | * @param timestamp | ||
268 | * the timestamp associated with the tuple | ||
269 | * | ||
270 | * @return A {@link Diff} describing how the timeline of the given tuple changed. | ||
271 | * | ||
272 | * @since 2.4 | ||
273 | */ | ||
274 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
275 | throw new UnsupportedOperationException("This is only supported by timely memory implementations!"); | ||
276 | } | ||
277 | |||
278 | /** | ||
279 | * Removes a tuple occurrence from the memory. | ||
280 | * | ||
281 | * @param tuple | ||
282 | * the tuple to be removed from the memory | ||
283 | * | ||
284 | * @return true if this was the the last occurrence of the signature (according to the mask) | ||
285 | */ | ||
286 | public boolean remove(final Tuple tuple) { | ||
287 | throw new UnsupportedOperationException("This is only supported by timeless memory implementations!"); | ||
288 | } | ||
289 | |||
290 | /** | ||
291 | * Removes a tuple occurrence from the memory with the given timestamp. | ||
292 | * | ||
293 | * @param tuple | ||
294 | * the tuple to be removed from the memory | ||
295 | * @param timestamp | ||
296 | * the timestamp associated with the tuple | ||
297 | * | ||
298 | * @return A {@link Diff} describing how the timeline of the given tuple changed. | ||
299 | * | ||
300 | * @since 2.4 | ||
301 | */ | ||
302 | public Diff<Timestamp> removeWithTimestamp(final Tuple tuple, final Timestamp timestamp) { | ||
303 | throw new UnsupportedOperationException("This is only supported by timely memory implementations!"); | ||
304 | } | ||
305 | |||
306 | /** | ||
307 | * Adds a tuple occurrence to the memory with the given signature. | ||
308 | * | ||
309 | * @param tuple | ||
310 | * the tuple to be added to the memory | ||
311 | * @param signature | ||
312 | * precomputed footprint of the tuple according to the mask | ||
313 | * | ||
314 | * @return true if new signature encountered (according to the mask) | ||
315 | */ | ||
316 | public boolean add(final Tuple tuple, final Tuple signature) { | ||
317 | throw new UnsupportedOperationException("This is only supported by timeless memory implementations!"); | ||
318 | } | ||
319 | |||
320 | /** | ||
321 | * Adds a tuple occurrence to the memory with the given signature and timestamp. | ||
322 | * | ||
323 | * @param tuple | ||
324 | * the tuple to be added to the memory | ||
325 | * @param signature | ||
326 | * precomputed footprint of the tuple according to the mask | ||
327 | * @param timestamp | ||
328 | * the timestamp associated with the tuple | ||
329 | * | ||
330 | * @return A {@link Diff} describing how the timeline of the given tuple changed. | ||
331 | * | ||
332 | * @since 2.4 | ||
333 | */ | ||
334 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Tuple signature, final Timestamp timestamp) { | ||
335 | throw new UnsupportedOperationException("This is only supported by timely memory implementations!"); | ||
336 | } | ||
337 | |||
338 | /** | ||
339 | * Adds a tuple occurrence to the memory. | ||
340 | * | ||
341 | * @param tuple | ||
342 | * the tuple to be added to the memory | ||
343 | * | ||
344 | * @return true if new signature encountered (according to the mask) | ||
345 | */ | ||
346 | public boolean add(final Tuple tuple) { | ||
347 | throw new UnsupportedOperationException("This is only supported by timeless memory implementations!"); | ||
348 | } | ||
349 | |||
350 | /** | ||
351 | * Adds a tuple occurrence to the memory with the given timestamp. | ||
352 | * | ||
353 | * @param tuple | ||
354 | * the tuple to be added to the memory | ||
355 | * @param timestamp | ||
356 | * the timestamp associated with the tuple | ||
357 | * | ||
358 | * @return A {@link Diff} describing how the timeline of the given tuple changed. | ||
359 | * | ||
360 | * @since 2.4 | ||
361 | */ | ||
362 | public Diff<Timestamp> addWithTimestamp(final Tuple tuple, final Timestamp timestamp) { | ||
363 | throw new UnsupportedOperationException("This is only supported by timely memory implementations!"); | ||
364 | } | ||
365 | |||
366 | protected MaskedTupleMemory(final TupleMask mask, final Object owner) { | ||
367 | super(); | ||
368 | this.mask = mask; | ||
369 | this.owner = owner; | ||
370 | } | ||
371 | |||
372 | protected IllegalStateException raiseDuplicateInsertion(final Tuple tuple) { | ||
373 | return new IllegalStateException(String.format("Duplicate insertion of tuple %s into %s", tuple, owner)); | ||
374 | } | ||
375 | |||
376 | protected IllegalStateException raiseDuplicateDeletion(final Tuple tuple) { | ||
377 | return new IllegalStateException(String.format("Duplicate deletion of tuple %s from %s", tuple, owner)); | ||
378 | } | ||
379 | |||
380 | @Override | ||
381 | public String toString() { | ||
382 | return getClass().getSimpleName() + "<" + mask + ">@" + owner; | ||
383 | } | ||
384 | |||
385 | } \ No newline at end of file | ||