aboutsummaryrefslogtreecommitdiffstats
path: root/model-data/src
diff options
context:
space:
mode:
authorLibravatar OszkarSemerath <semerath@mit.bme.hu>2021-08-15 00:22:49 +0200
committerLibravatar OszkarSemerath <semerath@mit.bme.hu>2021-08-15 00:22:49 +0200
commitc40355c1db1cac37cf91cd8d1b6ee23cf43c3c51 (patch)
treecc0cbc856023a0fab693896882a6cd4b6531e529 /model-data/src
parentMap -> iterable transformer (diff)
downloadrefinery-c40355c1db1cac37cf91cd8d1b6ee23cf43c3c51.tar.gz
refinery-c40355c1db1cac37cf91cd8d1b6ee23cf43c3c51.tar.zst
refinery-c40355c1db1cac37cf91cd8d1b6ee23cf43c3c51.zip
views of a for RelationRepresentations that can be queried.
Diffstat (limited to 'model-data/src')
-rw-r--r--model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationView.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationView.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationView.java
new file mode 100644
index 00000000..c52b9ff0
--- /dev/null
+++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationView.java
@@ -0,0 +1,90 @@
1package org.eclipse.viatra.solver.data.query;
2
3import java.util.function.BiFunction;
4import java.util.function.BiPredicate;
5
6import org.eclipse.viatra.solver.data.map.CursorAsIterator;
7import org.eclipse.viatra.solver.data.model.Model;
8import org.eclipse.viatra.solver.data.model.Tuple;
9import org.eclipse.viatra.solver.data.model.representation.RelationRepresentation;
10
11/**
12 * Represents a view of a {@link RelationRepresentation} that can be queried.
13 * @author Oszkar Semerath
14 *
15 * @param <D>
16 */
17public class RelationView<D>{
18 protected final Model model;
19 protected final RelationRepresentation<D> representation;
20 protected final BiPredicate<Tuple, D> filter;
21 protected final BiFunction<Tuple,D,Object[]> mapping;
22
23 protected RelationView(Model model, RelationRepresentation<D> representation, BiPredicate<Tuple,D> filter, BiFunction<Tuple,D,Object[]> mapping) {
24 this.model = model;
25 this.representation = representation;
26 this.filter = filter;
27 this.mapping = mapping;
28 checkRepresentation(model, representation);
29 }
30
31 protected RelationView(Model model, RelationRepresentation<D> representation, BiPredicate<Tuple,D> filter) {
32 this.model = model;
33 this.representation = representation;
34 this.filter = filter;
35 this.mapping = ((k,v)->toTuple1Array(k));
36 checkRepresentation(model, representation);
37 }
38 protected RelationView(Model model, RelationRepresentation<D> representation, BiFunction<Tuple,D,Object[]> mapping) {
39 this.model = model;
40 this.representation = representation;
41 this.filter = ((k,v)->true);
42 this.mapping = mapping;
43 checkRepresentation(model, representation);
44 }
45 protected RelationView(Model model, RelationRepresentation<D> representation) {
46 this.model = model;
47 this.representation = representation;
48 this.filter = ((k,v)->true);
49 this.mapping = (RelationView::toTuple1ArrayPlusValue);
50 checkRepresentation(model, representation);
51 }
52
53 private void checkRepresentation(Model model, RelationRepresentation<D> representation) {
54 if(!model.getDataRepresentations().contains(representation)) {
55 throw new IllegalArgumentException("Selected model does not contain representation " + representation + "!");
56 }
57 }
58
59 public Model getModel() {
60 return model;
61 }
62 public RelationRepresentation<D> getRepresentation() {
63 return representation;
64 }
65
66 public boolean get(Tuple t) {
67 D value = model.get(representation, t);
68 return value != representation.getDefaultValue() && filter.test(t,value);
69 }
70
71 public Iterable<Object[]> getAll() {
72 return (()->new CursorAsIterator<>(model.getAll(representation), mapping, filter));
73 }
74
75 public static Object[] toTuple1Array(Tuple t) {
76 Object[] result = new Object[t.getSize()];
77 for(int i = 0; i<t.getSize(); i++) {
78 result[i] = t.get(i);
79 }
80 return result;
81 }
82 public static <D> Object[] toTuple1ArrayPlusValue(Tuple t, D value) {
83 Object[] result = new Object[t.getSize()+1];
84 for(int i = 0; i<t.getSize(); i++) {
85 result[i] = t.get(i);
86 }
87 result[t.getSize()] = value;
88 return result;
89 }
90}