Search in sources :

Example 46 with FactMapping

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

the class RULEScenarioValidation method validate.

/**
 * Validate structure of a RULE test scenario.
 * Supported checks for each column:
 * - empty column skip
 * - instance type removed
 * - navigation of bean 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<>();
    Map<String, Object> beanInstanceMap = new HashMap<>();
    for (FactMapping factMapping : simulation.getScesimModelDescriptor().getFactMappings()) {
        if (isToSkip(factMapping)) {
            continue;
        }
        // try to navigate using all the steps to verify if structure is still valid
        List<String> steps = expressionElementToString(factMapping);
        try {
            String instanceClassName = factMapping.getFactIdentifier().getClassName();
            if (steps.isEmpty()) {
                // in case of top level simple types just try to load the class
                loadClass(instanceClassName, kieContainer.getClassLoader());
            } else {
                Object bean = beanInstanceMap.computeIfAbsent(instanceClassName, className -> fillBean(className, Collections.emptyMap(), kieContainer.getClassLoader()));
                List<String> stepsToField = steps.subList(0, steps.size() - 1);
                String lastStep = steps.get(steps.size() - 1);
                ScenarioBeanWrapper<?> beanBeforeLastStep = navigateToObject(bean, stepsToField, true);
                ScenarioBeanWrapper<?> beanWrapper = navigateToObject(beanBeforeLastStep.getBean(), Collections.singletonList(lastStep), false);
                String targetClassName = beanWrapper.getBeanClass() != null ? beanWrapper.getBeanClass().getName() : null;
                // check if target field has valid type
                if (!Objects.equals(factMapping.getClassName(), targetClassName)) {
                    errors.add(createFieldChangedError(factMapping, targetClassName));
                }
            }
        } catch (ScenarioException e) {
            errors.add(createGenericError(factMapping, e.getMessage()));
        }
    }
    return errors;
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScenarioBeanUtil.navigateToObject(org.drools.scenariosimulation.backend.util.ScenarioBeanUtil.navigateToObject) ScenarioException(org.drools.scenariosimulation.backend.runner.ScenarioException)

Example 47 with FactMapping

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

the class RULEScenarioValidationTest method validate.

@Test
public void validate() {
    RULEScenarioValidation validation = new RULEScenarioValidation();
    // 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 = validation.validate(test0, settingsLocal, kieContainerMock);
    checkResult(errorsTest0);
    // Test 1 - simple type
    Simulation test1 = new Simulation();
    test1.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", int.class.getCanonicalName()), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    List<FactMappingValidationError> errorsTest1 = validation.validate(test1, settingsLocal, kieContainerMock);
    checkResult(errorsTest1);
    FactMapping mySimpleType = test1.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", "notValidClass"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    mySimpleType.addExpressionElement("notValidClass", "notValidClass");
    errorsTest1 = validation.validate(test1, settingsLocal, kieContainerMock);
    checkResult(errorsTest1, new ExpectedError("Impossible to load class notValidClass"));
    // Test 2 - nested field
    Simulation test2 = new Simulation();
    // nameFM is valid
    FactIdentifier myFactIdentifier = FactIdentifier.create("mySimpleType", SampleBean.class.getCanonicalName());
    FactMapping nameFM = test2.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("name", FactMappingType.GIVEN));
    nameFM.addExpressionElement("SampleBean", String.class.getCanonicalName());
    nameFM.addExpressionElement("name", String.class.getCanonicalName());
    // parentFM is valid
    FactMapping parentFM = test2.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("parent", FactMappingType.EXPECT));
    parentFM.addExpressionElement("SampleBean", SampleBean.class.getCanonicalName());
    parentFM.addExpressionElement("parent", SampleBean.class.getCanonicalName());
    List<FactMappingValidationError> errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2);
    // parentFM is not valid anymore
    parentFM.addExpressionElement("notExisting", String.class.getCanonicalName());
    errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"));
    // nameWrongTypeFM has a wrong type
    FactMapping nameWrongTypeFM = test2.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("parent2", FactMappingType.EXPECT));
    nameWrongTypeFM.addExpressionElement("SampleBean", Integer.class.getCanonicalName());
    nameWrongTypeFM.addExpressionElement("name", Integer.class.getCanonicalName());
    errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "java.lang.String")));
    // Test 3 - list
    Simulation test3 = new Simulation();
    // topLevelListFM is valid
    FactMapping topLevelListFM = test3.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", List.class.getCanonicalName()), ExpressionIdentifier.create("name", FactMappingType.GIVEN));
    topLevelListFM.addExpressionElement("List", List.class.getCanonicalName());
    topLevelListFM.setGenericTypes(Collections.singletonList(String.class.getCanonicalName()));
    // addressesFM is valid
    FactMapping addressesFM = test3.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("addresses", FactMappingType.EXPECT));
    addressesFM.addExpressionElement("SampleBean", List.class.getCanonicalName());
    addressesFM.addExpressionElement("addresses", List.class.getCanonicalName());
    addressesFM.setGenericTypes(Collections.singletonList(String.class.getCanonicalName()));
    List<FactMappingValidationError> errorsTest3 = validation.validate(test3, settingsLocal, kieContainerMock);
    checkResult(errorsTest3);
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Test(org.junit.Test)

Example 48 with FactMapping

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

the class ScenarioCsvImportExportTest method generateHeader.

@Test
public void generateHeader() throws IOException {
    ScesimModelDescriptor simulationDescriptor = new ScesimModelDescriptor();
    FactMapping test1FactMapping = createFactMapping(simulationDescriptor, 1);
    FactMapping test2FactMapping = createFactMapping(simulationDescriptor, 2);
    scenarioCsvImportExport.generateHeader(Arrays.asList(test1FactMapping, test2FactMapping), printer);
    System.out.println("output.toString() = " + output.toString());
    String[] result = output.toString().split("\r\n");
    assertEquals(3, result.length);
    assertEquals("GIVEN,GIVEN", result[0]);
    assertEquals(instanceName + 1 + "," + instanceName + 2, result[1]);
    assertEquals(propertyName + 1 + "," + propertyName + 2, result[2]);
}
Also used : ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Test(org.junit.Test)

Example 49 with FactMapping

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

the class ScenarioCsvImportExportTest method createDummySimulation.

private Simulation createDummySimulation(int numberOfColumn, int numberOfRow) {
    Simulation simulation = new Simulation();
    ScesimModelDescriptor simulationDescriptor = simulation.getScesimModelDescriptor();
    simulationDescriptor.addFactMapping(FactIdentifier.INDEX, ExpressionIdentifier.INDEX).setExpressionAlias("Index");
    simulationDescriptor.addFactMapping(FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION).setExpressionAlias("Description");
    for (int col = 0; col < numberOfColumn; col += 1) {
        createFactMapping(simulationDescriptor, col);
    }
    for (int row = 0; row < numberOfRow; row += 1) {
        Scenario scenario = simulation.addData();
        scenario.addMappingValue(FactIdentifier.INDEX, ExpressionIdentifier.INDEX, row);
        scenario.setDescription("My scenario " + row);
        for (int col = 2; col < numberOfColumn + 2; col += 1) {
            FactMapping factMappingByIndex = simulationDescriptor.getFactMappingByIndex(col);
            scenario.addMappingValue(factMappingByIndex.getFactIdentifier(), factMappingByIndex.getExpressionIdentifier(), "value_" + row + "_" + (col - 2));
        }
    }
    return simulation;
}
Also used : ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) Scenario(org.drools.scenariosimulation.api.model.Scenario)

Example 50 with FactMapping

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

the class ScenarioValidationServiceTest method validateSimulationStructure.

@Test
public void validateSimulationStructure() {
    Simulation simulation = new Simulation();
    Settings settings = new Settings();
    ScenarioValidationService scenarioValidationServiceSpy = spy(new ScenarioValidationService() {

        @Override
        protected List<FactMappingValidationError> validateDMN(Simulation simulation, Settings settings, KieContainer kieContainer) {
            return Collections.emptyList();
        }

        @Override
        protected List<FactMappingValidationError> validateRULE(Simulation simulation, Settings settings, KieContainer kieContainer) {
            return Collections.emptyList();
        }

        @Override
        protected KieContainer getKieContainer(Path path) {
            return kieContainerMock;
        }
    });
    settings.setType(ScenarioSimulationModel.Type.DMN);
    scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock);
    verify(scenarioValidationServiceSpy, never()).validateDMN(eq(simulation), eq(settings), eq(kieContainerMock));
    verify(scenarioValidationServiceSpy, never()).validateRULE(eq(simulation), eq(settings), eq(kieContainerMock));
    reset(scenarioValidationServiceSpy);
    FactMapping sampleFactMapping = simulation.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("sample", String.class.getCanonicalName()), ExpressionIdentifier.create("sample", FactMappingType.GIVEN));
    sampleFactMapping.addExpressionElement("sample", String.class.getCanonicalName());
    settings.setType(ScenarioSimulationModel.Type.DMN);
    scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock);
    verify(scenarioValidationServiceSpy, timeout(1)).validateDMN(eq(simulation), eq(settings), eq(kieContainerMock));
    reset(scenarioValidationServiceSpy);
    settings.setType(ScenarioSimulationModel.Type.RULE);
    scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock);
    verify(scenarioValidationServiceSpy, timeout(1)).validateRULE(eq(simulation), eq(settings), eq(kieContainerMock));
    settings.setType(null);
    assertThatThrownBy(() -> scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock)).isInstanceOf(IllegalArgumentException.class);
}
Also used : Path(org.uberfire.backend.vfs.Path) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) Settings(org.drools.scenariosimulation.api.model.Settings) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

FactMapping (org.drools.scenariosimulation.api.model.FactMapping)55 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)11 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)10 List (java.util.List)9 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)8 Simulation (org.drools.scenariosimulation.api.model.Simulation)8 HashMap (java.util.HashMap)6 AbstractScesimData (org.drools.scenariosimulation.api.model.AbstractScesimData)6 ScenarioGridCellValue (org.drools.workbench.screens.scenariosimulation.client.values.ScenarioGridCellValue)6 ScenarioGridCell (org.drools.workbench.screens.scenariosimulation.client.widgets.ScenarioGridCell)6 AbstractScesimModel (org.drools.scenariosimulation.api.model.AbstractScesimModel)5 ScenarioGridColumn (org.drools.workbench.screens.scenariosimulation.client.widgets.ScenarioGridColumn)5 FactModelTree (org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree)5 ScenarioSimulationModel (org.drools.scenariosimulation.api.model.ScenarioSimulationModel)4 FactMappingValidationError (org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError)4 Before (org.junit.Before)4 Map (java.util.Map)3