Search in sources :

Example 1 with ElementRefExpression

use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression in project AGREE by loonwerks.

the class InputConstraintDialog method addNewEClassMenuItem.

private void addNewEClassMenuItem(final Menu menu, final Reference ref, final String label, final EClass eClass) {
    final MenuItem menuItem = new MenuItem(menu, SWT.RADIO);
    final boolean isInstanceOfEClass = eClass.isInstance(ref.get());
    menuItem.setSelection(isInstanceOfEClass);
    menuItem.setEnabled(!isInstanceOfEClass);
    menuItem.setText(label);
    menuItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            if (((MenuItem) e.widget).getSelection()) {
                final Object oldValue = ref.get();
                // Store interval from current expression in case it is needed to initialize new object
                final IntervalExpression oldInterval;
                if (oldValue instanceof RandomIntegerExpression) {
                    oldInterval = ((RandomIntegerExpression) oldValue).getInterval();
                } else if (oldValue instanceof RandomRealExpression) {
                    oldInterval = ((RandomRealExpression) oldValue).getInterval();
                } else if (oldValue instanceof IntervalExpression) {
                    oldInterval = (IntervalExpression) oldValue;
                } else {
                    oldInterval = null;
                }
                final Object newValue;
                if (eClass == icp.getIntervalExpression() && oldInterval != null) {
                    newValue = oldInterval;
                } else {
                    newValue = InputConstraintFactory.eINSTANCE.create(eClass);
                }
                ref.set(newValue);
                // Perform initialization on the new object
                if (newValue instanceof RandomIntegerExpression || newValue instanceof RandomRealExpression) {
                    final IntervalExpression newInterval = oldInterval == null ? InputConstraintFactory.eINSTANCE.createIntervalExpression() : oldInterval;
                    if (newValue instanceof RandomIntegerExpression) {
                        ((RandomIntegerExpression) newValue).setInterval(newInterval);
                    } else if (newValue instanceof RandomRealExpression) {
                        ((RandomRealExpression) newValue).setInterval(newInterval);
                    }
                } else if (newValue instanceof RandomElementExpression) {
                    ((RandomElementExpression) newValue).setSet(InputConstraintFactory.eINSTANCE.createSetExpression());
                } else if (newValue instanceof PreExpression && oldValue instanceof ElementRefExpression) {
                    ((PreExpression) newValue).setRef((ElementRefExpression) oldValue);
                }
                dlg.refreshContraint();
            }
        }
    });
}
Also used : RandomElementExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomElementExpression) RandomRealExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomRealExpression) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) RandomIntegerExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomIntegerExpression) MenuItem(org.eclipse.swt.widgets.MenuItem) EObject(org.eclipse.emf.ecore.EObject) IntervalExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression) PreExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)

Example 2 with ElementRefExpression

use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression in project AGREE by loonwerks.

the class InputConstraintDialog method addPreviousValueMenuItem.

private void addPreviousValueMenuItem(final Menu menu, final Reference ref) {
    final MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
    menuItem.setSelection(ref.get() instanceof PreExpression);
    menuItem.setText("Previous Value");
    menuItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            final boolean checked = ((MenuItem) e.widget).getSelection();
            if (checked) {
                final PreExpression preExpr = (PreExpression) InputConstraintFactory.eINSTANCE.create(InputConstraintPackage.eINSTANCE.getPreExpression());
                final Object oldValue = ref.get();
                if (oldValue instanceof ElementRefExpression) {
                    preExpr.setRef((ElementRefExpression) oldValue);
                }
                ref.set(preExpr);
            } else {
                ref.set(((PreExpression) ref.get()).getRef());
            }
            dlg.refreshContraint();
        }
    });
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) EObject(org.eclipse.emf.ecore.EObject) PreExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)

Example 3 with ElementRefExpression

use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression in project AGREE by loonwerks.

the class DefaultInputConstraintDialogModel method buildVariablesStream.

