Skip to content

Instantly share code, notes, and snippets.

@stefjoosten
Created December 30, 2020 14:12
Show Gist options
  • Save stefjoosten/0f274aaf84d7ac0cd3ebe6992b084aee to your computer and use it in GitHub Desktop.
Save stefjoosten/0f274aaf84d7ac0cd3ebe6992b084aee to your computer and use it in GitHub Desktop.
This jArchi-script ensures that all elements that share type and name are one and the same element. This will not affect any of your views. Executing this script is idempotent.
/* Merge all equal name-type elements.ajs
* Situation: Archi can make an element show up in different views. The relationships to and from that element in all these views are attached to the same element.
* Problem: If I accidentally make a copy of an element (e.g. by copying a view from one model to another), multiple elements may arise that have the same type and name.
* Such copies do not share relations, which violates an important benefit of Archi.
* Solution: This Archi-script ensures that all elements that share type and name are one and the same element. This will not affect any of your views. Executing this script is idempotent.
* Impact: after running this script
* - you will see that double occurrences in your elements list (in the "Models"-pane of Archi) are gone;
* - the information of all double occurrences has been accumulated in the remaining one without losing any information;
* - you will also see that the properties of the merged elements are all included in the remaining element;
* - your views remain exactly the same as before;
* - selecting any diagram object that is an instance of an Archi-element (characterized by its type and name) will show exactly the same analysis and property list, whatever view you are watching.
* (c) 2020 by Stef Joosten
*/
var debug = true;
debug ? console.show():true;
debug ? console.clear():true;
console.log("Making sure (within this model) that all elements that share type and name are one and the same element. This will not affect any of your views nor has any information been lost.");
// Every element that has duplicates is registered in the list 'registeredAsDuplicate'
// to avoid reporting them multiply.
var registeredAsDuplicate = $("#null");
$("element").each(function(elem0) {
// check if the element has not been registered already as duplicate.
if( ! registeredAsDuplicate.contains(elem0) ) {
// Find the duplicates of elem0, merge them and delete them.
$("element").each(function(elem) {
if (equiv(elem0,elem) && elem0.id!=elem.id) {
// console.log("diagram instances of "+elem.id+" will be replaced with "+elem0.id);
elem0.merge(elem);
registeredAsDuplicate.add(elem); // this registration prevents a Script Error: java.lang.NullPointerException
elem.delete();
}
});
}
});
// The equivalence of elements is abstracted as a function,
// so this will work for other equivalences as well.
function equiv(elem0,elem) {
return elem0.type === elem.type && elem0.name === elem.name
}
@stefjoosten
Copy link
Author

This script was built when working on issue archimatetool/archi#698.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment