Skip to content

Instantly share code, notes, and snippets.

@nfigay
Last active August 31, 2021 08:17
Show Gist options
  • Save nfigay/5118c48688df62df8aa5121a2d62fc67 to your computer and use it in GitHub Desktop.
Save nfigay/5118c48688df62df8aa5121a2d62fc67 to your computer and use it in GitHub Desktop.
This #jArchi script * This script creates a .puml file with all elements in the selected views - one file per view .A graph is produced, with Group and Grouping elements represented as nesting elements
console.log("PlantUML View export as Graph with Nesting Script (PlantUML-V2NG)");
load(__DIR__ + "lib/archimate.js");
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
let displayChildren = function f(object){
var did="#"+object.id;
$(did).children().filter('element').each(function(node) {
switch (node.type) {
case 'junction':
// At this time jArchi is not providing yet the access to the type of a Junction
// A temporary workaround for dealing with it, here again a dedicated property e.g. puml.junctionType which as value "And" or "Or"
index=ja_ArchiMateObjects.indexOf(node.type);
puml_junction=puml_ArchiMateObjects[index]; //I.e. "Junction_Or" - To be change with "Junction_And" if property.junctionType value is equal to "And"
fw.write(puml_junction);
break;
case 'location':
// Could be Business_Location or Other_Location for PlantUML-ArchiMate
// A way to take it into account is to define a dedicated property, e.g. puml.location which as value "Business" in order to use Business in place of Other (default)
fw.write("Other_Location");
break;
default:
index=ja_ArchiMateObjects.indexOf(node.type);
fw.write(puml_ArchiMateObjects[index]);
}
fw.write('('+node.id.replace(/-/g, "_")+',"'+node.name+'")');
if (node.type == "group" || node.type=="grouping"){
fw.write("{\n");
viewDisplayChildren(node);
fw.write("}\n")
} else {
fw.write("\n")
viewDisplayChildren(node);
}
}); 
}
var FileWriter=Java.type("java.io.FileWriter");
var view = selection.filter("archimate-diagram-model").first();
let viewDisplayChildren=displayChildren;
var fileName = window.promptSaveFile( { title: "Plant UML export", filterExtensions: [ "*.puml" ], fileName: view.name + ".puml" } );
if(fileName) {
// Write to file
var fw = new FileWriter(fileName);
fw.write("@startuml\n");
fw.write("!includeurl https://raw.githubusercontent.com/ebbypeter/Archimate-PlantUML/master/Archimate.puml\n");
fw.write("LAYOUT_AS_SKETCH()\n");
fw.write("LAYOUT_LEFT_RIGHT\n");
fw.write("'LAYOUT_TOP_DOWN\n");
viewDisplayChildren(view);
did="#"+view.id;
$(did).find().filter('relationship').each(function(relation) {
switch (relation.type) {
case "composition-relationship":
if (relation.source.concept.type=="grouping") {
// Don't draw if already reflected by nested groups or grouping on the view (archi like)
console.log ("relation.source:"+relation.source);
console.log ("relation.source.id:"+relation.source.id);
var grouping ="#"+relation.source.id;
console.log ("children:"+$(grouping).find());
console.log ("relation.source.concept.type:"+relation.source.concept.type);
console.log ("relation.target.concept.type:"+relation.target.concept.type);
var groupingChildren=[];
$(grouping).find().each(function(child){
groupingChildren.push(child.id);
});
console.log("target.id:" + relation.target.id);
console.log("children:" + groupingChildren);
var isAChild=groupingChildren.lastIndexOf(relation.target.id);
console.log ("the target being nested in the source is:"+ isAChild);
if (isAChild < 0){
console.log ("we create");
puml_Relation="Rel_"+relation.type.replace(/-relationship/g, "").capitalize()
+"("+relation.source.id.replace(/-/g, "_")+","+relation.target.id.replace(/-/g, "_")+',"'+relation.name+'")'+"\n";
fw.write (puml_Relation);
}
}
break;
default:
puml_Relation="Rel_"+relation.type.replace(/-relationship/g, "").capitalize()
+"("+relation.source.id.replace(/-/g, "_")+","+relation.target.id.replace(/-/g, "_")+',"'+relation.name+'")'+"\n";
fw.write (puml_Relation);
}
}); 
fw.write("@enduml\n");
fw.close(); // forgetting to close it results in a truncated file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment