Search in sources :

Example 1 with FactIdentifier

use of org.drools.scenariosimulation.api.model.FactIdentifier 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 FactIdentifier

use of org.drools.scenariosimulation.api.model.FactIdentifier in project drools by kiegroup.

the class RuleScenarioRunnerHelper method verifyConditions.

@Override
protected void verifyConditions(ScesimModelDescriptor scesimModelDescriptor, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluatorFactory expressionEvaluatorFactory, Map<String, Object> requestContext) {
    for (InstanceGiven input : scenarioRunnerData.getGivens()) {
        FactIdentifier factIdentifier = input.getFactIdentifier();
        List<ScenarioExpect> assertionOnFact = scenarioRunnerData.getExpects().stream().filter(elem -> !elem.isNewFact()).filter(elem -> Objects.equals(elem.getFactIdentifier(), factIdentifier)).collect(toList());
        // check if this fact has something to check
        if (assertionOnFact.isEmpty()) {
            continue;
        }
        getScenarioResultsFromGivenFacts(scesimModelDescriptor, assertionOnFact, input, expressionEvaluatorFactory).forEach(scenarioRunnerData::addResult);
    }
}
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) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier)

Example 3 with FactIdentifier

use of org.drools.scenariosimulation.api.model.FactIdentifier in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method groupByFactIdentifierAndFilterTest.

@Test
public void groupByFactIdentifierAndFilterTest() {
    Map<FactIdentifier, List<FactMappingValue>> scenario1Given = runnerHelper.groupByFactIdentifierAndFilter(scenario1.getUnmodifiableFactMappingValues(), FactMappingType.GIVEN);
    Map<FactIdentifier, List<FactMappingValue>> scenario1Expected = runnerHelper.groupByFactIdentifierAndFilter(scenario1.getUnmodifiableFactMappingValues(), FactMappingType.EXPECT);
    Map<FactIdentifier, List<FactMappingValue>> scenario2Given = runnerHelper.groupByFactIdentifierAndFilter(scenario2.getUnmodifiableFactMappingValues(), FactMappingType.GIVEN);
    Map<FactIdentifier, List<FactMappingValue>> scenario2Expected = runnerHelper.groupByFactIdentifierAndFilter(scenario2.getUnmodifiableFactMappingValues(), FactMappingType.EXPECT);
    assertEquals(1, scenario1Given.keySet().size());
    assertEquals(1, scenario1Expected.keySet().size());
    assertEquals(2, scenario2Given.keySet().size());
    assertEquals(2, scenario2Expected.keySet().size());
    assertEquals(1, scenario1Given.get(personFactIdentifier).size());
    assertEquals(1, scenario1Expected.get(personFactIdentifier).size());
    assertEquals(1, scenario2Given.get(disputeFactIdentifier).size());
    assertEquals(1, scenario2Expected.get(disputeFactIdentifier).size());
    Scenario scenario = new Scenario();
    scenario.addMappingValue(FactIdentifier.EMPTY, ExpressionIdentifier.DESCRIPTION, null);
    assertEquals(0, runnerHelper.groupByFactIdentifierAndFilter(scenario.getUnmodifiableFactMappingValues(), FactMappingType.GIVEN).size());
}
Also used : Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Scenario(org.drools.scenariosimulation.api.model.Scenario) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 4 with FactIdentifier

use of org.drools.scenariosimulation.api.model.FactIdentifier in project drools by kiegroup.

the class AbstractRunnerHelper method extractGivenValues.

protected List<InstanceGiven> extractGivenValues(ScesimModelDescriptor scesimModelDescriptor, List<FactMappingValue> factMappingValues, ClassLoader classLoader, ExpressionEvaluatorFactory expressionEvaluatorFactory) {
    List<InstanceGiven> instanceGiven = new ArrayList<>();
    Map<FactIdentifier, List<FactMappingValue>> groupByFactIdentifier = groupByFactIdentifierAndFilter(factMappingValues, FactMappingType.GIVEN);
    boolean hasError = false;
    for (Map.Entry<FactIdentifier, List<FactMappingValue>> entry : groupByFactIdentifier.entrySet()) {
        try {
            FactIdentifier factIdentifier = entry.getKey();
            // for each fact, create a map of path to fields and values to set
            Map<List<String>, Object> paramsForBean = getParamsForBean(scesimModelDescriptor, factIdentifier, entry.getValue(), expressionEvaluatorFactory);
            Object bean = createObject(getDirectMapping(paramsForBean), factIdentifier.getClassName(), paramsForBean, classLoader);
            instanceGiven.add(new InstanceGiven(factIdentifier, bean));
        } catch (Exception e) {
            String errorMessage = e.getMessage() != null ? e.getMessage() : e.getClass().getCanonicalName();
            logger.error("Error in GIVEN data " + entry.getKey() + ": " + errorMessage, e);
            hasError = true;
        }
    }
    if (hasError) {
        throw new ScenarioException("Error in GIVEN data");
    }
    return instanceGiven;
}
Also used : ArrayList(java.util.ArrayList) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) HashMap(java.util.HashMap) Map(java.util.Map) ScenarioSimulationSharedUtils.isCollectionOrMap(org.drools.scenariosimulation.api.utils.ScenarioSimulationSharedUtils.isCollectionOrMap)

Example 5 with FactIdentifier

use of org.drools.scenariosimulation.api.model.FactIdentifier 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)

Aggregations

FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)28 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)15 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)12 List (java.util.List)10 ArrayList (java.util.ArrayList)9 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)9 Test (org.junit.Test)9 Map (java.util.Map)8 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)8 Set (java.util.Set)6 Objects (java.util.Objects)5 Collectors.toList (java.util.stream.Collectors.toList)5 ExpressionElement (org.drools.scenariosimulation.api.model.ExpressionElement)5 FactMappingType (org.drools.scenariosimulation.api.model.FactMappingType)5 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)5 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)5 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)5 Supplier (java.util.function.Supplier)4 Collectors (java.util.stream.Collectors)4 FactMappingValueStatus (org.drools.scenariosimulation.api.model.FactMappingValueStatus)4