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