diff options
author | 2021-08-17 02:37:24 +0200 | |
---|---|---|
committer | 2021-08-17 02:37:24 +0200 | |
commit | b876d58edd3214906f94bf897e1fd6e9f7ac8734 (patch) | |
tree | 0ea83cdc0e38ba9e47759d53955fb879434253d0 /model-data | |
parent | Missed removing old version of the file (diff) | |
download | refinery-b876d58edd3214906f94bf897e1fd6e9f7ac8734.tar.gz refinery-b876d58edd3214906f94bf897e1fd6e9f7ac8734.tar.zst refinery-b876d58edd3214906f94bf897e1fd6e9f7ac8734.zip |
Views works as keys + they can be instantiated without model
Diffstat (limited to 'model-data')
12 files changed, 100 insertions, 119 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalQuery.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalQuery.java index bbff5563..39fc2482 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalQuery.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalQuery.java | |||
@@ -10,9 +10,7 @@ import java.util.Set; | |||
10 | 10 | ||
11 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatcher; | 11 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatcher; |
12 | import org.eclipse.viatra.query.runtime.api.GenericQuerySpecification; | 12 | import org.eclipse.viatra.query.runtime.api.GenericQuerySpecification; |
13 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification; | ||
14 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | 13 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; |
15 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | ||
16 | import org.eclipse.viatra.query.runtime.api.scope.QueryScope; | 14 | import org.eclipse.viatra.query.runtime.api.scope.QueryScope; |
17 | import org.eclipse.viatra.query.runtime.matchers.backend.QueryEvaluationHint; | 15 | import org.eclipse.viatra.query.runtime.matchers.backend.QueryEvaluationHint; |
18 | import org.eclipse.viatra.query.runtime.matchers.psystem.PBody; | 16 | import org.eclipse.viatra.query.runtime.matchers.psystem.PBody; |
@@ -27,7 +25,6 @@ import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.Positi | |||
27 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint; | 25 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint; |
28 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.BasePQuery; | 26 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.BasePQuery; |
29 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter; | 27 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter; |
30 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameterDirection; | ||
31 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery; | 28 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery; |
32 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PVisibility; | 29 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PVisibility; |
33 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | 30 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; |
@@ -80,7 +77,7 @@ public class RelationalQuery extends BasePQuery { | |||
80 | for(int i = 0; i<name.length; i++) { | 77 | for(int i = 0; i<name.length; i++) { |
81 | variables[i] = body.getOrCreateVariableByName(name[i]); | 78 | variables[i] = body.getOrCreateVariableByName(name[i]); |
82 | } | 79 | } |
83 | new TypeConstraint(body, Tuples.flatTupleOf(variables), view.getKey()); | 80 | new TypeConstraint(body, Tuples.flatTupleOf(variables), view); |
84 | return this; | 81 | return this; |
85 | } | 82 | } |
86 | 83 | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalScope.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalScope.java index f0a390aa..97b33935 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalScope.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/RelationalScope.java | |||
@@ -7,15 +7,18 @@ import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | |||
7 | import org.eclipse.viatra.query.runtime.api.scope.IEngineContext; | 7 | import org.eclipse.viatra.query.runtime.api.scope.IEngineContext; |
8 | import org.eclipse.viatra.query.runtime.api.scope.IIndexingErrorListener; | 8 | import org.eclipse.viatra.query.runtime.api.scope.IIndexingErrorListener; |
9 | import org.eclipse.viatra.query.runtime.api.scope.QueryScope; | 9 | import org.eclipse.viatra.query.runtime.api.scope.QueryScope; |
10 | import org.eclipse.viatra.solver.data.model.Model; | ||
10 | import org.eclipse.viatra.solver.data.model.Tuple; | 11 | import org.eclipse.viatra.solver.data.model.Tuple; |
11 | import org.eclipse.viatra.solver.data.query.internal.RelationUpdateListener; | 12 | import org.eclipse.viatra.solver.data.query.internal.RelationUpdateListener; |
12 | import org.eclipse.viatra.solver.data.query.internal.RelationalEngineContext; | 13 | import org.eclipse.viatra.solver.data.query.internal.RelationalEngineContext; |
13 | import org.eclipse.viatra.solver.data.query.view.RelationView; | 14 | import org.eclipse.viatra.solver.data.query.view.RelationView; |
14 | 15 | ||
15 | public class RelationalScope extends QueryScope{ | 16 | public class RelationalScope extends QueryScope{ |
17 | private final Model model; | ||
16 | private final RelationUpdateListener updateListener; | 18 | private final RelationUpdateListener updateListener; |
17 | 19 | ||
18 | public RelationalScope(Set<RelationView<?>> relationViews) { | 20 | public RelationalScope(Model model, Set<RelationView<?>> relationViews) { |
21 | this.model = model; | ||
19 | updateListener = new RelationUpdateListener(relationViews); | 22 | updateListener = new RelationUpdateListener(relationViews); |
20 | } | 23 | } |
21 | 24 | ||
@@ -26,6 +29,6 @@ public class RelationalScope extends QueryScope{ | |||
26 | @Override | 29 | @Override |
27 | protected IEngineContext createEngineContext(ViatraQueryEngine engine, IIndexingErrorListener errorListener, | 30 | protected IEngineContext createEngineContext(ViatraQueryEngine engine, IIndexingErrorListener errorListener, |
28 | Logger logger) { | 31 | Logger logger) { |
29 | return new RelationalEngineContext(this.updateListener); | 32 | return new RelationalEngineContext(model, updateListener); |
30 | } | 33 | } |
31 | } | 34 | } |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListener.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListener.java index de814936..c6d12614 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListener.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListener.java | |||
@@ -19,18 +19,18 @@ public class RelationUpdateListener { | |||
19 | view2Listeners.put(relationView, new HashSet<>()); | 19 | view2Listeners.put(relationView, new HashSet<>()); |
20 | } | 20 | } |
21 | } | 21 | } |
22 | public boolean containsRelationalView(RelationViewKey<?> relationalKey) { | 22 | public boolean containsRelationalView(RelationView<?> relationalKey) { |
23 | RelationView<?> relationView = relationalKey.getWrappedKey(); | 23 | RelationView<?> relationView = relationalKey.getWrappedKey(); |
24 | return view2Listeners.containsKey(relationView); | 24 | return view2Listeners.containsKey(relationView); |
25 | } | 25 | } |
26 | public void addListener(RelationViewKey<?> relationalKey, ITuple seed, IQueryRuntimeContextListener listener) { | 26 | public void addListener(RelationView<?> relationalKey, ITuple seed, IQueryRuntimeContextListener listener) { |
27 | RelationView<?> relationView = relationalKey.getWrappedKey(); | 27 | RelationView<?> relationView = relationalKey.getWrappedKey(); |
28 | if(view2Listeners.containsKey(relationView)) { | 28 | if(view2Listeners.containsKey(relationView)) { |
29 | RelationUpdateListenerEntry<?> entry = new RelationUpdateListenerEntry<>(relationalKey, seed, listener); | 29 | RelationUpdateListenerEntry<?> entry = new RelationUpdateListenerEntry<>(relationalKey, seed, listener); |
30 | view2Listeners.get(relationView).add(entry); | 30 | view2Listeners.get(relationView).add(entry); |
31 | } else throw new IllegalArgumentException(); | 31 | } else throw new IllegalArgumentException(); |
32 | } | 32 | } |
33 | public void removeListener(RelationViewKey<?> relationalKey, ITuple seed, IQueryRuntimeContextListener listener) { | 33 | public void removeListener(RelationView<?> relationalKey, ITuple seed, IQueryRuntimeContextListener listener) { |
34 | RelationView<?> relationView = relationalKey.getWrappedKey(); | 34 | RelationView<?> relationView = relationalKey.getWrappedKey(); |
35 | if(view2Listeners.containsKey(relationView)) { | 35 | if(view2Listeners.containsKey(relationView)) { |
36 | RelationUpdateListenerEntry<?> entry = new RelationUpdateListenerEntry<>(relationalKey, seed, listener); | 36 | RelationUpdateListenerEntry<?> entry = new RelationUpdateListenerEntry<>(relationalKey, seed, listener); |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java index def02aa5..55aed7c8 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java | |||
@@ -7,13 +7,14 @@ import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContextLis | |||
7 | import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple; | 7 | import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple; |
8 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | 8 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; |
9 | import org.eclipse.viatra.solver.data.model.Tuple; | 9 | import org.eclipse.viatra.solver.data.model.Tuple; |
10 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
10 | 11 | ||
11 | public class RelationUpdateListenerEntry<D> { | 12 | public class RelationUpdateListenerEntry<D> { |
12 | final RelationViewKey<D> key; | 13 | final RelationView<D> key; |
13 | final ITuple filter; | 14 | final ITuple filter; |
14 | final IQueryRuntimeContextListener listener; | 15 | final IQueryRuntimeContextListener listener; |
15 | 16 | ||
16 | public RelationUpdateListenerEntry(RelationViewKey<D> key, ITuple filter, IQueryRuntimeContextListener listener) { | 17 | public RelationUpdateListenerEntry(RelationView<D> key, ITuple filter, IQueryRuntimeContextListener listener) { |
17 | super(); | 18 | super(); |
18 | this.key = key; | 19 | this.key = key; |
19 | this.filter = filter; | 20 | this.filter = filter; |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationViewKey.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationViewKey.java deleted file mode 100644 index bae0af45..00000000 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationViewKey.java +++ /dev/null | |||
@@ -1,60 +0,0 @@ | |||
1 | package org.eclipse.viatra.solver.data.query.internal; | ||
2 | |||
3 | import java.util.Objects; | ||
4 | import java.util.UUID; | ||
5 | |||
6 | import org.eclipse.viatra.query.runtime.matchers.context.common.BaseInputKeyWrapper; | ||
7 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
8 | |||
9 | public class RelationViewKey<D> extends BaseInputKeyWrapper<RelationView<D>>{ | ||
10 | private final String uniqueName; | ||
11 | private final int arity; | ||
12 | |||
13 | public RelationViewKey(RelationView<D> wrappedKey, int arity) { | ||
14 | super(wrappedKey); | ||
15 | this.uniqueName = wrappedKey.getRepresentation().getName() + "-"+UUID.randomUUID(); | ||
16 | this.arity = arity; | ||
17 | } | ||
18 | |||
19 | @Override | ||
20 | public String getPrettyPrintableName() { | ||
21 | return wrappedKey.getRepresentation().getName(); | ||
22 | } | ||
23 | |||
24 | @Override | ||
25 | public String getStringID() { | ||
26 | return uniqueName; | ||
27 | } | ||
28 | |||
29 | @Override | ||
30 | public int getArity() { | ||
31 | return arity; | ||
32 | } | ||
33 | |||
34 | @Override | ||
35 | public boolean isEnumerable() { | ||
36 | return true; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public int hashCode() { | ||
41 | final int prime = 31; | ||
42 | int result = super.hashCode(); | ||
43 | result = prime * result + Objects.hash(uniqueName); | ||
44 | return result; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public boolean equals(Object obj) { | ||
49 | if (this == obj) | ||
50 | return true; | ||
51 | if (!super.equals(obj)) | ||
52 | return false; | ||
53 | if (!(obj instanceof RelationViewKey)) | ||
54 | return false; | ||
55 | RelationViewKey<?> other = (RelationViewKey<?>) obj; | ||
56 | return Objects.equals(uniqueName, other.uniqueName); | ||
57 | } | ||
58 | |||
59 | |||
60 | } | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalEngineContext.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalEngineContext.java index c46da1bb..01948828 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalEngineContext.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalEngineContext.java | |||
@@ -3,14 +3,15 @@ package org.eclipse.viatra.solver.data.query.internal; | |||
3 | import org.eclipse.viatra.query.runtime.api.scope.IBaseIndex; | 3 | import org.eclipse.viatra.query.runtime.api.scope.IBaseIndex; |
4 | import org.eclipse.viatra.query.runtime.api.scope.IEngineContext; | 4 | import org.eclipse.viatra.query.runtime.api.scope.IEngineContext; |
5 | import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext; | 5 | import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext; |
6 | import org.eclipse.viatra.solver.data.model.Model; | ||
6 | 7 | ||
7 | public class RelationalEngineContext implements IEngineContext{ | 8 | public class RelationalEngineContext implements IEngineContext{ |
8 | private final IBaseIndex baseIndex = new DummyBaseIndexer(); | 9 | private final IBaseIndex baseIndex = new DummyBaseIndexer(); |
9 | private final RelationalRuntimeContext runtimeContext; | 10 | private final RelationalRuntimeContext runtimeContext; |
10 | 11 | ||
11 | 12 | ||
12 | public RelationalEngineContext(RelationUpdateListener updateListener) { | 13 | public RelationalEngineContext(Model model, RelationUpdateListener updateListener) { |
13 | runtimeContext = new RelationalRuntimeContext(updateListener); | 14 | runtimeContext = new RelationalRuntimeContext(model, updateListener); |
14 | } | 15 | } |
15 | 16 | ||
16 | @Override | 17 | @Override |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalQueryMetaContext.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalQueryMetaContext.java index 457ed6fd..de500fc9 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalQueryMetaContext.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalQueryMetaContext.java | |||
@@ -10,6 +10,7 @@ import java.util.Set; | |||
10 | import org.eclipse.viatra.query.runtime.matchers.context.AbstractQueryMetaContext; | 10 | import org.eclipse.viatra.query.runtime.matchers.context.AbstractQueryMetaContext; |
11 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey; | 11 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey; |
12 | import org.eclipse.viatra.query.runtime.matchers.context.InputKeyImplication; | 12 | import org.eclipse.viatra.query.runtime.matchers.context.InputKeyImplication; |
13 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
13 | 14 | ||
14 | /** | 15 | /** |
15 | * The meta context information for String scopes. | 16 | * The meta context information for String scopes. |
@@ -25,7 +26,7 @@ public final class RelationalQueryMetaContext extends AbstractQueryMetaContext { | |||
25 | @Override | 26 | @Override |
26 | public boolean isStateless(IInputKey key) { | 27 | public boolean isStateless(IInputKey key) { |
27 | ensureValidKey(key); | 28 | ensureValidKey(key); |
28 | return key instanceof RelationViewKey<?>; | 29 | return key instanceof RelationView<?>; |
29 | } | 30 | } |
30 | 31 | ||
31 | @Override | 32 | @Override |
@@ -37,7 +38,7 @@ public final class RelationalQueryMetaContext extends AbstractQueryMetaContext { | |||
37 | @Override | 38 | @Override |
38 | public Map<Set<Integer>, Set<Integer>> getFunctionalDependencies(IInputKey key) { | 39 | public Map<Set<Integer>, Set<Integer>> getFunctionalDependencies(IInputKey key) { |
39 | ensureValidKey(key); | 40 | ensureValidKey(key); |
40 | if (key instanceof RelationViewKey) { | 41 | if (key instanceof RelationView) { |
41 | return new HashMap<Set<Integer>, Set<Integer>>(); | 42 | return new HashMap<Set<Integer>, Set<Integer>>(); |
42 | } else { | 43 | } else { |
43 | return Collections.emptyMap(); | 44 | return Collections.emptyMap(); |
@@ -45,7 +46,7 @@ public final class RelationalQueryMetaContext extends AbstractQueryMetaContext { | |||
45 | } | 46 | } |
46 | 47 | ||
47 | public void ensureValidKey(IInputKey key) { | 48 | public void ensureValidKey(IInputKey key) { |
48 | if (! (key instanceof RelationViewKey<?>)) | 49 | if (! (key instanceof RelationView<?>)) |
49 | illegalInputKey(key); | 50 | illegalInputKey(key); |
50 | } | 51 | } |
51 | 52 | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java index 8f6fb8dd..7d1682b2 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java | |||
@@ -19,12 +19,16 @@ import org.eclipse.viatra.query.runtime.matchers.tuple.Tuple; | |||
19 | import org.eclipse.viatra.query.runtime.matchers.tuple.TupleMask; | 19 | import org.eclipse.viatra.query.runtime.matchers.tuple.TupleMask; |
20 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | 20 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; |
21 | import org.eclipse.viatra.query.runtime.matchers.util.Accuracy; | 21 | import org.eclipse.viatra.query.runtime.matchers.util.Accuracy; |
22 | import org.eclipse.viatra.solver.data.model.Model; | ||
23 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
22 | 24 | ||
23 | public class RelationalRuntimeContext implements IQueryRuntimeContext { | 25 | public class RelationalRuntimeContext implements IQueryRuntimeContext { |
24 | private final RelationalQueryMetaContext metaContext = new RelationalQueryMetaContext(); | 26 | private final RelationalQueryMetaContext metaContext = new RelationalQueryMetaContext(); |
25 | private final RelationUpdateListener relationUpdateListener; | 27 | private final RelationUpdateListener relationUpdateListener; |
28 | private final Model model; | ||
26 | 29 | ||
27 | public RelationalRuntimeContext(RelationUpdateListener relationUpdateListener) { | 30 | public RelationalRuntimeContext(Model model, RelationUpdateListener relationUpdateListener) { |
31 | this.model = model; | ||
28 | this.relationUpdateListener = relationUpdateListener; | 32 | this.relationUpdateListener = relationUpdateListener; |
29 | } | 33 | } |
30 | 34 | ||
@@ -53,8 +57,8 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { | |||
53 | 57 | ||
54 | @Override | 58 | @Override |
55 | public boolean isIndexed(IInputKey key, IndexingService service) { | 59 | public boolean isIndexed(IInputKey key, IndexingService service) { |
56 | if(key instanceof RelationViewKey<?>) { | 60 | if(key instanceof RelationView<?>) { |
57 | RelationViewKey<?> relationalKey = (RelationViewKey<?>) key; | 61 | RelationView<?> relationalKey = (RelationView<?>) key; |
58 | return this.relationUpdateListener.containsRelationalView(relationalKey); | 62 | return this.relationUpdateListener.containsRelationalView(relationalKey); |
59 | } else { | 63 | } else { |
60 | return false; | 64 | return false; |
@@ -68,9 +72,9 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { | |||
68 | } | 72 | } |
69 | } | 73 | } |
70 | 74 | ||
71 | RelationViewKey<?> checkKey(IInputKey key) { | 75 | RelationView<?> checkKey(IInputKey key) { |
72 | if(key instanceof RelationViewKey) { | 76 | if(key instanceof RelationView) { |
73 | RelationViewKey<?> relationViewKey = (RelationViewKey<?>) key; | 77 | RelationView<?> relationViewKey = (RelationView<?>) key; |
74 | if(relationUpdateListener.containsRelationalView(relationViewKey)) { | 78 | if(relationUpdateListener.containsRelationalView(relationViewKey)) { |
75 | return relationViewKey; | 79 | return relationViewKey; |
76 | } else { | 80 | } else { |
@@ -83,8 +87,8 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { | |||
83 | 87 | ||
84 | @Override | 88 | @Override |
85 | public int countTuples(IInputKey key, TupleMask seedMask, ITuple seed) { | 89 | public int countTuples(IInputKey key, TupleMask seedMask, ITuple seed) { |
86 | RelationViewKey<?> relationalViewKey = checkKey(key); | 90 | RelationView<?> relationalViewKey = checkKey(key); |
87 | Iterable<Object[]> allObjects = relationalViewKey.getWrappedKey().getAll(); | 91 | Iterable<Object[]> allObjects = relationalViewKey.getAll(model); |
88 | Iterable<Object[]> filteredBySeed = filter(allObjects,objectArray -> isMatching(objectArray,seedMask,seed)); | 92 | Iterable<Object[]> filteredBySeed = filter(allObjects,objectArray -> isMatching(objectArray,seedMask,seed)); |
89 | Iterator<Object[]> iterator = filteredBySeed.iterator(); | 93 | Iterator<Object[]> iterator = filteredBySeed.iterator(); |
90 | int result = 0; | 94 | int result = 0; |
@@ -102,8 +106,8 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { | |||
102 | 106 | ||
103 | @Override | 107 | @Override |
104 | public Iterable<Tuple> enumerateTuples(IInputKey key, TupleMask seedMask, ITuple seed) { | 108 | public Iterable<Tuple> enumerateTuples(IInputKey key, TupleMask seedMask, ITuple seed) { |
105 | RelationViewKey<?> relationalViewKey = checkKey(key); | 109 | RelationView<?> relationalViewKey = checkKey(key); |
106 | Iterable<Object[]> allObjects = relationalViewKey.getWrappedKey().getAll(); | 110 | Iterable<Object[]> allObjects = relationalViewKey.getAll(model); |
107 | Iterable<Object[]> filteredBySeed = filter(allObjects,objectArray -> isMatching(objectArray,seedMask,seed)); | 111 | Iterable<Object[]> filteredBySeed = filter(allObjects,objectArray -> isMatching(objectArray,seedMask,seed)); |
108 | return map(filteredBySeed,Tuples::flatTupleOf); | 112 | return map(filteredBySeed,Tuples::flatTupleOf); |
109 | } | 113 | } |
@@ -134,20 +138,20 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { | |||
134 | 138 | ||
135 | @Override | 139 | @Override |
136 | public boolean containsTuple(IInputKey key, ITuple seed) { | 140 | public boolean containsTuple(IInputKey key, ITuple seed) { |
137 | RelationViewKey<?> relationalViewKey = checkKey(key); | 141 | RelationView<?> relationalViewKey = checkKey(key); |
138 | return relationalViewKey.getWrappedKey().get(seed.getElements()); | 142 | return relationalViewKey.get(model,seed.getElements()); |
139 | } | 143 | } |
140 | 144 | ||
141 | @Override | 145 | @Override |
142 | public void addUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) { | 146 | public void addUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) { |
143 | RelationViewKey<?> relationalKey = checkKey(key); | 147 | RelationView<?> relationalKey = checkKey(key); |
144 | this.relationUpdateListener.addListener(relationalKey, seed, listener); | 148 | this.relationUpdateListener.addListener(relationalKey, seed, listener); |
145 | 149 | ||
146 | } | 150 | } |
147 | 151 | ||
148 | @Override | 152 | @Override |
149 | public void removeUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) { | 153 | public void removeUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) { |
150 | RelationViewKey<?> relationalKey = checkKey(key); | 154 | RelationView<?> relationalKey = checkKey(key); |
151 | this.relationUpdateListener.removeListener(relationalKey, seed, listener); | 155 | this.relationUpdateListener.removeListener(relationalKey, seed, listener); |
152 | } | 156 | } |
153 | 157 | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FilteredRelationView.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FilteredRelationView.java index 4602af21..bfa3d13a 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FilteredRelationView.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FilteredRelationView.java | |||
@@ -11,7 +11,7 @@ public class FilteredRelationView<D> extends RelationView<D>{ | |||
11 | private final BiPredicate<Tuple,D> predicate; | 11 | private final BiPredicate<Tuple,D> predicate; |
12 | 12 | ||
13 | public FilteredRelationView(Model model, Relation<D> representation, BiPredicate<Tuple,D> predicate) { | 13 | public FilteredRelationView(Model model, Relation<D> representation, BiPredicate<Tuple,D> predicate) { |
14 | super(model, representation); | 14 | super(representation); |
15 | this.predicate = predicate; | 15 | this.predicate = predicate; |
16 | } | 16 | } |
17 | @Override | 17 | @Override |
@@ -19,13 +19,13 @@ public class FilteredRelationView<D> extends RelationView<D>{ | |||
19 | return toTuple1Array(key); | 19 | return toTuple1Array(key); |
20 | } | 20 | } |
21 | @Override | 21 | @Override |
22 | public boolean get(Object[] tuple) { | 22 | public boolean get(Model model, Object[] tuple) { |
23 | int[] content = new int[tuple.length]; | 23 | int[] content = new int[tuple.length]; |
24 | for(int i = 0; i<tuple.length; i++) { | 24 | for(int i = 0; i<tuple.length; i++) { |
25 | content[i] =((Tuple1)tuple[i]).get(0); | 25 | content[i] =((Tuple1)tuple[i]).get(0); |
26 | } | 26 | } |
27 | Tuple key = Tuple.of(content); | 27 | Tuple key = Tuple.of(content); |
28 | D value = this.model.get(representation, key); | 28 | D value = model.get(representation, key); |
29 | return filter(key, value); | 29 | return filter(key, value); |
30 | } | 30 | } |
31 | 31 | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FunctionalRelationView.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FunctionalRelationView.java index b8d0ef72..71759bc5 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FunctionalRelationView.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/FunctionalRelationView.java | |||
@@ -8,7 +8,7 @@ import org.eclipse.viatra.solver.data.model.representation.Relation; | |||
8 | public class FunctionalRelationView<D> extends RelationView<D> { | 8 | public class FunctionalRelationView<D> extends RelationView<D> { |
9 | 9 | ||
10 | public FunctionalRelationView(Model model, Relation<D> representation) { | 10 | public FunctionalRelationView(Model model, Relation<D> representation) { |
11 | super(model, representation); | 11 | super(representation); |
12 | } | 12 | } |
13 | 13 | ||
14 | @Override | 14 | @Override |
@@ -22,7 +22,7 @@ public class FunctionalRelationView<D> extends RelationView<D> { | |||
22 | } | 22 | } |
23 | 23 | ||
24 | @Override | 24 | @Override |
25 | public boolean get(Object[] tuple) { | 25 | public boolean get(Model model, Object[] tuple) { |
26 | int[] content = new int[tuple.length-1]; | 26 | int[] content = new int[tuple.length-1]; |
27 | for(int i = 0; i<tuple.length-1; i++) { | 27 | for(int i = 0; i<tuple.length-1; i++) { |
28 | content[i] =((Tuple1)tuple[i]).get(0); | 28 | content[i] =((Tuple1)tuple[i]).get(0); |
@@ -30,7 +30,7 @@ public class FunctionalRelationView<D> extends RelationView<D> { | |||
30 | Tuple key = Tuple.of(content); | 30 | Tuple key = Tuple.of(content); |
31 | @SuppressWarnings("unchecked") | 31 | @SuppressWarnings("unchecked") |
32 | D valueInTuple = (D) tuple[tuple.length-1]; | 32 | D valueInTuple = (D) tuple[tuple.length-1]; |
33 | D valueInMap = this.model.get(representation, key); | 33 | D valueInMap = model.get(representation, key); |
34 | return valueInTuple.equals(valueInMap); | 34 | return valueInTuple.equals(valueInMap); |
35 | } | 35 | } |
36 | 36 | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/RelationView.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/RelationView.java index 18d2692d..c5bc5228 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/RelationView.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/RelationView.java | |||
@@ -1,51 +1,85 @@ | |||
1 | package org.eclipse.viatra.solver.data.query.view; | 1 | package org.eclipse.viatra.solver.data.query.view; |
2 | 2 | ||
3 | import java.util.Objects; | ||
4 | |||
5 | import org.eclipse.viatra.query.runtime.matchers.context.common.BaseInputKeyWrapper; | ||
3 | import org.eclipse.viatra.solver.data.map.CursorAsIterator; | 6 | import org.eclipse.viatra.solver.data.map.CursorAsIterator; |
4 | import org.eclipse.viatra.solver.data.model.Model; | 7 | import org.eclipse.viatra.solver.data.model.Model; |
5 | import org.eclipse.viatra.solver.data.model.Tuple; | 8 | import org.eclipse.viatra.solver.data.model.Tuple; |
6 | import org.eclipse.viatra.solver.data.model.representation.Relation; | 9 | import org.eclipse.viatra.solver.data.model.representation.Relation; |
7 | import org.eclipse.viatra.solver.data.query.internal.RelationViewKey; | ||
8 | 10 | ||
9 | /** | 11 | /** |
10 | * Represents a view of a {@link Relation} that can be queried. | 12 | * Represents a view of a {@link Relation} that can be queried. |
13 | * | ||
11 | * @author Oszkar Semerath | 14 | * @author Oszkar Semerath |
12 | * | 15 | * |
13 | * @param <D> | 16 | * @param <D> |
14 | */ | 17 | */ |
15 | public abstract class RelationView<D>{ | 18 | public abstract class RelationView<D> extends BaseInputKeyWrapper<RelationView<D>> { |
16 | protected final Model model; | ||
17 | protected final Relation<D> representation; | 19 | protected final Relation<D> representation; |
18 | private final RelationViewKey<D> key; | 20 | |
19 | 21 | protected RelationView(Relation<D> representation) { | |
20 | protected RelationView(Model model, Relation<D> representation) { | 22 | super(null); |
21 | this.model = model; | 23 | this.wrappedKey = this; |
22 | this.representation = representation; | 24 | this.representation = representation; |
23 | key = new RelationViewKey<>(this, getArity()); | ||
24 | } | 25 | } |
25 | 26 | ||
26 | public Model getModel() { | 27 | @Override |
27 | return model; | 28 | public String getPrettyPrintableName() { |
29 | return representation.getName(); | ||
30 | } | ||
31 | |||
32 | @Override | ||
33 | public String getStringID() { | ||
34 | return representation.getName() + this.getClass().getName(); | ||
28 | } | 35 | } |
36 | |||
29 | public Relation<D> getRepresentation() { | 37 | public Relation<D> getRepresentation() { |
30 | return representation; | 38 | return representation; |
31 | } | 39 | } |
32 | 40 | ||
33 | public abstract int getArity(); | 41 | @Override |
42 | public boolean isEnumerable() { | ||
43 | return true; | ||
44 | } | ||
45 | |||
34 | protected abstract boolean filter(Tuple key, D value); | 46 | protected abstract boolean filter(Tuple key, D value); |
47 | |||
35 | protected abstract Object[] forwardMap(Tuple key, D value); | 48 | protected abstract Object[] forwardMap(Tuple key, D value); |
36 | public abstract boolean get(Object[] tuple); | 49 | |
50 | public abstract boolean get(Model model, Object[] tuple); | ||
37 | 51 | ||
38 | public Object[] transform(Tuple tuple, D value) { | 52 | public Object[] transform(Tuple tuple, D value) { |
39 | if(filter(tuple, value)) { | 53 | if (filter(tuple, value)) { |
40 | return forwardMap(tuple, value); | 54 | return forwardMap(tuple, value); |
41 | } else return null; | 55 | } else |
56 | return null; | ||
42 | } | 57 | } |
43 | 58 | ||
44 | public Iterable<Object[]> getAll() { | 59 | public Iterable<Object[]> getAll(Model model) { |
45 | return (()->new CursorAsIterator<>(model.getAll(representation), (k,v)->forwardMap(k,v), (k,v)->filter(k,v))); | 60 | return (() -> new CursorAsIterator<>(model.getAll(representation), (k, v) -> forwardMap(k, v), |
61 | (k, v) -> filter(k, v))); | ||
46 | } | 62 | } |
47 | 63 | ||
48 | public RelationViewKey<D> getKey() { | 64 | @Override |
49 | return key; | 65 | public int hashCode() { |
66 | final int prime = 31; | ||
67 | int result = 1; | ||
68 | result = prime * result + Objects.hash(representation); | ||
69 | return result; | ||
50 | } | 70 | } |
71 | |||
72 | @Override | ||
73 | public boolean equals(Object obj) { | ||
74 | if (this == obj) | ||
75 | return true; | ||
76 | if (!super.equals(obj)) | ||
77 | return false; | ||
78 | if (!(obj instanceof RelationView)) | ||
79 | return false; | ||
80 | @SuppressWarnings("unchecked") | ||
81 | RelationView<D> other = ((RelationView<D>) obj); | ||
82 | return Objects.equals(representation, other.representation); | ||
83 | } | ||
84 | |||
51 | } | 85 | } |
diff --git a/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java b/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java index ca73b103..1e0c1d5c 100644 --- a/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java +++ b/model-data/src/test/java/org/eclipse/viatra/solver/data/query/test/QueryTest.java | |||
@@ -39,7 +39,7 @@ class QueryTest { | |||
39 | 39 | ||
40 | RelationView<Boolean> persionView = new TupleRelationView(model, person); | 40 | RelationView<Boolean> persionView = new TupleRelationView(model, person); |
41 | 41 | ||
42 | RelationalScope scope = new RelationalScope(Set.of(persionView)); | 42 | RelationalScope scope = new RelationalScope(model,Set.of(persionView)); |
43 | 43 | ||
44 | GenericQuerySpecification<GenericPatternMatcher> personQuery = (new RelationalQuery("PersonQuery")) | 44 | GenericQuerySpecification<GenericPatternMatcher> personQuery = (new RelationalQuery("PersonQuery")) |
45 | .addParameter("p", persionView).addConstraint(persionView, "p").build(); | 45 | .addParameter("p", persionView).addConstraint(persionView, "p").build(); |
@@ -76,7 +76,7 @@ class QueryTest { | |||
76 | (k, v) -> v.must()); | 76 | (k, v) -> v.must()); |
77 | RelationView<TruthValue> friendMayView = new FilteredRelationView<TruthValue>(model, friend, (k, v) -> v.may()); | 77 | RelationView<TruthValue> friendMayView = new FilteredRelationView<TruthValue>(model, friend, (k, v) -> v.may()); |
78 | 78 | ||
79 | RelationalScope scope = new RelationalScope(Set.of(persionView, ageView, friendMustView, friendMayView)); | 79 | RelationalScope scope = new RelationalScope(model,Set.of(persionView, ageView, friendMustView, friendMayView)); |
80 | 80 | ||
81 | GenericQuerySpecification<GenericPatternMatcher> personQuery = (new RelationalQuery("PersonQuery")) | 81 | GenericQuerySpecification<GenericPatternMatcher> personQuery = (new RelationalQuery("PersonQuery")) |
82 | .addParameter("p", persionView).addConstraint(persionView, "p").build(); | 82 | .addParameter("p", persionView).addConstraint(persionView, "p").build(); |