Search in sources :

Example 21 with Simulation

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

the class ScenarioCsvImportExportTest method exportData.

@Test
public void exportData() throws IOException {
    int numberOfRow = 2;
    int numberOfColumn = 1;
    Simulation simulation = createDummySimulation(numberOfColumn, numberOfRow);
    List<String> exportData = Arrays.asList(scenarioCsvImportExport.exportData(simulation).split("\r\n"));
    assertEquals(numberOfRow + HEADER_SIZE, exportData.size());
    assertEquals("1,My scenario 1,value_1_0", exportData.get(4));
}
Also used : Simulation(org.drools.scenariosimulation.api.model.Simulation) Test(org.junit.Test)

Example 22 with Simulation

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

use of org.drools.scenariosimulation.api.model.Simulation 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")));
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) UnaryTestImpl(org.kie.dmn.feel.runtime.UnaryTestImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) DMNModel(org.kie.dmn.api.core.DMNModel) KieContainer(org.kie.api.runtime.KieContainer) DMNType(org.kie.dmn.api.core.DMNType) Test(org.junit.Test)

Example 24 with Simulation

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

the class DMNSimulationSettingsCreationStrategyTest method addEmptyColumnIfNeeded.

@Test
public void addEmptyColumnIfNeeded() {
    Simulation simulation = new Simulation();
    ScenarioWithIndex scenarioWithIndex = new ScenarioWithIndex(1, simulation.addData());
    ExpressionIdentifier givenExpressionIdentifier = ExpressionIdentifier.create("given1", GIVEN);
    ScesimModelDescriptor simulationDescriptor = simulation.getScesimModelDescriptor();
    simulationDescriptor.addFactMapping(FactIdentifier.EMPTY, givenExpressionIdentifier);
    dmnSimulationCreationStrategy.addEmptyColumnsIfNeeded(simulation, scenarioWithIndex);
    assertEquals(2, simulationDescriptor.getFactMappings().size());
    assertTrue(simulationDescriptor.getFactMappings().stream().anyMatch(elem -> EXPECT.equals(elem.getExpressionIdentifier().getType())));
    simulation = new Simulation();
    scenarioWithIndex = new ScenarioWithIndex(1, simulation.addData());
    ExpressionIdentifier expectExpressionIdentifier = ExpressionIdentifier.create("expect1", EXPECT);
    simulationDescriptor = simulation.getScesimModelDescriptor();
    simulationDescriptor.addFactMapping(FactIdentifier.EMPTY, expectExpressionIdentifier);
    dmnSimulationCreationStrategy.addEmptyColumnsIfNeeded(simulation, scenarioWithIndex);
    assertEquals(2, simulationDescriptor.getFactMappings().size());
    assertTrue(simulationDescriptor.getFactMappings().stream().anyMatch(elem -> GIVEN.equals(elem.getExpressionIdentifier().getType())));
}
Also used : ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) Arrays(java.util.Arrays) ScenarioSimulationModel(org.drools.scenariosimulation.api.model.ScenarioSimulationModel) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree) GIVEN(org.drools.scenariosimulation.api.model.FactMappingType.GIVEN) VALUE(org.drools.scenariosimulation.api.utils.ConstantsHolder.VALUE) DMNTypeService(org.drools.workbench.screens.scenariosimulation.service.DMNTypeService) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) Mockito.doReturn(org.mockito.Mockito.doReturn) AbstractDMNTest(org.drools.workbench.screens.scenariosimulation.backend.server.AbstractDMNTest) Settings(org.drools.scenariosimulation.api.model.Settings) Matchers.any(org.mockito.Matchers.any) List(java.util.List) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Assert.assertFalse(org.junit.Assert.assertFalse) Simulation(org.drools.scenariosimulation.api.model.Simulation) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) EXPECT(org.drools.scenariosimulation.api.model.FactMappingType.EXPECT) SortedMap(java.util.SortedMap) Mockito.mock(org.mockito.Mockito.mock) DMNType(org.kie.dmn.api.core.DMNType) Mock(org.mockito.Mock) FactModelTuple(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple) RunWith(org.junit.runner.RunWith) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DECISION(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree.Type.DECISION) Path(org.uberfire.backend.vfs.Path) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) Before(org.junit.Before) OTHER(org.drools.scenariosimulation.api.model.FactMappingType.OTHER) Assert.assertNotNull(org.junit.Assert.assertNotNull) Background(org.drools.scenariosimulation.api.model.Background) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) TreeMap(java.util.TreeMap) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Simulation(org.drools.scenariosimulation.api.model.Simulation) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) AbstractDMNTest(org.drools.workbench.screens.scenariosimulation.backend.server.AbstractDMNTest) Test(org.junit.Test)

Example 25 with Simulation

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

the class DMNSimulationSettingsCreationStrategyTest method createSimulation.

@Test
public void createSimulation() throws Exception {
    FactModelTuple factModelTuple = getFactModelTuple();
    final Path pathMock = mock(Path.class);
    final String dmnFilePath = "test";
    doReturn(factModelTuple).when(dmnSimulationCreationStrategy).getFactModelTuple(any(), any());
    final Simulation retrieved = dmnSimulationCreationStrategy.createSimulation(pathMock, dmnFilePath);
    assertNotNull(retrieved);
    verify(dmnTypeServiceMock, never()).initializeNameAndNamespace(any(Settings.class), eq(pathMock), eq(dmnFilePath));
}
Also used : Path(org.uberfire.backend.vfs.Path) Simulation(org.drools.scenariosimulation.api.model.Simulation) FactModelTuple(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple) Matchers.anyString(org.mockito.Matchers.anyString) Settings(org.drools.scenariosimulation.api.model.Settings) AbstractDMNTest(org.drools.workbench.screens.scenariosimulation.backend.server.AbstractDMNTest) Test(org.junit.Test)

Aggregations

Simulation (org.drools.scenariosimulation.api.model.Simulation)30 Settings (org.drools.scenariosimulation.api.model.Settings)13 Test (org.junit.Test)13 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)12 Background (org.drools.scenariosimulation.api.model.Background)11 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)11 ScenarioSimulationModel (org.drools.scenariosimulation.api.model.ScenarioSimulationModel)9 Path (org.uberfire.backend.vfs.Path)9 List (java.util.List)8 Before (org.junit.Before)7 Scenario (org.drools.scenariosimulation.api.model.Scenario)6 ScenarioWithIndex (org.drools.scenariosimulation.api.model.ScenarioWithIndex)6 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 AbstractScesimModel (org.drools.scenariosimulation.api.model.AbstractScesimModel)4 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)4 Inject (javax.inject.Inject)3 FactModelTuple (org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple)3 Matchers.anyString (org.mockito.Matchers.anyString)3