Self-maintaining diagrams in Enterprise Architect with EA-Matic

With EA-Matic you can develop add-ins for Enterprise Architect using the built-in scripting feature of EA.

This example shows how to make diagrams that maintain themselves to make sure they are always up-to-date.

The idea is that we want a diagram that shows all the relations of the owning object. The diagram should always be up-to-date without manual intervention.

EA-Matic auto_diagram

To indicate that this diagram is an automatic diagram we nest it under the class and prefix it with AUTO_ and on the diagram we place the owning object and its related elements

EA-Matic self-maintaining diagram

Usage of Enterprise Architect Add-in Framework

This script also illustrates how you can use the Enterprise Architect Add-in Framework within your script.

This extensive open source framework is the basis for the add-ins EA-Matic and EA Navigator, and offers a much more functional interface to the model then the standard EA API.

This code inializes the model object with the current Repository

<br />
'gets a new instance of the EAAddinFramework and initializes it with the EA.Repository<br />
function getEAAddingFrameworkModel()<br />
    'Initialize the EAAddinFramework model<br />
    dim model<br />
    set model = CreateObject(&quot;TSF.UmlToolingFramework.Wrappers.EA.Model&quot;)<br />
    model.initialize(Repository)<br />
    set getEAAddingFrameworkModel = model<br />
end function<br />

It is then further used to get the ConnectorWrapper based on the ElementID from the Info object

<br />
set connector = model.getRelationByID(connectorID)<br />

This connector is then used to access the elements that are connected by this Connector

<br />
 'get the related elements<br />
 dim relatedElements<br />
 set relatedElements = model.toArrayList(connector.relatedElements)<br />

Note that we need to use model.toArrayList to convert the C# generic List<> that cannot be used by VBScript to an ArrayList that can be used.

The code

Download the complete script: EA-Matic Self-maintaining diagram

In the first part we use the magic EA-Matic keyword to in order to tell EA-Matic it should use this script.

We also declare the important ID’s of the selected connector so we can compare that with the ID’s of this connector once it has changed.

The first event we use is the EA_OnPostNewConnector. This event will be fired after you create a new connector in EA.
In this event we add the newly related elements to each others auto-diagram, if any.

<br />
!INC Local Scripts.EAConstants-VBScript</p>
<p>' EA-Matic<br />
' This script, when used with EA-Matic will maintain auto-updating diagrams for elements.<br />
' A nested diagram with prefix AUTO_ will be considered an auto-updating diagram.<br />
' The diagram will keep track of all elements related to the owner of the auto diagram.<br />
'<br />
' Author:     Geert Bellekens<br />
' EA-Matic: https://bellekens.com/ea-matic/<br />
'<br />
'maintain a reference to the connector in context<br />
dim contextConnectorID<br />
dim oldClientID<br />
dim oldSupplierID</p>
<p>'a new connector has been created. Add the related elements to the auto-diagram<br />
function EA_OnPostNewConnector(Info)<br />
     'get the connector id from the Info<br />
     dim connectorID<br />
     connectorID = Info.Get(&quot;ConnectorID&quot;)<br />
     dim model<br />
     'get the model<br />
     set model = getEAAddingFrameworkModel()<br />
     dim connector<br />
     set connector = model.getRelationByID(connectorID)<br />
     'get the related elements<br />
     dim relatedElements<br />
     set relatedElements = model.toArrayList(connector.relatedElements)<br />
     'for i = 0 to attributes.Count - 1<br />
     if relatedElements.Count = 2 then<br />
        'once with the first<br />
        addRelatedElementoAutoDiagram relatedElements(0), relatedElements(1), model<br />
        'then with the second<br />
        addRelatedElementoAutoDiagram relatedElements(1), relatedElements(0), model<br />
     end if<br />
