diff options
Diffstat (limited to 'subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/ModelUpdateListener.java')
-rw-r--r-- | subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/ModelUpdateListener.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/ModelUpdateListener.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/ModelUpdateListener.java new file mode 100644 index 00000000..1ae3daa7 --- /dev/null +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/ModelUpdateListener.java | |||
@@ -0,0 +1,46 @@ | |||
1 | package tools.refinery.store.query.viatra.internal.update; | ||
2 | |||
3 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey; | ||
4 | import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContextListener; | ||
5 | import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple; | ||
6 | import tools.refinery.store.model.Model; | ||
7 | import tools.refinery.store.query.view.AnyRelationView; | ||
8 | import tools.refinery.store.query.view.RelationView; | ||
9 | |||
10 | import java.util.Collection; | ||
11 | import java.util.HashMap; | ||
12 | import java.util.Map; | ||
13 | |||
14 | public class ModelUpdateListener { | ||
15 | private final Map<AnyRelationView, RelationViewUpdateListener<?>> relationViewUpdateListeners; | ||
16 | |||
17 | public ModelUpdateListener(Model model, Collection<AnyRelationView> relationViews) { | ||
18 | relationViewUpdateListeners = new HashMap<>(relationViews.size()); | ||
19 | for (var relationView : relationViews) { | ||
20 | registerView(model, (RelationView<?>) relationView); | ||
21 | } | ||
22 | } | ||
23 | |||
24 | private <T> void registerView(Model model, RelationView<T> relationView) { | ||
25 | var listener = RelationViewUpdateListener.of(relationView); | ||
26 | var interpretation = model.getInterpretation(relationView.getSymbol()); | ||
27 | interpretation.addListener(listener, true); | ||
28 | relationViewUpdateListeners.put(relationView, listener); | ||
29 | } | ||
30 | |||
31 | public boolean containsRelationView(AnyRelationView relationView) { | ||
32 | return relationViewUpdateListeners.containsKey(relationView); | ||
33 | } | ||
34 | |||
35 | public void addListener(IInputKey key, AnyRelationView relationView, ITuple seed, | ||
36 | IQueryRuntimeContextListener listener) { | ||
37 | var relationViewUpdateListener = relationViewUpdateListeners.get(relationView); | ||
38 | relationViewUpdateListener.addFilter(key, seed, listener); | ||
39 | } | ||
40 | |||
41 | public void removeListener(IInputKey key, AnyRelationView relationView, ITuple seed, | ||
42 | IQueryRuntimeContextListener listener) { | ||
43 | var relationViewUpdateListener = relationViewUpdateListeners.get(relationView); | ||
44 | relationViewUpdateListener.removeFilter(key, seed, listener); | ||
45 | } | ||
46 | } | ||