@Override public int hashCode() { return Objects.hash(getExpressionElements(), getExpressionIdentifier(), getFactIdentifier(), getClassName(), getFactAlias(), getExpressionAlias()); } }
private FactMapping(FactMapping original) { original.expressionElements.forEach(expressionElement -> this.addExpressionElement(expressionElement.getStep(), original.className)); this.expressionIdentifier = original.expressionIdentifier; this.factIdentifier = original.factIdentifier; this.className = original.className; this.factAlias = original.factAlias; this.expressionAlias = original.expressionAlias; }
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; } }
private void cleanScenarios(FactMapping toRemove) { scenarios.forEach(e -> e.removeFactMappingValueByIdentifiers(toRemove.getFactIdentifier(), toRemove.getExpressionIdentifier())); } }
/** * Returns <code>true</code> when * <p> * instanceAssigned == true * </p><p> * <b>and</b> * </p><p> * !factMapping.getExpressionElements().isEmpty() * </p> * @param instanceAssigned * @param factMapping * @return */ protected boolean isPropertyAssigned(boolean instanceAssigned, FactMapping factMapping) { if (FactIdentifier.DESCRIPTION.equals(factMapping.getFactIdentifier())) { return true; } else { return instanceAssigned && (isSimpleJavaType(factMapping.getClassName()) || !factMapping.getExpressionElements().isEmpty()); } }
@Test public void getExpressionElementsWithoutClass() { FactMapping original = new FactMapping("FACT_ALIAS", new FactIdentifier("FI_TEST", "com.test.Foo"), new ExpressionIdentifier("EI_TEST", FactMappingType.GIVEN)); assertThatThrownBy(original::getExpressionElementsWithoutClass) .isInstanceOf(IllegalStateException.class) .hasMessage("ExpressionElements malformed"); assertEquals(0, original.getExpressionElements().size()); original.addExpressionElement("STEP", String.class.getCanonicalName()); assertEquals(0, original.getExpressionElementsWithoutClass().size()); assertEquals(1, original.getExpressionElements().size()); } }
@Test public void cloneFactMapping() { FactMapping original = new FactMapping("FACT_ALIAS", new FactIdentifier("FI_TEST", "com.test.Foo"), new ExpressionIdentifier("EI_TEST", FactMappingType.GIVEN)); original.addExpressionElement("FIRST_STEP", String.class.getName()); original.setExpressionAlias("EA_TEST"); FactMapping retrieved = original.cloneFactMapping(); assertTrue(retrieved.equals(original)); }
protected void setHeaderColumn(int columnIndex, FactMapping factMapping, boolean editableHeaders) { final FactIdentifier factIdentifier = factMapping.getFactIdentifier(); String columnId = factMapping.getExpressionIdentifier().getName(); String instanceTitle = factMapping.getFactAlias(); String propertyTitle = factMapping.getExpressionAlias(); String columnGroup = factMapping.getExpressionIdentifier().getType().name(); boolean isInstanceAssigned = isInstanceAssigned(factIdentifier); boolean isPropertyAssigned = isPropertyAssigned(isInstanceAssigned, factMapping); String placeHolder = getPlaceholder(isPropertyAssigned); ScenarioGridColumn scenarioGridColumn = getScenarioGridColumnLocal(instanceTitle, propertyTitle, columnId, columnGroup, factMapping.getExpressionIdentifier().getType(), placeHolder); scenarioGridColumn.setInstanceAssigned(isInstanceAssigned); scenarioGridColumn.setPropertyAssigned(isPropertyAssigned); scenarioGridColumn.setFactIdentifier(factIdentifier); scenarioGridColumn.setEditableHeaders(editableHeaders); // by default ScenarioGridColumnBuilders.ScenarioGridColumnBuilder.build() set ColumnWidthMode.auto to all generated columns if (FactMappingType.OTHER.equals(factMapping.getExpressionIdentifier().getType())) { scenarioGridColumn.setColumnWidthMode(ColumnWidthMode.FIXED); scenarioGridColumn.setMinimumWidth(scenarioGridColumn.getWidth()); } ((ScenarioGridModel) model).insertColumnGridOnly(columnIndex, scenarioGridColumn); }
protected SortedMap<String, FactModelTree> getInstanceMap(SortedMap<String, FactModelTree> sourceMap) { SortedMap<String, FactModelTree> toReturn = new TreeMap<>(); // map instance name to base class if (model != null) { final SimulationDescriptor simulationDescriptor = model.getSimulation().getSimulationDescriptor(); simulationDescriptor.getUnmodifiableFactMappings() .stream() .filter(factMapping -> !Objects.equals(FactMappingType.OTHER, factMapping.getExpressionIdentifier().getType())) .forEach(factMapping -> { String dataObjectName = factMapping.getFactIdentifier().getClassName(); if (dataObjectName.contains(".")) { dataObjectName = dataObjectName.substring(dataObjectName.lastIndexOf(".") + 1); } final String instanceName = factMapping.getFactAlias(); if (!instanceName.equals(dataObjectName)) { final FactModelTree factModelTree = sourceMap.get(dataObjectName); if (factModelTree != null) { toReturn.put(instanceName, factModelTree); } } }); } return toReturn; }
/** * If the <code>FactIdentifier</code> of the given <code>FactMapping</code> equals the one at <b>index</b>, update the <code>FactMapping.FactAlias</code> at <b>index</b> * position with the provided <b>value</b> * @param simulationDescriptor * @param factMappingReference * @param index * @param value */ protected void updateFactMapping(SimulationDescriptor simulationDescriptor, FactMapping factMappingReference, int index, String value) { final FactIdentifier factIdentifierReference = factMappingReference.getFactIdentifier(); FactMapping factMappingToCheck = simulationDescriptor.getFactMappingByIndex(index); final FactIdentifier factIdentifierToCheck = factMappingToCheck.getFactIdentifier(); if (Objects.equals(FactIdentifier.EMPTY, factIdentifierReference)) { if (Objects.equals(factIdentifierToCheck, factIdentifierReference) && Objects.equals(factMappingReference.getFactAlias(), factMappingToCheck.getFactAlias())) { ((ScenarioGridColumn) columns.get(index)).getInformationHeaderMetaData().setTitle(value); factMappingToCheck.setFactAlias(value); } } else if (Objects.equals(factIdentifierToCheck, factIdentifierReference)) { ((ScenarioGridColumn) columns.get(index)).getInformationHeaderMetaData().setTitle(value); factMappingToCheck.setFactAlias(value); } }
@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; } }
@Override public Function<String, String> from1_2to1_3() { return rawXml -> { ScenarioSimulationXMLPersistence xmlPersistence = ScenarioSimulationXMLPersistence.getInstance(); ScenarioSimulationModel model = xmlPersistence.unmarshal(rawXml, false); for (FactMapping factMapping : model.getSimulation().getSimulationDescriptor().getUnmodifiableFactMappings()) { factMapping.getExpressionElements().add(0, new ExpressionElement(factMapping.getFactIdentifier().getName())); } return xmlPersistence.marshal(model).replaceAll("<ScenarioSimulationModel version=\"1.2\">", "<ScenarioSimulationModel version=\"1.3\">"); }; } }
public List<FactMapping> getFactMappingsByFactName(String factName) { return internalFilter(e -> e.getFactIdentifier().getName().equalsIgnoreCase(factName)); }
try { final FactMapping createdFactMapping = simulationDescriptor.addFactMapping(columnIndex, instanceTitle, ((ScenarioGridColumn) column).getFactIdentifier(), ei); createdFactMapping.setExpressionAlias(propertyTitle); if (index == -1) { // This is actually an append super.appendColumn(column); 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 isPropertyAssigned() { factMappingDescription.getExpressionElements().clear(); assertTrue(scenarioGrid.isPropertyAssigned(false, factMappingDescription)); assertTrue(scenarioGrid.isPropertyAssigned(true, factMappingDescription)); factMappingDescription.getExpressionElements().add(new ExpressionElement("test")); assertTrue(scenarioGrid.isPropertyAssigned(false, factMappingDescription)); assertTrue(scenarioGrid.isPropertyAssigned(true, factMappingDescription)); factMappingGiven.getExpressionElements().clear(); assertFalse(scenarioGrid.isPropertyAssigned(false, factMappingGiven)); assertFalse(scenarioGrid.isPropertyAssigned(true, factMappingGiven)); factMappingGiven.getExpressionElements().add(new ExpressionElement("test")); assertFalse(scenarioGrid.isPropertyAssigned(false, factMappingGiven)); assertTrue(scenarioGrid.isPropertyAssigned(true, factMappingGiven)); factMappingInteger.getExpressionElements().clear(); assertFalse(scenarioGrid.isPropertyAssigned(false, factMappingInteger)); assertTrue(scenarioGrid.isPropertyAssigned(true, factMappingInteger)); }
public Map.Entry<String, String> getValidPlaceholders() { String instanceTitle; String propertyTitle; do { int nextColumnCount = nextColumnCount(); instanceTitle = FactMapping.getInstancePlaceHolder(nextColumnCount); propertyTitle = FactMapping.getPropertyPlaceHolder(nextColumnCount); } while (!isNewInstanceName(instanceTitle) || !isNewPropertyName(propertyTitle)); return new AbstractMap.SimpleEntry<>(instanceTitle, propertyTitle); }
public Map<List<String>, Object> getParamsForBean(SimulationDescriptor simulationDescriptor, FactIdentifier factIdentifier, List<FactMappingValue> factMappingValues, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator) { Map<List<String>, Object> paramsForBean = new HashMap<>(); for (FactMappingValue factMappingValue : factMappingValues) { ExpressionIdentifier expressionIdentifier = factMappingValue.getExpressionIdentifier(); FactMapping factMapping = simulationDescriptor.getFactMapping(factIdentifier, expressionIdentifier) .orElseThrow(() -> new IllegalStateException("Wrong expression, this should not happen")); List<String> pathToField = factMapping.getExpressionElementsWithoutClass().stream() .map(ExpressionElement::getStep).collect(toList()); try { Object value = expressionEvaluator.getValueForGiven(factMapping.getClassName(), factMappingValue.getRawValue()); paramsForBean.put(pathToField, value); } catch (IllegalArgumentException e) { factMappingValue.setError(true); throw new ScenarioException(e.getMessage(), e); } } return paramsForBean; }
public FactMapping cloneFactMapping() { return new FactMapping(this); }
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())); }
public void updateHeader(int columnIndex, int headerRowIndex, String value) { final ScenarioHeaderMetaData editedMetadata = (ScenarioHeaderMetaData) getColumns().get(columnIndex).getHeaderMetaData().get(headerRowIndex); // do not update if old and new value are the same if (Objects.equals(editedMetadata.getTitle(), value)) { return; } SimulationDescriptor simulationDescriptor = simulation.getSimulationDescriptor(); FactMapping factMappingToEdit = simulationDescriptor.getFactMappingByIndex(columnIndex); if (editedMetadata.isInstanceHeader()) { // we have to update title and value for every column of the group IntStream.range(0, getColumnCount()).forEach(index -> { updateFactMapping(simulationDescriptor, factMappingToEdit, index, value); }); eventBus.fireEvent(new ReloadRightPanelEvent(false)); } else { editedMetadata.setTitle(value); factMappingToEdit.setExpressionAlias(value); } }