end function</p>
<p>'adds the related element to the auto_updatediagrams if any<br />
function addRelatedElementoAutoDiagram(element,relatedElement, model)<br />
    'get the diagram owned by this element<br />
    dim ownedDiagrams<br />
    set ownedDiagrams = model.toArrayList(element.ownedDiagrams)<br />
    for each diagram In ownedDiagrams<br />
        'check the name of the diagram<br />
        if Left(diagram.name,LEN(&quot;AUTO_&quot;)) = &quot;AUTO_&quot; then<br />
            'add the related element to the diagram<br />
            diagram.addToDiagram(relatedElement)<br />
        end if<br />
    next<br />
end function<br />

The next part deals with the deletion of a connector. Each time a connector is about to be deleted EA fires the EA_OnPreDeleteConnector event.
We remove the connected elements from their respective auto-diagrams, but only if this connector was the last connector between the two elements.

<br />
' A connector will be deleted. Remove the elements from the auto-diagram<br />
function EA_OnPreDeleteConnector(Info)<br />
     'get the connector id from the Info<br />
     dim connectorID<br />
     connectorID = Info.Get(&quot;ConnectorID&quot;)<br />
     dim model<br />
     'get the model<br />
     set model = getEAAddingFrameworkModel()<br />
     dim connector<br />
     set connector = model.getRelationByID(connectorID)<br />
     'get the related elements<br />
     dim relatedElements<br />
     set relatedElements = model.toArrayList(connector.relatedElements)<br />
     'for i = 0 to attributes.Count - 1<br />
     if relatedElements.Count = 2 then<br />
        'we only need to remove the related element if they are not connected anymore after deleting the connector<br />
        'so only if there is only one relationship between the two elements<br />
        if sharedRelationsCount(relatedElements(0), relatedElements(1), model) &lt;= 1 then<br />
            'once with the first<br />
            removeRelatedElemenFromAutoDiagram relatedElements(0), relatedElements(1), model<br />
            'then with the second<br />
            removeRelatedElemenFromAutoDiagram relatedElements(1), relatedElements(0), model<br />
        end if<br />
     end if<br />
end function</p>
<p>'returns the number of relations that connecto both elements<br />
function sharedRelationsCount(elementA, elementB, model)<br />
    'start counting at zero<br />
    sharedRelationsCount = 0<br />
    'get the relationships for both objects<br />
    dim relationsA<br />
    set relationsA = model.toArrayList(elementA.relationships)<br />
    dim relationsB<br />
    set relationsB = model.toArrayList(elementB.relationships)<br />
    for each relationA in relationsA<br />
        for each relationB in relationsB<br />
            'if both relations have the same ID then we have a shared relation<br />
            if relationA.id = relationB.id then<br />
                sharedRelationsCount = sharedRelationsCount +1<br />
            end if<br />
        next<br />
    next<br />
end function</p>
<p>' Removes the related element from the auto update diagram if any.<br />
function removeRelatedElemenFromAutoDiagram(element,relatedElement, model)<br />
    'get the diagram owned by this element<br />
    dim ownedDiagrams<br />
    set ownedDiagrams = model.toArrayList(element.ownedDiagrams)<br />
    for each diagram In ownedDiagrams<br />
        dim diagram<br />
        set diagram = ownedDiagrams(i)<br />
        'check the name of the diagram<br />
        if Left(diagram.name,LEN(&quot;AUTO_&quot;)) = &quot;AUTO_&quot; then<br />
            'Removing elements from a diagram in unfortunately not implemented in the EAAddinFramework so we'll have to do it in the script<br />
            dim eaDiagram<br />
            set eaDiagram = diagram.wrappedDiagram<br />
            for i = 0 to eaDiagram.DiagramObjects.Count -1<br />
                dim diagramObject<br />
                set diagramObject = eaDiagram.DiagramObjects.GetAt(i)<br />
                if diagramObject.ElementID = relatedElement.id then<br />
                    'remove the diagramObject<br />
                    eaDiagram.DiagramObjects.Delete(i)<br />
                    'refresh the diagram after we changed it<br />
                    diagram.reFresh()<br />
                    'exit the loop we have delete the diagramobject<br />
                    exit for<br />
                end if<br />
            next<br />
        end if<br />
    next<br />
