Search in sources :

Example 6 with FactMapping

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

the class DMNSimulationSettingsCreationStrategyTest method verifySimulationCreated.

private void verifySimulationCreated(boolean hasInput, boolean hasOutput) throws Exception {
    final Path pathMock = mock(Path.class);
    final String dmnFilePath = "test";
    FactModelTuple factModelTuple = getFactModelTuple(hasInput, hasOutput);
    doReturn(factModelTuple).when(dmnSimulationCreationStrategy).getFactModelTuple(any(), any());
    Simulation simulation = dmnSimulationCreationStrategy.createSimulation(pathMock, dmnFilePath);
    assertNotNull(simulation);
    List<FactMapping> factMappings = simulation.getScesimModelDescriptor().getFactMappings();
    if (hasInput) {
        assertTrue(factMappings.stream().anyMatch(elem -> GIVEN.equals(elem.getExpressionIdentifier().getType())));
    } else {
        assertEquals(1, factMappings.stream().filter(elem -> GIVEN.equals(elem.getExpressionIdentifier().getType())).count());
    }
    if (hasOutput) {
        assertTrue(factMappings.stream().anyMatch(elem -> EXPECT.equals(elem.getExpressionIdentifier().getType())));
    } else {
        assertEquals(1, factMappings.stream().filter(elem -> EXPECT.equals(elem.getExpressionIdentifier().getType())).count());
    }
}
Also used : Path(org.uberfire.backend.vfs.Path) 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) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) FactModelTuple(org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple) Matchers.anyString(org.mockito.Matchers.anyString)

Example 7 with FactMapping

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

the class ScenarioCsvImportExport method importData.

public <T extends AbstractScesimData> AbstractScesimModel<T> importData(String raw, AbstractScesimModel<T> originalScesimModel) throws IOException {
    CSVParser csvParser = CSVFormat.DEFAULT.parse(new StringReader(raw));
    AbstractScesimModel<T> toReturn = originalScesimModel.cloneModel();
    toReturn.clearDatas();
    List<FactMapping> factMappings = toReturn.getScesimModelDescriptor().getUnmodifiableFactMappings();
    List<CSVRecord> csvRecords = csvParser.getRecords();
    if (csvRecords.size() < HEADER_SIZE) {
        throw new IllegalArgumentException("Malformed file, missing header");
    }
    csvRecords = csvRecords.subList(HEADER_SIZE, csvRecords.size());
    for (CSVRecord csvRecord : csvRecords) {
        T scesimDataToFill = toReturn.addData();
        if (csvRecord.size() != factMappings.size()) {
            throw new IllegalArgumentException("Malformed row " + csvRecord);
        }
        for (int i = 0; i < factMappings.size(); i += 1) {
            FactMapping factMapping = factMappings.get(i);
            String valueToImport = "".equals(csvRecord.get(i)) ? null : csvRecord.get(i);
            scesimDataToFill.addMappingValue(factMapping.getFactIdentifier(), factMapping.getExpressionIdentifier(), valueToImport);
        }
    }
    return toReturn;
}
Also used : FactMapping(org.drools.scenariosimulation.api.model.FactMapping) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVRecord(org.apache.commons.csv.CSVRecord)

Example 8 with FactMapping

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

the class ScenarioCsvImportExport method exportData.

public String exportData(AbstractScesimModel<? extends AbstractScesimData> scesimModel) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    List<FactMapping> factMappings = scesimModel.getScesimModelDescriptor().getUnmodifiableFactMappings();
    CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT);
    generateHeader(factMappings, printer);
    for (AbstractScesimData scesimData : scesimModel.getUnmodifiableData()) {
        List<Object> values = new ArrayList<>();
        for (FactMapping factMapping : factMappings) {
            Optional<FactMappingValue> factMappingValue = scesimData.getFactMappingValue(factMapping.getFactIdentifier(), factMapping.getExpressionIdentifier());
            values.add(factMappingValue.map(FactMappingValue::getRawValue).orElse(""));
        }
        printer.printRecord(values.toArray());
    }
    printer.close();
    return stringBuilder.toString();
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) AbstractScesimData(org.drools.scenariosimulation.api.model.AbstractScesimData) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ArrayList(java.util.ArrayList)

Example 9 with FactMapping

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

the class SimulationSettingsCreationStrategy method createEmptyColumn.

/**
 * Create an empty column using factMappingType defined. The new column will be added as last column of
 * the group (GIVEN/EXPECT) (see findLastIndexOfGroup)
 *
 * @param simulationDescriptor
 * @param scesimDataWithIndex
 * @param placeholderId
 * @param factMappingType
 * @param columnIndex
 */
default void createEmptyColumn(ScesimModelDescriptor simulationDescriptor, ScesimDataWithIndex scesimDataWithIndex, int placeholderId, FactMappingType factMappingType, int columnIndex) {
    int row = scesimDataWithIndex.getIndex();
    final ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create(row + "|" + placeholderId, factMappingType);
    final FactMapping factMapping = simulationDescriptor.addFactMapping(columnIndex, FactMapping.getInstancePlaceHolder(placeholderId), FactIdentifier.EMPTY, expressionIdentifier);
    factMapping.setExpressionAlias(FactMapping.getPropertyPlaceHolder(placeholderId));
    scesimDataWithIndex.getScesimData().addMappingValue(FactIdentifier.EMPTY, expressionIdentifier, null);
}
Also used : FactMapping(org.drools.scenariosimulation.api.model.FactMapping) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier)

Example 10 with FactMapping

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

the class ScenarioValidationService method validateSimulationStructure.

/**
 * Validate the structure of a simulation. It does not validate the content of the cells
 * @param simulation to validate
 * @param settings
 * @param path to test scenario file
 * @return list of validation errors
 */
public List<FactMappingValidationError> validateSimulationStructure(Simulation simulation, Settings settings, Path path) {
    // skip validation (and compilation) if there are no columns to validate
    List<FactMapping> factMappings = simulation.getScesimModelDescriptor().getFactMappings();
    if (factMappings.stream().allMatch(AbstractScenarioValidation::isToSkip)) {
        return Collections.emptyList();
    }
    KieContainer kieContainer = getKieContainer(path);
    ScenarioSimulationModel.Type type = settings.getType();
    if (DMN.equals(type)) {
        return validateDMN(simulation, settings, kieContainer);
    } else if (RULE.equals(type)) {
        return validateRULE(simulation, settings, kieContainer);
    } else {
        throw new IllegalArgumentException("Only DMN and RULE test scenarios can be validated");
    }
}
Also used : FactMapping(org.drools.scenariosimulation.api.model.FactMapping) ScenarioSimulationModel(org.drools.scenariosimulation.api.model.ScenarioSimulationModel) KieContainer(org.kie.api.runtime.KieContainer)

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