Search in sources :

Example 1 with ExpressionIdentifier

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

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

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

the class AbstractRunnerHelperTest method isFactMappingValueToSkip.

@Test
public void isFactMappingValueToSkip() {
    FactIdentifier factIdentifier = FactIdentifier.create("MyInstance", String.class.getCanonicalName());
    ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create("MyProperty", FactMappingType.GIVEN);
    FactMappingValue factMappingValueWithValidValue = new FactMappingValue(factIdentifier, expressionIdentifier, VALUE);
    assertFalse(abstractRunnerHelper.isFactMappingValueToSkip(factMappingValueWithValidValue));
    FactMappingValue factMappingValueWithoutValue = new FactMappingValue(factIdentifier, expressionIdentifier, null);
    assertTrue(abstractRunnerHelper.isFactMappingValueToSkip(factMappingValueWithoutValue));
}
Also used : ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Test(org.junit.Test)

Example 4 with ExpressionIdentifier

use of org.drools.scenariosimulation.api.model.ExpressionIdentifier in project drools-wb by kiegroup.

the class SimulationSettingsCreationStrategyTest method createEmptyColumn.

@Test
public void createEmptyColumn() {
    ArgumentCaptor<ExpressionIdentifier> expressionIdentifierCaptor1 = ArgumentCaptor.forClass(ExpressionIdentifier.class);
    ArgumentCaptor<ExpressionIdentifier> expressionIdentifierCaptor2 = ArgumentCaptor.forClass(ExpressionIdentifier.class);
    int placeholderId = 1;
    int columnIndex = 0;
    SimulationSettingsCreationStrategy simulationSettingsCreationStrategy = new SimulationSettingsCreationStrategy() {

        @Override
        public Simulation createSimulation(Path context, String value) {
            return null;
        }

        @Override
        public Background createBackground(Path context, String dmnFilePath) {
            return null;
        }

        @Override
        public Settings createSettings(Path context, String value) {
            return null;
        }
    };
    ScesimModelDescriptor simulationDescriptorSpy = spy(new ScesimModelDescriptor());
    Scenario scenarioSpy = spy(new Scenario());
    ScenarioWithIndex scenarioWithIndex = new ScenarioWithIndex(1, scenarioSpy);
    simulationSettingsCreationStrategy.createEmptyColumn(simulationDescriptorSpy, scenarioWithIndex, placeholderId, GIVEN, columnIndex);
    verify(simulationDescriptorSpy, times(1)).addFactMapping(eq(columnIndex), eq(FactMapping.getInstancePlaceHolder(placeholderId)), eq(FactIdentifier.EMPTY), expressionIdentifierCaptor1.capture());
    assertEquals(GIVEN, expressionIdentifierCaptor1.getValue().getType());
    verify(scenarioSpy, times(1)).addMappingValue(eq(FactIdentifier.EMPTY), expressionIdentifierCaptor2.capture(), isNull());
    assertEquals(GIVEN, expressionIdentifierCaptor2.getValue().getType());
}
Also used : Path(org.uberfire.backend.vfs.Path) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) Scenario(org.drools.scenariosimulation.api.model.Scenario) Test(org.junit.Test)

Example 5 with ExpressionIdentifier

use of org.drools.scenariosimulation.api.model.ExpressionIdentifier in project drools-wb by kiegroup.

the class ScenarioGridTest method getSimulation.

private Simulation getSimulation() {
    Simulation toReturn = new Simulation();
    ScesimModelDescriptor simulationDescriptor = toReturn.getScesimModelDescriptor();
    simulationDescriptor.addFactMapping(FactIdentifier.INDEX.getName(), FactIdentifier.INDEX, ExpressionIdentifier.INDEX);
    simulationDescriptor.addFactMapping(FactIdentifier.DESCRIPTION.getName(), FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION);
    Scenario scenario = toReturn.addData();
    int row = toReturn.getUnmodifiableData().indexOf(scenario);
    scenario.setDescription(null);
    // Add GIVEN Facts
    IntStream.range(2, 4).forEach(id -> {
        ExpressionIdentifier givenExpression = ExpressionIdentifier.create(row + "|" + id, FactMappingType.GIVEN);
        simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, givenExpression);
        scenario.addMappingValue(FactIdentifier.EMPTY, givenExpression, null);
    });
    // Add EXPECT Facts
    IntStream.range(2, 4).forEach(id -> {
        // This is to have consistent labels/names even when adding columns at runtime
        id += 2;
        ExpressionIdentifier expectedExpression = ExpressionIdentifier.create(row + "|" + id, FactMappingType.EXPECT);
        simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, expectedExpression);
        scenario.addMappingValue(FactIdentifier.EMPTY, expectedExpression, null);
    });
    return toReturn;
}
Also used : ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) Simulation(org.drools.scenariosimulation.api.model.Simulation) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) Scenario(org.drools.scenariosimulation.api.model.Scenario)

Aggregations

ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)19 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)11 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)9 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)7 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Scenario (org.drools.scenariosimulation.api.model.Scenario)4 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)4 ScenarioGridColumn (org.drools.workbench.screens.scenariosimulation.client.widgets.ScenarioGridColumn)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Background (org.drools.scenariosimulation.api.model.Background)3 FactMappingType (org.drools.scenariosimulation.api.model.FactMappingType)3 ScenarioWithIndex (org.drools.scenariosimulation.api.model.ScenarioWithIndex)3 Settings (org.drools.scenariosimulation.api.model.Settings)3 ScenarioGridCellValue (org.drools.workbench.screens.scenariosimulation.client.values.ScenarioGridCellValue)3 ScenarioGridCell (org.drools.workbench.screens.scenariosimulation.client.widgets.ScenarioGridCell)3 Before (org.junit.Before)3