end function<br />

The next part deals with the diagram being opened, or shown. Since we are adding elements automatically, we want to use EA’s automatic layout to make the diagram presentable. The downside of this however is that EA also opens the diagram when doing a layout. So instead of doing an auto-layout directly after editing the diagram, we do one as soon as the user opens the diagram, or switches to an already opened diagram. We use the events EA_OnPostOpenDiagram and EA_OnTabChanged

<br />
'autodiagrams are automatically layouted when opened<br />
function EA_OnPostOpenDiagram(DiagramID)<br />
    dim model<br />
    'get the model<br />
    set model = getEAAddingFrameworkModel()<br />
    layoutAutoDiagram DiagramID, model<br />
end function</p>
<p>'autodiagrams are automatically layouted when we tab is switched to them<br />
function EA_OnTabChanged(TabName, DiagramID)<br />
    if  DiagramID &gt; 0 then<br />
        dim model<br />
        'get the model<br />
        set model = getEAAddingFrameworkModel()<br />
        layoutAutoDiagram DiagramID, model<br />
    end if<br />
end function</p>
<p>'layout the auto diagram<br />
function layoutAutoDiagram(diagramID, model)<br />
    dim diagram<br />
    set diagram = model.getDiagramByID(DiagramID)<br />
    'if the diagram is an auto diagram then we do an automatic layout<br />
    if Left(diagram.name,LEN(&quot;AUTO_&quot;)) = &quot;AUTO_&quot; then<br />
        'auto layout diagram<br />
        dim diagramGUIDXml<br />
        'The project interface needs GUID's in XML format, so we need to convert first.<br />
        diagramGUIDXml = Repository.GetProjectInterface().GUIDtoXML(diagram.wrappedDiagram.DiagramGUID)<br />
        'Then call the layout operation<br />
        Repository.GetProjectInterface().LayoutDiagramEx diagramGUIDXml, lsDiagramDefault, 4, 20 , 20, false<br />
    end if<br />
end function<br />

The only thing left now is to update the diagram in case a user changes a connector, and for instance drags one of the ends to another element. In order to do this we need to record the id’s of both ends of the connector once it is selected in EA_OnContextItemChanged and compare that to the id’s of this same connector after it has been changed in EA_OnNotifyContextItemModified

<br />
'keep a reference to the selected connector<br />
function EA_OnContextItemChanged(GUID, ot)<br />
    'we only do something when the context item is a connector<br />
    if ot = otConnector then<br />
        dim model<br />
        'get the model<br />
        set model = getEAAddingFrameworkModel()<br />
        'get the connector<br />
        dim contextConnector<br />
        set contextConnector = model.getRelationByGUID(GUID)<br />
        'MsgBox(TypeName(contextConnector))<br />
        contextConnectorID = contextConnector.id<br />
        oldClientID = contextConnector.WrappedConnector.ClientID<br />
        oldSupplierID = contextConnector.WrappedConnector.SupplierID<br />
    end if<br />
