Perfect!
That did it, thank you.
I used Repository.GetElementByID(connector.ClientID).Stereotype
Complete code, in case anyone needs it:
!INC Local Scripts.EAConstants-JScript
/*
* Script Name: Given a package, will iterate through Business Capabilities, and tag them
* for realisation from Application Components
* Author: Aaron A
* Purpose: Prep a set of capabilities for heatmap
* Date:
*/
var tagName = "AssociatedApplications";
var cStereotype = "ArchiMate_Capability";
function main()
{
// Show the script output window
Repository.EnsureOutputVisible( "Script" );
debug( "In Script to Update Capability Tags " + tagName);
// Get the root package to work on
var contextObjectType = Repository.GetContextItemType();
if ( contextObjectType == otPackage )
{
// Get the context object as a package
var contextPackage = Repository.GetContextObject();
var rootElements = contextPackage.Elements;
var rootCount = rootElements.Count;
var iterator = 0;
while (rootCount > iterator)
{
var capabilityElement = rootElements.GetAt(iterator);
debug( "Root Capability: " + capabilityElement.Name);
updateTag(capabilityElement);
iterator = iterator + 1;
}
}
else
{
debug( "This script requires a package to be selected.\n" +
"Please select a package and try again.", promptOK );
}
debug( "END OF SCRIPT" );
}
/* For a given capability element, resets the tag, iterates through sub-cabilities
and assigns the return of tagConnections() to the tag. The assignment of the tag
value is cumulative based on the sub-elements
@param[in] capability (EA.Element) The primary capability that we will apply tag to, and
iterate from
*/
function updateTag(capability /* : EA.Element */)
{
debug( "In Capability: " + capability.Name);
if (!(capability.Stereotype == cStereotype))
{
// not a candidate for this
return;
}
var elementTag = 0;
var subCapabilities = capability.Elements;
var subCount = subCapabilities.Count;
//has children?
if (subCount > 0)
{
var iterator = 0;
while (subCount > iterator)
{
var subCapability = subCapabilities.GetAt(iterator);
debug( "Sub-Capability: " + subCapability.Name);
// adds connections sum
elementTag = elementTag + updateTag(subCapability);
debug( "Cumulative tag: " + elementTag);
iterator = iterator + 1;
}
}
// adds connections to this capability
elementTag = elementTag + tagConnections(capability);
SetElementTaggedValue(capability, tagName, elementTag, true)
debug( "Final tag: " + elementTag);
return elementTag;
}
/*
Goes through connections for the capability, and returns the number of connections to
an application component.
@param[in] capability (EA.Element) The primary capability that we will interrogate
*/
function tagConnections( capability /* BusinessCapability as EA.Element */ )
{
var connections = capability.Connectors;
var connectorsToApplications = 0;
var connectionsCount = connections.Count;
//has connections?
if (connectionsCount > 0)
{
var iterator = 0;
while (connectionsCount > iterator)
{
var connector = connections.GetAt(iterator);
debug( "Connector Stereotype: " + connector.Stereotype);
// if the capability has been realised (logical limiter, to application components)
if (connector.Stereotype == "ArchiMate_Realization" && Repository.GetElementByID(connector.ClientID).Stereotype =="ArchiMate_ApplicationComponent")
{
debug("Found connector: " + capability.Name);
debug("Client: " + Repository.GetElementByID(connector.ClientID).Stereotype);
connectorsToApplications = connectorsToApplications + 1;
}
iterator = iterator + 1;
}
}
debug( "Total connectors for: " + capability.Name + " = " + connectorsToApplications);
return connectorsToApplications;
}
main();
/**
* Sets the specified TaggedValue on the provided element. If the provided element does not already
* contain a TaggedValue with the specified name, a new TaggedValue is created with the requested
* name and value. If a TaggedValue already exists with the specified name then action to take is
* determined by the replaceExisting variable. If replaceExisting is set to true, the existing value
* is replaced with the specified value, if not, a new TaggedValue is created with the new value.
*
* @param[in] theElement (EA.Element) The element to set the TaggedValue value on
* @param[in] taggedValueName (String) The name of the TaggedValue to set
* @param[in] taggedValueValue (variant) The value of the TaggedValue to set
* @param[in] replaceExisting (boolean) If a TaggedValue of the same name already exists, specifies
* whether to replace it, or create a new TaggedValue.
*/
function SetElementTaggedValue( theElement /* : EA.Element */, taggedValueName /* : String */, taggedValueValue /* : variant */, replaceExisting /* : boolean */ ) /* : void */
{
if ( theElement != null && taggedValueName.length > 0 )
{
var taggedValue as EA.TaggedValue;
taggedValue = null;
// If replace existing was specified then attempt to get a tagged value from the element
// with the provided name
if ( replaceExisting )
taggedValue = theElement.TaggedValues.GetByName( taggedValueName );
if ( taggedValue == null )
{
taggedValue = theElement.TaggedValues.AddNew( taggedValueName, taggedValueValue );
}
else
{
taggedValue.Value = taggedValueValue;
}
taggedValue.Update();
}
}
function debug( debugString /* String */)
{
Session.Output( debugString );
}