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);
}
}
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();
}
}
});
}
}
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);
}
}
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);
}
}
}
Aggregations