Search in sources :

Example 1 with Aadl2Factory

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);
        }
    });
}
Also used : ComponentClassifier(org.osate.aadl2.ComponentClassifier) FeatureType(org.osate.aadl2.FeatureType) SubcomponentType(org.osate.aadl2.SubcomponentType) FeatureGroupPrototypeActual(org.osate.aadl2.FeatureGroupPrototypeActual) FeaturePrototypeBinding(org.osate.aadl2.FeaturePrototypeBinding) ComponentCategory(org.osate.aadl2.ComponentCategory) ComponentPrototype(org.osate.aadl2.ComponentPrototype) FeatureGroupPrototype(org.osate.aadl2.FeatureGroupPrototype) ComponentPrototypeActual(org.osate.aadl2.ComponentPrototypeActual) EObject(org.eclipse.emf.ecore.EObject) FeaturePrototypeReference(org.osate.aadl2.FeaturePrototypeReference) AccessType(org.osate.aadl2.AccessType) FeatureGroupPrototypeBinding(org.osate.aadl2.FeatureGroupPrototypeBinding) ComponentPrototypeBinding(org.osate.aadl2.ComponentPrototypeBinding) Aadl2Factory(org.osate.aadl2.Aadl2Factory) DirectionType(org.osate.aadl2.DirectionType) PortSpecification(org.osate.aadl2.PortSpecification) FeaturePrototype(org.osate.aadl2.FeaturePrototype) AccessSpecification(org.osate.aadl2.AccessSpecification)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)1 Aadl2Factory (org.osate.aadl2.Aadl2Factory)1 AccessSpecification (org.osate.aadl2.AccessSpecification)1 AccessType (org.osate.aadl2.AccessType)1 ComponentCategory (org.osate.aadl2.ComponentCategory)1 ComponentClassifier (org.osate.aadl2.ComponentClassifier)1 ComponentPrototype (org.osate.aadl2.ComponentPrototype)1 ComponentPrototypeActual (org.osate.aadl2.ComponentPrototypeActual)1 ComponentPrototypeBinding (org.osate.aadl2.ComponentPrototypeBinding)1 DirectionType (org.osate.aadl2.DirectionType)1 FeatureGroupPrototype (org.osate.aadl2.FeatureGroupPrototype)1 FeatureGroupPrototypeActual (org.osate.aadl2.FeatureGroupPrototypeActual)1 FeatureGroupPrototypeBinding (org.osate.aadl2.FeatureGroupPrototypeBinding)1 FeaturePrototype (org.osate.aadl2.FeaturePrototype)1 FeaturePrototypeBinding (org.osate.aadl2.FeaturePrototypeBinding)1 FeaturePrototypeReference (org.osate.aadl2.FeaturePrototypeReference)1 FeatureType (org.osate.aadl2.FeatureType)1 PortSpecification (org.osate.aadl2.PortSpecification)1 SubcomponentType (org.osate.aadl2.SubcomponentType)1