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
70
71
72
73
74
75
76
|
/*******************************************************************************
* Copyright (c) 2010-2013, Zoltan Ujhelyi, Akos Horvath, Istvan Rath and Daniel Varro
* Copyright (c) 2023 The Refinery Authors <https://refinery.tools/>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-v20.html.
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package tools.refinery.store.query.viatra.internal.localsearch;
import org.eclipse.viatra.query.runtime.localsearch.MatchingFrame;
import org.eclipse.viatra.query.runtime.localsearch.matcher.ISearchContext;
import org.eclipse.viatra.query.runtime.localsearch.operations.ISearchOperation.ISearchOperationExecutor;
import java.util.Iterator;
/**
* An operation that can be used to enumerate all possible values for a single position based on a constraint
* @author Zoltan Ujhelyi, Akos Horvath
* @since 2.0
*/
abstract class ExtendOperationExecutor<T> implements ISearchOperationExecutor {
private Iterator<? extends T> it;
/**
* Returns an iterator with the possible options from the current state
* @since 2.0
*/
@SuppressWarnings("squid:S1452")
protected abstract Iterator<? extends T> getIterator(MatchingFrame frame, ISearchContext context);
/**
* Updates the frame with the next element of the iterator. Called during {@link #execute(MatchingFrame, ISearchContext)}.
*
* @return true if the update is successful or false otherwise; in case of false is returned, the next element should be taken from the iterator.
* @since 2.0
*/
protected abstract boolean fillInValue(T newValue, MatchingFrame frame, ISearchContext context);
/**
* Restores the frame to the state before {@link #fillInValue(Object, MatchingFrame, ISearchContext)}. Called during
* {@link #onBacktrack(MatchingFrame, ISearchContext)}.
*
* @since 2.0
*/
protected abstract void cleanup(MatchingFrame frame, ISearchContext context);
@Override
public void onInitialize(MatchingFrame frame, ISearchContext context) {
it = getIterator(frame, context);
}
@Override
public void onBacktrack(MatchingFrame frame, ISearchContext context) {
it = null;
}
/**
* Fixed version of {@link org.eclipse.viatra.query.runtime.localsearch.operations.ExtendOperationExecutor#execute}
* that handles failed unification of variables correctly.
* @param frame The matching frame to extend.
* @param context The search context.
* @return {@code true} if an extension was found, {@code false} otherwise.
*/
@Override
public boolean execute(MatchingFrame frame, ISearchContext context) {
while (it.hasNext()) {
var newValue = it.next();
if (fillInValue(newValue, frame, context)) {
return true;
}
}
return false;
}
}
|