diff options
Diffstat (limited to 'subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/memories/DefaultMaskedTupleMemory.java')
-rw-r--r-- | subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/memories/DefaultMaskedTupleMemory.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/memories/DefaultMaskedTupleMemory.java b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/memories/DefaultMaskedTupleMemory.java new file mode 100644 index 00000000..92081409 --- /dev/null +++ b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/memories/DefaultMaskedTupleMemory.java | |||
@@ -0,0 +1,127 @@ | |||
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; | ||
11 | |||
12 | import java.util.Collection; | ||
13 | import java.util.Iterator; | ||
14 | import java.util.Map; | ||
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.util.CollectionsFactory; | ||
20 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory.MemoryType; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.IMemoryView; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.IMultiLookup; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.IMultiLookup.ChangeGranularity; | ||
24 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
25 | |||
26 | /** | ||
27 | * @author Gabor Bergmann | ||
28 | * | ||
29 | * Default implementation that covers all cases. | ||
30 | * | ||
31 | * @since 2.0 | ||
32 | */ | ||
33 | public final class DefaultMaskedTupleMemory<Timestamp extends Comparable<Timestamp>> | ||
34 | extends MaskedTupleMemory<Timestamp> { | ||
35 | /** | ||
36 | * Maps a signature tuple to the bucket of tuples with the given signature. | ||
37 | * | ||
38 | * @since 2.0 | ||
39 | */ | ||
40 | protected IMultiLookup<Tuple, Tuple> signatureToTuples; | ||
41 | |||
42 | /** | ||
43 | * @param mask | ||
44 | * The mask used to index the matchings | ||
45 | * @param owner | ||
46 | * the object "owning" this memory | ||
47 | * @param bucketType | ||
48 | * the kind of tuple collection maintained for each indexer bucket | ||
49 | * @since 2.0 | ||
50 | */ | ||
51 | public DefaultMaskedTupleMemory(TupleMask mask, MemoryType bucketType, Object owner) { | ||
52 | super(mask, owner); | ||
53 | signatureToTuples = CollectionsFactory.<Tuple, Tuple> createMultiLookup(Object.class, bucketType, Object.class); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public boolean add(Tuple tuple) { | ||
58 | Tuple signature = mask.transform(tuple); | ||
59 | return add(tuple, signature); | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public boolean add(Tuple tuple, Tuple signature) { | ||
64 | try { | ||
65 | return signatureToTuples.addPair(signature, tuple) == ChangeGranularity.KEY; | ||
66 | } catch (IllegalStateException ex) { // ignore worthless internal exception details | ||
67 | throw raiseDuplicateInsertion(tuple); | ||
68 | } | ||
69 | |||
70 | } | ||
71 | |||
72 | @Override | ||
73 | public boolean remove(Tuple tuple) { | ||
74 | Tuple signature = mask.transform(tuple); | ||
75 | return remove(tuple, signature); | ||
76 | } | ||
77 | |||
78 | @Override | ||
79 | public boolean remove(Tuple tuple, Tuple signature) { | ||
80 | try { | ||
81 | return signatureToTuples.removePair(signature, tuple) == ChangeGranularity.KEY; | ||
82 | } catch (IllegalStateException ex) { // ignore worthless internal exception details | ||
83 | throw raiseDuplicateDeletion(tuple); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | @Override | ||
88 | public Map<Tuple, Timeline<Timestamp>> getWithTimeline(ITuple signature) { | ||
89 | throw new UnsupportedOperationException("Timeless memories do not support timestamp-based lookup!"); | ||
90 | } | ||
91 | |||
92 | @Override | ||
93 | public Collection<Tuple> get(ITuple signature) { | ||
94 | IMemoryView<Tuple> bucket = signatureToTuples.lookupUnsafe(signature); | ||
95 | return bucket == null ? null : bucket.distinctValues(); | ||
96 | } | ||
97 | |||
98 | @Override | ||
99 | public void clear() { | ||
100 | signatureToTuples.clear(); | ||
101 | } | ||
102 | |||
103 | @Override | ||
104 | public Iterable<Tuple> getSignatures() { | ||
105 | return signatureToTuples.distinctKeys(); | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public Iterator<Tuple> iterator() { | ||
110 | return signatureToTuples.distinctValues().iterator(); | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | public int getTotalSize() { | ||
115 | int i = 0; | ||
116 | for (Tuple key : signatureToTuples.distinctKeys()) { | ||
117 | i += signatureToTuples.lookup(key).size(); | ||
118 | } | ||
119 | return i; | ||
120 | } | ||
121 | |||
122 | @Override | ||
123 | public int getKeysetSize() { | ||
124 | return signatureToTuples.countKeys(); | ||
125 | } | ||
126 | |||
127 | } | ||