/** Adds a new constraint. */ private void addConstraint() { IDecisionVariable variable = getVariable(); ConstraintEditorDialog dlg = new ConstraintEditorDialog(getShell(), variable, null); if (ConstraintEditorDialog.OK == dlg.open()) { String constraint = dlg.getConstraintText(); if (constraint.length() > 0) { try { if (null == variableValue) { variableValue = (ContainerValue) ValueFactory.createValue( variable.getDeclaration().getType(), ValueFactory.EMPTY); variable.setValue(variableValue, AssignmentState.ASSIGNED); } if (null != variableValue) { Value val = createConstraintValue(constraint); checkForDuplicates(val, -1); variableValue.addElement(val); TableItem item = new TableItem(tableViewer.getTable(), SWT.NULL); item.setText(constraint); setDirty(); } } catch (ValueDoesNotMatchTypeException e) { exceptionDialog(e); } catch (ConfigurationException e) { exceptionDialog(e); } } } }
/** * Turns a constraint container into string representations. * * @param constraints the constraints * @return the string representations */ String[] toConstraints(ContainerValue constraints) { List<String> tmp = new ArrayList<String>(); AbstractVariable decl = getVariable().getDeclaration(); IModelElement context = decl; while (null != context && !(context instanceof Compound)) { context = context.getParent(); } if (!(context instanceof Compound)) { context = decl; } for (int t = 0; t < constraints.getElementSize(); t++) { Value cVal = constraints.getElement(t); if (cVal instanceof ConstraintValue) { ConstraintValue val = (ConstraintValue) cVal; tmp.add(StringProvider.toIvmlString(val.getValue(), context)); } } return tmp.toArray(new String[tmp.size()]); }
/** Removes the selected constraint. */ private void removeSelectedConstraint() { int index = tableViewer.getTable().getSelectionIndex(); if (index >= 0) { if (null != variableValue) { variableValue.removeElement(index); tableViewer.getTable().remove(index); setDirty(); } } }
/** * Checks for duplicates within {@link #variableValue}. This is a workaround for the problem that * container values currently do not check for duplicate values while adding/setting values. * * @param value the new value * @param allowedPos an allowed position in case of value replacements, may be negative in order * to indicate that there is no such position * @throws ValueDoesNotMatchTypeException in case of a duplicate in a Set */ private void checkForDuplicates(Value value, int allowedPos) throws ValueDoesNotMatchTypeException { if (null != variableValue && Set.TYPE.isAssignableFrom(getVariable().getDeclaration().getType())) { int index = variableValue.indexOf(value); if (index >= 0) { if (allowedPos < 0 || index != allowedPos) { throw new ValueDoesNotMatchTypeException( "Duplicate constraint is not allowed", ValueDoesNotMatchTypeException.NOT_ALLOWED_VALUE_STRUCTURE); } } } }