Synchronize object and classifier name 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 EA-Matic script keeps the names of objects synchronized with the name of their classifier.

When modelling with objects the name of the object is often left empty because it doesn’t have any significance at that point. One of the downsides of this practice is that in things like the traceability view, or in the links view the object shows up without a name, which of course doesn’t help much.

EA-Matic synchronize classifier

One of the solutions for this problem is to give the object the same name as its classifier. But that is a manual tedious process, and in case the classifier changes name you have the change the name of all the instances.

EA-Matic synchronize classifier_after

The script ensures that the name of the instances is always the same as the name of the classifier.

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 element based on the GUID passed as parameter

<br />
set element = model.getElementWrapperByGUID(GUID)<br />

The model element can be used to search elements based on an sql query

<br />
 query = &quot;select o.Object_ID from t_object o where o.classifier =&quot; &amp; element.id<br />
 dim objects<br />
 set objects = model.toArrayList(model.getElementWrappersByQuery(query))<br />

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

The code

Download the complete script: EA-Matic Sychronize object names

There are two events that we use. EA_OnNotifyContextItemModified to react to a name change of either the object or the classifier and EA_OnPostNewElement to change the name of a new object immediately after it has been created.

<br />
function EA_OnNotifyContextItemModified(GUID, ot)<br />
    dim model<br />
    set model = getEAAddingFrameworkModel()<br />
    'only do something when the changed object is an element<br />
    if ot = otElement then<br />
        dim element<br />
        set element = model.getElementWrapperByGUID(GUID)<br />
        synchronizeObjectNames element, model<br />
    end if<br />
end function</p>
<p>function EA_OnPostNewElement(Info)<br />
    'Get the model<br />
    dim model<br />
    set model = getEAAddingFrameworkModel()<br />
    'get the elementID from Info<br />
    dim elementID<br />
    elementID = Info.Get(&quot;ElementID&quot;)<br />
    'get the element being deleted<br />
    dim element<br />
    set element = model.getElementWrapperByID(elementID)<br />
    synchronizeObjectNames element, model<br />
end function<br />

These two events then use the function synchronizeObjectNames to make sure the name of the object is the same as the name of the classifier

<br />
function synchronizeObjectNames(element, model)<br />
    'first check if this is an object<br />
    if element.WrappedElement.Type = &quot;Object&quot; AND element.WrappedElement.ClassifierID &gt; 0 then<br />
        dim classifier<br />
        set classifier = model.getElementWrapperByID(element.WrappedElement.ClassifierID)<br />
        if not classifier is nothing AND classifier.name &lt;&gt; element.name then<br />
            element.name = classifier.name<br />
            element.save<br />
        end if<br />
    else<br />
        'get all objects having this element as their classifier<br />
        dim query<br />
        query = &quot;select o.Object_ID from t_object o where o.classifier =&quot; &amp; element.id<br />
        dim objects<br />
        set objects = model.toArrayList(model.getElementWrappersByQuery(query))<br />
        'loop objects<br />
        dim obj<br />
        for each obj in objects<br />
            'rename the object if the name is different from the classifiers name<br />
            if obj.name &lt;&gt; element.name then<br />
                obj.name = element.name<br />
                obj.save<br />
            end if<br />
        next<br />
    end if<br />
end function<br />

Leave a Reply

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