Search in sources :

Example 26 with FactModelTree

use of org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree in project drools-wb by kiegroup.

the class DMNTypeServiceImplTest method createTopLevelFactModelTreeCompositeCollection.

@Test
public void createTopLevelFactModelTreeCompositeCollection() throws WrongDMNTypeException {
    // Single property collection retrieve
    DMNType compositePerson = getCompositeCollection();
    TreeMap<String, FactModelTree> hiddenFactSimpleCollection = new TreeMap<>();
    FactModelTree retrieved = dmnTypeServiceImpl.createTopLevelFactModelTree("testPath", IMPORTED_PREFIX, compositePerson, hiddenFactSimpleCollection, FactModelTree.Type.INPUT);
    assertNotNull(retrieved);
    assertEquals("testPath", retrieved.getFactName());
    assertEquals(1, retrieved.getSimpleProperties().size());
    assertTrue(retrieved.getSimpleProperties().containsKey(VALUE));
    assertEquals("java.util.List", retrieved.getSimpleProperties().get(VALUE).getTypeName());
    assertFalse(retrieved.getSimpleProperties().get(VALUE).getBaseTypeName().isPresent());
    assertEquals("java.util.List", retrieved.getSimpleProperties().get(VALUE).getPropertyTypeNameToVisualize());
    assertTrue(retrieved.getExpandableProperties().isEmpty());
    assertEquals(1, retrieved.getGenericTypesMap().size());
    assertTrue(retrieved.getGenericTypesMap().containsKey(VALUE));
    assertNotNull(retrieved.getGenericTypesMap().get(VALUE));
    assertEquals(1, retrieved.getGenericTypesMap().get(VALUE).size());
    assertEquals(compositePerson.getName(), retrieved.getGenericTypesMap().get(VALUE).get(0));
    assertEquals(IMPORTED_PREFIX, retrieved.getImportPrefix());
}
Also used : TreeMap(java.util.TreeMap) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree) DMNType(org.kie.dmn.api.core.DMNType) Test(org.junit.Test)

Example 27 with FactModelTree

use of org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree in project drools-wb by kiegroup.

the class DMNTypeServiceImpl method retrieveFactModelTuple.

@Override
public FactModelTuple retrieveFactModelTuple(Path path, String dmnPath) {
    DMNModel dmnModel = getDMNModel(path, dmnPath);
    SortedMap<String, FactModelTree> visibleFacts = new TreeMap<>();
    SortedMap<String, FactModelTree> hiddenFacts = new TreeMap<>();
    ErrorHolder errorHolder = new ErrorHolder();
    Map<String, String> importedModelsMap = dmnModel.getDefinitions().getImport().stream().collect(Collectors.toMap(Import::getNamespace, Import::getName));
    for (InputDataNode input : dmnModel.getInputs()) {
        final DMNType type = input.getType();
        final String importPrefix = importedModelsMap.getOrDefault(input.getModelNamespace(), null);
        final String name = importedModelsMap.containsKey(input.getModelNamespace()) ? importedModelsMap.get(input.getModelNamespace()) + "." + input.getName() : input.getName();
        checkTypeSupport(type, errorHolder, name);
        try {
            visibleFacts.put(name, createTopLevelFactModelTree(name, importPrefix, type, hiddenFacts, FactModelTree.Type.INPUT));
        } catch (WrongDMNTypeException e) {
            throw ExceptionUtilities.handleException(e);
        }
    }
    for (DecisionNode decision : dmnModel.getDecisions()) {
        DMNType type = decision.getResultType();
        final String importPrefix = importedModelsMap.getOrDefault(decision.getModelNamespace(), null);
        final String name = importedModelsMap.containsKey(decision.getModelNamespace()) ? importedModelsMap.get(decision.getModelNamespace()) + "." + decision.getName() : decision.getName();
        checkTypeSupport(type, errorHolder, name);
        try {
            visibleFacts.put(name, createTopLevelFactModelTree(name, importPrefix, type, hiddenFacts, FactModelTree.Type.DECISION));
        } catch (WrongDMNTypeException e) {
            throw ExceptionUtilities.handleException(e);
        }
    }
    FactModelTuple factModelTuple = new FactModelTuple(visibleFacts, hiddenFacts);
    errorHolder.getMultipleNestedCollection().forEach(factModelTuple::addMultipleNestedCollectionError);
    errorHolder.getMultipleNestedObject().forEach(factModelTuple::addMultipleNestedObjectError);
    return factModelTuple;
}
Also used : FactModelTuple(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) TreeMap(java.util.TreeMap) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree) WrongDMNTypeException(org.drools.workbench.screens.scenariosimulation.backend.server.exceptions.WrongDMNTypeException) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) DMNModel(org.kie.dmn.api.core.DMNModel) DMNType(org.kie.dmn.api.core.DMNType)

