use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression in project AGREE by loonwerks.
the class InputConstraintDialog method addNegateMenuItem.
private void addNegateMenuItem(final Menu menu, final Reference ref) {
final MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
menuItem.setSelection(ref.get() instanceof NegativeExpression);
menuItem.setText("Negate");
menuItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final boolean checked = ((MenuItem) e.widget).getSelection();
if (checked) {
final NegativeExpression negExpr = (NegativeExpression) InputConstraintFactory.eINSTANCE.create(InputConstraintPackage.eINSTANCE.getNegativeExpression());
negExpr.setValue((ScalarExpression) ref.get());
ref.set(negExpr);
} else {
ref.set(((NegativeExpression) ref.get()).getValue());
}
dlg.refreshContraint();
}
});
}
use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression in project AGREE by loonwerks.
the class InputConstraintDialog method addBooleanLiteralMenuItem.
private void addBooleanLiteralMenuItem(final Menu menu, final Reference ref, final boolean newValue) {
final MenuItem menuItem = new MenuItem(menu, SWT.RADIO);
menuItem.setSelection(ref.get() instanceof BooleanLiteral && ((BooleanLiteral) ref.get()).isValue() == newValue);
menuItem.setText(Boolean.toString(newValue));
menuItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
if (((MenuItem) e.widget).getSelection()) {
final BooleanLiteral lit = InputConstraintFactory.eINSTANCE.createBooleanLiteral();
lit.setValue(newValue);
// Remove parent negation for boolean literals
final Reference refToEdit = (ref.getParent() != null && ref.getParent().get() instanceof NegativeExpression) ? ref.getParent() : ref;
refToEdit.set(lit);
dlg.refreshContraint();
}
}
});
}
use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression in project AGREE by loonwerks.
the class InputConstraintDialog method newLink.
private void newLink(final Composite container, final Reference originalRef, final String txt) {
// Determine Text for the Node
final Link newLink = new Link(container, SWT.NONE);
newLink.setText("<a>" + txt + "</a>");
newLink.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final Menu menu = new Menu(newLink);
// Dispose of the old menu if it exists
if (newLink.getMenu() != null) {
newLink.getMenu().dispose();
}
newLink.setMenu(menu);
menu.setVisible(true);
// Edit the negative expression itself rather than actual value
Reference ref = originalRef;
if ((ref.getParent() != null && ref.getParent().get() instanceof NegativeExpression)) {
ref = ref.getParent();
}
// Populate menu
if (ref instanceof RootConstraintReference) {
addEditExpressionMenuItem(menu, ref);
addScalarExpressionsMenuItems(menu, true, ref);
addNewEClassMenuItem(menu, ref, "Interval", icp.getIntervalExpression());
addNewEClassMenuItem(menu, ref, "Set", icp.getSetExpression());
} else {
final EClassifier type = ref.getEType();
if (ref instanceof ListMemberReference && ref.get() != null) {
addRemoveMenuItem(menu, ref);
}
if (ref instanceof StructuralFeatureReference && ((StructuralFeatureReference) ref).getEStructuralFeature() == icp.getIntervalExpression_LeftClosed()) {
addBooleanMenuItem(menu, ref, "( - Exclusive", false);
addBooleanMenuItem(menu, ref, "[ - Inclusive", true);
} else if (ref instanceof StructuralFeatureReference && ((StructuralFeatureReference) ref).getEStructuralFeature() == icp.getIntervalExpression_RightClosed()) {
addBooleanMenuItem(menu, ref, ") - Exclusive", false);
addBooleanMenuItem(menu, ref, "] - Inclusive", true);
} else if (type == icp.getScalarExpression()) {
addEditExpressionMenuItem(menu, ref);
final EStructuralFeature sf = ref instanceof StructuralFeatureReference ? ((StructuralFeatureReference) ref).getEStructuralFeature() : null;
final boolean showBoolean = sf != icp.getIntervalExpression_Left() && sf != icp.getIntervalExpression_Right();
addScalarExpressionsMenuItems(menu, showBoolean, ref);
} else if (type == icp.getElementRefExpression()) {
addElementReferenceMenuItems(menu, ref);
} else if (type == icp.getOperator()) {
final Reference parent = ref.getParent();
addEditExpressionMenuItem(menu, parent);
addNegateMenuItem(menu, parent.getParent() != null && parent.getParent().get() instanceof NegativeExpression ? parent.getParent() : parent);
addOperatorMenuItem(menu, ref, Operator.ADDITION);
addOperatorMenuItem(menu, ref, Operator.SUBTRACTION);
addOperatorMenuItem(menu, ref, Operator.MULTIPLICATION);
addOperatorMenuItem(menu, ref, Operator.DIVISION);
} else {
throw new RuntimeException("Unhandled type: " + type);
}
}
}
});
}
use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression in project AGREE by loonwerks.
the class InputConstraintDialog method addTextExpressionMenuItem.
/**
* This function will bubble up to edit the parent if the parent is a binary or negative expression
* @param menu
* @param ref
* @param label
* @param expectedType
* @param defaultExpressionTxt used if the reference is null
*/
private void addTextExpressionMenuItem(final Menu menu, Reference ref, final String label, final ResultType expectedType, final String defaultExpressionTxt) {
// If the value is part of a binary expression, prompt the user to edit the entire containing binary expression.
while (ref.getParent() != null && (ref.getParent().get() instanceof BinaryExpression || ref.getParent().get() instanceof NegativeExpression)) {
ref = ref.getParent();
}
final Reference refToEdit = ref;
final MenuItem menuItem = new MenuItem(menu, SWT.NONE);
menuItem.setText(label);
// Disable the menu item if unable to unparse
final InputConstraint ic = (InputConstraint) refToEdit.get();
final String exprTxt = unparse(ic);
menuItem.setEnabled(exprTxt != null);
if (menuItem.isEnabled()) {
menuItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final InputDialog txtExpressionDlg = new InputDialog(dlg.getShell(), "Edit Expression", "Edit Expression", exprTxt, value -> getParseAndValidateResult(value, expectedType).getErrorMessage());
if (txtExpressionDlg.open() == Window.OK) {
refToEdit.set(getParseAndValidateResult(txtExpressionDlg.getValue(), expectedType).getInputConstraint());
dlg.refreshContraint();
}
}
});
}
}
use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression in project AGREE by loonwerks.
the class InputConstraintDialog method createConstraintWidgets.
private void createConstraintWidgets(final Composite container, final Object parentValue, final Reference ref) {
final Object value = ref.get();
if (value instanceof IntervalExpression) {
final IntervalExpression ie = (IntervalExpression) value;
if (ref.getEType() == icp.getIntervalExpression()) {
newLabel(container, "interval");
} else {
newLink(container, ref, "interval");
}
newLink(container, new StructuralFeatureReference(ref, ie, icp.getIntervalExpression_LeftClosed()), ie.isLeftClosed() ? "[" : "(");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, ie, icp.getIntervalExpression_Left()));
newLabel(container, ",");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, ie, icp.getIntervalExpression_Right()));
newLink(container, new StructuralFeatureReference(ref, ie, icp.getIntervalExpression_RightClosed()), ie.isRightClosed() ? "]" : ")");
} else if (value instanceof SetExpression) {
final SetExpression se = (SetExpression) value;
if (ref.getEType() == icp.getSetExpression()) {
newLabel(container, "set");
} else {
newLink(container, ref, "set");
}
newLabel(container, "{");
final int numberOfMembers = se.getMembers().size();
for (int i = 0; i < numberOfMembers; i++) {
createConstraintWidgets(container, value, new ListMemberReference(ref, icp.getSetExpression_Members().getEType(), se.getMembers(), i));
newLabel(container, ",");
}
newLink(container, new ListMemberReference(ref, icp.getSetExpression_Members().getEType(), se.getMembers(), -1), "<add>");
newLabel(container, "}");
} else if (value instanceof BinaryExpression) {
final BinaryExpression be = (BinaryExpression) value;
boolean showParentheses = parentValue instanceof NegativeExpression;
if (!showParentheses && parentValue instanceof BinaryExpression) {
final BinaryExpression parentBe = (BinaryExpression) parentValue;
final boolean isAddOrSubstract = be.getOp() == Operator.ADDITION || be.getOp() == Operator.SUBTRACTION;
final boolean isParentAddOrSubstract = parentBe.getOp() == Operator.ADDITION || parentBe.getOp() == Operator.SUBTRACTION;
if (isAddOrSubstract && !isParentAddOrSubstract) {
showParentheses = true;
}
}
if (showParentheses) {
newLabel(container, "(");
}
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, be, icp.getBinaryExpression_Left()));
newLink(container, new StructuralFeatureReference(ref, be, icp.getBinaryExpression_Op()), be.getOp().toString());
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, be, icp.getBinaryExpression_Right()));
if (showParentheses) {
newLabel(container, ")");
}
} else if (value instanceof NegativeExpression) {
final NegativeExpression ne = (NegativeExpression) value;
newLabel(container, "-");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, ne, icp.getNegativeExpression_Value()));
} else if (value instanceof PreExpression) {
newLink(container, ref, "previous value of");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, (PreExpression) value, icp.getPreExpression_Ref()));
} else if (value instanceof RandomElementExpression) {
newLink(container, ref, "random element");
newLabel(container, "in");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, (RandomElementExpression) value, icp.getRandomElementExpression_Set()));
} else if (value instanceof RandomIntegerExpression) {
newLink(container, ref, "random integer");
newLabel(container, "in");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, (RandomIntegerExpression) value, icp.getRandomIntegerExpression_Interval()));
} else if (value instanceof RandomRealExpression) {
newLink(container, ref, "random real");
newLabel(container, "in");
createConstraintWidgets(container, value, new StructuralFeatureReference(ref, (RandomRealExpression) value, icp.getRandomRealExpression_Interval()));
} else if (value instanceof ConstRefExpression) {
newLink(container, ref, model.unparse((ConstRefExpression) value));
} else if (value instanceof ElementRefExpression) {
newLink(container, ref, model.unparse((ElementRefExpression) value));
} else if (value instanceof RealLiteral) {
final RealLiteral rl = (RealLiteral) value;
newLink(container, ref, rl.getValue().toString());
} else if (value instanceof IntegerLiteral) {
final IntegerLiteral il = (IntegerLiteral) value;
newLink(container, ref, il.getValue().toString());
} else if (value instanceof BooleanLiteral) {
final BooleanLiteral bl = (BooleanLiteral) value;
newLink(container, ref, Boolean.toString(bl.isValue()));
} else if (value == null) {
newLink(container, ref, "<select>");
} else {
throw new RuntimeException("Unexpected value: " + value);
}
}
Aggregations