diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/eval/EvaluatorCore.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/eval/EvaluatorCore.java | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/eval/EvaluatorCore.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/eval/EvaluatorCore.java new file mode 100644 index 00000000..c45c6048 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/eval/EvaluatorCore.java | |||
@@ -0,0 +1,180 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2013, Bergmann Gabor, 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.rete.eval; | ||
10 | |||
11 | import java.util.Collections; | ||
12 | import java.util.Iterator; | ||
13 | import java.util.Map; | ||
14 | import java.util.Set; | ||
15 | |||
16 | import org.apache.log4j.Logger; | ||
17 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext; | ||
18 | import tools.refinery.viatra.runtime.matchers.psystem.IExpressionEvaluator; | ||
19 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.TupleValueProvider; | ||
21 | import tools.refinery.viatra.runtime.matchers.tuple.Tuples; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.Sets; | ||
23 | |||
24 | /** | ||
25 | * An instance of this class performs the evaluation of Java expressions. | ||
26 | * | ||
27 | * @author Bergmann Gabor | ||
28 | * @author Tamas Szabo | ||
29 | * @since 1.5 | ||
30 | */ | ||
31 | public abstract class EvaluatorCore { | ||
32 | |||
33 | protected Logger logger; | ||
34 | protected IExpressionEvaluator evaluator; | ||
35 | /** | ||
36 | * @since 2.4 | ||
37 | */ | ||
38 | protected int sourceTupleWidth; | ||
39 | private Map<String, Integer> parameterPositions; | ||
40 | protected IQueryRuntimeContext runtimeContext; | ||
41 | protected IEvaluatorNode evaluatorNode; | ||
42 | |||
43 | public EvaluatorCore(final Logger logger, final IExpressionEvaluator evaluator, | ||
44 | final Map<String, Integer> parameterPositions, final int sourceTupleWidth) { | ||
45 | this.logger = logger; | ||
46 | this.evaluator = evaluator; | ||
47 | this.parameterPositions = parameterPositions; | ||
48 | this.sourceTupleWidth = sourceTupleWidth; | ||
49 | } | ||
50 | |||
51 | public void init(final IEvaluatorNode evaluatorNode) { | ||
52 | this.evaluatorNode = evaluatorNode; | ||
53 | this.runtimeContext = evaluatorNode.getReteContainer().getNetwork().getEngine().getRuntimeContext(); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * @since 2.4 | ||
58 | */ | ||
59 | public abstract Iterable<Tuple> performEvaluation(final Tuple input); | ||
60 | |||
61 | protected abstract String evaluationKind(); | ||
62 | |||
63 | public Object evaluateTerm(final Tuple input) { | ||
64 | // actual evaluation | ||
65 | Object result = null; | ||
66 | try { | ||
67 | final TupleValueProvider tupleParameters = new TupleValueProvider(runtimeContext.unwrapTuple(input), | ||
68 | parameterPositions); | ||
69 | result = evaluator.evaluateExpression(tupleParameters); | ||
70 | } catch (final Exception e) { | ||
71 | logger.warn(String.format( | ||
72 | "The incremental pattern matcher encountered an error during %s evaluation for pattern(s) %s over values %s. Error message: %s. (Developer note: %s in %s)", | ||
73 | evaluationKind(), evaluatorNode.prettyPrintTraceInfoPatternList(), prettyPrintTuple(input), | ||
74 | e.getMessage(), e.getClass().getSimpleName(), this.evaluatorNode), e); | ||
75 | result = errorResult(); | ||
76 | } | ||
77 | |||
78 | return result; | ||
79 | } | ||
80 | |||
81 | protected String prettyPrintTuple(final Tuple tuple) { | ||
82 | return tuple.toString(); | ||
83 | } | ||
84 | |||
85 | protected Object errorResult() { | ||
86 | return null; | ||
87 | } | ||
88 | |||
89 | public static class PredicateEvaluatorCore extends EvaluatorCore { | ||
90 | |||
91 | public PredicateEvaluatorCore(final Logger logger, final IExpressionEvaluator evaluator, | ||
92 | final Map<String, Integer> parameterPositions, final int sourceTupleWidth) { | ||
93 | super(logger, evaluator, parameterPositions, sourceTupleWidth); | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public Iterable<Tuple> performEvaluation(final Tuple input) { | ||
98 | final Object result = evaluateTerm(input); | ||
99 | if (Boolean.TRUE.equals(result)) { | ||
100 | return Collections.singleton(input); | ||
101 | } else { | ||
102 | return null; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | @Override | ||
107 | protected String evaluationKind() { | ||
108 | return "check()"; | ||
109 | } | ||
110 | |||
111 | } | ||
112 | |||
113 | public static class FunctionEvaluatorCore extends EvaluatorCore { | ||
114 | |||
115 | /** | ||
116 | * @since 2.4 | ||
117 | */ | ||
118 | protected final boolean isUnwinding; | ||
119 | |||
120 | public FunctionEvaluatorCore(final Logger logger, final IExpressionEvaluator evaluator, | ||
121 | final Map<String, Integer> parameterPositions, final int sourceTupleWidth) { | ||
122 | this(logger, evaluator, parameterPositions, sourceTupleWidth, false); | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * @since 2.4 | ||
127 | */ | ||
128 | public FunctionEvaluatorCore(final Logger logger, final IExpressionEvaluator evaluator, | ||
129 | final Map<String, Integer> parameterPositions, final int sourceTupleWidth, final boolean isUnwinding) { | ||
130 | super(logger, evaluator, parameterPositions, sourceTupleWidth); | ||
131 | this.isUnwinding = isUnwinding; | ||
132 | } | ||
133 | |||
134 | @Override | ||
135 | public Iterable<Tuple> performEvaluation(final Tuple input) { | ||
136 | final Object result = evaluateTerm(input); | ||
137 | if (result != null) { | ||
138 | if (this.isUnwinding) { | ||
139 | final Set<?> resultAsSet = (result instanceof Set<?>) ? (Set<?>) result | ||
140 | : (result instanceof Iterable<?>) ? Sets.newSet((Iterable<?>) result) : null; | ||
141 | |||
142 | if (resultAsSet != null) { | ||
143 | return () -> { | ||
144 | final Iterator<?> wrapped = resultAsSet.iterator(); | ||
145 | return new Iterator<Tuple>() { | ||
146 | @Override | ||
147 | public boolean hasNext() { | ||
148 | return wrapped.hasNext(); | ||
149 | } | ||
150 | |||
151 | @Override | ||
152 | public Tuple next() { | ||
153 | final Object next = wrapped.next(); | ||
154 | return Tuples.staticArityLeftInheritanceTupleOf(input, | ||
155 | runtimeContext.wrapElement(next)); | ||
156 | } | ||
157 | }; | ||
158 | }; | ||
159 | } else { | ||
160 | throw new IllegalStateException( | ||
161 | "This is an unwinding evaluator, which expects the evaluation result to either be a set or an iterable, but it was " | ||
162 | + result); | ||
163 | } | ||
164 | } else { | ||
165 | return Collections.singleton( | ||
166 | Tuples.staticArityLeftInheritanceTupleOf(input, runtimeContext.wrapElement(result))); | ||
167 | } | ||
168 | } else { | ||
169 | return null; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | @Override | ||
174 | protected String evaluationKind() { | ||
175 | return "eval" + (this.isUnwinding ? "Unwind" : "") + "()"; | ||
176 | } | ||
177 | |||
178 | } | ||
179 | |||
180 | } | ||