use of org.drools.scenariosimulation.api.model.FactMapping 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;
}
use of org.drools.scenariosimulation.api.model.FactMapping in project drools-wb by kiegroup.
the class ScenarioGridTest method setup.
@Before
public void setup() {
simulation = getSimulation();
when(scenarioGridColumnMock.getPropertyHeaderMetaData()).thenReturn(propertyHeaderMetadataMock);
when(scenarioGridModelMock.getAbstractScesimModel()).thenReturn(Optional.of(simulation));
when(scenarioGridModelMock.getGridWidget()).thenReturn(GridWidget.SIMULATION);
when(scenarioGridModelMock.getScenarioExpressionCellTextAreaSingletonDOMElementFactory()).thenReturn(expressionCellTextAreaSingletonDOMElementFactoryMock);
when(scenarioGridModelMock.getCollectionEditorSingletonDOMElementFactory()).thenReturn(collectionEditorSingletonDOMElementFactory);
factIdentifierGiven = FactIdentifier.create("GIVEN", "GIVEN");
factIdentifierInteger = FactIdentifier.create("Integer", "java.lang.Integer");
factMappingDescription = new FactMapping(EXPRESSION_ALIAS_DESCRIPTION, FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION);
factMappingGiven = new FactMapping(EXPRESSION_ALIAS_GIVEN, factIdentifierGiven, new ExpressionIdentifier("GIVEN", FactMappingType.GIVEN));
factMappingInteger = new FactMapping(EXPRESSION_ALIAS_INTEGER, factIdentifierInteger, new ExpressionIdentifier("GIVEN", FactMappingType.GIVEN));
scenarioGridSpy = spy(new ScenarioGrid(scenarioGridModelMock, scenarioGridLayerMock, scenarioGridRendererMock, scenarioContextMenuRegistryMock) {
@Override
protected <T extends AbstractScesimData> void appendRow(int rowIndex, T scesimData) {
// do nothing
}
@Override
protected ScenarioSimulationBuilders.HeaderBuilder getHeaderBuilderLocal(String instanceTitle, String propertyTitle, String columnId, String columnGroup, FactMappingType factMappingType) {
return headerBuilderMock;
}
@Override
protected ScenarioGridColumn getScenarioGridColumnLocal(ScenarioSimulationBuilders.HeaderBuilder headerBuilder, String placeHolder) {
return scenarioGridColumnMock;
}
@Override
protected BaseGridRendererHelper getBaseGridRendererHelper() {
return rendererHelperMock;
}
@Override
public Viewport getViewport() {
return viewportMock;
}
@Override
protected ScenarioHeaderMetaData getColumnScenarioHeaderMetaData(final ScenarioGridColumn scenarioGridColumn, final int rowIndex) {
return propertyHeaderMetadataMock;
}
@Override
protected EnableTestToolsEvent getEnableTestToolsEvent(final ScenarioGrid scenarioGrid, final ScenarioGridColumn scenarioGridColumn, final ScenarioHeaderMetaData scenarioHeaderMetaData, int uiColumnIndex, String group) {
return new EnableTestToolsEvent();
}
@Override
public Layer getLayer() {
return scenarioGridLayerMock;
}
});
when(rendererHelperMock.getRenderingInformation()).thenReturn(renderingInformationMock);
when(renderingInformationMock.getHeaderRowsHeight()).thenReturn(HEADER_ROWS_HEIGHT);
when(renderingInformationMock.getFloatingBlockInformation()).thenReturn(floatingBlockInformationMock);
when(propertyHeaderMetadataMock.getColumnGroup()).thenReturn(GRID_COLUMN_GROUP);
scenarioGridSpy.setEventBus(eventBusMock);
}
use of org.drools.scenariosimulation.api.model.FactMapping in project drools-wb by kiegroup.
the class DMNScenarioValidationTest method validate.
@Test
public void validate() {
DMNScenarioValidation validationSpy = spy(new DMNScenarioValidation() {
@Override
protected DMNModel getDMNModel(KieContainer kieContainer, String dmnPath) {
return dmnModelMock;
}
});
// Test 0 - skip empty or not GIVEN/EXPECT columns
Simulation test0 = new Simulation();
test0.getScesimModelDescriptor().addFactMapping(FactIdentifier.DESCRIPTION, ExpressionIdentifier.create(VALUE, FactMappingType.OTHER));
test0.getScesimModelDescriptor().addFactMapping(FactIdentifier.EMPTY, ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
List<FactMappingValidationError> errorsTest0 = validationSpy.validate(test0, settingsLocal, null);
checkResult(errorsTest0);
// Test 1 - simple type
Simulation test1 = new Simulation();
test1.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", "tMYSIMPLETYPE"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
createDMNType("mySimpleType", "mySimpleType");
List<FactMappingValidationError> errorsTest1 = validationSpy.validate(test1, settingsLocal, null);
checkResult(errorsTest1);
// Test 2 - nested field
Simulation test2 = new Simulation();
// nameFM is valid
FactIdentifier myComplexFactIdentifier = FactIdentifier.create("myComplexType", "tMYCOMPLEXTYPE");
FactMapping nameFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("name", FactMappingType.GIVEN));
nameFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
nameFM.addExpressionElement("name", "tNAME");
createDMNType("myComplexType", "myComplexType", "name");
// parentFM is valid
FactMapping parentFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent", FactMappingType.EXPECT));
parentFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
parentFM.addExpressionElement("parent", "tPARENT");
createDMNType("myComplexType", "myComplexType", "parent");
List<FactMappingValidationError> errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
checkResult(errorsTest2);
// parentFM is not valid anymore
parentFM.addExpressionElement("notExisting", "notExisting");
errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"));
// nameWrongTypeFM has a wrong type
FactMapping nameWrongTypeFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent2", FactMappingType.EXPECT));
nameWrongTypeFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
nameWrongTypeFM.addExpressionElement("name", Integer.class.getCanonicalName());
errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "tNAME")));
// color parameter - Constraint added for its type (string to tColor with allowed values)
FactMapping colorsAddedConstraintFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent3", FactMappingType.GIVEN));
colorsAddedConstraintFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
colorsAddedConstraintFM.addExpressionElement("color", BuiltInType.STRING.getName());
createDMNType("myComplexType", "myComplexType", "color");
DMNType baseDMNType = initDMNType(BuiltInType.STRING);
when(mapOfMockDecisions.get("myComplexType").getResultType().getFields().get("color").getAllowedValues()).thenReturn(Arrays.asList(new UnaryTestImpl(null, "Value")));
when(mapOfMockDecisions.get("myComplexType").getResultType().getFields().get("color").getBaseType()).thenReturn(baseDMNType);
errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "tNAME")), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_ADDED_CONSTRAINT_ERROR, Collections.emptyList()));
// age parameter - Constraint removed for its type (tAge to numeric without allowed values)
FactMapping ageConstraintFM = test2.getScesimModelDescriptor().addFactMapping(myComplexFactIdentifier, ExpressionIdentifier.create("parent4", FactMappingType.GIVEN));
ageConstraintFM.addExpressionElement("tMYCOMPLEXTYPE", "tMYCOMPLEXTYPE");
ageConstraintFM.addExpressionElement("age", "age");
createDMNType("myComplexType", "myComplexType", "age");
errorsTest2 = validationSpy.validate(test2, settingsLocal, null);
checkResult(errorsTest2, new ExpectedError("Impossible to find field 'notExisting' in type 'tPARENT'"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "tNAME")), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_ADDED_CONSTRAINT_ERROR, Collections.emptyList()), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_REMOVED_CONSTRAINT_ERROR, Collections.emptyList()));
// Test 3 - list
Simulation test3 = new Simulation();
// topLevelListFM is valid
FactMapping topLevelListFM = test3.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("myList", List.class.getCanonicalName()), ExpressionIdentifier.create("name", FactMappingType.GIVEN));
topLevelListFM.addExpressionElement("tPERSON", List.class.getCanonicalName());
topLevelListFM.setGenericTypes(Collections.singletonList("tPERSON"));
createDMNType("myList", "person");
when(mapOfMockDecisions.get("myList").getResultType().isCollection()).thenReturn(true);
// addressesFM is valid
FactMapping addressesFM = test3.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("myComplexObject", "tMYCOMPLEXOBJECT"), ExpressionIdentifier.create("addresses", FactMappingType.EXPECT));
addressesFM.addExpressionElement("tMYCOMPLEXOBJECT", "tMYCOMPLEXOBJECT");
addressesFM.addExpressionElement("addresses", List.class.getCanonicalName());
addressesFM.setGenericTypes(Collections.singletonList("tADDRESSES"));
createDMNType("myComplexObject", "myComplexObject", "addresses");
when(mapOfMockDecisions.get("myComplexObject").getResultType().getFields().get("addresses").isCollection()).thenReturn(true);
List<FactMappingValidationError> errorsTest3 = validationSpy.validate(test3, settingsLocal, null);
checkResult(errorsTest3);
// Test 4 - not existing node
Simulation test4 = new Simulation();
FactMapping factMappingNodeRemoved = test4.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", "tMYSIMPLETYPE"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
factMappingNodeRemoved.addExpressionElement("tMYSIMPLETYPE", "tMYSIMPLETYPE");
when(dmnModelMock.getDecisionByName(anyString())).thenReturn(null);
List<FactMappingValidationError> errorsTest4 = validationSpy.validate(test4, settingsLocal, null);
checkResult(errorsTest4, new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_NODE_CHANGED_ERROR, Arrays.asList("tMYSIMPLETYPE", "node not found")));
}
use of org.drools.scenariosimulation.api.model.FactMapping in project drools-wb by kiegroup.
the class FactMappingValidationErrorTest method setup.
@Before
public void setup() {
factMapping = new FactMapping(FactIdentifier.create("myType", "tMYTYPE"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
factMapping.setFactAlias("FactAlias");
factMapping.setExpressionAlias("ExpressionAlias");
}
use of org.drools.scenariosimulation.api.model.FactMapping in project drools-wb by kiegroup.
the class DMNScenarioValidation method validate.
/**
* Validate structure of a DMN test scenario.
* Supported checks for each column:
* - empty column skip
* - DMN node removed
* - simple type becomes complex type
* - navigation of data type still valid
* - field type changed
* @param simulation
* @param settings
* @param kieContainer
* @return
*/
@Override
public List<FactMappingValidationError> validate(Simulation simulation, Settings settings, KieContainer kieContainer) {
List<FactMappingValidationError> errors = new ArrayList<>();
String dmnFilePath = settings.getDmnFilePath();
DMNModel dmnModel = getDMNModel(kieContainer, dmnFilePath);
for (FactMapping factMapping : simulation.getScesimModelDescriptor().getFactMappings()) {
if (isToSkip(factMapping)) {
continue;
}
String nodeName = factMapping.getFactIdentifier().getName();
DMNType factDMNType;
try {
factDMNType = dmnModel.getDecisionByName(nodeName) != null ? dmnModel.getDecisionByName(nodeName).getResultType() : dmnModel.getInputByName(nodeName).getType();
} catch (NullPointerException e) {
errors.add(createNodeChangedError(factMapping, "node not found"));
continue;
}
List<String> steps = expressionElementToString(factMapping);
try {
DMNType fieldType = navigateDMNType(factDMNType, steps);
if (!isDMNFactMappingValid(factMapping, fieldType)) {
errors.add(defineFieldChangedError(factMapping, factDMNType, fieldType));
}
} catch (IllegalStateException e) {
errors.add(createGenericError(factMapping, e.getMessage()));
}
}
return errors;
}
Aggregations