Search in sources :

Example 1 with ConstRefExpression

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

the class InputConstraintDialog method addReferenceMenuItems.

private void addReferenceMenuItems(final Menu menu, final Reference ref) {
    final MenuItem constantsMenuItem = new MenuItem(menu, SWT.CASCADE);
    constantsMenuItem.setText("Constants");
    final Menu constantsMenu = new Menu(menu);
    constantsMenuItem.setMenu(constantsMenu);
    final Map<String, List<ConstRefExpression>> pkgToConstants = new HashMap<>();
    Iterable<ConstRefExpression> constIteratable = () -> model.getConstants().iterator();
    for (final ConstRefExpression constExpression : constIteratable) {
        final String pkgName = String.join("::", constExpression.getPackageSegments().toArray(new String[0])).toLowerCase();
        List<ConstRefExpression> pkgConstants = pkgToConstants.get(pkgName);
        if (pkgConstants == null) {
            pkgConstants = new ArrayList<>();
            pkgToConstants.put(pkgName, pkgConstants);
        }
        pkgConstants.add(constExpression);
    }
    final SelectionListener constMenuItemSelectionListener = new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            final ConstRefExpression constExpr = (ConstRefExpression) e.widget.getData();
            ref.set(constExpr);
            dlg.refreshContraint();
        }
    };
    final MenuListener constMenuListener = new MenuListener() {

        @Override
        public void menuHidden(final MenuEvent e) {
        }

        @Override
        public void menuShown(final MenuEvent e) {
            final Menu pkgConstantsMenu = (Menu) e.widget;
            // Only create menu items the first time the menu is shown
            if (pkgConstantsMenu.getItemCount() == 0) {
                final String pkgName = (String) pkgConstantsMenu.getData();
                for (final ConstRefExpression constExpr : (Iterable<ConstRefExpression>) pkgToConstants.get(pkgName).stream().sorted((ce1, ce2) -> ce1.getConstantName().compareToIgnoreCase(ce2.getConstantName()))::iterator) {
                    final MenuItem constMenuItem = new MenuItem(pkgConstantsMenu, SWT.CASCADE);
                    constMenuItem.setText(constExpr.getConstantName());
                    constMenuItem.setData(constExpr);
                    constMenuItem.addSelectionListener(constMenuItemSelectionListener);
                }
            }
        }
    };
    for (final String pkgName : pkgToConstants.keySet().stream().sorted().collect(Collectors.toList())) {
        final Menu pkgMenu = new Menu(constantsMenu);
        final MenuItem pkgMenuItem = new MenuItem(constantsMenu, SWT.CASCADE);
        pkgMenuItem.setText(pkgName);
        pkgMenuItem.setMenu(pkgMenu);
        pkgMenu.addMenuListener(constMenuListener);
        pkgMenu.setData(pkgName);
    }
    addElementReferenceMenuItems(menu, ref);
}
Also used : HashMap(java.util.HashMap) ConstRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression) MenuListener(org.eclipse.swt.events.MenuListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) ArrayList(java.util.ArrayList) Menu(org.eclipse.swt.widgets.Menu) SelectionListener(org.eclipse.swt.events.SelectionListener) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 2 with ConstRefExpression

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

the class DefaultInputConstraintDialogModel method createConstRefExpression.

private ConstRefExpression createConstRefExpression(final String constantId) {
    final ConstRefExpression result = InputConstraintFactory.eINSTANCE.createConstRefExpression();
    final String[] segments = constantId.split("::");
    if (segments.length < 2) {
        throw new RuntimeException("Invalid constant id: " + constantId);
    }
    result.setConstantName(segments[segments.length - 1]);
    result.getPackageSegments().addAll(Arrays.asList(segments).subList(0, segments.length - 1));
    return result;
}
Also used : ConstRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression)

Example 3 with ConstRefExpression

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

Example 4 with ConstRefExpression

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

the class InputConstraintDialog method main.

