# Edit

## Access to Session/ExecutionManager/EditingDomain

When we open an aird (doucle click on it),

A `Session` is created containing a `TransactionalEditingDomain` which contains a `ResourceSet` containing all `Resource(s)` loaded, containing all EObjects. It is editable through a `ExecutionManager`.

- `EObject` is the EMF element (Function, Component, graphical element, etc)
- `Resource` is the EMF representation of the XML files (aird, melodymodeller) containing all EObject
- `ResourceSet` is the list of all Resources loaded (melodymodeller, aird, odesign, etc)
- `TransactionalEditingDomain` allows to access element/model/diagrams in a Thread-safe environnement. All modifications in Capella are done within it.
- `ExecutionManager` is the API main access to be able to edit an element.
- `Session` is the API main access to Diagrams

## Session

Session

SessionManager

DialectManager

org.eclipse.sirius.business.api.query

org.eclipse.sirius.diagram

org.eclipse.sirius.diagram.business.api.query

    Session session = SessionManager.INSTANCE.getSession(EObject object);
    Session session = SessionManager.INSTANCE.getExistingSession(EcoreUtil2.getURI(airdFile));
    TransactionalEditingDomain domain = session.getTransactionalEditingDomain();

## TransactionalEditingDomain

TransactionalEditingDomain

TransactionHelper

    TransactionalEditingDomain domain = TransactionHelper.getEditingDomain(element)
    TransactionalEditingDomain domain = TransactionHelper.getEditingDomain(resource)

TransactionUtil

EObjectExt

    Collection<EObject> subComponents = EObjectExt.getAll(element, CsPackage.Literals.COMPONENT)

## ExecutionManager

ExecutionManager

TransactionHelper

AbstractReadWriteCommand

    ExecutionManager manager = TransactionHelper.getExecutionManager(element)
    ExecutionManager manager = TransactionHelper.getExecutionManager(resource)

Create a new TransactionalEditingDomain for Capella models:

    ExecutionManager manager = ExecutionManagerRegistry.getInstance().addNewManager();
    TransactionalEditingDomain domain = manager.getEditingDomain();

## IFile/Resource

EcoreUtil2

    EcoreUtil2.getFile(Resource)
    EcoreUtil2.getURI(file)

    element.eResource()

ResourcesPlugin

    ResourcesPlugin.getWorkspace().getRoot().getProject("project").getFile("toto.melodymodeller");

## Helpers

Many helpers exist in Capella, they are mainly located inside following plugins and have *Ext name

org.polarsys.capella.core.data.helpers

org.polarsys.capella.core.model.helpers

`CapellaElementExt.creationService(x)` Helps to create additional elements like if it was created from Add Element menu. (for instance, the Part aside the Component, or min/max cardinalities on a Property)

For instance

`FunctionExt, FunctionalExchangeExt, ComponentExt`

All capella elements have derived methods allowing direct access to other interesting elements. For instance, for an actor, actor.getAllocatedFunctions() retrieve the list of the allocated functions). These methods are computed in :

    org.polarsys.capella.core.data.helpers
    org.polarsys.capella.core.data.helpers.*.delegates.**Helper

It can be interesting to see how these methods are computed to understand how a reference between two element is stocked in the model.

## Access all diagrams

    IFile airdFile = ...
    Session session = SessionManager.INSTANCE.getExistingSession(EcoreUtil2.getURI(airdFile));
    for (DRepresentationDescriptor descriptor :  DialectManager.INSTANCE.getAllRepresentationDescriptors(session)) {
       descriptor.getRepresentation(); //will return the diagram
       descriptor.getTarget()); //will return the element owning the diagram (for instance, the PhysicalComponent in a PAB diagram)
    }

## Access all graphical elements in a diagram

    DDiagram diagram = (DDiagram)descriptor.getRepresentation(); //warning! directCast but there may have also DTable, DTree..
    for (DDiagramElement element: diagram.getDiagramElements()) { //for all main elements in diagram
       System.out.println(element); //here we have the graphical element

       if (element instanceof DEdge) {
         DEdge edge = (DEdge)element;
         edge.getSourceNode()
         edge.getTargetNode()
         edge.getStyle()

       } else if (element instanceof DNodeContainer, DNode, DNodeList) {
         ((DNodeContainer/DNode/DNodeList)element).getOwnedBorderedNodes()
         ((DNodeContainer/DNode/DNodeList)element).getStyle()

         Node a = SiriusGMFHelper.getGmfNode(element); //here we have the GMF graphical element
         System.out.println("located: "+a.getLayoutConstraint()); //display the location of the element
       }

    }

## Edit an element

All editions in Capella is made through an TransactionalEditingDomain that guarantee a Thread-safe environnement. Any modification on model, diagram must be done through a command, like :

    EObject element = ...
    ExecutionManager manager = TransactionHelper.getExecutionManager(element).execute(new AbstractReadWriteCommand() {

      @Override
      public void run() {
        if (element instanceof PhysicalComponent) {
          PhysicalComponent pc = (PhysicalComponent)element;
          pc.setName("toto");
          CapellaElementExt.creationService(pc); //to create the related part
        }
      }
    });

## Create an element

To create an element, you need to know its type (for instance by looking at the properties view) and know from which metamodel it comes from. See Metamodel Tutorial

    EObject element = ... //an element from physical layer
    ExecutionManager manager = TransactionHelper.getExecutionManager(element).execute(new AbstractReadWriteCommand() {

      @Override
      public void run() {
        BlockArchitecture physicalArchitecture = BlockArchitectureExt.getRootBlockArchitexture(element);
        PhysicalFunction pf = (PhysicalFunction)BlockArchitectureExt.getRootFunction(physicalArchitecture);

        PhysicalFunction myNewFunc = PaFactory.eINSTANCE.createPhysicalFunction("new function");
        pf.getOwnedPhysicalFunctions().add(myNewFunc);

      }
    });

## EMF API (Generic access)

EObject

EClass

EStructuralFeature

Resource

ResourceSet

    EObject object = ...

    element.eResource()

    object.eContents() //returns all direct children of the element

    object.eAllContents() //returns all children recursively

    object.eClass() //returns the Metaclass of the given element

    object.eClass().getAllReferences() //returns the Metaclass references of the given element

    object.eClass().getAllAttributes() //returns the Metaclass references of the given element

    object.eGet(object.eClass().getEStructuralFeature("name")) // returns the name of the element

    //BUT, if you know the metaclass, it's faster to use getName() method. ((PhysicalFunction)object.getName()) for instance

    object.eSet(object.eClass().getEStructuralFeature("name"), "newName") //set the name of the element

    ((EList)object.eGet(object.eClass().getEStructuralFeature("ownedElements"))).add(element) //add element to object.ownedElements