aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java
blob: b8a54046c0005f2a8c31705585d0a6f72de7c362 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tools.refinery.store.query.view;

import tools.refinery.store.map.CursorAsIterator;
import tools.refinery.store.model.Model;
import tools.refinery.store.model.Tuple;
import tools.refinery.store.model.representation.Relation;

import java.util.Objects;
import java.util.UUID;

/**
 * Represents a view of a {@link Relation} that can be queried.
 *
 * @param <D>
 * @author Oszkar Semerath
 */
public abstract class RelationView<D> {
	private final Relation<D> representation;

	private final String name;

	protected RelationView(Relation<D> representation, String name) {
		this.representation = representation;
		this.name = name;
	}

	protected RelationView(Relation<D> representation) {
		this(representation, UUID.randomUUID().toString());
	}

	public abstract int getArity();

	public Relation<D> getRepresentation() {
		return representation;
	}

	public String getName() {
		return representation.getName() + "#" + name;
	}

	public abstract boolean filter(Tuple key, D value);

	public abstract Object[] forwardMap(Tuple key, D value);

	public abstract boolean get(Model model, Object[] tuple);

	public Iterable<Object[]> getAll(Model model) {
		return (() -> new CursorAsIterator<>(model.getAll(representation), this::forwardMap, this::filter));
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Objects.hash(representation);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof RelationView))
			return false;
		@SuppressWarnings("unchecked")
		RelationView<D> other = ((RelationView<D>) obj);
		return Objects.equals(representation, other.representation);
	}
}