private void buildVariablesStream(Stream.Builder<ElementRefExpression> sb, final Collection<?> elements, final SimulationEngineState engineState, final Deque<String> elementIds) {
    for (final Object element : elements) {
        if (!engineState.isElementHidden(element)) {
            final String elementName = engineState.getElementName(element);
            elementIds.push(elementName);
            try {
                final ElementRefExpression newExpr = InputConstraintFactory.eINSTANCE.createElementRefExpression();
                final EList<String> exprIds = newExpr.getIds();
                for (final Iterator<String> it = elementIds.descendingIterator(); it.hasNext(); ) {
                    exprIds.add(it.next());
                }
                sb.add(newExpr);
                buildVariablesStream(sb, engineState.getChildElements(element), engineState, elementIds);
            } finally {
                elementIds.pop();
            }
        }
    }
}
Also used : ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)

Example 4 with ElementRefExpression

use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression in project AGREE by loonwerks.

the class InputConstraintDialog method addElementReferenceMenuItems.

private void addElementReferenceMenuItems(final Menu menu, final Reference ref) {
    // Variables
    final MenuItem variablesMenuItem = new MenuItem(menu, SWT.CASCADE);
    variablesMenuItem.setText("Variables");
    final Menu variablesMenu = new Menu(menu);
    variablesMenuItem.setMenu(variablesMenu);
    final SelectionListener variableMenuItemSelectionListener = new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            final ElementRefExpression variable = (ElementRefExpression) e.widget.getData();
            ref.set(variable);
            dlg.refreshContraint();
        }
    };
    // Create menu items
    // Sort variables to ensure menu items are created in a order that ensures the parent menu item exists.
    final Map<String, MenuItem> idToMenuItemMap = new HashMap<>();
    for (final ElementRefExpression variable : model.getVariables().sorted(InputConstraintDialog::compareElementRef).collect(Collectors.toList())) {
        final List<String> ids = variable.getIds();
        final String variableId = String.join(".", ids);
        final String parentId = String.join(".", ids.subList(0, ids.size() - 1));
        final Menu parentMenu;
        if (parentId.length() == 0) {
            parentMenu = variablesMenu;
        } else {
            final MenuItem parentMenuItem = Objects.requireNonNull(idToMenuItemMap.get(parentId), "Unable to retrieve parent menu item");
            if (parentMenuItem.getMenu() == null) {
                final Menu newMenu = new Menu(parentMenuItem.getParent());
                parentMenuItem.setMenu(newMenu);
                // Create menu item for the variable itself. Unable to select menu items which have children.
                final MenuItem selfMenuItem = new MenuItem(newMenu, SWT.NONE);
                selfMenuItem.setText("<self>");
                selfMenuItem.setData(parentMenuItem.getData());
                selfMenuItem.addSelectionListener(variableMenuItemSelectionListener);
            }
            parentMenu = parentMenuItem.getMenu();
        }
        // Create a menu item for the variable
        final MenuItem variableMenuItem = new MenuItem(parentMenu, SWT.CASCADE);
        variableMenuItem.setText(ids.get(ids.size() - 1));
        variableMenuItem.setData(variable);
        variableMenuItem.addSelectionListener(variableMenuItemSelectionListener);
        idToMenuItemMap.put(variableId, variableMenuItem);
    }
}
Also used : HashMap(java.util.HashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) Menu(org.eclipse.swt.widgets.Menu) SelectionListener(org.eclipse.swt.events.SelectionListener) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)

Example 5 with ElementRefExpression

use of edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression 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

ElementRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)8 PreExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression)4 ConstRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression)3 IntervalExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression)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 EObject (org.eclipse.emf.ecore.EObject)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3 SelectionEvent (org.eclipse.swt.events.SelectionEvent)3 MenuItem (org.eclipse.swt.widgets.MenuItem)3 BinaryExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression)2 BooleanLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral)2 IntegerLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral)2 NegativeExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)2 RealLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RealLiteral)2 SetExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.SetExpression)2 InputConstraintStandaloneSetup (edu.uah.rsesc.aadlsimulator.xtext.InputConstraintStandaloneSetup)1 TestReferenceTypeResolver (edu.uah.rsesc.aadlsimulator.xtext.TestReferenceTypeResolver)1 InputConstraint (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint)1