Instantly share code, notes, and snippets.
Forked from timolaine/Insert Labels of Related Elements.ajs
Created
May 14, 2024 20:34
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save ovace/224bf19ce00914acd73c1a11dfb7fa51 to your computer and use it in GitHub Desktop.
Insert Labels of Related Elements in Archi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* WARNING The script modifies the view by adding note elements. | |
* This script takes the elements selected by the user and lists elements related to them | |
* under them as notes. This is useful when for example: | |
* - communicating with stakeholders who don't know the meaning of ArchiMate relations | |
* - the relations are complex | |
* - all the relations are not visible in any single views | |
* - the relations are not visible in any view | |
* - the elements are related through more than one relation, for example a business | |
* service might be related to an application component via a business process | |
* (the default path used in the script) | |
* The output can be modified by changing the path variable. | |
* The notes are color coded as a reading aid. The palette can be modified. | |
* | |
* Requires: jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/ | |
* Author: Timo Laine www.timoroso.com | |
*/ | |
/* | |
* This defines the path that is to be traversed from selected elements. | |
* The path can have any number of parts. Each part consists of: | |
* 1. Direction of the relation (0 = outgoing, 1 = incoming, 2 = either one) | |
* 2. Type of the relation concept | |
* 3. Type of the element concept at the other end of the relation concept. | |
*/ | |
path = [ | |
// follow incoming realization relations to business processes | |
[ 1, "realization-relationship", "business-process" ], | |
// follow incoming serving relations to application components | |
[ 1, "serving-relationship", "application-component" ] | |
]; | |
/* | |
path = [ | |
// follow incoming or outgoing flow relations to application components | |
[ 2, "flow-relationship", "application-component" ] | |
]; | |
*/ | |
/* | |
* This defines the color palette to be used. | |
* This could be developed further into something more sophisticated. | |
*/ | |
palette = [ | |
"#ff00ff", | |
"#ffb6c1", | |
"#1e90ff", | |
"#f0e68c", | |
"#87ceeb", | |
"#2f4f4f", | |
"#800000", | |
"#006400", | |
"#000080", | |
"#ff0000", | |
"#ffa500", | |
"#ffff00", | |
"#c71585", | |
"#00ff00", | |
"#00fa9a", | |
"#0000ff" | |
]; | |
/* | |
* A recursive function to traverse the defined path. The arguments are: | |
* element: the element from which to start following the path | |
* path: the path itself | |
*/ | |
function relatedElements(element, path) { | |
var elements = jArchi(); | |
var lowerElements = jArchi(); | |
var returnableElements = jArchi(); | |
var direction = -1; | |
var targetElement; | |
var relationType = path[0][1]; | |
var elementType = path[0][2]; | |
switch (path[0][0]) { | |
case 1: | |
relationTargets = $(element.concept).inRels(relationType).sourceEnds(); | |
break; | |
case 0: | |
relationTargets = $(element.concept).outRels(relationType).targetEnds(); | |
break; | |
default: | |
case 2: | |
relationTargets = $(element.concept).inRels(relationType).sourceEnds() | |
.add($(element.concept).outRels(relationType).targetEnds()); | |
} | |
relationTargets.each(function(targetElement) { | |
if ((targetElement.type == elementType) && | |
(!elements.is("#" + targetElement.id))) { | |
elements.add(targetElement); | |
} | |
}); | |
if (path.length > 1) { | |
elements.each(function(el) { | |
traversedElements = relatedElements(el, path.slice(1, path.length)); | |
traversedElements.each(function(elementToAdd) { | |
if (!lowerElements.is("#" + elementToAdd.id)) { | |
lowerElements.add(elementToAdd); | |
} | |
}); | |
}); | |
returnableElements = lowerElements; | |
} else { | |
returnableElements = elements; | |
} | |
return returnableElements; | |
} | |
/* | |
* Main code | |
*/ | |
var selected = $(selection); | |
elementsAndTheirRelatedElements = {}; | |
allRelatedElements = jArchi(); | |
for (el = 0; el < selected.length; el++) { | |
element = selected[el]; | |
var elementsRelated = relatedElements(element, path); | |
elementsAndTheirRelatedElements[element] = elementsRelated; | |
elementsRelated.each(function(elementToAdd) { | |
if (!allRelatedElements.is("#" + elementToAdd.id)) { | |
allRelatedElements.add(elementToAdd); | |
} | |
}); | |
} | |
selected.each(function(selectedElement) { | |
var element = selectedElement; | |
labeltext = ""; | |
relatedElements = elementsAndTheirRelatedElements[element]; | |
for (relEl = 0; relEl < relatedElements.length; relEl++) { | |
var note = element.view.createObject("note", | |
element.bounds.x + 10, | |
element.bounds.y + element.bounds.height + relEl * 20, | |
element.bounds.width - 20, | |
20); | |
relatedElement = relatedElements[relEl]; | |
labeltext = relatedElement.name; | |
note.setText(labeltext); | |
note.borderType = BORDER.RECTANGLE; | |
note.fontSize = 9; | |
note.fillColor=palette[allRelatedElements.indexOf(relatedElement)]; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment