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());
}
}
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;
}
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();
}
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);
}
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");
}
}
Aggregations