Search in sources :

Example 1 with ExpressionEvaluator

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

the class DMNScenarioRunnerHelper method verifyConditions.

@Override
protected void verifyConditions(ScesimModelDescriptor scesimModelDescriptor, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, Map<String, Object> requestContext) {
    DMNResult dmnResult = (DMNResult) requestContext.get(DMNScenarioExecutableBuilder.DMN_RESULT);
    List<DMNMessage> dmnMessages = dmnResult.getMessages();
    for (ScenarioExpect output : scenarioRunnerData.getExpects()) {
        FactIdentifier factIdentifier = output.getFactIdentifier();
        String decisionName = factIdentifier.getName();
        DMNDecisionResult decisionResult = dmnResult.getDecisionResultByName(decisionName);
        if (decisionResult == null) {
            throw new ScenarioException("DMN execution has not generated a decision result with name " + decisionName);
        }
        for (FactMappingValue expectedResult : output.getExpectedResult()) {
            ExpressionIdentifier expressionIdentifier = expectedResult.getExpressionIdentifier();
            FactMapping factMapping = scesimModelDescriptor.getFactMapping(factIdentifier, expressionIdentifier).orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen"));
            ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(expectedResult);
            ScenarioResult scenarioResult = fillResult(expectedResult, () -> getSingleFactValueResult(factMapping, expectedResult, decisionResult, dmnMessages, expressionEvaluator), expressionEvaluator);
            scenarioRunnerData.addResult(scenarioResult);
        }
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) DMNMessage(org.kie.dmn.api.core.DMNMessage) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult)

Example 2 with ExpressionEvaluator

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

the class AbstractRunnerHelperTest method fillResult.

@Test
public void fillResult() {
    FactIdentifier factIdentifier = FactIdentifier.create("MyInstance", String.class.getCanonicalName());
    ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create("MyProperty", FactMappingType.GIVEN);
    FactMappingValue expectedResultSpy = spy(new FactMappingValue(factIdentifier, expressionIdentifier, VALUE));
    AtomicReference<ValueWrapper> resultWrapperAtomicReference = new AtomicReference<>();
    Supplier<ValueWrapper<?>> resultWrapperSupplier = resultWrapperAtomicReference::get;
    ExpressionEvaluator expressionEvaluator = new BaseExpressionEvaluator(AbstractRunnerHelper.class.getClassLoader());
    // Success
    resultWrapperAtomicReference.set(ValueWrapper.of(VALUE));
    assertTrue(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).resetStatus();
    reset(expectedResultSpy);
    // Fail with expected value
    resultWrapperAtomicReference.set(ValueWrapper.errorWithValidValue(VALUE, "value1"));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).setErrorValue(VALUE);
    reset(expectedResultSpy);
    // Fail with exception while reverting actual value
    resultWrapperAtomicReference.set(ValueWrapper.errorWithValidValue(VALUE, "value1"));
    ExpressionEvaluator expressionEvaluatorMock = mock(ExpressionEvaluator.class);
    when(expressionEvaluatorMock.fromObjectToExpression(any())).thenThrow(new IllegalArgumentException("Error"));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluatorMock).getResult());
    verify(expectedResultSpy, times(1)).setExceptionMessage("Error");
    reset(expectedResultSpy);
    // Fail in collection case
    List<String> pathToValue = Arrays.asList("field1", "fields2");
    resultWrapperAtomicReference.set(ValueWrapper.errorWithCollectionPathToValue(VALUE, pathToValue));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).setCollectionPathToValue(pathToValue);
    verify(expectedResultSpy, times(1)).setErrorValue(VALUE);
    // Fail with exception
    resultWrapperAtomicReference.set(ValueWrapper.errorWithMessage("detailedError"));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).setExceptionMessage("detailedError");
}
Also used : ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) AtomicReference(java.util.concurrent.atomic.AtomicReference) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) Test(org.junit.Test)

Example 3 with ExpressionEvaluator

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

the class AbstractRunnerHelperTest method getResultWrapper.

