Search in sources :

Example 1 with NegativeExpression

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();
        }
    });
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) ScalarExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ScalarExpression) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)

Example 2 with NegativeExpression

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();
            }
        }
    });
}
Also used : BooleanLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)

Example 3 with NegativeExpression

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);
                }
            }
        }
    });
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Link(org.eclipse.swt.widgets.Link) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)

Example 4 with NegativeExpression

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();
                }
            }
        });
    }
}
Also used : InputConstraintFactory(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraintFactory) Arrays(java.util.Arrays) PreExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) RandomElementExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomElementExpression) MenuEvent(org.eclipse.swt.events.MenuEvent) Composite(org.eclipse.swt.widgets.Composite) Map(java.util.Map) Widget(org.eclipse.swt.widgets.Widget) RandomIntegerExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomIntegerExpression) IntegerLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Button(org.eclipse.swt.widgets.Button) BooleanLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral) InputConstraintStandaloneSetup(edu.uah.rsesc.aadlsimulator.xtext.InputConstraintStandaloneSetup) EObject(org.eclipse.emf.ecore.EObject) Display(org.eclipse.swt.widgets.Display) Collectors(java.util.stream.Collectors) ResultType(edu.uah.rsesc.aadlsimulator.xtext.util.ResultType) Objects(java.util.Objects) List(java.util.List) Window(org.eclipse.jface.window.Window) Stream(java.util.stream.Stream) MenuItem(org.eclipse.swt.widgets.MenuItem) RowLayoutFactory(org.eclipse.jface.layout.RowLayoutFactory) SWT(org.eclipse.swt.SWT) RandomRealExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomRealExpression) MenuListener(org.eclipse.swt.events.MenuListener) IntervalExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression) Operator(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.Operator) Label(org.eclipse.swt.widgets.Label) SelectionListener(org.eclipse.swt.events.SelectionListener) ScalarExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ScalarExpression) InputDialog(org.eclipse.jface.dialogs.InputDialog) BinaryExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression) InputConstraintPackage(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraintPackage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ControlEvent(org.eclipse.swt.events.ControlEvent) InputConstraintHelper(edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintHelper) EClass(org.eclipse.emf.ecore.EClass) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression) GridData(org.eclipse.swt.layout.GridData) RowData(org.eclipse.swt.layout.RowData) Link(org.eclipse.swt.widgets.Link) RealLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RealLiteral) Shell(org.eclipse.swt.widgets.Shell) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) ConstRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression) SetExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.SetExpression) EClassifier(org.eclipse.emf.ecore.EClassifier) TestReferenceTypeResolver(edu.uah.rsesc.aadlsimulator.xtext.TestReferenceTypeResolver) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint) Dialog(org.eclipse.jface.dialogs.Dialog) ControlAdapter(org.eclipse.swt.events.ControlAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression) Control(org.eclipse.swt.widgets.Control) InputDialog(org.eclipse.jface.dialogs.InputDialog) BinaryExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)

Example 5 with NegativeExpression

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);
    }
}
Also used : SetExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.SetExpression) ConstRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression) BooleanLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral) RandomIntegerExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomIntegerExpression) PreExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression) RealLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RealLiteral) BinaryExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression) RandomElementExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomElementExpression) RandomRealExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomRealExpression) EObject(org.eclipse.emf.ecore.EObject) IntervalExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression) IntegerLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)

Aggregations

NegativeExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)7 BooleanLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral)4 RealLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RealLiteral)4 BinaryExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression)3 ConstRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression)3 ElementRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)3 IntegerLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral)3 IntervalExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression)3 PreExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression)3 RandomElementExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomElementExpression)3 RandomIntegerExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomIntegerExpression)3 RandomRealExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomRealExpression)3 ScalarExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ScalarExpression)3 SetExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.SetExpression)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3 SelectionEvent (org.eclipse.swt.events.SelectionEvent)3 EClassifier (org.eclipse.emf.ecore.EClassifier)2 EObject (org.eclipse.emf.ecore.EObject)2 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 MenuItem (org.eclipse.swt.widgets.MenuItem)2