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

import tools.refinery.viatra.runtime.rete.index.IterableIndexer;
import tools.refinery.store.map.Cursor;
import tools.refinery.store.tuple.Tuple;

import java.util.Iterator;

class FunctionalCursor<T> implements Cursor<Tuple, T> {
	private final IterableIndexer indexer;
	private final Iterator<tools.refinery.viatra.runtime.matchers.tuple.Tuple> iterator;
	private boolean terminated;
	private Tuple key;
	private T value;

	public FunctionalCursor(IterableIndexer indexer) {
		this.indexer = indexer;
		iterator = indexer.getSignatures().iterator();
	}

	@Override
	public Tuple getKey() {
		return key;
	}

	@Override
	public T getValue() {
		return value;
	}

	@Override
	public boolean isTerminated() {
		return terminated;
	}

	@Override
	public boolean move() {
		if (!terminated && iterator.hasNext()) {
			var match = iterator.next();
			key = MatcherUtils.toRefineryTuple(match);
			value = MatcherUtils.getSingleValue(indexer.get(match));
			return true;
		}
		terminated = true;
		return false;
	}
}