aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/view/RelationView.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/view/RelationView.java')
-rw-r--r--subprojects/store-query/src/main/java/tools/refinery/store/query/view/RelationView.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/RelationView.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/RelationView.java
new file mode 100644
index 00000000..d7164b3b
--- /dev/null
+++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/RelationView.java
@@ -0,0 +1,77 @@
1package tools.refinery.store.query.view;
2
3import tools.refinery.store.map.CursorAsIterator;
4import tools.refinery.store.model.Model;
5import tools.refinery.store.representation.Symbol;
6import tools.refinery.store.tuple.Tuple;
7
8import java.util.Objects;
9import java.util.UUID;
10
11/**
12 * Represents a view of a {@link Symbol} that can be queried.
13 *
14 * @param <T>
15 * @author Oszkar Semerath
16 */
17public abstract non-sealed class RelationView<T> implements AnyRelationView {
18 private final Symbol<T> symbol;
19
20 private final String viewName;
21
22 protected RelationView(Symbol<T> symbol, String viewName) {
23 this.symbol = symbol;
24 this.viewName = viewName;
25 }
26
27 protected RelationView(Symbol<T> representation) {
28 this(representation, UUID.randomUUID().toString());
29 }
30
31 @Override
32 public Symbol<T> getSymbol() {
33 return symbol;
34 }
35
36 @Override
37 public String getViewName() {
38 return viewName;
39 }
40
41 @Override
42 public String name() {
43 return symbol.name() + "#" + viewName;
44 }
45
46 public abstract boolean filter(Tuple key, T value);
47
48 public abstract Object[] forwardMap(Tuple key, T value);
49
50 @Override
51 public Iterable<Object[]> getAll(Model model) {
52 return (() -> new CursorAsIterator<>(model.getInterpretation(symbol).getAll(), this::forwardMap, this::filter));
53 }
54
55 @Override
56 public String toString() {
57 return name();
58 }
59
60 @Override
61 public String toReferenceString() {
62 return "@RelationView(\"%s\") %s".formatted(viewName, symbol.name());
63 }
64
65 @Override
66 public boolean equals(Object o) {
67 if (this == o) return true;
68 if (o == null || getClass() != o.getClass()) return false;
69 RelationView<?> that = (RelationView<?>) o;
70 return Objects.equals(symbol, that.symbol) && Objects.equals(viewName, that.viewName);
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hash(symbol, viewName);
76 }
77}