aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/tables/SimpleUnaryTable.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/tables/SimpleUnaryTable.java')
-rw-r--r--subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/tables/SimpleUnaryTable.java140
1 files changed, 140 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/tables/SimpleUnaryTable.java b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/tables/SimpleUnaryTable.java
new file mode 100644
index 00000000..428ea78b
--- /dev/null
+++ b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/tables/SimpleUnaryTable.java
@@ -0,0 +1,140 @@
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
10package tools.refinery.viatra.runtime.matchers.scopes.tables;
11
12import java.util.Optional;
13import java.util.stream.Stream;
14
15import tools.refinery.viatra.runtime.matchers.context.IInputKey;
16import tools.refinery.viatra.runtime.matchers.tuple.ITuple;
17import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
18import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
19import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
20import tools.refinery.viatra.runtime.matchers.util.Accuracy;
21import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
22import tools.refinery.viatra.runtime.matchers.util.Direction;
23import tools.refinery.viatra.runtime.matchers.util.IMemory;
24
25/**
26 * Simple value set.
27 * <p>
28 * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
29 * part of a work in progress. There is no guarantee that this API will
30 * work or that it will remain the same.
31 *
32 * @since 2.0
33 * @author Gabor Bergmann
34 */
35public class SimpleUnaryTable<Value> extends AbstractIndexTable implements ITableWriterUnary.Table<Value> {
36
37 protected IMemory<Value> values = CollectionsFactory.createMultiset(); // TODO use SetMemory if unique
38
39 private boolean unique;
40
41 /**
42 * @param unique
43 * client promises to only insert a given tuple with multiplicity one
44 */
45 public SimpleUnaryTable(IInputKey inputKey, ITableContext tableContext, boolean unique) {
46 super(inputKey, tableContext);
47 this.unique = unique;
48 if (1 != inputKey.getArity())
49 throw new IllegalArgumentException(inputKey.toString());
50 }
51
52 @Override
53 public void write(Direction direction, Value value) {
54 if (direction == Direction.INSERT) {
55 boolean changed = values.addOne(value);
56 if (unique && !changed) {
57 String msg = String.format(
58 "Error: trying to add duplicate value %s to the unique set %s. This indicates some errors in underlying model representation.",
59 value, getInputKey().getPrettyPrintableName());
60 logError(msg);
61 }
62 if (changed && emitNotifications) {
63 deliverChangeNotifications(Tuples.staticArityFlatTupleOf(value), true);
64 }
65 } else { // DELETE
66 boolean changed = values.removeOne(value);
67 if (unique && !changed) {
68 String msg = String.format(
69 "Error: trying to remove duplicate value %s from the unique set %s. This indicates some errors in underlying model representation.",
70 value, getInputKey().getPrettyPrintableName());
71 logError(msg);
72 }
73 if (changed && emitNotifications) {
74 deliverChangeNotifications(Tuples.staticArityFlatTupleOf(value), false);
75 }
76 }
77 }
78
79 @Override
80 @SuppressWarnings("unchecked")
81 public boolean containsTuple(ITuple seed) {
82 return values.containsNonZero((Value) seed.get(0));
83 }
84
85 @Override
86 public int countTuples(TupleMask seedMask, ITuple seed) {
87 if (seedMask.getSize() == 0) { // unseeded
88 return values.size();
89 } else {
90 @SuppressWarnings("unchecked")
91 Value value = (Value) seed.get(0);
92 return values.containsNonZero(value) ? 1 : 0;
93 }
94 }
95
96
97 @Override
98 public Optional<Long> estimateProjectionSize(TupleMask groupMask, Accuracy requiredAccuracy) {
99 // always exact count
100 if (groupMask.getSize() == 0) {
101 return values.isEmpty() ? Optional.of(0L) : Optional.of(1L);
102 } else {
103 return Optional.of((long)values.size());
104 }
105 }
106
107
108 @Override
109 public Stream<? extends Tuple> streamTuples(TupleMask seedMask, ITuple seed) {
110 if (seedMask.getSize() == 0) { // unseeded
111 return values.distinctValues().stream().map(Tuples::staticArityFlatTupleOf);
112 } else {
113 @SuppressWarnings("unchecked")
114 Value value = (Value) seed.get(0);
115 if (values.containsNonZero(value))
116 return Stream.of(Tuples.staticArityFlatTupleOf(value));
117 else
118 return Stream.empty();
119 }
120 }
121
122 @Override
123 public Iterable<? extends Object> enumerateValues(TupleMask seedMask, ITuple seed) {
124 if (seedMask.getSize() == 0) { // unseeded
125 return values;
126 } else {
127 throw new IllegalArgumentException(seedMask.toString());
128 }
129 }
130 @Override
131 public Stream<? extends Object> streamValues(TupleMask seedMask, ITuple seed) {
132 if (seedMask.getSize() == 0) { // unseeded
133 return values.asStream();
134 } else {
135 throw new IllegalArgumentException(seedMask.toString());
136 }
137 }
138
139
140}