|
19 | 19 |
|
20 | 20 | import lombok.extern.slf4j.Slf4j; |
21 | 21 | import org.powerflows.dmn.engine.evaluator.context.EvaluationContext; |
22 | | -import org.powerflows.dmn.engine.evaluator.expression.comparator.ObjectsComparator; |
23 | | -import org.powerflows.dmn.engine.evaluator.expression.provider.EvaluationProviderFactory; |
| 22 | +import org.powerflows.dmn.engine.evaluator.entry.mode.provider.EvaluationModeProvider; |
| 23 | +import org.powerflows.dmn.engine.evaluator.entry.mode.provider.EvaluationModeProviderFactory; |
24 | 24 | import org.powerflows.dmn.engine.evaluator.expression.provider.ExpressionEvaluationProvider; |
| 25 | +import org.powerflows.dmn.engine.evaluator.expression.provider.ExpressionEvaluationProviderFactory; |
25 | 26 | import org.powerflows.dmn.engine.evaluator.type.converter.TypeConverter; |
26 | 27 | import org.powerflows.dmn.engine.evaluator.type.converter.TypeConverterFactory; |
27 | 28 | import org.powerflows.dmn.engine.evaluator.type.value.SpecifiedTypeValue; |
| 29 | +import org.powerflows.dmn.engine.model.decision.EvaluationMode; |
28 | 30 | import org.powerflows.dmn.engine.model.decision.field.Input; |
29 | 31 | import org.powerflows.dmn.engine.model.decision.field.ValueType; |
30 | 32 | import org.powerflows.dmn.engine.model.decision.rule.entry.InputEntry; |
31 | 33 |
|
| 34 | +import java.io.Serializable; |
| 35 | + |
32 | 36 | @Slf4j |
33 | 37 | public class InputEntryEvaluator { |
34 | 38 |
|
35 | | - private final EvaluationProviderFactory evaluationProviderFactory; |
| 39 | + private final ExpressionEvaluationProviderFactory expressionEvaluationProviderFactory; |
36 | 40 | private final TypeConverterFactory typeConverterFactory; |
37 | | - private final ObjectsComparator objectsComparator; |
| 41 | + private final EvaluationModeProviderFactory evaluationModeProviderFactory; |
38 | 42 |
|
39 | 43 |
|
40 | | - public InputEntryEvaluator(final EvaluationProviderFactory evaluationProviderFactory, |
| 44 | + public InputEntryEvaluator(final ExpressionEvaluationProviderFactory expressionEvaluationProviderFactory, |
41 | 45 | final TypeConverterFactory typeConverterFactory, |
42 | | - final ObjectsComparator objectsComparator) { |
43 | | - this.evaluationProviderFactory = evaluationProviderFactory; |
| 46 | + final EvaluationModeProviderFactory evaluationModeProviderFactory) { |
| 47 | + this.expressionEvaluationProviderFactory = expressionEvaluationProviderFactory; |
44 | 48 | this.typeConverterFactory = typeConverterFactory; |
45 | | - this.objectsComparator = objectsComparator; |
| 49 | + this.evaluationModeProviderFactory = evaluationModeProviderFactory; |
46 | 50 | } |
47 | 51 |
|
48 | | - public boolean evaluate(final InputEntry inputEntry, final Input input, final EvaluationContext evaluationContext) { |
49 | | - final ExpressionEvaluationProvider inputEntryExpressionEvaluator = evaluationProviderFactory.getInstance(inputEntry.getExpression().getType()); |
| 52 | + public boolean evaluate(final InputEntry inputEntry, |
| 53 | + final Input input, |
| 54 | + final EvaluationContext evaluationContext) { |
| 55 | + log.debug("Starting evaluation of input entry: {} with input: {} and evaluation context: {}", inputEntry, input, evaluationContext); |
| 56 | + |
| 57 | + final ExpressionEvaluationProvider inputEntryExpressionEvaluator = expressionEvaluationProviderFactory.getInstance(inputEntry.getExpression().getType()); |
50 | 58 | final TypeConverter typeConverter = typeConverterFactory.getInstance(input.getType()); |
51 | 59 |
|
52 | 60 | if (!isInputEvaluated(input, evaluationContext)) { |
53 | | - final ExpressionEvaluationProvider inputExpressionEvaluator = evaluationProviderFactory.getInstance(input.getExpression().getType()); |
54 | | - final Object evaluatedInputValue = inputExpressionEvaluator.evaluateInput(input, evaluationContext); |
| 61 | + final ExpressionEvaluationProvider inputExpressionEvaluator = expressionEvaluationProviderFactory.getInstance(input.getExpression().getType()); |
| 62 | + final Serializable evaluatedInputValue = inputExpressionEvaluator.evaluateInput(input, evaluationContext); |
55 | 63 |
|
56 | 64 | evaluationContext.addVariable(input.getName(), evaluatedInputValue); |
57 | 65 | } |
58 | 66 |
|
59 | 67 | final Object inputValue = evaluationContext.get(inputEntry.getName()); |
60 | 68 | final SpecifiedTypeValue<?> typedInputValue = typeConverter.convert(inputValue); |
61 | | - |
62 | 69 | final Object inputEntryValue = inputEntryExpressionEvaluator.evaluateEntry(inputEntry.getExpression(), evaluationContext); |
63 | 70 |
|
64 | | - final boolean result; |
65 | | - |
66 | | - if (!ValueType.BOOLEAN.equals(input.getType())) { |
67 | | - if (inputEntryValue.equals(Boolean.TRUE)) { |
68 | | - result = true; |
69 | | - } else if (inputEntryValue.equals(Boolean.FALSE)) { |
70 | | - result = false; |
| 71 | + final SpecifiedTypeValue<?> typedInputEntryValue; |
| 72 | + if (isBoolean(inputEntryValue)) { |
| 73 | + final TypeConverter booleanTypeConverter; |
| 74 | + if (ValueType.BOOLEAN == input.getType()) { |
| 75 | + booleanTypeConverter = typeConverter; |
71 | 76 | } else { |
72 | | - final SpecifiedTypeValue<?> typedInputEntryValue = typeConverter.convert(inputEntryValue); |
73 | | - result = objectsComparator.isInputEntryValueEqualInputValue(typedInputEntryValue, typedInputValue); |
| 77 | + booleanTypeConverter = typeConverterFactory.getInstance(ValueType.BOOLEAN); |
74 | 78 | } |
| 79 | + |
| 80 | + typedInputEntryValue = booleanTypeConverter.convert(inputEntryValue); |
75 | 81 | } else { |
76 | | - final SpecifiedTypeValue<?> typedInputEntryValue = typeConverter.convert(inputEntryValue); |
77 | | - result = objectsComparator.isInputEntryValueEqualInputValue(typedInputEntryValue, typedInputValue); |
| 82 | + typedInputEntryValue = typeConverter.convert(inputEntryValue); |
78 | 83 | } |
79 | 84 |
|
| 85 | + final EvaluationMode evaluationMode = inputEntry.getEvaluationMode(); |
| 86 | + final EvaluationModeProvider evaluationModeProvider = evaluationModeProviderFactory.getInstance(evaluationMode); |
| 87 | + final boolean result = evaluationModeProvider.isPositive(input.getType(), typedInputEntryValue, typedInputValue); |
| 88 | + |
80 | 89 | log.debug("Evaluated input entry result: {}", result); |
81 | 90 |
|
82 | 91 | return result; |
83 | 92 | } |
84 | 93 |
|
| 94 | + private boolean isBoolean(final Object value) { |
| 95 | + return Boolean.TRUE.equals(value) || Boolean.FALSE.equals(value); |
| 96 | + } |
| 97 | + |
85 | 98 | private boolean isInputEvaluated(final Input input, final EvaluationContext evaluationContext) { |
86 | 99 | return evaluationContext.isPresent(input.getName()); |
87 | 100 | } |
|
0 commit comments