diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExpressionEval.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExpressionEval.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExpressionEval.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExpressionEval.java new file mode 100644 index 00000000..7186f4ac --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExpressionEval.java | |||
@@ -0,0 +1,104 @@ | |||
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.extend; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Collections; | ||
13 | import java.util.Iterator; | ||
14 | import java.util.List; | ||
15 | import java.util.Map; | ||
16 | import java.util.Set; | ||
17 | import java.util.function.Function; | ||
18 | |||
19 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
20 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
21 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
22 | import tools.refinery.viatra.runtime.localsearch.operations.MatchingFrameValueProvider; | ||
23 | import tools.refinery.viatra.runtime.matchers.psystem.IExpressionEvaluator; | ||
24 | |||
25 | /** | ||
26 | * Calculates the result of an expression and stores it inside a variable for future reference. | ||
27 | * | ||
28 | * @author Zoltan Ujhelyi | ||
29 | * | ||
30 | */ | ||
31 | public class ExpressionEval implements ISearchOperation { | ||
32 | |||
33 | private class Executor extends SingleValueExtendOperationExecutor<Object> { | ||
34 | |||
35 | public Executor(int position) { | ||
36 | super(position); | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public Iterator<?> getIterator(MatchingFrame frame, ISearchContext context) { | ||
41 | try { | ||
42 | Object result = evaluator.evaluateExpression(new MatchingFrameValueProvider(frame, nameMap)); | ||
43 | if (!unwind && result != null){ | ||
44 | return Collections.singletonList(result).iterator(); | ||
45 | } else if (unwind && result instanceof Set<?>) { | ||
46 | return ((Set<?>)result).iterator(); | ||
47 | } else { | ||
48 | return Collections.emptyIterator(); | ||
49 | } | ||
50 | } catch (Exception e) { | ||
51 | context.getLogger().warn("Error while evaluating expression", e); | ||
52 | return Collections.emptyIterator(); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public ISearchOperation getOperation() { | ||
58 | return ExpressionEval.this; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | private final IExpressionEvaluator evaluator; | ||
63 | private final boolean unwind; | ||
64 | private final Map<String, Integer> nameMap; | ||
65 | private final int position; | ||
66 | |||
67 | public ExpressionEval(IExpressionEvaluator evaluator, Map<String, Integer> nameMap, int position) { | ||
68 | this(evaluator, nameMap, false, position); | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * @since 2.7 | ||
73 | */ | ||
74 | public ExpressionEval(IExpressionEvaluator evaluator, Map<String, Integer> nameMap, boolean unwind, int position) { | ||
75 | this.evaluator = evaluator; | ||
76 | this.nameMap = nameMap; | ||
77 | this.unwind = unwind; | ||
78 | this.position = position; | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public String toString() { | ||
83 | return toString(Object::toString); | ||
84 | } | ||
85 | |||
86 | @Override | ||
87 | public String toString(Function<Integer, String> variableMapping) { | ||
88 | return "extend -"+variableMapping.apply(position)+" = expression "+evaluator.getShortDescription(); | ||
89 | } | ||
90 | |||
91 | @Override | ||
92 | public ISearchOperationExecutor createExecutor() { | ||
93 | return new Executor(position); | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public List<Integer> getVariablePositions() { | ||
98 | // XXX not sure if this is the correct implementation to get the affected variable indicies | ||
99 | List<Integer> variables = new ArrayList<>(); | ||
100 | variables.addAll(nameMap.values()); | ||
101 | return variables; | ||
102 | } | ||
103 | |||
104 | } | ||