Tuesday, 08 April 2014 20:51

Enterprise Architect project browser scripts: sort elements by alias or by tagged value

Written by
Rate this item
(2 votes)

I recently imported a number of requirements in my Enterprise Architect project with the following details: title, reference (stored in the requirement's alias), and description. By default Sparx Enterprise Architect sorts requirements within a given package by the element's name in the alphabetical order. When a package contains various types of elements (e.g. classes, interfaces, use cases, etc.), each group of elements from the same type is sorted separately. The following illustrates a mix of requirements, classes, and interfaces created within a package, in their default sorting order:

sparx enterprise architect project browser

 

Going back to my requirements, I needed to sort them by the alias value e.g. REQ-TEST-001, REQ-TEST-002, REQ-TEST-003, etc. On the following screenshot, I illustrated on the left hand side the default sorting order in Enterprise Architect project browser, and on the right hand side the list of requirements in the desired alias order. 

sparx enterprise architect project browser sort by script

 

This article explains how to create and use an Enterprise Architect Project Browser script aimed at sorting elements from a selected package by the alias name. An additional script is provided at the end of the article to sort elements by a dedicated Tagged Value called "SortingOrder".

Enterprise Architect Project Browser scripts

Enterprise Architect lets you create project browser scripts. Once created in your project, these are available via a right click on a selected package from the browser, as illustrated below:

sparx systems enterprise architect project browser script

 

Sort by alias project browser script

To add the SortByAlias Project Browser script to your modelling project:

Step 1: open the scripting view using the Enterprise Architect menu Tools > Scripting.

Step 2: click on "New Project Browser Group" to store all user-defined scripts, e.g. Project Browser scripts.

Step 3: click on "New script > New VBScript" to define the "Sort By Alias" script, e.g. SortByAlias.

sparx enterprise architect project browser script sortbyalias

Step 4: open the script and copy/paste the following content (or download the VBScript text file here). 

  1. option explicit
  2. !INC Local Scripts.EAConstants-VBScript
  3. !INC EAScriptLib.VBScript-Logging
  4. ' Script Name: SortbyAlias 
  5. ' Author: Guillaume FINANCE, guillaume[at]umlchannel.com
  6. ' Purpose: Sort elements contained in the selected package from the Project Browser by the Alias name
  7. ' Date: 03/04/2014
  8. sub SortDictionary (objDict)
  9.    ' constants
  10.    Const dictKey  = 1
  11.    Const dictItem = 2
  12.    ' variables
  13.    Dim strDict()
  14.    Dim objKey
  15.    Dim strKey,strItem
  16.    Dim X,Y,Z
  17.    ' get the dictionary count
  18.    Z = objDict.Count 
  19.    ' sorting needs more than one item
  20.    If Z > 1 Then
  21.      ' create an array to store dictionary information
  22.      ReDim strDict(Z,2)
  23.      X = 0
  24.      ' populate the string array
  25.      For Each objKey In objDict
  26.          strDict(X,dictKey)  = CStr(objKey)
  27.          strDict(X,dictItem) = CStr(objDict(objKey))
  28.          X = X + 1
  29.      Next 
  30.      ' perform a a shell sort of the string array
  31.      For X = 0 To (Z - 2)
  32.        For Y = X To (Z - 1)
  33.          If StrComp(strDict(X,1),strDict(Y,1),vbTextCompare) > 0 Then
  34.              strKey  = strDict(X,dictKey)
  35.              strItem = strDict(X,dictItem)
  36.              strDict(X,dictKey)  = strDict(Y,dictKey)
  37.              strDict(X,dictItem) = strDict(Y,dictItem)
  38.              strDict(Y,dictKey)  = strKey
  39.              strDict(Y,dictItem) = strItem
  40.          End If
  41.        Next
  42.      Next
  43.      ' erase the contents of the dictionary object
  44.      objDict.RemoveAll
  45.      ' repopulate the dictionary with the sorted information
  46.      For X = 0 To (Z - 1)
  47.        objDict.Add strDict(X,dictKey), strDict(X,dictItem)
  48.      Next
  49. ' sort the package elements based on the new sorting order
  50. dim newOrder
  51. newOrder = 0
  52. dim theItem
  53. dim eaelement
  54.  for each objKey in objDict
  55. theItem = objDict.Item(objKey)
  56. Set eaelement = Repository.GetElementByGuid(theItem)
  57. 'change the position of the element in the package to the new sorting order value
  58. eaelement.TreePos = CLng(newOrder)
  59. eaelement.Update()
  60. newOrder = newOrder + 1
  61. next
  62.    end if
  63. end sub
  64.  
  65. sub sortElementsbyAlias (selectedPackage)
  66. LOGInfo("Processing selected package " & selectedPackage.Name)
  67. dim elements as EA.Collection
  68. dim i
  69. dim processedElements
  70. set processedElements = CreateObject( "Scripting.Dictionary" )
  71. set elements = selectedPackage.Elements
  72. for i = 0 to elements.Count - 1
  73. dim currentElement as EA.Element
  74. set currentElement = elements.GetAt( i )
  75. LOGInfo("Processing " & currentElement.Type & " no " & i & " with alias " & currentElement.Alias & "(" &  currentElement.ElementGUID & ")")
  76. processedElements.Add currentElement.Alias, currentElement.ElementGUID
  77. next
  78. LOGInfo("Sorting package elements")
  79. SortDictionary processedElements
  80. end sub
  81. '
  82. ' Project Browser Script main function
  83. '
  84. sub OnProjectBrowserScript()
  85. Repository.ClearOutput "Script"
  86. LOGInfo( "Starting SortbyAlias script" )
  87. LOGInfo( "==============================" )
  88. ' Get the type of element selected in the Project Browser
  89. dim treeSelectedType
  90. treeSelectedType = Repository.GetTreeSelectedItemType()
  91. select case treeSelectedType
  92. case otPackage
  93. ' Code for when a package is selected
  94. dim thePackage as EA.Package
  95. set thePackage = Repository.GetTreeSelectedObject()
  96. sortElementsbyAlias thePackage
  97. Repository.RefreshModelView (thePackage.PackageID)
  98. case else
  99. ' Error message
  100. Session.Prompt "This script does not support items of this type.", promptOK
  101. end select
  102. end sub
  103. OnProjectBrowserScript

Step 5: save the script (Ctrl-S).

Step 6: right click on the target package from the Project Browser where elements need to be sorted by the alias, and select Scripts > SortbyAlias

Result: the requirements have been sorted by the alias value as illustrated below.

sparx-enterprise-architect-project_browser_script_sortbyalias_running result

This sorting order is consistent with the requirements' alias values:

sparx enterprise architect project_browser_script_sortbyalias done

Sort by tagged value 'Sorting Order' project browser script

In some cases, the alias value may not be appropriate to sort your elements within the selected package. Based on the above SortByAlias script, I created the SortbyTaggedValue_SortingOrder script aimed at sorting elements using a tagged valued created for this purpose, SortingOrder.

To illustrate its use, I added the SortingOrder tagged value to each requirement:

sparx enterprise architect project_browser_script_sortby tagged value

To add the SortByTaggedValue Project Browser script to your project:

Step 1: open the scripting view using the Enterprise Architect menu Tools > Scripting.

Step 2: open the Project Browser scripts group.

Step 3: click on "New script > New VBScript" to define the "Sort By Tagged Value" script, e.g. SortbyTaggedValue_SortingOrder.

Step 4: open the script and copy/paste the following content from the script that can be downloaded here

Step 5: save the script (Ctrl-S).

Step 6: right click on the target package from the Project Browser where elements need to be sorted by the alias, and select Scripts > SortbyTaggedValue_SortingOrder.

sparx enterprise architect project_browser_script_sortby tagged value open

 

Result: the requirements have been sorted by the SortingOrder tagged value as illustrated below.

sparx enterprise architect project_browser_script_sortby tagged value result

 

This article illustrated how Sparx Enterprise Architect can be tailored to introduce new features via user defined scripts, e.g. to apply a new sorting rule on the elements from a selected package. It could be further improved e.g. to sort elements within the sub packages and so on.

 

Read 26072 times Last modified on Wednesday, 09 April 2014 04:55
Guillaume

Guillaume Finance

VISEO (Sparx EA Expert, OMG OCSMP Model User certified)
 
Modelling consultant and expert on Sparx Systems Enterprise Architect modelling tool and solution, I'm helping clients with the model-based approach using standards for a number of contexts including:
- Software analysis, design and architecture with UML.
- Systems Engineering and MBSE with SysML.
- Enterprise Architecture, IT landscape with UML or ArchiMate.
- Business processes with BPMN.
My other activities include:
- Defining and maintaining the model repository including requirements, analysis and design for software projects.
- Remote support and expertise on Sparx Enterprise Architect modelling.
- Running training sessions on UML or SysML with Sparx Systems Enterprise Architect.
- Installation and configuration of Prolaborate web solution for Sparx EA.
 
I publish articles and news about modelling languages and Enterprise Architect on my blog www.umlchannel.com, maintain eaUtils free addin: www.eautils.com, and I participate in the European EA User Group events www.eausergroup.com
Contact details: guillaume[at]umlchannel.com

www.umlchannel.com

5 comments

Login to post comments