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);
}
}
}
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");
}
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));
}
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());
}
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;
}
Aggregations