diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/ExpressionCheck.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/ExpressionCheck.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/ExpressionCheck.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/ExpressionCheck.java new file mode 100644 index 00000000..fc9efdca --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/ExpressionCheck.java | |||
@@ -0,0 +1,78 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2013, Zoltan Ujhelyi, Istvan Rath and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.localsearch.operations.check; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.List; | ||
13 | import java.util.Map; | ||
14 | import java.util.function.Function; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
17 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
18 | import tools.refinery.viatra.runtime.localsearch.operations.CheckOperationExecutor; | ||
19 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
20 | import tools.refinery.viatra.runtime.localsearch.operations.MatchingFrameValueProvider; | ||
21 | import tools.refinery.viatra.runtime.matchers.psystem.IExpressionEvaluator; | ||
22 | |||
23 | /** | ||
24 | * @author Zoltan Ujhelyi | ||
25 | * @noextend This class is not intended to be subclassed by clients. | ||
26 | */ | ||
27 | public class ExpressionCheck implements ISearchOperation { | ||
28 | |||
29 | private class Executor extends CheckOperationExecutor { | ||
30 | |||
31 | @Override | ||
32 | protected boolean check(MatchingFrame frame, ISearchContext context) { | ||
33 | try { | ||
34 | boolean result = (Boolean) evaluator.evaluateExpression(new MatchingFrameValueProvider(frame, nameMap)); | ||
35 | return result; | ||
36 | } catch (Exception e) { | ||
37 | context.getLogger().warn("Error while evaluating expression", e); | ||
38 | return false; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public ISearchOperation getOperation() { | ||
44 | return ExpressionCheck.this; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | IExpressionEvaluator evaluator; | ||
49 | Map<String, Integer> nameMap; | ||
50 | |||
51 | public ExpressionCheck(IExpressionEvaluator evaluator, Map<String, Integer> nameMap) { | ||
52 | super(); | ||
53 | this.evaluator = evaluator; | ||
54 | this.nameMap = nameMap; | ||
55 | } | ||
56 | |||
57 | @Override | ||
58 | public ISearchOperationExecutor createExecutor() { | ||
59 | return new Executor(); | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public List<Integer> getVariablePositions() { | ||
64 | // XXX not sure if this is the correct implementation to get the affected variable indicies | ||
65 | return new ArrayList<>(nameMap.values()); | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public String toString() { | ||
70 | return toString(Object::toString); | ||
71 | } | ||
72 | |||
73 | @Override | ||
74 | public String toString(Function<Integer, String> variableMapping) { | ||
75 | return "check expression "+evaluator.getShortDescription(); | ||
76 | } | ||
77 | |||
78 | } | ||