@Test
public void getResultWrapper() {
    ExpressionEvaluator expressionEvaluatorMock = mock(ExpressionEvaluator.class);
    Object resultRaw = "test";
    Object expectedResultRaw = "";
    String collectionWrongValue = "value";
    String collectionValuePath = "Item #: 1";
    String genericErrorMessage = "errorMessage";
    // case 1: succeed
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(ExpressionEvaluatorResult.ofSuccessful());
    ValueWrapper valueWrapper = abstractRunnerHelper.getResultWrapper(String.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, String.class);
    assertTrue(valueWrapper.isValid());
    assertNull(valueWrapper.getCollectionPathToValue());
    // case 2: failed with actual value
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(ExpressionEvaluatorResult.ofFailed());
    valueWrapper = abstractRunnerHelper.getResultWrapper(String.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, String.class);
    assertFalse(valueWrapper.isValid());
    assertEquals(resultRaw, valueWrapper.getValue());
    assertNull(valueWrapper.getCollectionPathToValue());
    // case 3: failed without actual value (list)
    valueWrapper = abstractRunnerHelper.getResultWrapper(List.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, List.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertTrue(valueWrapper.getCollectionPathToValue().isEmpty());
    assertNull(valueWrapper.getValue());
    // case 4: failed without actual value (map)
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertTrue(valueWrapper.getCollectionPathToValue().isEmpty());
    assertNull(valueWrapper.getValue());
    // case 5: failed with wrong value (list)
    ExpressionEvaluatorResult result = ExpressionEvaluatorResult.ofFailed(collectionWrongValue, collectionValuePath);
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(result);
    valueWrapper = abstractRunnerHelper.getResultWrapper(List.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, List.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertEquals(collectionWrongValue, valueWrapper.getValue());
    // case 6: failed without actual value (map)
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertEquals(collectionWrongValue, valueWrapper.getValue());
    // case 7: failed without wrong value (list)
    result = ExpressionEvaluatorResult.ofFailed(null, collectionValuePath);
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(result);
    valueWrapper = abstractRunnerHelper.getResultWrapper(List.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, List.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertNull(valueWrapper.getValue());
    // case 8: failed without actual value (map)
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertNull(valueWrapper.getValue());
    // case 9: failed with generic exception
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenThrow(new IllegalArgumentException(genericErrorMessage));
    FactMappingValue expectedResult5 = new FactMappingValue();
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), expectedResult5, expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertEquals(genericErrorMessage, valueWrapper.getErrorMessage().get());
    assertEquals(genericErrorMessage, expectedResult5.getExceptionMessage());
}
Also used : ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ExpressionEvaluatorResult(org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult) List(java.util.List) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ExpressionEvaluator

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

the class RuleScenarioRunnerHelper method getScenarioResultsFromGivenFacts.

protected List<ScenarioResult> getScenarioResultsFromGivenFacts(ScesimModelDescriptor scesimModelDescriptor, List<ScenarioExpect> scenarioOutputsPerFact, InstanceGiven input, ExpressionEvaluatorFactory expressionEvaluatorFactory) {
    Object factInstance = input.getValue();
    List<ScenarioResult> scenarioResults = new ArrayList<>();
    for (ScenarioExpect scenarioExpect : scenarioOutputsPerFact) {
        if (scenarioExpect.isNewFact()) {
            continue;
        }
        for (FactMappingValue expectedResult : scenarioExpect.getExpectedResult()) {
            ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(expectedResult);
            ScenarioResult scenarioResult = fillResult(expectedResult, () -> createExtractorFunction(expressionEvaluator, expectedResult, scesimModelDescriptor).apply(factInstance), expressionEvaluator);
            scenarioResults.add(scenarioResult);
        }
    }
    return scenarioResults;
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ArrayList(java.util.ArrayList) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)

Example 5 with ExpressionEvaluator

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

the class RuleScenarioRunnerHelper method executeScenario.

@Override
protected Map<String, Object> executeScenario(KieContainer kieContainer, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, ScesimModelDescriptor scesimModelDescriptor, Settings settings) {
    if (!Type.RULE.equals(settings.getType())) {
        throw new ScenarioException("Impossible to run a not-RULE simulation with RULE runner");
    }
    RuleScenarioExecutableBuilder ruleScenarioExecutableBuilder = createBuilderWrapper(kieContainer, settings);
    if (settings.getRuleFlowGroup() != null) {
        ruleScenarioExecutableBuilder.setActiveRuleFlowGroup(settings.getRuleFlowGroup());
    }
    loadInputData(scenarioRunnerData.getBackgrounds(), ruleScenarioExecutableBuilder);
    loadInputData(scenarioRunnerData.getGivens(), ruleScenarioExecutableBuilder);
    // all new facts should be verified internally to the working memory
    scenarioRunnerData.getExpects().stream().filter(ScenarioExpect::isNewFact).flatMap(output -> output.getExpectedResult().stream().map(ScenarioResult::new)).forEach(scenarioResult -> {
        Class<?> clazz = ScenarioBeanUtil.loadClass(scenarioResult.getFactIdentifier().getClassName(), kieContainer.getClassLoader());
        ExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory.getOrCreate(scenarioResult.getFactMappingValue());
        scenarioRunnerData.addResult(scenarioResult);
        ruleScenarioExecutableBuilder.addInternalCondition(clazz, createExtractorFunction(expressionEvaluator, scenarioResult.getFactMappingValue(), scesimModelDescriptor), scenarioResult);
    });
    return ruleScenarioExecutableBuilder.run();
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) RuleScenarioExecutableBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder) ScenarioResultMetadata(org.drools.scenariosimulation.backend.runner.model.ScenarioResultMetadata) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ExpressionElement(org.drools.scenariosimulation.api.model.ExpressionElement) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScenarioBeanUtil.fillBean(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.fillBean) Map(java.util.Map) COVERAGE_LISTENER(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.COVERAGE_LISTENER) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) RuleScenarioExecutableBuilder.createBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.createBuilder) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) ScenarioBeanWrapper(org.drools.scenariosimulation.backend.util.ScenarioBeanWrapper) CoverageAgendaListener(org.drools.scenariosimulation.backend.fluent.CoverageAgendaListener) Settings(org.drools.scenariosimulation.api.model.Settings) ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ScenarioBeanUtil(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil) Set(java.util.Set) ConstantsHolder(org.drools.scenariosimulation.api.utils.ConstantsHolder) KieContainer(org.kie.api.runtime.KieContainer) Objects(java.util.Objects) Type(org.drools.scenariosimulation.api.model.ScenarioSimulationModel.Type) ExpressionEvaluatorFactory(org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorFactory) List(java.util.List) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) Collectors.toList(java.util.stream.Collectors.toList) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) RULES_AVAILABLE(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder.RULES_AVAILABLE) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) RuleScenarioExecutableBuilder(org.drools.scenariosimulation.backend.fluent.RuleScenarioExecutableBuilder)

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