use of edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintHelper in project AGREE by loonwerks.
the class TestGrammar method testConstraint.
private static void testConstraint(final String constraint, final ResultType expectedType, final int numberOfPreviousSteps, final boolean isConstraintValid) {
com.google.inject.Injector injector = new InputConstraintStandaloneSetup().createInjectorAndDoEMFRegistration();
final InputConstraintHelper parserHelper = injector.getInstance(InputConstraintHelper.class);
final ReferenceTypeResolver refResolver = new TestReferenceTypeResolver();
final InputConstraintHelper.Result parseAndValidateResult = parserHelper.parseAndValidate(constraint, refResolver, expectedType, numberOfPreviousSteps);
if (parseAndValidateResult.isValid()) {
if (isConstraintValid) {
System.out.println("Success: Constructed AST for constraint: " + constraint + " : " + parseAndValidateResult.getInputConstraint());
// Try to unparse the constraint
parserHelper.unparse(parseAndValidateResult.getInputConstraint());
} else {
System.err.println("Error: Invalid constraint: " + constraint + " passed test.");
}
} else {
if (isConstraintValid) {
System.err.println("Error: Failed to create AST from constraint: " + constraint + ". " + parseAndValidateResult.getErrorMessage());
} else {
System.out.println("Success: Expected error for : " + constraint + " : " + parseAndValidateResult.getErrorMessage());
}
}
}
use of edu.uah.rsesc.aadlsimulator.xtext.util.InputConstraintHelper 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);
}
}
Aggregations