Sparx EA: Bulk Updates using PowerShell


Series Overview

I’ve been working a lot with Sparx Enterprise Architect and ArchiMate over the past few years, and although I am a big fan of formalized modelling using ArchiMate I’ve found myself frustrated with some of the limitations of the Sparx EA tool.

Over a small series of posts I’m intending to share a few of the tricks I’ve developed over time to address these frustrations.  These posts are fairly technical in nature, and address the usage of Sparx EA as an architecture tool, as opposed to the practice of defining the architecture itself.  The other posts in this series are below;

Saving Time with Scripted Updates

In my last post I walked through how I’ve been connecting to the Sparx EA repository using PowerShell and executing queries to retrieve model elements. In this post I’ll be building on that with an example of using the same technique to make bulk updates more efficient.

There are a number of ways I’ve found this useful as an efficiency tool in my day-to-day use of Sparx EA;

  • Quickly creating a set of new elements from a spreadsheet
  • Quick check that all elements of a specific stereotype have all mandatory tagged values and add any that are missing
  • Facilitating Import / Export Methods for element catalogs managed within the architecture model

For this example, I’m going to concentrate on adding a missing tagged value for a set of elements. I use this frequently, so as to ensure that all elements have the minimum set of attributes required for our organisation standards.

I should not here that while I do use the term ‘bulk updates’, this is still performing row-level operations – however since we can leverage SQL statements to quickly identify the set of elements to update this technique is much more efficient than iterating through elements in the repository.

Selecting Elements to Update

Using with the technique discussed in my previous post to use pre-select and filter a set of elements from the Sparx EA model, we can quickly identify all elements of a specific type where the tagged value is missing.

function addTagToAllElements ($tagName, $tagValue, $elementStereotype)
{

$sql = “SELECT o.Object_ID, o.Name, tv.Value
FROM t_object o
LEFT OUTER JOIN t_objectproperties tv on o.Object_ID = t.Object_ID and t.Property = ‘$tagName
WHERE o.Stereotype=’ArchiMate_$elementStereotype
AND tv.Object_ID is null
[xml]$response = $repository.SQLQuery($sql);
$nodes = $response.SelectNodes(“//*[local-name()=’Row’]”)
Write-Output “Found $($nodes.Count) rows to update”$loopCounter = 1
foreach ($node in $nodes)
{

Write-Output “Updating element $loopCounter”
[EA.Element]$element = $repository.GetElementByID($node.Object_ID
$taggedValues = $element.TaggedValues
addTagToElement -element $element -tagName $tagName -tagValue $tagValue
$loopCounter = $loopCounter + 1

}

}

Saving the Tagged Value

Using the object model to add a new tagged value is straight forward, as shown below.

function addTagToElement([EA.Element] $element, $tagName, $tagValue, $tagNotes){

EA.ElementClass] $e = $element
$newTag = $e.TaggedValues.AddNew($tagName, $tagValue)
$newTag.Notes = $tagNotes
$newTag.Update() | Out-Null
$e.Update() | Out-Null

}

Usage

With these functions defined, it becomes possible to build an ad-hoc script that can be run when necessary to make sure all the expected tagged values have been setup across the model.

addTagToAllElements -tagName “Category” -tagValue “” -elementStereotype “ApplicationComponent”
addTagToAllElements -tagName “Lifecycle Status” -tagValue “” -elementStereotype “ApplicationComponent”
addTagToAllElements -tagName “Vendor” -tagValue “” -elementStereotype “ApplicationComponent”
addTagToAllElements -tagName “Availability Pattern” -tagValue “” -elementStereotype “ApplicationComponent”
addTagToAllElements -tagName “Date Introduced” -tagValue “” -elementStereotype “ApplicationComponent”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s