Example 28 with FactModelTree

use of org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree in project drools-wb by kiegroup.

the class DMNTypeServiceImpl method createFactModelTreeForCollection.

/**
 * Creates a <code>FactModelTree</code> for <code>DMNType</code> where <code>DMNType.isCollection()</code> == <code>true</code>
 * @param factName
 * @param type
 * @param hiddenFacts
 * @param fmType
 * @return
 * @throws WrongDMNTypeException if <code>DMNType.isCollection()</code> != <code>true</code>
 */
protected FactModelTree createFactModelTreeForCollection(Map<String, List<String>> genericTypeInfoMap, String factName, String importPrefix, DMNType type, SortedMap<String, FactModelTree> hiddenFacts, FactModelTree.Type fmType, List<String> alreadyVisited) throws WrongDMNTypeException {
    if (!type.isCollection() && !isToBeManagedAsCollection(type)) {
        throw new WrongDMNTypeException();
    }
    String typeName = type.getName();
    populateGeneric(genericTypeInfoMap, VALUE, typeName);
    FactModelTree toReturn = createFactModelTreeForSimple(factName, importPrefix, typeName, List.class.getCanonicalName(), genericTypeInfoMap, fmType);
    if (!hiddenFacts.containsKey(typeName) && !alreadyVisited.contains(typeName)) {
        alreadyVisited.add(typeName);
        FactModelTree genericFactModelTree = createFactModelTreeForGenericType(new HashMap<>(), typeName, importPrefix, typeName, typeName, type, hiddenFacts, fmType, alreadyVisited);
        hiddenFacts.put(typeName, genericFactModelTree);
    }
    return toReturn;
}
Also used : WrongDMNTypeException(org.drools.workbench.screens.scenariosimulation.backend.server.exceptions.WrongDMNTypeException) ArrayList(java.util.ArrayList) List(java.util.List) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree)

Example 29 with FactModelTree

use of org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree in project drools-wb by kiegroup.

the class DMNSimulationSettingsCreationStrategy method internalAddToScenario.

protected void internalAddToScenario(FactMappingExtractor factMappingExtractor, FactModelTree factModelTree, List<String> readOnlyPreviousSteps, Map<String, FactModelTree> hiddenValues, Set<String> alreadyVisited) {
    List<String> previousSteps = new ArrayList<>(readOnlyPreviousSteps);
    // if is a simple type it generates a single column
    if (factModelTree.isSimple()) {
        FactModelTree.PropertyTypeName factType = factModelTree.getSimpleProperties().get(VALUE);
        factMappingExtractor.getFactMapping(factModelTree, VALUE, previousSteps, factType.getTypeName());
    } else // otherwise it adds a column for each simple properties direct or nested
    {
        factModelTree.getSimpleProperties().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
            String factName = entry.getKey();
            String factTypeName = entry.getValue().getTypeName();
            FactMapping factMapping = factMappingExtractor.getFactMapping(factModelTree, factName, previousSteps, factTypeName);
            if (ScenarioSimulationSharedUtils.isList(factTypeName)) {
                factMapping.setGenericTypes(factModelTree.getGenericTypeInfo(factName));
            }
            factMapping.addExpressionElement(factName, factTypeName);
        });
        factModelTree.getExpandableProperties().entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(entry -> {
            String factType = entry.getValue();
            FactModelTree nestedModelTree = hiddenValues.get(factType);
            if (previousSteps.isEmpty()) {
                previousSteps.add(factModelTree.getFactName());
            }
            ArrayList<String> currentSteps = new ArrayList<>(previousSteps);
            currentSteps.add(entry.getKey());
            if (!alreadyVisited.contains(nestedModelTree.getFactName())) {
                alreadyVisited.add(factModelTree.getFactName());
                internalAddToScenario(factMappingExtractor, nestedModelTree, currentSteps, hiddenValues, alreadyVisited);
            }
        });
    }
}
Also used : FactMapping(org.drools.scenariosimulation.api.model.FactMapping) ArrayList(java.util.ArrayList) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree)

