aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store/src/main
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-09-26 01:42:58 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-10-03 20:06:52 +0200
commit1be64083c2c38eedeffc7cea9f51afbed4d46d28 (patch)
tree75c67f3c09cb8f61809291b58188320c7cde995c /subprojects/store/src/main
parentrefactor: move viatra into a separate subproject (diff)
downloadrefinery-1be64083c2c38eedeffc7cea9f51afbed4d46d28.tar.gz
refinery-1be64083c2c38eedeffc7cea9f51afbed4d46d28.tar.zst
refinery-1be64083c2c38eedeffc7cea9f51afbed4d46d28.zip
refactor: remove viatra dependency from store
Diffstat (limited to 'subprojects/store/src/main')
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java44
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java52
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java35
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java14
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java52
5 files changed, 109 insertions, 88 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java
new file mode 100644
index 00000000..a21a38ba
--- /dev/null
+++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java
@@ -0,0 +1,44 @@
1package tools.refinery.store.query.view;
2
3import tools.refinery.store.model.Model;
4import tools.refinery.store.model.Tuple;
5import tools.refinery.store.model.Tuple.Tuple1;
6import tools.refinery.store.model.representation.Relation;
7
8public abstract class AbstractFilteredRelationView<D> extends RelationView<D> {
9 protected AbstractFilteredRelationView(Relation<D> representation, String name) {
10 super(representation, name);
11 }
12
13 protected AbstractFilteredRelationView(Relation<D> representation) {
14 super(representation);
15 }
16
17 @Override
18 public Object[] forwardMap(Tuple key, D value) {
19 return toTuple1Array(key);
20 }
21
22 @Override
23 public boolean get(Model model, Object[] tuple) {
24 int[] content = new int[tuple.length];
25 for (int i = 0; i < tuple.length; i++) {
26 content[i] = ((Tuple1) tuple[i]).get(0);
27 }
28 Tuple key = Tuple.of(content);
29 D value = model.get(getRepresentation(), key);
30 return filter(key, value);
31 }
32
33 public int getArity() {
34 return this.getRepresentation().getArity();
35 }
36
37 private static Object[] toTuple1Array(Tuple t) {
38 Object[] result = new Object[t.getSize()];
39 for (int i = 0; i < t.getSize(); i++) {
40 result[i] = Tuple.of(t.get(i));
41 }
42 return result;
43 }
44}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java
index 3531195a..bb5d0237 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java
@@ -1,48 +1,34 @@
1package tools.refinery.store.query.view; 1package tools.refinery.store.query.view;
2 2
3import java.util.function.BiPredicate;
4
5import tools.refinery.store.model.Model;
6import tools.refinery.store.model.Tuple; 3import tools.refinery.store.model.Tuple;
7import tools.refinery.store.model.Tuple.Tuple1;
8import tools.refinery.store.model.representation.Relation; 4import tools.refinery.store.model.representation.Relation;
9 5
10public class FilteredRelationView<D> extends RelationView<D>{ 6import java.util.function.BiPredicate;
11 private final BiPredicate<Tuple,D> predicate; 7import java.util.function.Predicate;
12 8
13 public FilteredRelationView(Relation<D> representation, BiPredicate<Tuple,D> predicate) { 9public class FilteredRelationView<D> extends AbstractFilteredRelationView<D> {
14 super(representation); 10 private final BiPredicate<Tuple, D> predicate;
11
12 public FilteredRelationView(Relation<D> representation, String name, BiPredicate<Tuple, D> predicate) {
13 super(representation, name);
15 this.predicate = predicate; 14 this.predicate = predicate;
16 } 15 }
17 @Override 16
18 protected Object[] forwardMap(Tuple key, D value) { 17 public FilteredRelationView(Relation<D> representation, BiPredicate<Tuple, D> predicate) {
19 return toTuple1Array(key); 18 super(representation);
20 } 19 this.predicate = predicate;
21 @Override
22 public boolean get(Model model, Object[] tuple) {
23 int[] content = new int[tuple.length];
24 for(int i = 0; i<tuple.length; i++) {
25 content[i] =((Tuple1)tuple[i]).get(0);
26 }
27 Tuple key = Tuple.of(content);
28 D value = model.get(representation, key);
29 return filter(key, value);
30 } 20 }
31 21
32 public static Object[] toTuple1Array(Tuple t) { 22 public FilteredRelationView(Relation<D> representation, String name, Predicate<D> predicate) {
33 Object[] result = new Object[t.getSize()]; 23 this(representation, name, (k, v) -> predicate.test(v));
34 for(int i = 0; i<t.getSize(); i++) {
35 result[i] = Tuple.of(t.get(i));
36 }
37 return result;
38 } 24 }
39 25
40 @Override 26 public FilteredRelationView(Relation<D> representation, Predicate<D> predicate) {
41 public int getArity() { 27 this(representation, (k, v) -> predicate.test(v));
42 return this.representation.getArity();
43 } 28 }
29
44 @Override 30 @Override
45 protected boolean filter(Tuple key, D value) { 31 public boolean filter(Tuple key, D value) {
46 return this.predicate.test(key, value); 32 return this.predicate.test(key, value);
47 } 33 }
48} 34}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java
index db9ba4b8..54baa050 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java
@@ -6,45 +6,48 @@ import tools.refinery.store.model.Tuple.Tuple1;
6import tools.refinery.store.model.representation.Relation; 6import tools.refinery.store.model.representation.Relation;
7 7
8public class FunctionalRelationView<D> extends RelationView<D> { 8public class FunctionalRelationView<D> extends RelationView<D> {
9 public FunctionalRelationView(Relation<D> representation, String name) {
10 super(representation, name);
11 }
9 12
10 public FunctionalRelationView(Relation<D> representation) { 13 public FunctionalRelationView(Relation<D> representation) {
11 super(representation); 14 super(representation);
12 } 15 }
13 16
14 @Override 17 @Override
15 protected boolean filter(Tuple key, D value) { 18 public boolean filter(Tuple key, D value) {
16 return true; 19 return true;
17 } 20 }
18 21
19 @Override 22 @Override
20 protected Object[] forwardMap(Tuple key, D value) { 23 public Object[] forwardMap(Tuple key, D value) {
21 return toTuple1ArrayPlusValue(key, value); 24 return toTuple1ArrayPlusValue(key, value);
22 } 25 }
23 26
24 @Override 27 @Override
25 public boolean get(Model model, Object[] tuple) { 28 public boolean get(Model model, Object[] tuple) {
26 int[] content = new int[tuple.length-1]; 29 int[] content = new int[tuple.length - 1];
27 for(int i = 0; i<tuple.length-1; i++) { 30 for (int i = 0; i < tuple.length - 1; i++) {
28 content[i] =((Tuple1)tuple[i]).get(0); 31 content[i] = ((Tuple1) tuple[i]).get(0);
29 } 32 }
30 Tuple key = Tuple.of(content); 33 Tuple key = Tuple.of(content);
31 @SuppressWarnings("unchecked") 34 @SuppressWarnings("unchecked")
32 D valueInTuple = (D) tuple[tuple.length-1]; 35 D valueInTuple = (D) tuple[tuple.length - 1];
33 D valueInMap = model.get(representation, key); 36 D valueInMap = model.get(getRepresentation(), key);
34 return valueInTuple.equals(valueInMap); 37 return valueInTuple.equals(valueInMap);
35 } 38 }
36 39
37 public static <D> Object[] toTuple1ArrayPlusValue(Tuple t, D value) { 40 @Override
38 Object[] result = new Object[t.getSize()+1]; 41 public int getArity() {
39 for(int i = 0; i<t.getSize(); i++) { 42 return getRepresentation().getArity() + 1;
43 }
44
45 private static <D> Object[] toTuple1ArrayPlusValue(Tuple t, D value) {
46 Object[] result = new Object[t.getSize() + 1];
47 for (int i = 0; i < t.getSize(); i++) {
40 result[i] = Tuple.of(t.get(i)); 48 result[i] = Tuple.of(t.get(i));
41 } 49 }
42 result[t.getSize()] = value; 50 result[t.getSize()] = value;
43 return result; 51 return result;
44 } 52 }
45
46 @Override
47 public int getArity() {
48 return this.representation.getArity()+1;
49 }
50} 53}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java
index 6fa387a1..b9a9ed9f 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java
@@ -3,14 +3,18 @@ package tools.refinery.store.query.view;
3import tools.refinery.store.model.Tuple; 3import tools.refinery.store.model.Tuple;
4import tools.refinery.store.model.representation.Relation; 4import tools.refinery.store.model.representation.Relation;
5 5
6public class KeyOnlyRelationView extends FilteredRelationView<Boolean>{ 6public class KeyOnlyRelationView extends AbstractFilteredRelationView<Boolean> {
7 public static final String VIEW_NAME = "key";
8
9 private final Boolean defaultValue;
7 10
8 public KeyOnlyRelationView(Relation<Boolean> representation) { 11 public KeyOnlyRelationView(Relation<Boolean> representation) {
9 super(representation, (k,v)->true); 12 super(representation, VIEW_NAME);
13 defaultValue = representation.getDefaultValue();
10 } 14 }
15
11 @Override 16 @Override
12 protected boolean filter(Tuple key, Boolean value) { 17 public boolean filter(Tuple key, Boolean value) {
13 return !value.equals(representation.getDefaultValue()); 18 return !value.equals(defaultValue);
14 } 19 }
15
16} 20}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java
index fd55eed4..b8a54046 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java
@@ -1,66 +1,51 @@
1package tools.refinery.store.query.view; 1package tools.refinery.store.query.view;
2 2
3import java.util.Objects;
4
5import org.eclipse.viatra.query.runtime.matchers.context.common.BaseInputKeyWrapper;
6
7import tools.refinery.store.map.CursorAsIterator; 3import tools.refinery.store.map.CursorAsIterator;
8import tools.refinery.store.model.Model; 4import tools.refinery.store.model.Model;
9import tools.refinery.store.model.Tuple; 5import tools.refinery.store.model.Tuple;
10import tools.refinery.store.model.representation.Relation; 6import tools.refinery.store.model.representation.Relation;
11 7
8import java.util.Objects;
9import java.util.UUID;
10
12/** 11/**
13 * Represents a view of a {@link Relation} that can be queried. 12 * Represents a view of a {@link Relation} that can be queried.
14 *
15 * @author Oszkar Semerath
16 * 13 *
17 * @param <D> 14 * @param <D>
15 * @author Oszkar Semerath
18 */ 16 */
19public abstract class RelationView<D> extends BaseInputKeyWrapper<RelationView<D>> { 17public abstract class RelationView<D> {
20 protected final Relation<D> representation; 18 private final Relation<D> representation;
21 19
22 protected RelationView(Relation<D> representation) { 20 private final String name;
23 super(null); 21
24 this.wrappedKey = this; 22 protected RelationView(Relation<D> representation, String name) {
25 this.representation = representation; 23 this.representation = representation;
24 this.name = name;
26 } 25 }
27 26
28 @Override 27 protected RelationView(Relation<D> representation) {
29 public String getPrettyPrintableName() { 28 this(representation, UUID.randomUUID().toString());
30 return representation.getName();
31 } 29 }
32 30
33 @Override 31 public abstract int getArity();
34 public String getStringID() {
35 return representation.getName() + this.getClass().getName();
36 }
37 32
38 public Relation<D> getRepresentation() { 33 public Relation<D> getRepresentation() {
39 return representation; 34 return representation;
40 } 35 }
41 36
42 @Override 37 public String getName() {
43 public boolean isEnumerable() { 38 return representation.getName() + "#" + name;
44 return true;
45 } 39 }
46 40
47 protected abstract boolean filter(Tuple key, D value); 41 public abstract boolean filter(Tuple key, D value);
48 42
49 protected abstract Object[] forwardMap(Tuple key, D value); 43 public abstract Object[] forwardMap(Tuple key, D value);
50 44
51 public abstract boolean get(Model model, Object[] tuple); 45 public abstract boolean get(Model model, Object[] tuple);
52
53 @SuppressWarnings("squid:S1168")
54 public Object[] transform(Tuple tuple, D value) {
55 if (filter(tuple, value)) {
56 return forwardMap(tuple, value);
57 } else
58 return null;
59 }
60 46
61 public Iterable<Object[]> getAll(Model model) { 47 public Iterable<Object[]> getAll(Model model) {
62 return (() -> new CursorAsIterator<>(model.getAll(representation), (k, v) -> forwardMap(k, v), 48 return (() -> new CursorAsIterator<>(model.getAll(representation), this::forwardMap, this::filter));
63 (k, v) -> filter(k, v)));
64 } 49 }
65 50
66 @Override 51 @Override
@@ -81,5 +66,4 @@ public abstract class RelationView<D> extends BaseInputKeyWrapper<RelationView<D
81 RelationView<D> other = ((RelationView<D>) obj); 66 RelationView<D> other = ((RelationView<D>) obj);
82 return Objects.equals(representation, other.representation); 67 return Objects.equals(representation, other.representation);
83 } 68 }
84
85} 69}