aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/update/TupleChangingRelationViewUpdateListener.java
blob: be4951af3e8f8279117b6350cc04125b29088b49 (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.viatra.internal.update;

import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples;
import tools.refinery.store.model.Interpretation;
import tools.refinery.store.query.viatra.internal.ViatraModelQueryAdapterImpl;
import tools.refinery.store.query.view.RelationView;
import tools.refinery.store.tuple.Tuple;

import java.util.Arrays;

public class TupleChangingRelationViewUpdateListener<T> extends RelationViewUpdateListener<T> {
	private final RelationView<T> relationView;

	TupleChangingRelationViewUpdateListener(ViatraModelQueryAdapterImpl adapter, RelationView<T> relationView,
											Interpretation<T> interpretation) {
        super(adapter, interpretation);
        this.relationView = relationView;
	}

	@Override
	public void put(Tuple key, T fromValue, T toValue, boolean restoring) {
		boolean fromPresent = relationView.filter(key, fromValue);
		boolean toPresent = relationView.filter(key, toValue);
		if (fromPresent) {
			if (toPresent) { // value change
				var fromArray = relationView.forwardMap(key, fromValue);
				var toArray = relationView.forwardMap(key, toValue);
				if (!Arrays.equals(fromArray, toArray)) {
					processUpdate(Tuples.flatTupleOf(fromArray), false);
					processUpdate(Tuples.flatTupleOf(toArray), true);
				}
			} else { // fromValue disappears
				processUpdate(Tuples.flatTupleOf(relationView.forwardMap(key, fromValue)), false);
			}
		} else if (toPresent) { // toValue appears
			processUpdate(Tuples.flatTupleOf(relationView.forwardMap(key, toValue)), true);
		}
	}
}