Skip to content

Commit

Permalink
Added a support for a default alias of the entry name in the input sc…
Browse files Browse the repository at this point in the history
…ope. Resolved #130
  • Loading branch information
mariuszkumor committed Jan 8, 2019
1 parent 0c8a432 commit b737820
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean evaluate(final InputEntry inputEntry,

final Object inputValue = evaluationContext.get(inputEntry.getName());
final SpecifiedTypeValue<?> typedInputValue = typeConverter.convert(inputValue);
final Object inputEntryValue = inputEntryExpressionEvaluator.evaluateEntry(inputEntry.getExpression(), evaluationContext);
final Object inputEntryValue = inputEntryExpressionEvaluator.evaluateEntry(inputEntry, evaluationContext);

final SpecifiedTypeValue<?> typedInputEntryValue;
if (isBoolean(inputEntryValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import lombok.extern.slf4j.Slf4j;
import org.powerflows.dmn.engine.evaluator.context.EvaluationContext;
import org.powerflows.dmn.engine.evaluator.expression.provider.ExpressionEvaluationProviderFactory;
import org.powerflows.dmn.engine.evaluator.expression.provider.ExpressionEvaluationProvider;
import org.powerflows.dmn.engine.evaluator.expression.provider.ExpressionEvaluationProviderFactory;
import org.powerflows.dmn.engine.evaluator.type.converter.TypeConverter;
import org.powerflows.dmn.engine.evaluator.type.converter.TypeConverterFactory;
import org.powerflows.dmn.engine.model.decision.field.Output;
Expand All @@ -45,7 +45,7 @@ public EntryResult evaluate(final OutputEntry outputEntry, final Output output,
final ExpressionEvaluationProvider expressionEvaluator = expressionEvaluationProviderFactory.getInstance(outputEntry.getExpression().getType());
final TypeConverter typeConverter = typeConverterFactory.getInstance(output.getType());

final Serializable outputEntryValue = expressionEvaluator.evaluateEntry(outputEntry.getExpression(), evaluationContext);
final Serializable outputEntryValue = expressionEvaluator.evaluateEntry(outputEntry, evaluationContext);

//Needed for the output entry value validation.
//Correct build means the output entry value has a type compatible with the output definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package org.powerflows.dmn.engine.evaluator.expression.provider;

import org.powerflows.dmn.engine.evaluator.context.EvaluationContext;
import org.powerflows.dmn.engine.model.decision.expression.Expression;
import org.powerflows.dmn.engine.model.decision.field.Input;
import org.powerflows.dmn.engine.model.decision.rule.entry.Entry;

import java.io.Serializable;

public interface ExpressionEvaluationProvider {

Serializable evaluateInput(Input input, EvaluationContext evaluationContext);

Serializable evaluateEntry(Expression entryExpression, EvaluationContext evaluationContext);
Serializable evaluateEntry(Entry entry, EvaluationContext evaluationContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
package org.powerflows.dmn.engine.evaluator.expression.provider;

import org.powerflows.dmn.engine.evaluator.context.EvaluationContext;
import org.powerflows.dmn.engine.model.decision.expression.Expression;
import org.powerflows.dmn.engine.model.decision.field.Input;
import org.powerflows.dmn.engine.model.decision.rule.entry.Entry;

import java.io.Serializable;

class FeelExpressionEvaluationProvider implements ExpressionEvaluationProvider {

@Override
public Serializable evaluateEntry(final Expression expression, final EvaluationContext evaluationContext) {
public Serializable evaluateInput(final Input input, final EvaluationContext evaluationContext) {
throw new UnsupportedOperationException();
}

@Override
public Serializable evaluateInput(final Input input, final EvaluationContext evaluationContext) {
public Serializable evaluateEntry(final Entry entry, final EvaluationContext evaluationContext) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
package org.powerflows.dmn.engine.evaluator.expression.provider;

import org.powerflows.dmn.engine.evaluator.context.EvaluationContext;
import org.powerflows.dmn.engine.model.decision.expression.Expression;
import org.powerflows.dmn.engine.model.decision.field.Input;
import org.powerflows.dmn.engine.model.decision.rule.entry.Entry;

import java.io.Serializable;

class JuelExpressionEvaluationProvider implements ExpressionEvaluationProvider {

@Override
public Serializable evaluateEntry(final Expression expression, final EvaluationContext evaluationContext) {
public Serializable evaluateInput(final Input input, final EvaluationContext evaluationContext) {
throw new UnsupportedOperationException();
}

@Override
public Serializable evaluateInput(final Input input, final EvaluationContext evaluationContext) {
public Serializable evaluateEntry(final Entry entry, final EvaluationContext evaluationContext) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import lombok.extern.slf4j.Slf4j;
import org.powerflows.dmn.engine.evaluator.context.EvaluationContext;
import org.powerflows.dmn.engine.model.decision.expression.Expression;
import org.powerflows.dmn.engine.model.decision.field.Input;
import org.powerflows.dmn.engine.model.decision.rule.entry.Entry;

import java.io.Serializable;

Expand All @@ -43,10 +43,10 @@ public Serializable evaluateInput(final Input input, final EvaluationContext eva
}

@Override
public Serializable evaluateEntry(final Expression entryExpression, final EvaluationContext evaluationContext) {
log.debug("Starting evaluation of entry with expression: {} and evaluation context: {}", entryExpression, evaluationContext);
public Serializable evaluateEntry(final Entry entry, final EvaluationContext evaluationContext) {
log.debug("Starting evaluation of entry {} with evaluation context: {}", entry, evaluationContext);

final Serializable result = entryExpression.getValue();
final Serializable result = entry.getExpression().getValue();

log.debug("Evaluated entry result: {}", result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.powerflows.dmn.engine.evaluator.expression.script.bindings.ContextVariablesBindings;
import org.powerflows.dmn.engine.model.decision.expression.Expression;
import org.powerflows.dmn.engine.model.decision.field.Input;
import org.powerflows.dmn.engine.model.decision.rule.entry.Entry;

import javax.script.Bindings;
import javax.script.ScriptEngine;
Expand Down Expand Up @@ -51,20 +52,31 @@ public Serializable evaluateInput(final Input input, final EvaluationContext eva
}

@Override
public Serializable evaluateEntry(final Expression entryExpression, final EvaluationContext evaluationContext) {
log.debug("Starting evaluation of entry with expression: {} and evaluation context: {}", entryExpression, evaluationContext);
public Serializable evaluateEntry(final Entry entry, final EvaluationContext evaluationContext) {
log.debug("Starting evaluation of entry {} with evaluation context: {}", entry, evaluationContext);

final Serializable result = evaluate(entryExpression, evaluationContext);
final Serializable result = evaluate(entry, evaluationContext);

log.debug("Evaluated entry result: {}", result);

return result;
}

private Serializable evaluate(final Entry entry, final EvaluationContext evaluationContext) {
final ScriptEngine scriptEngine = scriptEngineProvider.getScriptEngine(entry.getExpression().getType());
final Bindings bindings = ContextVariablesBindings.create(scriptEngine.createBindings(), evaluationContext, entry.getName());

return evaluate(entry.getExpression(), scriptEngine, bindings);
}

private Serializable evaluate(final Expression expression, final EvaluationContext evaluationContext) {
final ScriptEngine scriptEngine = scriptEngineProvider.getScriptEngine(expression.getType());
final Bindings bindings = ContextVariablesBindings.create(scriptEngine.createBindings(), evaluationContext);

return evaluate(expression, scriptEngine, bindings);
}

private Serializable evaluate(final Expression expression, final ScriptEngine scriptEngine, final Bindings bindings) {
final Serializable result;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class ContextVariablesBindings implements Bindings {

private final Bindings bindings;
private final static String DEFAULT_INPUT_NAME_ALIAS = "cellInput";

private ContextVariablesBindings(final Bindings bindings, final EvaluationContext evaluationContext) {
this.bindings = bindings;
Expand All @@ -36,10 +37,23 @@ private ContextVariablesBindings(final Bindings bindings, final EvaluationContex
.forEach(variableName -> this.bindings.put(variableName, evaluationContext.get(variableName)));
}

public static ContextVariablesBindings create(final Bindings bindings, final EvaluationContext evaluationContext) {
private ContextVariablesBindings(final Bindings bindings, final EvaluationContext evaluationContext, final String inputName) {
this(bindings, evaluationContext);

this.bindings.put(DEFAULT_INPUT_NAME_ALIAS, evaluationContext.get(inputName));
}

public static ContextVariablesBindings create(final Bindings bindings,
final EvaluationContext evaluationContext) {
return new ContextVariablesBindings(bindings, evaluationContext);
}

public static ContextVariablesBindings create(final Bindings bindings,
final EvaluationContext evaluationContext,
final String inputName) {
return new ContextVariablesBindings(bindings, evaluationContext, inputName);
}

@Override
public Object put(final String name, final Object value) {
return bindings.put(name, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018-present PowerFlows.org - all rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.powerflows.dmn.engine.model.decision.rule.entry;

import lombok.ToString;
import org.powerflows.dmn.engine.model.decision.expression.Expression;

import java.io.Serializable;

@ToString
public class Entry implements Serializable {

protected String name;
protected Expression expression;

Entry() {
}

public String getName() {
return name;
}

public Expression getExpression() {
return expression;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@
import java.util.function.Consumer;
import java.util.function.Function;

@EqualsAndHashCode
@ToString
public class InputEntry implements Serializable {
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class InputEntry extends Entry {

private static final long serialVersionUID = 1;

private String name;
private Expression expression;
private EvaluationMode evaluationMode;

private InputEntry() {
super();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
import java.util.function.Consumer;
import java.util.function.Function;

@EqualsAndHashCode
@ToString
public class OutputEntry implements Serializable {
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class OutputEntry extends Entry {

private static final long serialVersionUID = 1;

private String name;
private Expression expression;

private OutputEntry() {
super();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import org.powerflows.dmn.engine.evaluator.expression.script.ScriptEngineProvide
import org.powerflows.dmn.engine.model.decision.expression.Expression
import org.powerflows.dmn.engine.model.decision.expression.ExpressionType
import org.powerflows.dmn.engine.model.decision.field.Input
import org.powerflows.dmn.engine.model.decision.rule.entry.InputEntry
import org.powerflows.dmn.engine.model.decision.rule.entry.OutputEntry
import org.powerflows.dmn.engine.model.evaluation.variable.DecisionVariables
import spock.lang.Specification
import spock.lang.Unroll
Expand All @@ -40,12 +42,13 @@ class GroovyExpressionEvaluationProviderSpec extends Specification {
final Object entryExpressionValue, final Object contextVariable, final boolean expectedEntryResult) {
given:
final Expression entryExpression = [value: entryExpressionValue, type: ExpressionType.GROOVY]
final InputEntry inputEntry = [expression: entryExpression] as InputEntry

final DecisionVariables decisionVariables = new DecisionVariables([x: contextVariable, TestInputName: true])
final EvaluationContext evaluationContext = new EvaluationContext(decisionVariables)

when:
final boolean inputEntryResult = expressionEvaluationProvider.evaluateEntry(entryExpression, evaluationContext)
final boolean inputEntryResult = expressionEvaluationProvider.evaluateEntry(inputEntry, evaluationContext)

then:
inputEntryResult == expectedEntryResult
Expand Down Expand Up @@ -88,9 +91,10 @@ class GroovyExpressionEvaluationProviderSpec extends Specification {
final Expression entryExpression = [value: outputEntryValue, type: ExpressionType.GROOVY]
final DecisionVariables decisionVariables = new DecisionVariables([:])
final EvaluationContext contextVariables = new EvaluationContext(decisionVariables)
final OutputEntry outputEntry = [expression: entryExpression] as OutputEntry

when:
expressionEvaluationProvider.evaluateEntry(entryExpression, contextVariables)
expressionEvaluationProvider.evaluateEntry(outputEntry, contextVariables)

then:
final EvaluationException exception = thrown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import org.powerflows.dmn.engine.evaluator.expression.script.ScriptEngineProvide
import org.powerflows.dmn.engine.model.decision.expression.Expression
import org.powerflows.dmn.engine.model.decision.expression.ExpressionType
import org.powerflows.dmn.engine.model.decision.field.Input
import org.powerflows.dmn.engine.model.decision.rule.entry.InputEntry
import org.powerflows.dmn.engine.model.decision.rule.entry.OutputEntry
import org.powerflows.dmn.engine.model.evaluation.variable.DecisionVariables
import spock.lang.Specification
import spock.lang.Unroll
Expand All @@ -40,12 +42,13 @@ class JavaScriptExpressionEvaluationProviderSpec extends Specification {
final Object entryExpressionValue, final Object contextVariable, final boolean expectedEntryResult) {
given:
final Expression entryExpression = [value: entryExpressionValue, type: ExpressionType.JAVASCRIPT]
final InputEntry inputEntry = [expression: entryExpression] as InputEntry

final DecisionVariables decisionVariables = new DecisionVariables([x: contextVariable, TestInputName: true])
final EvaluationContext evaluationContext = new EvaluationContext(decisionVariables)

when:
final boolean inputEntryResult = expressionEvaluationProvider.evaluateEntry(entryExpression, evaluationContext)
final boolean inputEntryResult = expressionEvaluationProvider.evaluateEntry(inputEntry, evaluationContext)

then:
inputEntryResult == expectedEntryResult
Expand Down Expand Up @@ -86,11 +89,13 @@ class JavaScriptExpressionEvaluationProviderSpec extends Specification {
given:
final String outputEntryValue = 'x'
final Expression entryExpression = [value: outputEntryValue, type: ExpressionType.JAVASCRIPT]
final OutputEntry outputEntry = [expression: entryExpression] as OutputEntry

final DecisionVariables decisionVariables = new DecisionVariables([:])
final EvaluationContext contextVariables = new EvaluationContext(decisionVariables)

when:
expressionEvaluationProvider.evaluateEntry(entryExpression, contextVariables)
expressionEvaluationProvider.evaluateEntry(outputEntry, contextVariables)

then:
final EvaluationException exception = thrown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.powerflows.dmn.engine.evaluator.context.EvaluationContext
import org.powerflows.dmn.engine.model.decision.expression.Expression
import org.powerflows.dmn.engine.model.decision.expression.ExpressionType
import org.powerflows.dmn.engine.model.decision.field.Input
import org.powerflows.dmn.engine.model.decision.rule.entry.InputEntry
import org.powerflows.dmn.engine.model.evaluation.variable.DecisionVariables
import spock.lang.Specification

Expand All @@ -33,12 +34,13 @@ class LiteralExpressionEvaluationProviderSpec extends Specification {
final Object inputEntryValue = 5
final Object contextVariable = 6
final Expression entryExpression = [value: inputEntryValue, type: ExpressionType.LITERAL]
final InputEntry inputEntry = [expression: entryExpression] as InputEntry

final DecisionVariables decisionVariables = new DecisionVariables([TestInputName: contextVariable])
final EvaluationContext evaluationContext = new EvaluationContext(decisionVariables)

when:
final boolean inputEntryResult = expressionEvaluationProvider.evaluateEntry(entryExpression, evaluationContext)
final boolean inputEntryResult = expressionEvaluationProvider.evaluateEntry(inputEntry, evaluationContext)

then:
inputEntryResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class DecisionUtilSpec extends Specification {
then:
final DecisionBuildException exception = thrown()
exception != null
exception.getMessage() == 'Can not set value 5 for InputEntry(name=null, expression=null, evaluationMode=null)'
exception.getMessage() == 'Can not set value 5 for InputEntry(super=Entry(name=null, expression=null), evaluationMode=null)'
}

void 'should throw exception when can not find field by type'() {
Expand Down

0 comments on commit b737820

Please sign in to comment.