Example 30 with FactModelTree

use of org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree in project drools-wb by kiegroup.

the class AbstractDataManagementStrategy method getInstanceMap.

/**
 * Returns a <code>Map</code> of the <b>instances</b> as defined in the grid and the mapped <code>FactModelTree</code>
 * @param sourceMap
 * @return
 */
public SortedMap<String, FactModelTree> getInstanceMap(SortedMap<String, FactModelTree> sourceMap) {
    SortedMap<String, FactModelTree> toReturn = new TreeMap<>();
    // map instance name to base class
    if (model != null) {
        final ScesimModelDescriptor simulationDescriptor = model.getSimulation().getScesimModelDescriptor();
        final ScenarioSimulationModel.Type type = model.getSettings().getType();
        simulationDescriptor.getUnmodifiableFactMappings().stream().filter(factMapping -> !Objects.equals(FactMappingType.OTHER, factMapping.getExpressionIdentifier().getType())).forEach(factMapping -> {
            String dataObjectName = ScenarioSimulationModel.Type.DMN.equals(type) ? factMapping.getFactIdentifier().getClassName() : factMapping.getFactIdentifier().getClassNameWithoutPackage().replace("$", ".");
            final String instanceName = factMapping.getFactAlias();
            if (!instanceName.equals(dataObjectName)) {
                final FactModelTree factModelTree = sourceMap.get(dataObjectName);
                if (factModelTree != null) {
                    toReturn.put(instanceName, factModelTree);
                }
            }
        });
    }
    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) GridWidget(org.drools.workbench.screens.scenariosimulation.client.enums.GridWidget) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExpressionElement(org.drools.scenariosimulation.api.model.ExpressionElement) ScenarioSimulationContext(org.drools.workbench.screens.scenariosimulation.client.commands.ScenarioSimulationContext) AbstractScesimModel(org.drools.scenariosimulation.api.model.AbstractScesimModel) VALUE(org.drools.scenariosimulation.api.utils.ConstantsHolder.VALUE) Map(java.util.Map) AbstractScesimGridModel(org.drools.workbench.screens.scenariosimulation.client.models.AbstractScesimGridModel) ScesimModelDescriptor(org.drools.scenariosimulation.api.model.ScesimModelDescriptor) TestToolsPresenterData(org.drools.workbench.screens.scenariosimulation.client.rightpanel.TestToolsPresenterData) Set(java.util.Set) FactMappingType(org.drools.scenariosimulation.api.model.FactMappingType) Collectors(java.util.stream.Collectors) TestToolsView(org.drools.workbench.screens.scenariosimulation.client.rightpanel.TestToolsView) Objects(java.util.Objects) ScenarioGridColumn(org.drools.workbench.screens.scenariosimulation.client.widgets.ScenarioGridColumn) List(java.util.List) TreeMap(java.util.TreeMap) Collections(java.util.Collections) SortedMap(java.util.SortedMap) AbstractScesimData(org.drools.scenariosimulation.api.model.AbstractScesimData) TreeMap(java.util.TreeMap) FactModelTree(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree) ScenarioSimulationModel(org.drools.scenariosimulation.api.model.ScenarioSimulationModel)

Aggregations

FactModelTree (org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTree)47 Test (org.junit.Test)19 TreeMap (java.util.TreeMap)16 Matchers.anyString (org.mockito.Matchers.anyString)14 HashMap (java.util.HashMap)10 DMNType (org.kie.dmn.api.core.DMNType)10 ArrayList (java.util.ArrayList)9 AbstractScenarioSimulationEditorTest (org.drools.workbench.screens.scenariosimulation.client.editor.AbstractScenarioSimulationEditorTest)8 FactModelTuple (org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple)7 List (java.util.List)6 Map (java.util.Map)6 SortedMap (java.util.SortedMap)6 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)6 ScenarioSimulationModel (org.drools.scenariosimulation.api.model.ScenarioSimulationModel)4 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 AbstractScesimData (org.drools.scenariosimulation.api.model.AbstractScesimData)3 AbstractScesimModel (org.drools.scenariosimulation.api.model.AbstractScesimModel)3 FactMappingType (org.drools.scenariosimulation.api.model.FactMappingType)3