end function</p>
<p>'a connector has changed, we need to update the auto-diagrams<br />
function EA_OnNotifyContextItemModified(GUID, ot)<br />
    'we only do something when the context item is a connector<br />
    if ot = otConnector then<br />
        dim model<br />
        'get the model<br />
        set model = getEAAddingFrameworkModel()<br />
        'get the connector<br />
        dim changedConnector<br />
        set changedConnector = model.getRelationByGUID(GUID)<br />
        'check if we are talking about the same connector<br />
        if changedConnector.WrappedConnector.ConnectorID = contextConnectorID then<br />
            dim supplier<br />
            dim client<br />
            'check the client side<br />
            if changedConnector.WrappedConnector.ClientID &lt;&gt;  oldClientID then<br />
                'get supplier<br />
                set supplier = model.getElementWrapperByID(changedConnector.WrappedConnector.SupplierID)<br />
                'remove old client from supplier and vice versa<br />
                set client = model.getElementWrapperByID(oldClientID)<br />
                if not client is nothing then<br />
                    removeRelatedElemenFromAutoDiagram supplier,client, model<br />
                    removeRelatedElemenFromAutoDiagram client, supplier, model<br />
                end if<br />
                'add new client<br />
                set client = model.getElementWrapperByID(changedConnector.WrappedConnector.ClientID)<br />
                addRelatedElementoAutoDiagram supplier,client, model<br />
            end if<br />
            'check the supplier side<br />
            if changedConnector.WrappedConnector.SupplierID &lt;&gt; oldSupplierID then<br />
                'get client<br />
                set client = model.getElementWrapperByID(changedConnector.WrappedConnector.ClientID)<br />
                'remove old supplier from client and vice versa<br />
                set supplier = model.getElementWrapperByID(oldSupplierID)<br />
                if not supplier is nothing then<br />
                    removeRelatedElemenFromAutoDiagram client,supplier, model<br />
                    removeRelatedElemenFromAutoDiagram supplier,client, model<br />
                end if<br />
                'add new supplier<br />
                set supplier = model.getElementWrapperByID(changedConnector.WrappedConnector.SupplierID)<br />
                addRelatedElementoAutoDiagram client,supplier, model<br />
            end if<br />
        end if<br />
    end if<br />
end function<br />

6 thoughts on “Self-maintaining diagrams in Enterprise Architect with EA-Matic

  1. Interesting work, Geert. You have explained the way the add-in is implemented. But can you clarify a bit more its use? It is supposed that the architect write the first time the diagram, then someone changes the code related to that diagram, and the latter automatically update itself? Are different usage scenario possible?

    1. Andrea,

      Yes indeed, you make the diagram once, and then it keeps itself updated with the changes in the model (only the model, not the code).
      The example uses all relations of class, but you could easily adapt the script to only show a specific type of relation, the owned classes of a package, or something else.
      At one of my clients we use to have this type of diagrams, and it was a constant worry to keep them up to date.

      1. What is the difference with the standard behavior of EA? I mean, in some case, also EA update a diagram (for example, if in another diagram you add a relation between two elements present in the first one.
        By the way, I am trying to use the Automation Interface to produce something similar, but from a different perspective. For me the code plays its role in the lifecycle. Some updates come from a reverse engineering of the source code. In this case, I am working on automatic layout algorithms to layout the diagrams according to some levelization techniques. What I am discovering is that the API is less power than I thought. It is a pity. Prbably I would work directly in the repository database… not the way I would like to program.

        1. Andrea,
          The difference is that EA only adds relations to a diagram if both elements are present on the diagram. My script will add or remove elements in reaction to relations being added or deleted.
          Creating your own layout algorithm seems like a fun but complicated project. And indeed the API is just a very thin skin on top of the EA database. That is also the reason why I wrote the EA Addin Framework. It shields me from all the dirty database operations and provides a clean and functional interface to work with. I haven’t done much with diagrams yet, so that part hasn’t been fully developed yet.

      2. I didn’t know that you are the author of the EA Addin Framework. It is available also through the Sparx EA site, isn’t it? Is it free to use in our own projects? Is it also documented? I am not doing exactly what it offers, but it can be of some help to check its features. At the moment, I have developed a wrapper layer based on abstractions like ModelRepository, Graph, DependencyGraph, Node, and other useful abstractions such as Cycle, etc. useful for reasoning about the layout of diagrams. Of course, these Core abstractions decouples all the rest of my framework from the knowledge and the manipulation of EA objects. Nice to see different approaches.

        1. Yes I am. The EA Addin Framework is completely open source under the BSD license, which means you can do pretty much anything with it. It not available on Sparx’s website, but from GitHub. I’ve written an article about how to use it. How to use the Enterprise Architect Add-in Framework
          You might need to remove the references to LogicNP.CryptoLicensing from License before you can compile the project.
          There’s a fair amount of comments in the code, but for the moment there’s no other documentation then that.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.