public static void main(final String[] args) {
    new Display();
    final com.google.inject.Injector injector = new InputConstraintStandaloneSetup().createInjectorAndDoEMFRegistration();
    final InputConstraintHelper icHelper = injector.getInstance(InputConstraintHelper.class);
    final TestReferenceTypeResolver resolver = new TestReferenceTypeResolver();
    // Create model to provide information to the dialog.
    final Model model = new Model() {

        @Override
        public String unparse(final InputConstraint ic) {
            return icHelper.unparse(ic);
        }

        @Override
        public InputConstraintHelper.Result parseAndValidate(final String str, final ResultType expectedType, final int numberOfPreviousSteps) {
            return icHelper.parseAndValidate(str, resolver, expectedType, numberOfPreviousSteps);
        }

        @Override
        public InputConstraintHelper.Result validate(final InputConstraint ic, final ResultType expectedType, final int numberOfPreviousSteps) {
            return icHelper.validate(ic, resolver, expectedType, numberOfPreviousSteps);
        }

        @Override
        public Stream<ElementRefExpression> getVariables() {
            final Stream.Builder<ElementRefExpression> builder = Stream.builder();
            builder.add(createElementRefExpression("a1", "b", "d"));
            builder.add(createElementRefExpression("a1"));
            builder.add(createElementRefExpression("a1", "b"));
            builder.add(createElementRefExpression("a1", "b", "c"));
            builder.add(createElementRefExpression("a2", "b"));
            builder.add(createElementRefExpression("a2"));
            return builder.build();
        }

        private ElementRefExpression createElementRefExpression(String... ids) {
            final ElementRefExpression result = InputConstraintFactory.eINSTANCE.createElementRefExpression();
            result.getIds().addAll(Arrays.asList(ids));
            return result;
        }

        @Override
        public ResultType getElementReferenceType(final ElementRefExpression reference) {
            return resolver.getElementReferenceType(reference);
        }

        @Override
        public Stream<ConstRefExpression> getConstants() {
            final Stream.Builder<ConstRefExpression> builder = Stream.builder();
            builder.add(createConstRefExpression("C1", "my_pkg"));
            builder.add(createConstRefExpression("C2", "my_pkg", "inner_pkg"));
            builder.add(createConstRefExpression("C3A", "my_pkg", "inner_pkg", "inner_inner_pkg"));
            builder.add(createConstRefExpression("C3B", "my_pkg", "inner_pkg", "inner_inner_pkg"));
            builder.add(createConstRefExpression("C3C", "my_pkg", "inner_pkg", "inner_inner_pkg"));
            builder.add(createConstRefExpression("C3D", "my_pkg", "inner_pkg", "inner_inner_pkg"));
            return builder.build();
        }

        private ConstRefExpression createConstRefExpression(final String constantName, final String... packageSegments) {
            final ConstRefExpression result = InputConstraintFactory.eINSTANCE.createConstRefExpression();
            result.setConstantName(constantName);
            result.getPackageSegments().addAll(Arrays.asList(packageSegments));
            return result;
        }

        @Override
        public ResultType getConstReferenceType(final ConstRefExpression reference) {
            return resolver.getConstReferenceType(reference);
        }
    };
    // Show the dialog
    final InputConstraint initialConstraint = icHelper.parse("5 + 5").getInputConstraint();
    final Result result = InputConstraintDialog.show(null, model, "z", initialConstraint, ResultType.INTEGER, 1);
    if (result == null) {
        System.out.println("Dialog canceled");
    } else {
        System.out.println("Result:");
        System.out.println(result.inputConstraint);
    }
}
Also used : ConstRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression) InputConstraintHelper(edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintHelper) ResultType(edu.uah.rsesc.aadlsimulator.xtext.util.ResultType) TestReferenceTypeResolver(edu.uah.rsesc.aadlsimulator.xtext.TestReferenceTypeResolver) Stream(java.util.stream.Stream) InputConstraintStandaloneSetup(edu.uah.rsesc.aadlsimulator.xtext.InputConstraintStandaloneSetup) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint) Display(org.eclipse.swt.widgets.Display) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)

Example 5 with ConstRefExpression

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

the class InputConstraintSemanticSequencer method sequence.

@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
    EPackage epackage = semanticObject.eClass().getEPackage();
    ParserRule rule = context.getParserRule();
    Action action = context.getAssignedAction();
    Set<Parameter> parameters = context.getEnabledBooleanParameters();
    if (epackage == InputConstraintPackage.eINSTANCE)
        switch(semanticObject.eClass().getClassifierID()) {
            case InputConstraintPackage.BINARY_EXPRESSION:
                sequence_AddSub_MultDiv(context, (BinaryExpression) semanticObject);
                return;
            case InputConstraintPackage.BOOLEAN_LITERAL:
                sequence_BooleanLiteral(context, (BooleanLiteral) semanticObject);
                return;
            case InputConstraintPackage.CONST_REF_EXPRESSION:
                sequence_ConstRef(context, (ConstRefExpression) semanticObject);
                return;
            case InputConstraintPackage.ELEMENT_REF_EXPRESSION:
                sequence_ElementRef(context, (ElementRefExpression) semanticObject);
                return;
            case InputConstraintPackage.INTEGER_LITERAL:
                sequence_IntegerLiteral(context, (IntegerLiteral) semanticObject);
                return;
            case InputConstraintPackage.INTERVAL_EXPRESSION:
                sequence_Interval(context, (IntervalExpression) semanticObject);
                return;
            case InputConstraintPackage.NEGATIVE_EXPRESSION:
                sequence_Negative(context, (NegativeExpression) semanticObject);
                return;
            case InputConstraintPackage.PRE_EXPRESSION:
                sequence_Pre(context, (PreExpression) semanticObject);
                return;
            case InputConstraintPackage.RANDOM_ELEMENT_EXPRESSION:
                sequence_RandomElement(context, (RandomElementExpression) semanticObject);
                return;
            case InputConstraintPackage.RANDOM_INTEGER_EXPRESSION:
                sequence_RandomInteger(context, (RandomIntegerExpression) semanticObject);
                return;
            case InputConstraintPackage.RANDOM_REAL_EXPRESSION:
                sequence_RandomReal(context, (RandomRealExpression) semanticObject);
                return;
            case InputConstraintPackage.REAL_LITERAL:
                sequence_RealLiteral(context, (RealLiteral) semanticObject);
                return;
            case InputConstraintPackage.SET_EXPRESSION:
                sequence_Set(context, (SetExpression) semanticObject);
                return;
        }
    if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) ConstRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression) SetExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.SetExpression) BooleanLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral) RandomIntegerExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomIntegerExpression) PreExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression) EPackage(org.eclipse.emf.ecore.EPackage) 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) Parameter(org.eclipse.xtext.Parameter) IntervalExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression) IntegerLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral) ElementRefExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression) NegativeExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)

Aggregations

ConstRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression)5 ElementRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)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 IntervalExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression)2 NegativeExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)2 PreExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression)2 RandomElementExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomElementExpression)2 RandomIntegerExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomIntegerExpression)2 RandomRealExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomRealExpression)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 InputConstraintHelper (edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintHelper)1 ResultType (edu.uah.rsesc.aadlsimulator.xtext.util.ResultType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1