use of org.osate.aadl2.Aadl2Factory in project osate2 by osate.
the class FeatureReferenceBindingType method createNewBindings.
/**
* Creates new prototype binding business objects based on the state of the model. Bindings which are not valid are ignored.
* @param parent is the parent node for binding. Used to retrieve child binding nodes.
* @param classifier is the classifier which to use to retrieve prototypes
* @param boBeingModified is the top level business object which is being modified. Used to add imports to appropriate package.
* @param newBindings is the list to which to add created bindings.
*/
protected final void createNewBindings(final PrototypeBindingsModelNode parent, final Object classifier, final Element boBeingModified, final List<PrototypeBinding> newBindings) {
final Aadl2Factory f = AgeAadlUtil.getAadl2Factory();
// Check for a binding for each prototype of the specified classifier
AadlPrototypeUtil.getAllPrototypes(classifier).forEachOrdered(p -> {
final PrototypeBindingsModelNode child = new PrototypeBindingsModelNode(parent, p);
final PrototypeBindingsModelNodeData cd = data(child);
final EObject childClassifier = cd.classifier == null ? null : cd.classifier.getResolvedValue(getResourceSet());
if (p instanceof ComponentPrototype) {
// Ignore classifiers which are not of the appropriate type. They could be carry overs from children which no longer exist
if (childClassifier instanceof SubcomponentType) {
final ComponentPrototypeBinding b = f.createComponentPrototypeBinding();
b.setFormal(p);
final ComponentPrototypeActual a = b.createActual();
// Set subcomponent type and create child bindings
a.setSubcomponentType((SubcomponentType) childClassifier);
AadlImportsUtil.ensurePackageIsImportedForClassifier(boBeingModified, childClassifier);
createNewBindings(child, childClassifier, boBeingModified, a.getBindings());
// Determine and set the component category
final ComponentCategory category;
if (childClassifier instanceof ComponentClassifier) {
category = ((ComponentClassifier) childClassifier).getCategory();
} else if (childClassifier instanceof ComponentPrototype) {
category = ((ComponentPrototype) childClassifier).getCategory();
} else {
category = null;
}
if (category == null) {
throw new RuntimeException("Unable to determine component category for " + childClassifier);
}
a.setCategory(category);
// Add the binding to the specified list
newBindings.add(b);
}
} else if (p instanceof FeatureGroupPrototype) {
if (childClassifier instanceof FeatureType) {
// Create the binding and the actual
final FeatureGroupPrototypeBinding b = f.createFeatureGroupPrototypeBinding();
b.setFormal(p);
final FeatureGroupPrototypeActual a = b.createActual();
// Set feature type and create child bindings
a.setFeatureType((FeatureType) childClassifier);
AadlImportsUtil.ensurePackageIsImportedForClassifier(boBeingModified, childClassifier);
createNewBindings(child, childClassifier, boBeingModified, a.getBindings());
// Add the binding to the specified list
newBindings.add(b);
}
} else if (p instanceof FeaturePrototype) {
// Ignore any bindings for which a type is not set
if (cd.type != null) {
final FeaturePrototypeBinding b = f.createFeaturePrototypeBinding();
b.setFormal(p);
if (cd.type instanceof AccessSpecificationBindingType) {
if (cd.direction instanceof AccessType) {
final AccessSpecificationBindingType type = (AccessSpecificationBindingType) cd.type;
final AccessSpecification actual = (AccessSpecification) b.createActual(Aadl2Package.eINSTANCE.getAccessSpecification());
// Configure the category and kind of the access specification
actual.setCategory(type.getCategory());
actual.setKind((AccessType) cd.direction);
// Set optional classifier
if (childClassifier instanceof ComponentClassifier) {
actual.setClassifier((ComponentClassifier) childClassifier);
AadlImportsUtil.ensurePackageIsImportedForClassifier(boBeingModified, childClassifier);
}
}
} else if (cd.type instanceof PortSpecificationBindingType) {
if (cd.direction instanceof DirectionType) {
final PortSpecificationBindingType type = (PortSpecificationBindingType) cd.type;
final PortSpecification actual = (PortSpecification) b.createActual(Aadl2Package.eINSTANCE.getPortSpecification());
actual.setCategory(type.getCategory());
// Set the direction
final DirectionType direction = (DirectionType) cd.direction;
switch(direction) {
case IN:
actual.setIn(true);
actual.setOut(false);
break;
case OUT:
actual.setIn(false);
actual.setOut(true);
break;
case IN_OUT:
actual.setIn(true);
actual.setOut(true);
break;
}
// Set optional classifier
if (childClassifier instanceof ComponentClassifier) {
AadlImportsUtil.ensurePackageIsImportedForClassifier(boBeingModified, childClassifier);
actual.setClassifier((ComponentClassifier) childClassifier);
}
}
} else if (cd.type instanceof FeatureReferenceBindingType) {
// Classifier is required
if (childClassifier instanceof FeaturePrototype) {
final FeaturePrototypeReference actual = (FeaturePrototypeReference) b.createActual(Aadl2Package.eINSTANCE.getFeaturePrototypeReference());
// Direction is optional. Ignore IN_OUT as it is not a valid option
if (cd.direction instanceof DirectionType) {
final DirectionType direction = (DirectionType) cd.direction;
switch(direction) {
case IN:
actual.setIn(true);
actual.setOut(false);
break;
case OUT:
actual.setIn(false);
actual.setOut(true);
break;
default:
break;
}
}
actual.setPrototype((FeaturePrototype) childClassifier);
}
} else {
throw new RuntimeException("Unexpected type: " + cd.type);
}
// Don't add the binding if it wasn't completely created
if (b.getActual() != null) {
// Add the binding to the specified list
newBindings.add(b);
}
}
} else {
throw new RuntimeException("Unexpected prototype class: " + p);
}
});
}
Aggregations