public Simulation cloneSimulation() { Simulation toReturn = new Simulation(); toReturn.getSimulationDescriptor().setType(simulationDescriptor.getType()); toReturn.getSimulationDescriptor().setDmnFilePath(simulationDescriptor.getDmnFilePath()); toReturn.getSimulationDescriptor().setDmoSession(simulationDescriptor.getDmoSession()); final List<FactMapping> originalFactMappings = this.simulationDescriptor.getUnmodifiableFactMappings(); for (int i = 0; i < originalFactMappings.size(); i++) { final FactMapping originalFactMapping = originalFactMappings.get(i); toReturn.simulationDescriptor.addFactMapping(i, originalFactMapping); } this.scenarios.forEach(scenario -> toReturn.scenarios.add(scenario.cloneScenario())); return toReturn; }
@Test(expected = IndexOutOfBoundsException.class) public void removeFactMappingByIndex() { int testingIndex = 0; simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier); assertNotNull(simulationDescriptor.getFactMappingByIndex(testingIndex)); simulationDescriptor.removeFactMappingByIndex(testingIndex); simulationDescriptor.getFactMappingByIndex(testingIndex); }
public void sort() { factMappingValues.sort((a, b) -> { Integer aIndex = simulationDescriptor.getIndexByIdentifier(a.getFactIdentifier(), a.getExpressionIdentifier()); Integer bIndex = simulationDescriptor.getIndexByIdentifier(b.getFactIdentifier(), b.getExpressionIdentifier()); return aIndex.compareTo(bIndex); }); }
public void removeFactMappingByIndex(int index) { cleanScenarios(simulationDescriptor.getFactMappingByIndex(index)); simulationDescriptor.removeFactMappingByIndex(index); }
protected void setHeaderColumns(Simulation simulation) { final List<FactMapping> factMappings = simulation.getSimulationDescriptor().getUnmodifiableFactMappings(); boolean editableHeaders = !simulation.getSimulationDescriptor().getType().equals(ScenarioSimulationModel.Type.DMN); IntStream.range(0, factMappings.size()) .forEach(columnIndex -> { setHeaderColumn(columnIndex, factMappings.get(columnIndex), editableHeaders); }); }
public FactMapping addFactMapping(String factAlias, FactIdentifier factIdentifier, ExpressionIdentifier expressionIdentifier) { return addFactMapping(factMappings.size(), factAlias, factIdentifier, expressionIdentifier); }
@Test public void removeFactMapping() { FactMapping retrieved = simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier); assertTrue(simulationDescriptor.getUnmodifiableFactMappings().contains(retrieved)); simulationDescriptor.removeFactMapping(retrieved); assertFalse(simulationDescriptor.getUnmodifiableFactMappings().contains(retrieved)); }
protected Simulation getSimulation(ScenarioSimulationModel.Type selectedType, String value) { Simulation toReturn = new Simulation(); SimulationDescriptor simulationDescriptor = toReturn.getSimulationDescriptor(); simulationDescriptor.setType(selectedType); switch (selectedType) { case DMN: simulationDescriptor.setDmnFilePath(value); break; case RULE: default: simulationDescriptor.setDmoSession(value); simulationDescriptor.addFactMapping(FactIdentifier.INDEX.getName(), FactIdentifier.INDEX, ExpressionIdentifier.INDEX); simulationDescriptor.addFactMapping(FactIdentifier.DESCRIPTION.getName(), FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION); final FactMapping givenFactMapping = simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, givenExpression); givenFactMapping.setExpressionAlias(FactMapping.getPropertyPlaceHolder(id)); scenario.addMappingValue(FactIdentifier.EMPTY, givenExpression, null); final FactMapping expectedFactMapping = simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, expectedExpression); expectedFactMapping.setExpressionAlias(FactMapping.getPropertyPlaceHolder(id)); scenario.addMappingValue(FactIdentifier.EMPTY, expectedExpression, null);
public static BiFunction<KieContainer, Simulation, AbstractScenarioRunner> getSpecificRunnerProvider(Simulation simulation) { if (Type.RULE.equals(simulation.getSimulationDescriptor().getType())) { return RuleScenarioRunner::new; } else if (Type.DMN.equals(simulation.getSimulationDescriptor().getType())) { return DMNScenarioRunner::new; } else { throw new IllegalArgumentException("Impossible to run simulation of type " + simulation.getSimulationDescriptor().getType()); } }
@Test public void moveFactMappingTest() { ExpressionIdentifier expressionIdentifier2 = ExpressionIdentifier.create("Test expression 2", FactMappingType.GIVEN); ExpressionIdentifier expressionIdentifier3 = ExpressionIdentifier.create("Test expression 3", FactMappingType.GIVEN); FactMapping factMapping1 = simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier); FactMapping factMapping2 = simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier2); FactMapping factMapping3 = simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier3); List<FactMapping> factMappings = simulationDescriptor.getUnmodifiableFactMappings(); assertEquals(factMappings.get(0), factMapping1); assertEquals(factMappings.get(1), factMapping2); assertEquals(factMappings.get(2), factMapping3); simulationDescriptor.moveFactMapping(0, 1); factMappings = simulationDescriptor.getUnmodifiableFactMappings(); assertEquals(factMappings.get(0), factMapping2); assertEquals(factMappings.get(1), factMapping1); assertEquals(factMappings.get(2), factMapping3); simulationDescriptor.moveFactMapping(2, 1); factMappings = simulationDescriptor.getUnmodifiableFactMappings(); assertEquals(factMappings.get(0), factMapping2); assertEquals(factMappings.get(1), factMapping3); assertEquals(factMappings.get(2), factMapping1); simulationDescriptor.moveFactMapping(2, 2); factMappings = simulationDescriptor.getUnmodifiableFactMappings(); assertEquals(factMappings.get(0), factMapping2); assertEquals(factMappings.get(1), factMapping3); assertEquals(factMappings.get(2), factMapping1); }
@Test public void unmarshalRULE() throws Exception { String toUnmarshal = getFileContent("scesim-rule.scesim"); final ScenarioSimulationModel retrieved = ScenarioSimulationXMLPersistence.getInstance().unmarshal(toUnmarshal); assertEquals(retrieved.getSimulation().getSimulationDescriptor().getType(), ScenarioSimulationModel.Type.RULE); assertNotNull(retrieved.getSimulation().getSimulationDescriptor().getDmoSession()); assertNull(retrieved.getSimulation().getSimulationDescriptor().getDmnFilePath()); }
@Override public Simulation createSimulation(Path context, String value) throws Exception { Simulation toReturn = new Simulation(); SimulationDescriptor simulationDescriptor = toReturn.getSimulationDescriptor(); simulationDescriptor.setType(ScenarioSimulationModel.Type.RULE); simulationDescriptor.setDmoSession(value); Scenario scenario = createScenario(toReturn, simulationDescriptor); int row = toReturn.getUnmodifiableScenarios().indexOf(scenario); // Add GIVEN Fact int id = 1; ExpressionIdentifier givenExpression = ExpressionIdentifier.create(row + "|" + id, FactMappingType.GIVEN); final FactMapping givenFactMapping = simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, givenExpression); givenFactMapping.setExpressionAlias(FactMapping.getPropertyPlaceHolder(id)); scenario.addMappingValue(FactIdentifier.EMPTY, givenExpression, null); // Add EXPECT Fact id = 2; ExpressionIdentifier expectedExpression = ExpressionIdentifier.create(row + "|" + id, FactMappingType.EXPECT); final FactMapping expectedFactMapping = simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, expectedExpression); expectedFactMapping.setExpressionAlias(FactMapping.getPropertyPlaceHolder(id)); scenario.addMappingValue(FactIdentifier.EMPTY, expectedExpression, null); return toReturn; } }
public static String getPropertyName(final Simulation simulation, final int columnIndex) { return String.join(".", simulation.getSimulationDescriptor().getFactMappingByIndex(columnIndex).getExpressionElementsWithoutClass() .stream() .map(ExpressionElement::getStep) .collect(Collectors.toSet())); }
final int columnIndex = index == -1 ? getColumnCount() : index; try { final FactMapping createdFactMapping = simulationDescriptor.addFactMapping(columnIndex, instanceTitle, ((ScenarioGridColumn) column).getFactIdentifier(), ei); createdFactMapping.setExpressionAlias(propertyTitle); if (index == -1) { // This is actually an append IntStream.range(instanceLimits.getMinRowIndex(), instanceLimits.getMaxRowIndex() + 1) .filter(currentIndex -> currentIndex != columnIndex) .forEach(currentIndex -> simulationDescriptor.getFactMappingByIndex(currentIndex).setFactAlias(createdFactMapping.getFactAlias())); selectColumn(columns.indexOf(column)); } catch (Throwable t) {
@Test public void removeFactMapping() { assertEquals(2, simulation.getUnmodifiableScenarios().get(0).getUnmodifiableFactMappingValues().size()); assertEquals(1, simulation.getSimulationDescriptor().getUnmodifiableFactMappings().size()); simulation.removeFactMapping(simulation.getSimulationDescriptor().getFactMappingByIndex(0)); assertEquals(1, simulation.getUnmodifiableScenarios().get(0).getUnmodifiableFactMappingValues().size()); assertEquals(0, simulation.getSimulationDescriptor().getUnmodifiableFactMappings().size()); }
@Test public void getIndexByIdentifierTest() { List<FactMapping> originalFactMappings = IntStream.range(0, 2).boxed() .map(i -> simulationDescriptor .addFactMapping(FactIdentifier.create("test " + i, String.class.getCanonicalName()), this.expressionIdentifier) ).collect(Collectors.toList()); int indexToCheck = 0; int indexRetrieved = simulationDescriptor.getIndexByIdentifier(originalFactMappings.get(indexToCheck).getFactIdentifier(), this.expressionIdentifier); assertEquals(indexToCheck, indexRetrieved); indexToCheck = 1; indexRetrieved = simulationDescriptor.getIndexByIdentifier(originalFactMappings.get(indexToCheck).getFactIdentifier(), this.expressionIdentifier); assertEquals(indexToCheck, indexRetrieved); }
/** * Method to bind the data serialized inside backend <code>ScenarioSimulationModel</code> * @param simulation */ public void bindContent(Simulation simulation) { this.simulation = simulation; checkSimulation(); columnCounter.set(simulation.getSimulationDescriptor().getUnmodifiableFactMappings().size()); }
private Simulation getSimulation() { Simulation toReturn = new Simulation(); SimulationDescriptor simulationDescriptor = toReturn.getSimulationDescriptor(); simulationDescriptor.setType(ScenarioSimulationModel.Type.RULE); simulationDescriptor.addFactMapping(FactIdentifier.INDEX.getName(), FactIdentifier.INDEX, ExpressionIdentifier.INDEX); simulationDescriptor.addFactMapping(FactIdentifier.DESCRIPTION.getName(), FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION); Scenario scenario = toReturn.addScenario(); int row = toReturn.getUnmodifiableScenarios().indexOf(scenario); scenario.setDescription(null); // Add GIVEN Facts IntStream.range(2, 4).forEach(id -> { ExpressionIdentifier givenExpression = ExpressionIdentifier.create(row + "|" + id, FactMappingType.GIVEN); simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, givenExpression); scenario.addMappingValue(FactIdentifier.EMPTY, givenExpression, null); }); // Add EXPECT Facts IntStream.range(2, 4).forEach(id -> { id += 2; // This is to have consistent labels/names even when adding columns at runtime ExpressionIdentifier expectedExpression = ExpressionIdentifier.create(row + "|" + id, FactMappingType.EXPECT); simulationDescriptor.addFactMapping(FactMapping.getInstancePlaceHolder(id), FactIdentifier.EMPTY, expectedExpression); scenario.addMappingValue(FactIdentifier.EMPTY, expectedExpression, null); }); return toReturn; } }
@Override public RequestContext executeScenario(KieContainer kieContainer, ScenarioRunnerData scenarioRunnerData, ExpressionEvaluator expressionEvaluator, SimulationDescriptor simulationDescriptor) { if (!ScenarioSimulationModel.Type.DMN.equals(simulationDescriptor.getType())) { throw new ScenarioException("Impossible to run a not-DMN simulation with DMN runner"); } DMNScenarioExecutableBuilder executableBuilder = DMNScenarioExecutableBuilder.createBuilder(kieContainer); executableBuilder.setActiveModel(simulationDescriptor.getDmnFilePath()); for (ScenarioGiven input : scenarioRunnerData.getGivens()) { executableBuilder.setValue(input.getFactIdentifier().getName(), input.getValue()); } return executableBuilder.run(); }
@Test public void moveFactMappingOldFailTest() { ExpressionIdentifier expressionIdentifier2 = ExpressionIdentifier.create("Test expression 2", FactMappingType.GIVEN); simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier); simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier2); muteException(() -> { simulationDescriptor.moveFactMapping(2, 0); fail(); }, IllegalArgumentException.class); muteException(() -> { simulationDescriptor.moveFactMapping(-1, 0); fail(); }, IllegalArgumentException.class); muteException(() -> { simulationDescriptor.moveFactMapping(0, 2); fail(); }, IllegalArgumentException.class); muteException(() -> { simulationDescriptor.moveFactMapping(0, -1); fail(); }, IllegalArgumentException.class); }