aboutsummaryrefslogtreecommitdiffstats
path: root/model-data/src/main/java/org/eclipse/viatra/solver/data/query/view/RelationView.java
blob: 18d2692d92a3a6620481be1a0e4c2ecc4c0f9344 (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
48
49
50
51
package org.eclipse.viatra.solver.data.query.view;

import org.eclipse.viatra.solver.data.map.CursorAsIterator;
import org.eclipse.viatra.solver.data.model.Model;
import org.eclipse.viatra.solver.data.model.Tuple;
import org.eclipse.viatra.solver.data.model.representation.Relation;
import org.eclipse.viatra.solver.data.query.internal.RelationViewKey;

/**
 * Represents a view of a {@link Relation} that can be queried.
 * @author Oszkar Semerath
 *
 * @param <D>
 */
public abstract class RelationView<D>{
	protected final Model model;
	protected final Relation<D> representation;
	private final RelationViewKey<D> key;
	
	protected RelationView(Model model, Relation<D> representation) {
		this.model = model;
		this.representation = representation;
		key = new RelationViewKey<>(this, getArity());
	}
	
	public Model getModel() {
		return model;
	}
	public Relation<D> getRepresentation() {
		return representation;
	}
	
	public abstract int getArity();
	protected abstract boolean filter(Tuple key, D value);
	protected abstract Object[] forwardMap(Tuple key, D value);
	public abstract boolean get(Object[] tuple);

	public Object[] transform(Tuple tuple, D value) {
		if(filter(tuple, value)) {
			return forwardMap(tuple, value);
		} else return null;
	}

	public Iterable<Object[]> getAll() {
		return (()->new CursorAsIterator<>(model.getAll(representation), (k,v)->forwardMap(k,v), (k,v)->filter(k,v)));
	}
	
	public RelationViewKey<D> getKey() {
		return key;
	}
}