Search in sources :

Example 1 with ResultType

use of edu.uah.rsesc.aadlsimulator.xtext.util.ResultType in project AGREE by loonwerks.

the class InputConstraintValidator method checkBinaryExpressionTypes.

@Check
public void checkBinaryExpressionTypes(final BinaryExpression binaryExpr) {
    final InputConstraintTypeDeterminer typeDeterminer = getTypeDeterminer();
    final ResultType leftType = typeDeterminer.determineType(binaryExpr.getLeft());
    final ResultType rightType = typeDeterminer.determineType(binaryExpr.getRight());
    if (leftType != rightType) {
        error("Both sides of a binary expression must be of the same type. Left: " + leftType + ". Right: " + rightType + ".", InputConstraintPackage.Literals.BINARY_EXPRESSION__LEFT);
    } else if (leftType == ResultType.INTEGER && binaryExpr.getOp() == Operator.DIVISION) {
        error("Integer division is not supported.", InputConstraintPackage.Literals.BINARY_EXPRESSION__LEFT);
    }
}
Also used : ResultType(edu.uah.rsesc.aadlsimulator.xtext.util.ResultType) InputConstraintTypeDeterminer(edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintTypeDeterminer) Check(org.eclipse.xtext.validation.Check)

Example 2 with ResultType

use of edu.uah.rsesc.aadlsimulator.xtext.util.ResultType 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 3 with ResultType

use of edu.uah.rsesc.aadlsimulator.xtext.util.ResultType 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 4 with ResultType

use of edu.uah.rsesc.aadlsimulator.xtext.util.ResultType in project AGREE by loonwerks.

the class InputConstraintValidator method checkIntervalTypes.

@Check
public void checkIntervalTypes(final IntervalExpression intervalExpr) {
    if (intervalExpr != null && intervalExpr != null) {
        final InputConstraintTypeDeterminer typeDeterminer = getTypeDeterminer();
        final ResultType leftType = typeDeterminer.determineType(intervalExpr.getLeft());
        final ResultType rightType = typeDeterminer.determineType(intervalExpr.getRight());
        if (leftType != rightType) {
            error("Both sides of an interval must be of the same type. Left: " + leftType + ". Right: " + rightType + ".", InputConstraintPackage.Literals.INTERVAL_EXPRESSION__LEFT);
        }
        if (!(leftType == null || leftType == ResultType.INTEGER || leftType == ResultType.REAL)) {
            error("Interval bounds must be of type integer or real. Type: " + leftType + ".", InputConstraintPackage.Literals.INTERVAL_EXPRESSION__LEFT);
        }
    }
}
Also used : ResultType(edu.uah.rsesc.aadlsimulator.xtext.util.ResultType) InputConstraintTypeDeterminer(edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintTypeDeterminer) Check(org.eclipse.xtext.validation.Check)

Aggregations

ResultType (edu.uah.rsesc.aadlsimulator.xtext.util.ResultType)4 InputConstraintStandaloneSetup (edu.uah.rsesc.aadlsimulator.xtext.InputConstraintStandaloneSetup)2 TestReferenceTypeResolver (edu.uah.rsesc.aadlsimulator.xtext.TestReferenceTypeResolver)2 ConstRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ConstRefExpression)2 ElementRefExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.ElementRefExpression)2 InputConstraint (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint)2 InputConstraintHelper (edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintHelper)2 InputConstraintTypeDeterminer (edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintTypeDeterminer)2 Stream (java.util.stream.Stream)2 Display (org.eclipse.swt.widgets.Display)2 BinaryExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression)1 BooleanLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral)1 InputConstraintFactory (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraintFactory)1 InputConstraintPackage (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraintPackage)1 IntegerLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral)1 IntervalExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntervalExpression)1 NegativeExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.NegativeExpression)1 Operator (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.Operator)1 PreExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.PreExpression)1 RandomElementExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.RandomElementExpression)1