diff options
Diffstat (limited to 'subprojects/store/src')
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 @@ | |||
1 | package tools.refinery.store.query.view; | ||
2 | |||
3 | import tools.refinery.store.model.Model; | ||
4 | import tools.refinery.store.model.Tuple; | ||
5 | import tools.refinery.store.model.Tuple.Tuple1; | ||
6 | import tools.refinery.store.model.representation.Relation; | ||
7 | |||
8 | public 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 @@ | |||
1 | package tools.refinery.store.query.view; | 1 | package tools.refinery.store.query.view; |
2 | 2 | ||
3 | import java.util.function.BiPredicate; | ||
4 | |||
5 | import tools.refinery.store.model.Model; | ||
6 | import tools.refinery.store.model.Tuple; | 3 | import tools.refinery.store.model.Tuple; |
7 | import tools.refinery.store.model.Tuple.Tuple1; | ||
8 | import tools.refinery.store.model.representation.Relation; | 4 | import tools.refinery.store.model.representation.Relation; |
9 | 5 | ||
10 | public class FilteredRelationView<D> extends RelationView<D>{ | 6 | import java.util.function.BiPredicate; |
11 | private final BiPredicate<Tuple,D> predicate; | 7 | import java.util.function.Predicate; |
12 | 8 | ||
13 | public FilteredRelationView(Relation<D> representation, BiPredicate<Tuple,D> predicate) { | 9 | public 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; | |||
6 | import tools.refinery.store.model.representation.Relation; | 6 | import tools.refinery.store.model.representation.Relation; |
7 | 7 | ||
8 | public class FunctionalRelationView<D> extends RelationView<D> { | 8 | public 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; | |||
3 | import tools.refinery.store.model.Tuple; | 3 | import tools.refinery.store.model.Tuple; |
4 | import tools.refinery.store.model.representation.Relation; | 4 | import tools.refinery.store.model.representation.Relation; |
5 | 5 | ||
6 | public class KeyOnlyRelationView extends FilteredRelationView<Boolean>{ | 6 | public 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 @@ | |||
1 | package tools.refinery.store.query.view; | 1 | package tools.refinery.store.query.view; |
2 | 2 | ||
3 | import java.util.Objects; | ||
4 | |||
5 | import org.eclipse.viatra.query.runtime.matchers.context.common.BaseInputKeyWrapper; | ||
6 | |||
7 | import tools.refinery.store.map.CursorAsIterator; | 3 | import tools.refinery.store.map.CursorAsIterator; |
8 | import tools.refinery.store.model.Model; | 4 | import tools.refinery.store.model.Model; |
9 | import tools.refinery.store.model.Tuple; | 5 | import tools.refinery.store.model.Tuple; |
10 | import tools.refinery.store.model.representation.Relation; | 6 | import tools.refinery.store.model.representation.Relation; |
11 | 7 | ||
8 | import java.util.Objects; | ||
9 | import 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 | */ |
19 | public abstract class RelationView<D> extends BaseInputKeyWrapper<RelationView<D>> { | 17 | public 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 | } |