Search in sources :

Example 6 with ExpressionEvaluator

use of org.drools.scenariosimulation.backend.expression.ExpressionEvaluator in project drools by kiegroup.

the class AbstractRunnerHelper method getParamsForBean.

protected Map<List<String>, Object> getParamsForBean(ScesimModelDescriptor scesimModelDescriptor, FactIdentifier factIdentifier, List<FactMappingValue> factMappingValues, ExpressionEvaluatorFactory expressionEvaluatorFactory) {
    Map<List<String>, Object> paramsForBean = new HashMap<>();
    boolean hasError = false;
    for (FactMappingValue factMappingValue : factMappingValues) {
        ExpressionIdentifier expressionIdentifier = factMappingValue.getExpressionIdentifier();
        FactMapping factMapping = scesimModelDescriptor.getFactMapping(factIdentifier, expressionIdentifier).orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen"));
        List<String> pathToField = factMapping.getExpressionElementsWithoutClass().stream().map(ExpressionElement::getStep).collect(toList());
        ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(factMappingValue);
        try {
            Object value = expressionEvaluator.evaluateLiteralExpression((String) factMappingValue.getRawValue(), factMapping.getClassName(), factMapping.getGenericTypes());
            paramsForBean.put(pathToField, value);
        } catch (RuntimeException e) {
            factMappingValue.setExceptionMessage(e.getMessage());
            hasError = true;
        }
    }
    if (hasError) {
        throw new ScenarioException("Error in one or more input values");
    }
    return paramsForBean;
}
Also used : FactMapping(org.drools.scenariosimulation.api.model.FactMapping) HashMap(java.util.HashMap) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Aggregations

FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)6 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)6 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)3 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)3 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)3 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)3 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)3 Map (java.util.Map)2 Collectors.toList (java.util.stream.Collectors.toList)2 BaseExpressionEvaluator (org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1