aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/ModelUpdateListener.java
blob: 8a46706674751846945a45ac18f59cbf6ee66d27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package tools.refinery.store.query.viatra.internal.update;

import org.eclipse.viatra.query.runtime.matchers.context.IInputKey;
import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContextListener;
import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple;
import tools.refinery.store.query.viatra.internal.ViatraModelQueryAdapterImpl;
import tools.refinery.store.query.view.AnyRelationView;
import tools.refinery.store.query.view.RelationView;

import java.util.HashMap;
import java.util.Map;

public class ModelUpdateListener {
	private final Map<AnyRelationView, RelationViewUpdateListener<?>> relationViewUpdateListeners;

	public ModelUpdateListener(ViatraModelQueryAdapterImpl adapter) {
		var relationViews = adapter.getStoreAdapter().getInputKeys().keySet();
		relationViewUpdateListeners = new HashMap<>(relationViews.size());
		for (var relationView : relationViews) {
			registerView(adapter, (RelationView<?>) relationView);
		}
	}

	private <T> void registerView(ViatraModelQueryAdapterImpl adapter, RelationView<T> relationView) {
		var listener = RelationViewUpdateListener.of(adapter, relationView);
		var model = adapter.getModel();
		var interpretation = model.getInterpretation(relationView.getSymbol());
		interpretation.addListener(listener, true);
		relationViewUpdateListeners.put(relationView, listener);
	}

	public boolean containsRelationView(AnyRelationView relationView) {
		return relationViewUpdateListeners.containsKey(relationView);
	}

	public void addListener(IInputKey key, AnyRelationView relationView, ITuple seed,
							IQueryRuntimeContextListener listener) {
		var relationViewUpdateListener = relationViewUpdateListeners.get(relationView);
		relationViewUpdateListener.addFilter(key, seed, listener);
	}

	public void removeListener(IInputKey key, AnyRelationView relationView, ITuple seed,
							   IQueryRuntimeContextListener listener) {
		var relationViewUpdateListener = relationViewUpdateListeners.get(relationView);
		relationViewUpdateListener.removeFilter(key, seed, listener);
	}
}