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

'gets a new instance of the EAAddinFramework and initializes it with the EA.Repository
function getEAAddingFrameworkModel()
    'Initialize the EAAddinFramework model
    dim model
    set model = CreateObject("TSF.UmlToolingFramework.Wrappers.EA.Model")
    model.initialize(Repository)
    set getEAAddingFrameworkModel = model
end function

It is then further used to get the element based on the GUID passed as parameter

set element = model.getElementWrapperByGUID(GUID)

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

 query = "select o.Object_ID from t_object o where o.classifier =" & element.id
 dim objects
 set objects = model.toArrayList(model.getElementWrappersByQuery(query))

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.

function EA_OnNotifyContextItemModified(GUID, ot)
    dim model
    set model = getEAAddingFrameworkModel()
    'only do something when the changed object is an element
    if ot = otElement then
        dim element
        set element = model.getElementWrapperByGUID(GUID)
        synchronizeObjectNames element, model
    end if
end function

function EA_OnPostNewElement(Info)
    'Get the model
    dim model
    set model = getEAAddingFrameworkModel()
    'get the elementID from Info
    dim elementID
    elementID = Info.Get("ElementID")
    'get the element being deleted
    dim element
    set element = model.getElementWrapperByID(elementID)
    synchronizeObjectNames element, model
end function

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

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

Leave a Reply