Search in sources :

Example 21 with FactIdentifier

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

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

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

the class DMNSimulationSettingsCreationStrategy method createSimulation.

@Override
public Simulation createSimulation(Path context, String dmnFilePath) {
    final FactModelTuple factModelTuple = getFactModelTuple(context, dmnFilePath);
    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);
    ScenarioWithIndex scenarioWithIndex = createScesimDataWithIndex(toReturn, simulationDescriptor, ScenarioWithIndex::new);
    AtomicInteger id = new AtomicInteger(1);
    final Collection<FactModelTree> visibleFactTrees = factModelTuple.getVisibleFacts().values();
    final Map<String, FactModelTree> hiddenValues = factModelTuple.getHiddenFacts();
    visibleFactTrees.stream().sorted((a, b) -> {
        Type aType = a.getType();
        Type bType = b.getType();
        int inputFirstOrder = Type.INPUT.equals(aType) ? -1 : 1;
        return aType.equals(bType) ? 0 : inputFirstOrder;
    }).forEach(factModelTree -> {
        FactIdentifier factIdentifier = FactIdentifier.create(factModelTree.getFactName(), factModelTree.getFactName(), factModelTree.getImportPrefix());
        FactMappingExtractor factMappingExtractor = new FactMappingExtractor(factIdentifier, scenarioWithIndex.getIndex(), id, convert(factModelTree.getType()), simulationDescriptor, scenarioWithIndex.getScesimData());
        addFactMapping(factMappingExtractor, factModelTree, new ArrayList<>(), hiddenValues);
    });
    addEmptyColumnsIfNeeded(toReturn, scenarioWithIndex);
    return toReturn;
}
Also used : ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) ScenarioSimulationModel(org.drools.scenariosimulation.api.model.ScenarioSimulationModel) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree) FactModelTuple(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) GIVEN(org.drools.scenariosimulation.api.model.FactMappingType.GIVEN) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) Type(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree.Type) VALUE(org.drools.scenariosimulation.api.utils.ConstantsHolder.VALUE) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScenarioSimulationSharedUtils(org.drools.scenariosimulation.api.utils.ScenarioSimulationSharedUtils) DMNTypeService(org.drools.workbench.screens.scenariosimulation.service.DMNTypeService) Map(java.util.Map) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) Path(org.uberfire.backend.vfs.Path) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex) Collection(java.util.Collection) Settings(org.drools.scenariosimulation.api.model.Settings) Set(java.util.Set) FactMappingType(org.drools.scenariosimulation.api.model.FactMappingType) List(java.util.List) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) ApplicationScoped(javax.enterprise.context.ApplicationScoped) EXPECT(org.drools.scenariosimulation.api.model.FactMappingType.EXPECT) AbstractScesimData(org.drools.scenariosimulation.api.model.AbstractScesimData) FactModelTuple(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree) Type(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree.Type) FactMappingType(org.drools.scenariosimulation.api.model.FactMappingType) Simulation(org.drools.scenariosimulation.api.model.Simulation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScenarioWithIndex(org.drools.scenariosimulation.api.model.ScenarioWithIndex)

Example 24 with FactIdentifier

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

the class AbstractSelectedColumnCommand method setPropertyHeader.

/**
 * It assigns a property to the selected <code>ScenarioGridColumn</code>
 * @param context It contains the <b>Context</b> inside which the commands will be executed
 * @param selectedColumn The selected <code>ScenarioGridColumn</code> where the command was launched
 * @param propertyNameElements The <code>List</code> with the path instance_name.property.name (eg. Author.isAlive)
 * @param propertyClass it contains the full classname of the property (eg. com.Author)
 */
protected void setPropertyHeader(ScenarioSimulationContext context, ScenarioGridColumn selectedColumn, String canonicalClassName, List<String> propertyNameElements, String propertyClass) {
    String instanceAliasName = propertyNameElements.get(0);
    final FactIdentifier factIdentifier = setEditableHeadersAndGetFactIdentifier(context, selectedColumn, instanceAliasName, canonicalClassName);
    String propertyTitle = getPropertyHeaderTitle(context, propertyNameElements, factIdentifier);
    this.setPropertyHeader(context, selectedColumn, factIdentifier, propertyNameElements, propertyClass, propertyTitle);
}
Also used : FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier)

Example 25 with FactIdentifier

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

the class AbstractScesimGridModelTest method commonCheckSameInstanceHeader.

private void commonCheckSameInstanceHeader(String columnClassName, String value, boolean expected) {
    FactIdentifier factIdentifierMock = mock(FactIdentifier.class);
    when(factIdentifierMock.getClassNameWithoutPackage()).thenReturn(columnClassName);
    when(factMappingMock.getFactIdentifier()).thenReturn(factIdentifierMock);
    when(simulationDescriptorMock.getFactMappingByIndex(COLUMN_INDEX)).thenReturn(factMappingMock);
    try {
        abstractScesimGridModelSpy.checkSameInstanceHeader(COLUMN_INDEX, value);
        verify(abstractScesimGridModelSpy, times(1)).isSameInstanceType(eq(COLUMN_INDEX), eq(value));
    } catch (Exception e) {
        if (expected) {
            fail("No exception expected, retrieved " + e.getMessage());
        }
    }
}
Also used : FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier)

Aggregations

FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)28 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)15 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)12 List (java.util.List)10 ArrayList (java.util.ArrayList)9 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)9 Test (org.junit.Test)9 Map (java.util.Map)8 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)8 Set (java.util.Set)6 Objects (java.util.Objects)5 Collectors.toList (java.util.stream.Collectors.toList)5 ExpressionElement (org.drools.scenariosimulation.api.model.ExpressionElement)5 FactMappingType (org.drools.scenariosimulation.api.model.FactMappingType)5 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)5 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)5 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)5 Supplier (java.util.function.Supplier)4 Collectors (java.util.stream.Collectors)4 FactMappingValueStatus (org.drools.scenariosimulation.api.model.FactMappingValueStatus)4