public FactMapping getFactMapping(FactModelTree factModelTree, String propertyName, List<String> previousSteps, String factType) { String factAlias = previousSteps.size() > 0 ? previousSteps.get(0) : factModelTree.getFactName(); ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create(row + "|" + id.getAndIncrement(), type); final FactMapping factMapping = simulationDescriptor.addFactMapping(factAlias, factIdentifier, expressionIdentifier); List<String> localPreviousStep = new ArrayList<>(previousSteps); localPreviousStep.add(propertyName); String expressionAlias = String.join(".", localPreviousStep.size() > 1 ? localPreviousStep.subList(1, localPreviousStep.size()) : localPreviousStep); factMapping.setExpressionAlias(expressionAlias); previousSteps.forEach(step -> factMapping.addExpressionElement(step, factType)); if (previousSteps.isEmpty()) { factMapping.addExpressionElement(factModelTree.getFactName(), factType); } scenario.addMappingValue(factIdentifier, expressionIdentifier, null); return factMapping; } }
/** * This method update the type mapped inside a give column and updates the underlying model * @param columnIndex * @param column * @param value * @param lastLevelClassName * @param keepData */ public void updateColumnProperty(int columnIndex, final GridColumn<?> column, String value, String lastLevelClassName, boolean keepData) { checkSimulation(); List<GridCellValue<?>> originalValues = new ArrayList<>(); if (keepData) { IntStream.range(0, getRowCount()) .forEach(rowIndex -> originalValues.add(getCell(rowIndex, columnIndex).getValue())); } deleteColumn(columnIndex); String group = ((ScenarioGridColumn) column).getInformationHeaderMetaData().getColumnGroup(); String columnId = ((ScenarioGridColumn) column).getInformationHeaderMetaData().getColumnId(); String[] elements = value.split("\\."); ExpressionIdentifier ei = ExpressionIdentifier.create(columnId, FactMappingType.valueOf(group)); commonAddColumn(columnIndex, column, ei); final FactMapping factMappingByIndex = simulation.getSimulationDescriptor().getFactMappingByIndex(columnIndex); IntStream.range(0, elements.length) .forEach(stepIndex -> factMappingByIndex.addExpressionElement(elements[stepIndex], lastLevelClassName)); if (keepData) { IntStream.range(0, getRowCount()) .forEach(rowIndex -> setCellValue(rowIndex, columnIndex, originalValues.get(rowIndex))); } }
@Before public void init() { simulationDescriptor = new SimulationDescriptor(); factIdentifier = FactIdentifier.create("test fact", String.class.getCanonicalName()); expressionIdentifier = ExpressionIdentifier.create("test expression", FactMappingType.EXPECT); }
@Before public void init() { simulationDescriptor = new SimulationDescriptor(); factIdentifier = FactIdentifier.create("test fact", String.class.getCanonicalName()); expressionIdentifier = ExpressionIdentifier.create("test expression", FactMappingType.EXPECT); }
/** * This method <i>add</i> or <i>insert</i> a new column to the grid <b>and</b> to the underlying model, depending on the index value. * If index == -1 -> add, otherwise insert. It automatically creates default <code>FactIdentifier</code> (for String class) and <code>ExpressionIdentifier</code> * @param index * @param column */ protected void commonAddColumn(final int index, final GridColumn<?> column) { String group = ((ScenarioGridColumn) column).getInformationHeaderMetaData().getColumnGroup(); String columnId = ((ScenarioGridColumn) column).getInformationHeaderMetaData().getColumnId(); final ExpressionIdentifier ei = ExpressionIdentifier.create(columnId, FactMappingType.valueOf(group)); commonAddColumn(index, column, ei); }
@Test public void extractExpectedValuesTest() { List<ScenarioExpect> scenario1Outputs = runnerHelper.extractExpectedValues(scenario1.getUnmodifiableFactMappingValues()); assertEquals(1, scenario1Outputs.size()); scenario2.addOrUpdateMappingValue(FactIdentifier.create("TEST", String.class.getCanonicalName()), ExpressionIdentifier.create("TEST", FactMappingType.EXPECT), "TEST"); List<ScenarioExpect> scenario2Outputs = runnerHelper.extractExpectedValues(scenario2.getUnmodifiableFactMappingValues()); assertEquals(3, scenario2Outputs.size()); assertEquals(1, scenario2Outputs.stream().filter(ScenarioExpect::isNewFact).count()); }
/** * This method update the instance mapped inside a given column * @param columnIndex * @param column */ public void updateColumnInstance(int columnIndex, final GridColumn<?> column) { checkSimulation(); deleteColumn(columnIndex); String group = ((ScenarioGridColumn) column).getInformationHeaderMetaData().getColumnGroup(); String columnId = ((ScenarioGridColumn) column).getInformationHeaderMetaData().getColumnId(); ExpressionIdentifier ei = ExpressionIdentifier.create(columnId, FactMappingType.valueOf(group)); commonAddColumn(columnIndex, column, ei); }
@Before public void init() { simulation = new Simulation(); simulationDescriptor = simulation.getSimulationDescriptor(); scenario = simulation.addScenario(); factIdentifier = FactIdentifier.create("test fact", String.class.getCanonicalName()); expressionIdentifier = ExpressionIdentifier.create("test expression", FactMappingType.EXPECT); }
@Before public void init() { simulation = new Simulation(); simulationDescriptor = simulation.getSimulationDescriptor(); scenario = simulation.addScenario(); factIdentifier = FactIdentifier.create("test fact", String.class.getCanonicalName()); expressionIdentifier = ExpressionIdentifier.create("test expression", FactMappingType.EXPECT); }
@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 Simulation getSimulation(int numberOfColumns, int numberOfRows) { Simulation simulation = new Simulation(); SimulationDescriptor simulationDescriptor = simulation.getSimulationDescriptor(); simulationDescriptor.addFactMapping(FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION); // generate simulationDescriptor IntStream.range(0, numberOfColumns).forEach(columnIndex -> { simulationDescriptor.addFactMapping(FactIdentifier.create(getFactName(columnIndex), String.class.getCanonicalName()), ExpressionIdentifier.create(getColName(columnIndex), FactMappingType.EXPECT) ); }); // generate scenarios IntStream.range(0, numberOfRows).forEach(rowIndex -> { final Scenario scenario = simulation.addScenario(); scenario.setDescription(getRowName(rowIndex)); IntStream.range(0, numberOfColumns).forEach( columnIndex -> { scenario.addMappingValue(FactIdentifier.create(getFactName(columnIndex), String.class.getCanonicalName()), ExpressionIdentifier.create(getColName(columnIndex), FactMappingType.EXPECT), getCellValue(columnIndex, rowIndex)); }); }); return simulation; }
@Before public void setup() { simulation = new Simulation(); FactIdentifier factIdentifier = FactIdentifier.create("Test", String.class.getCanonicalName()); ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create("Test", FactMappingType.GIVEN); simulation.getSimulationDescriptor().addFactMapping(factIdentifier, expressionIdentifier); originalScenario = simulation.addScenario(); originalScenario.setDescription("Test Description"); originalScenario.addMappingValue(factIdentifier, expressionIdentifier, "TEST"); }
@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 sortTest() { ExpressionIdentifier expressionIdentifier2 = ExpressionIdentifier.create("Test expression 2", FactMappingType.GIVEN); simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier); simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier2); scenario.addMappingValue(factIdentifier, expressionIdentifier2, "Test 2"); FactMappingValue factMappingValue1 = scenario.addMappingValue(factIdentifier, this.expressionIdentifier, "Test"); assertEquals(scenario.getUnmodifiableFactMappingValues().get(1), factMappingValue1); scenario.sort(); assertNotEquals(scenario.getUnmodifiableFactMappingValues().get(1), factMappingValue1); assertEquals(scenario.getUnmodifiableFactMappingValues().get(0), factMappingValue1); } }
@Before public void setup() { simulation = new Simulation(); FactIdentifier factIdentifier = FactIdentifier.create("Test", String.class.getCanonicalName()); ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create("Test", FactMappingType.GIVEN); simulation.getSimulationDescriptor().addFactMapping(factIdentifier, expressionIdentifier); originalScenario = simulation.addScenario(); originalScenario.setDescription("Test Description"); originalScenario.addMappingValue(factIdentifier, expressionIdentifier, "TEST"); }
@Test public void sortTest() { ExpressionIdentifier expressionIdentifier2 = ExpressionIdentifier.create("Test expression 2", FactMappingType.GIVEN); simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier); simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier2); scenario.addMappingValue(factIdentifier, expressionIdentifier2, "Test 2"); FactMappingValue factMappingValue1 = scenario.addMappingValue(factIdentifier, this.expressionIdentifier, "Test"); assertEquals(scenario.getUnmodifiableFactMappingValues().get(1), factMappingValue1); scenario.sort(); assertNotEquals(scenario.getUnmodifiableFactMappingValues().get(1), factMappingValue1); assertEquals(scenario.getUnmodifiableFactMappingValues().get(0), factMappingValue1); } }
simulation = new Simulation(); personFactIdentifier = FactIdentifier.create("Fact 1", Person.class.getCanonicalName()); firstNameGivenExpressionIdentifier = ExpressionIdentifier.create("First Name Given", FactMappingType.GIVEN); firstNameGivenFactMapping = simulation.getSimulationDescriptor().addFactMapping(personFactIdentifier, firstNameGivenExpressionIdentifier); firstNameGivenFactMapping.addExpressionElement("Fact 1", String.class.getCanonicalName()); amountGivenExpressionIdentifier = ExpressionIdentifier.create("Amount Given", FactMappingType.GIVEN); amountNameGivenFactMapping = simulation.getSimulationDescriptor().addFactMapping(disputeFactIdentifier, amountGivenExpressionIdentifier); amountNameGivenFactMapping.addExpressionElement("Fact 2", Double.class.getCanonicalName()); amountNameGivenFactMapping.addExpressionElement("amount", Double.class.getCanonicalName()); firstNameExpectedExpressionIdentifier = ExpressionIdentifier.create("First Name Expected", FactMappingType.EXPECT); firstNameExpectedFactMapping = simulation.getSimulationDescriptor().addFactMapping(personFactIdentifier, firstNameExpectedExpressionIdentifier); firstNameExpectedFactMapping.addExpressionElement("Fact 1", String.class.getCanonicalName()); firstNameExpectedFactMapping.addExpressionElement("firstName", String.class.getCanonicalName()); amountExpectedExpressionIdentifier = ExpressionIdentifier.create("Amount Expected", FactMappingType.EXPECT); amountNameExpectedFactMapping = simulation.getSimulationDescriptor().addFactMapping(disputeFactIdentifier, amountExpectedExpressionIdentifier); amountNameExpectedFactMapping.addExpressionElement("Fact 2", Double.class.getCanonicalName());
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; } }
@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); }
@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); }