UML & SysML modelling languages

Expertise and blog articles on UML, SysML, and Enterprise Architect modelling tool

version francaiseTwitterVideos UMLChannel SparxSystems EA YouTubeLinkedIn
Wednesday, 09 April 2014 00:00

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

Written by
Rate this item
(1 Vote)

Important:

  • This script has been updated on the 1/09/2014 to resolve the case where elements in the package have an empty alias.
  • Features from this script have been enhanced and are now maintained in eaUtils Sparx add-in; please click here for more information.

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