aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/ExtendOperationExecutor.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/ExtendOperationExecutor.java')
-rw-r--r--subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/ExtendOperationExecutor.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/ExtendOperationExecutor.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/ExtendOperationExecutor.java
new file mode 100644
index 00000000..a72c30dd
--- /dev/null
+++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/ExtendOperationExecutor.java
@@ -0,0 +1,77 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2013, Zoltan Ujhelyi, Akos Horvath, Istvan Rath and Daniel Varro
3 * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/>
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-v20.html.
7 *
8 * SPDX-License-Identifier: EPL-2.0
9 *******************************************************************************/
10package tools.refinery.viatra.runtime.localsearch.operations;
11
12import tools.refinery.viatra.runtime.localsearch.MatchingFrame;
13import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext;
14import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation.ISearchOperationExecutor;
15
16import java.util.Iterator;
17
18/**
19 * An operation that can be used to enumerate all possible values for a single position based on a constraint
20 * @author Zoltan Ujhelyi, Akos Horvath
21 * @noextend This class is not intended to be subclassed by clients.
22 * @since 2.0
23 */
24public abstract class ExtendOperationExecutor<T> implements ISearchOperationExecutor {
25
26 private Iterator<? extends T> it;
27
28 /**
29 * Returns an iterator with the possible options from the current state
30 * @since 2.0
31 */
32 protected abstract Iterator<? extends T> getIterator(MatchingFrame frame, ISearchContext context);
33 /**
34 * Updates the frame with the next element of the iterator. Called during {@link #execute(MatchingFrame, ISearchContext)}.
35 *
36 * @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.
37 * @since 2.0
38 */
39 protected abstract boolean fillInValue(T newValue, MatchingFrame frame, ISearchContext context);
40
41 /**
42 * Restores the frame to the state before {@link #fillInValue(Object, MatchingFrame, ISearchContext)}. Called during
43 * {@link #onBacktrack(MatchingFrame, ISearchContext)}.
44 *
45 * @since 2.0
46 */
47 protected abstract void cleanup(MatchingFrame frame, ISearchContext context);
48
49 @Override
50 public void onInitialize(MatchingFrame frame, ISearchContext context) {
51 it = getIterator(frame, context);
52 }
53
54 @Override
55 public void onBacktrack(MatchingFrame frame, ISearchContext context) {
56 it = null;
57
58 }
59
60 /**
61 * Fixed version that handles failed unification of variables correctly.
62 * @param frame The matching frame to extend.
63 * @param context The search context.
64 * @return {@code true} if an extension was found, {@code false} otherwise.
65 */
66 @Override
67 public boolean execute(MatchingFrame frame, ISearchContext context) {
68 while (it.hasNext()) {
69 var newValue = it.next();
70 if (fillInValue(newValue, frame, context)) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77}