Use scripting language to access models in Astah
We have a free plug-in to enable you to access models within Astah using script language called “Astah Script Plug-in“.
Using this plug-in enables you to perform what Astah’s standard functionalities do not offer such as extracting model information as you want and edit them at all once..etc.
This page will give you sample scripts that you can start using today!
(These sample are available in Astah Professional & Community 7.0 and GSN 1.1 or later. If you are using older version, you need to edit – this page may help.)
Create Models/Diagrams
Get Model Information
- List up all the classes on a currently-open diagram
- List up all the Index information defined in ER model
- List up all the Package and Class names
- List up all the properties of selected model element
- Count classes on a currently-open diagram
- Export Class information in csv format
- List up all the messages without Operation in Sequence diagram
- List up TaggedValues
- List up TaggedValues of selected model elements on diagram
- List up TaggedValues of selected models in the structure tree
- List up models by its stereotype
Edit/Search Models
- Search class with specific keyword and add color and notes
- Add Setter/Getter to selected Attributes
- Add Stereotype to selected Models
- Rename Lifelines with related Actor’s Type name
- Delete all the notes from currently-open diagram
Mind Map
- List up all the topics on currently-open Mind Map
- Close/Open topics by specifying level
- Add index numbers to all the topics from the top
- Export Mind Map topics with hyperlink information in text format
Others
- Show edition of currently-running Astah
- Show a dialogue to use Java GUI
- Export diagrams to PlantUML and mermaid.js
GSN
- List up all the solutions in currently-opened diagram
- List up all the solutions in currently-opened diagram with Statements
- List up all the solutions with statements in project file
- List up all the solutions that don’t have Hyperlinks
- Generate a Mind Map from GSN
Create a new class diagram
var newDiagramName = 'New Class Diagram'; run(); function run() { if (!isSupportedAstah()) { print('This edition is not supported'); } with(new JavaImporter( com.change_vision.jude.api.inf.editor)) { //Edit the astah model TransactionManager.beginTransaction(); var editor = astah.getDiagramEditorFactory().getClassDiagramEditor(); var newDgm = editor.createClassDiagram(astah.getProject(), newDiagramName); TransactionManager.endTransaction(); print('New Class Diagram was created!'); } //Open the diagram var dgmViewManager = astah.getViewManager().getDiagramViewManager(); dgmViewManager.open(newDgm); } function isSupportedAstah() { var edition = astah.getAstahEdition(); print('Your edition is ' + edition); if (edition == 'professional' || edition == 'UML') { return true; } else { return false; } }
Create a new ER Entity with logical and physical names
var entities = [ 'Entity1LogicalName', 'Entity1PhysicalName', 'Entity2LogicalName', 'Entity2PhysicalName', 'Entity3LogicalName', 'Entity3PhysicalName' ]; run(); function run() { //Edit the astah model var editor = astah.getModelEditorFactory().getERModelEditor(); var erModel = getOrCreateERModel(editor); var erSchema = erModel.getSchemata()[0]; with(new JavaImporter( com.change_vision.jude.api.inf.editor)) { for (var i = 0; i 0) { return elements[0]; } //Create ERModel TransactionManager.beginTransaction(); erModel = editor.createERModel(astah.getProject(), 'ER Model'); TransactionManager.endTransaction(); } return erModel; }
List up all the classes on a currently-open diagram
//This script writes out all the classifiers in the current Astah project. with(new JavaImporter( com.change_vision.jude.api.inf.model)) { classes = astah.findElements(IClass.class); for(var i in classes) { print(classes[i].getName()); } }
List up all the Index information defined in ER model
with(new JavaImporter( com.change_vision.jude.api.inf.model)) { var entities = astah.findElements(IEREntity.class); for(var i in entities) { var entity = entities[i]; var indexList = entity.getERIndices(); if (indexList.length > 0) { print('ENTITY: ' + entity.getName()); for(var j in indexList) { var index = indexList[j]; //print index print(' INDEX[' + j + ']: ' + index.getName()); print(', ' + index.getKind()); if (index.isUnique()) print(', unique'); print(''); //print attributes var attributes = index.getERAttributes(); for(var k in attributes) { var attribute = attributes[k]; print(' ATTR[' + k + ']: ' + attribute.getName()); } } print(''); } } }
List up all the Package and Class names
run(); function run() { var project = astah.getProject(); printPackageInfo(project); } function printPackageInfo(iPackage) { print("Package name: " + iPackage.getName()); print("Package definition: " + iPackage.getDefinition()); with(new JavaImporter( com.change_vision.jude.api.inf.model)) { // Display packages and classes var namedElements = iPackage.getOwnedElements(); for (var i in namedElements) { var namedElement = namedElements[i]; if (namedElement instanceof IPackage) { printPackageInfo(namedElement); } else if (namedElement instanceof IClass) { printClassInfo(namedElement); } } } } function printClassInfo(iClass) { print("Class name: " + iClass.getName()); print("Class definition: " + iClass.getDefinition()); // Display all attributes var iAttributes = iClass.getAttributes(); for (var i in iAttributes) { printAttributeInfo(iAttributes[i]); } // Display all operations var iOperations = iClass.getOperations(); for (var i in iOperations) { printOperationInfo(iOperations[i]); } // Display inner class information var classes = iClass.getNestedClasses(); for (var i in classes) { printClassInfo(classes[i]); } } function printOperationInfo(iOperation) { print("Operation name: " + iOperation.getName()); print("Operation returnType: " + iOperation.getReturnTypeExpression()); print("Operation definition: " + iOperation.getDefinition()); } function printAttributeInfo(iAttribute) { print("Attribute name: " + iAttribute.getName()); print("Attribute type: " + iAttribute.getTypeExpression()); print("Attribute definition: " + iAttribute.getDefinition()); }
List up all the properties of selected model element
run(); function run() { var targets = getSelectedPresentationsInDiagramEditor(); if (targets.length === 0) { print('Please select an element in a diagram'); return; } for (var i in targets) { printAllProperties(targets[i]); } } function getSelectedPresentationsInDiagramEditor() { var diagramViewManager = astah.getViewManager().getDiagramViewManager(); return diagramViewManager.getSelectedPresentations(); } function printAllProperties(presentation) { with(new JavaImporter( java.util)) { var props = presentation.getProperties(); var keyList = new ArrayList(props.keySet()); Collections.sort(keyList); print('---------------------------'); for (var i = 0; i < keyList.size(); i++) { var key = keyList.get(i); var value = props.get(key); print(key + ': ' + value); } print('---------------------------'); } }
Count classes on a currently-open diagram
with (new JavaImporter( com.change_vision.jude.api.inf.model)) { classes = astah.findElements(IClass.class); print('Class # = ' + classes.length); }
Export Class information in csv format
//CSV format: // "Name of a class", "Name of the parent model", "Definition of the class" run(); function run() { exportClassesInCsv(); } function exportClassesInCsv() { with(new JavaImporter( com.change_vision.jude.api.inf.model)) { classes = astah.findElements(IClass.class); } var csvFile = selectCsvFile(); if (csvFile == null) { print('Canceled'); return; } print('Selected file = ' + csvFile.getAbsolutePath()); if (csvFile.exists()) { var msg = "Do you want to overwrite?"; var ret = JOptionPane.showConfirmDialog(scriptWindow, msg); if (ret != JOptionPane.YES_OPTION) { print('Canceled'); return; } } with(new JavaImporter( java.io)) { var writer = new BufferedWriter(new FileWriter(csvFile)); for(var i in classes) { var clazz = classes[i]; var rowData = []; rowData.push(clazz.getName()); rowData.push(clazz.getOwner().getName()); rowData.push(clazz.getDefinition()); writeRow(writer, rowData); } writer.close(); } } function selectCsvFile() { with(new JavaImporter( java.io, javax.swing)) { var chooser = new JFileChooser(); var selected = chooser.showSaveDialog(scriptWindow); if (selected == JFileChooser.APPROVE_OPTION) { var file = chooser.getSelectedFile(); if (file.getName().toLowerCase().endsWith('.csv')) { return file; } else { return new File(file.getAbsolutePath() + '.csv'); } } else { return null; } } } function writeRow(writer, rowData) { for (var i in rowData) { writeItem(writer, rowData[i]); if (i < rowData.length - 1) { writer.write(','); } } writer.newLine(); } function writeItem(writer, data) { writer.write('"'); writer.write(escapeDoubleQuotes(data)); writer.write('"'); } function escapeDoubleQuotes(data) { return data.replaceAll("\"", "\"\""); }
List up all the messages without Operation in Sequence diagram
var COLOR_PROPERTY_KEY = "font.color"; var newColor = '#0000ff'; run(); function run() { var targets = searchMessagesWithoutOperation(); if (targets.length === 0) { print('No target messages found'); return; } /* TransactionManager.beginTransaction(); for (var i in targets) { edit(targets[i]); } TransactionManager.endTransaction(); println('The color of the messages were changed.'); */ } function searchMessagesWithoutOperation() { with(new JavaImporter( com.change_vision.jude.api.inf.model)) { var targets = []; var messages = astah.findElements(IMessage.class); for (var i in messages) { var message = messages[i]; if (message.isReturnMessage() || message.isCreateMessage()) { continue; //ignore } var operation = message.getOperation(); if (operation === null) { targets.push(message); print('HIT: Message [' + message.getName() + '] in Sequence diagram [' + message.getPresentations()[0].getDiagram().getFullName('::') + ']'); } } } return targets; } function edit(target) { var presentations = target.getPresentations(); for (var j in presentations) { var messagePs = presentations[j]; // Change color of the message messagePs.setProperty(COLOR_PROPERTY_KEY, newColor); } }
List up TaggedValues
function printTaggedValueInfo(model) { if(model == null) { return } var taggedValues = model.getTaggedValues() for each(var taggedValue in taggedValues) { print(taggedValue.getKey() + ":" + taggedValue.getValue()) } }
List up TaggedValues of selected model elements on diagram
function printSelectedPresentationsTaggedValueInfo() { var viewManager = astah.getViewManager() var diagramViewManager = viewManager.getDiagramViewManager() var selectedPresentations = diagramViewManager.getSelectedPresentations() for each(var selectedPresentation in selectedPresentations) { printTaggedValueInfo(selectedPresentation.getModel()) } }
List up TaggedValues of selected models in the structure tree
function printSelectedEntitiesTaggedValueInfo() { var viewManager = astah.getViewManager() var projectViewManager = viewManager.getProjectViewManager() var selectedEntities = projectViewManager.getSelectedEntities() for each(var selectedEntity in selectedEntities) { printTaggedValueInfo(selectedEntity) } }
List up models by its stereotype
var IElement = Java.type('com.change_vision.jude.api.inf.model.IElement'); var Arrays = Java.type('java.util.Arrays'); // Search by stereotype var stereotype = "stereotype" for each(var element in astah.findElements(IElement.class)) { if(containsStereotype(element, stereotype)) { print(element.getName()) } } function containsStereotype(element, stereotype) { var stereotypes = Arrays.asList(element.getStereotypes()) return stereotypes.contains(stereotype) }
Search class with specific keyword and add color and notes
load("nashorn:mozilla_compat.js"); println = print; // Add colors to model elements if they had the same keyword in [Definition Field] importPackage(com.change_vision.jude.api.inf.model); importPackage(com.change_vision.jude.api.inf.editor); importPackage(com.change_vision.jude.api.inf.presentation); // Color to add var COLOR = "#BBCCFF"; // Keyword to look for in Definition field var IDENTICAL_STRING = "a"; run(); function run() { var currentDiagram = getCurrentDiagram(); if (currentDiagram == null) { println("error: Diagram is not opened.\n\nPlease open a diagram."); return; } var presentations = getPresentations(currentDiagram); var chengeColorPresentations = getIdenticalDefinitionPresentations(presentations, IDENTICAL_STRING); setColor(chengeColorPresentations, COLOR); } function getCurrentDiagram() { try{ var viewManager = astah.getViewManager(); var diagramViewManager = viewManager.getDiagramViewManager(); return diagramViewManager.getCurrentDiagram(); } catch(e) { return null; } } function getPresentations(currentDiagram) { try { return currentDiagram.getPresentations(); } catch (e) { return new IPresentation[0]; } } function getIdenticalDefinitionPresentations(presentations, identicalString) { var regularExpression = new RegExp("(.*)(" + identicalString + ")(.*)"); var identicalDefinitionPresentations = []; for(var i in presentations) { var presentation = presentations[i]; if (presentation == null) { continue; } var model = presentation.getModel(); if (model == null) { continue; } if (!(model instanceof INamedElement)) { continue; } var definition = model.getDefinition(); if (definition.match(regularExpression)) { identicalDefinitionPresentations.push(presentation); } } return identicalDefinitionPresentations; } function setColor(presentations, color) { try { TransactionManager.beginTransaction(); for(var i in presentations) { var presentation = presentations[i]; presentation.setProperty(PresentationPropertyConstants.Key.FILL_COLOR, color); } TransactionManager.endTransaction(); println("info: " + presentations.length + "model elements changed its colors."); } catch (e) { TransactionManager.abortTransaction(); println("error: Failed to color."); println(e); } }
Add Setter/Getter to selected Attributes
run(); function run() { var attributes = getSelectedAttributesInProjectView(); if (attributes.length === 0) { print('Please select attributes that you want to add setter/getter in StructureTree'); return; } with(new JavaImporter( com.change_vision.jude.api.inf.editor)) { TransactionManager.beginTransaction(); for (var i in attributes) { addSetterGetter(attributes[i]); } TransactionManager.endTransaction(); } } function getSelectedAttributesInProjectView() { with(new JavaImporter( com.change_vision.jude.api.inf.model)) { var attributes = []; var projectViewManager = astah.getViewManager().getProjectViewManager(); var selectedEntities = projectViewManager.getSelectedEntities(); for (var i in selectedEntities) { var entity = selectedEntities[i]; if (entity instanceof IAttribute) { attributes.push(entity); print('Target attribute: ' + entity.getName()); } } } return attributes; } function addSetterGetter(attribute) { var editor = astah.getModelEditorFactory().getBasicModelEditor(); var clazz = attribute.getOwner(); var attributeName = attribute.getName(); //setter var setter = editor.createOperation(clazz, getSetterName(attributeName), 'void'); editor.createParameter(setter, attribute.getName(), attribute.getType()); print('Added Setter Operation: ' + setter.getName()); //getter var getter = editor.createOperation(clazz, getGetterName(attributeName), attribute.getType()); print('Added Getter Operation: ' + getter.getName()); } function getSetterName(attributeName) { return 'set' + attributeName; } function getGetterName(attributeName) { return 'get' + attributeName; }
Add Stereotype to selected Models
var newStereotype = "New Stereotype"; run(); function run() { var targets = getSelectedClassesInDiagramEditor(); if (targets.length === 0) { print('Please select classes you want to add the stereotype to.'); return; } with(new JavaImporter( com.change_vision.jude.api.inf.editor)) { TransactionManager.beginTransaction(); for (var i in targets) { addStereotype(targets[i]); } TransactionManager.endTransaction(); } } function getSelectedClassesInDiagramEditor() { var targets = []; var diagramViewManager = astah.getViewManager().getDiagramViewManager(); var selectedPss = diagramViewManager.getSelectedPresentations(); for (var i in selectedPss) { var ps = selectedPss[i]; //println(ps.getType()); if (ps.getType() == 'Class' && !(ps.getModel() in targets)) { targets.push(ps.getModel()); print('HIT: ' + ps.getModel().getName()); } } return targets; } function addStereotype(target) { target.addStereotype(newStereotype); }
Rename Lifelines with related Actor’s Type name
run(); function run() { var TransactionManager = Java.type('com.change_vision.jude.api.inf.editor.TransactionManager'); var targets = searchTargetLifeline(); if (targets.length === 0) { print('No target lifeline found.'); return; } TransactionManager.beginTransaction(); for (var i in targets) { updateName(targets[i]); } TransactionManager.endTransaction(); print('The name of lifelines were updated.'); } function searchTargetLifeline() { var ILifeline = Java.type('com.change_vision.jude.api.inf.model.ILifeline'); var targets = []; var lifelines = astah.findElements(ILifeline.class); for (var i in lifelines) { var lifeline = lifelines[i]; var type = lifeline.getBase(); if (type != null && type.hasStereotype('actor')) { targets.push(lifeline); print('HIT: ' + lifeline.getName() + ': ' + type.getName()); } } return targets; } function updateName(lifeline) { var orgName = lifeline.getName(); var type = lifeline.getBase(); var typeName = type.getName(); var newName = typeName.charAt(0).toLowerCase() + typeName.slice(1); lifeline.setName(newName); print('Updated: ' + orgName + ' ---> ' + newName); }
Delete all the notes from currently-open diagram
run(); function run() { var notes = getAllNotesInDiagramEditor(); if (notes.length === 0) return; var TransactionManager = Java.type('com.change_vision.jude.api.inf.editor.TransactionManager'); TransactionManager.beginTransaction(); notes.forEach(function(note) { deleteElement(note); }); TransactionManager.endTransaction(); } function getAllNotesInDiagramEditor() { var diagramViewManager = astah.getViewManager().getDiagramViewManager(); var diagram = diagramViewManager.getCurrentDiagram(); if (diagram === null) { print('Open any diagram.'); return []; } var presentations = Java.from(diagram.getPresentations()); return presentations.filter(function(p) { return p.type.equals('Note'); }); } function deleteElement(presentation) { var modelEditor = astah.getModelEditorFactory().getBasicModelEditor(); modelEditor.delete(presentation.getModel()); }
List up all the topics on currently-open Mind Map
//This script writes out a list of text in the current mindmap. //The format is like a WiKi. var depth = 0; var INDENT_STR = ' '; //2 spaces var ITEM_MARKER_STR = '* '; run(); function run() { with(new JavaImporter( com.change_vision.jude.api.inf.model)) { var diagramViewManager = astah.getViewManager().getDiagramViewManager(); var diagram = diagramViewManager.getCurrentDiagram(); if (!(diagram instanceof IMindMapDiagram)) { print('Open a mindmap and run again.'); return; } var rootTopic = diagram.getRoot(); depth = 0; printTopics(rootTopic); } } function printTopics(topic) { var topicLabel = topic.getLabel().replaceAll('\n', ' '); print(getIndent(depth) + ITEM_MARKER_STR + topicLabel); var topics = topic.getChildren(); depth++; for (var i in topics) { if (topics[i].getType() == 'Topic') { //skip MMBoundary printTopics(topics[i]); } } depth--; } function getIndent(depth) { var indent = ''; for (var i = 0; i < depth; i++) { indent += INDENT_STR; } return indent; }
Close/Open topics by specifying level
var PresentationPropertyConstants = Java.type('com.change_vision.jude.api.inf.presentation.PresentationPropertyConstants'); var IMindMapDiagram = Java.type('com.change_vision.jude.api.inf.model.IMindMapDiagram'); var TransactionManager = Java.type('com.change_vision.jude.api.inf.editor.TransactionManager'); var level = 0; var doOpen = undefined; var TARGET_LEVEL = 1;//Specify the target level run(); function run() { var diagramViewManager = astah.getViewManager().getDiagramViewManager(); var diagram = diagramViewManager.getCurrentDiagram(); if (!(diagram instanceof IMindMapDiagram)) { print('Open a mindmap and run again.'); return; } var rootTopic = diagram.getRoot(); TransactionManager.beginTransaction(); reverseTopicVisibility(rootTopic); TransactionManager.endTransaction(); if (doOpen) { print('Opened subtopics of LEVEL ' + TARGET_LEVEL); } else { print('Closed subtopics of LEVEL ' + TARGET_LEVEL); } } function reverseTopicVisibility(topic) { if (level === TARGET_LEVEL) { if (doOpen == undefined) { if (topic.getProperty('sub_topic_visibility') == 'false') { doOpen = true; } else { doOpen = false; } } topic.setProperty('sub_topic_visibility', doOpen); } var topics = topic.getChildren(); level++; for (var i in topics) { if (topics[i].getType() =='Topic') { //skip MMBoundary reverseTopicVisibility(topics[i]); } } level--; }
Add index numbers to all the topics from the top
var PresentationPropertyConstants = Java.type('com.change_vision.jude.api.inf.presentation.PresentationPropertyConstants'); var TransactionManager = Java.type('com.change_vision.jude.api.inf.editor.TransactionManager'); var number = 'icon'; // please select text or icon. putSerialNumber(); function putSerialNumber() { var topics = getSelectedTopicsInDiagramEditor(); if (topics.length === 0) { print('Please select a topic'); return; } topics.forEach(function (topic) { if (number === 'text') { setNumberText(topic); } else if (number === 'icon') { setNumberIcon(topic); } }); } function getSelectedTopicsInDiagramEditor() { var diagramViewManager = astah.getViewManager().getDiagramViewManager(); var presentations = diagramViewManager.getSelectedPresentations(); return Java.from(presentations).filter(function(p) { return p.getType() === 'Topic'; }); } function setNumberText(topic) { var REGEXP_START_WITH_NUMBER = /^\d+\.\s/; var children = Java.from(topic.getChildren()); TransactionManager.beginTransaction(); children.forEach(function(child, index) { var label = child.getLabel(); if (label.search(REGEXP_START_WITH_NUMBER) !== -1) { label = label.replace(REGEXP_START_WITH_NUMBER, ''); } var no = index + 1; child.setLabel(no + '. ' + label); }); TransactionManager.endTransaction(); } function setNumberIcon(topic) { var REGEXP_NUMBER_ICON = /^number_\d+/; var children = Java.from(topic.getChildren()); TransactionManager.beginTransaction(); children.forEach(function(child, index) { var icons = child.getProperty('icons'); var iconsToKeep = icons.replace(REGEXP_NUMBER_ICON, ''); var no = index + 1; child.setProperty('icons', 'number_' + no + ',' + iconsToKeep); }); TransactionManager.endTransaction(); }
Export Mind Map topics with hyperlink information in text format
var depth = 0; var INDENT_STR = '\t'; run(); function run() { depth = 0; with(new JavaImporter( com.change_vision.jude.api.inf.model)) { var diagramViewManager = astah.getViewManager().getDiagramViewManager(); var diagram = diagramViewManager.getCurrentDiagram(); if (!(diagram instanceof IMindMapDiagram)) { print('Open a mindmap and run again.'); return; } printTopics(diagram.getRoot()); } } function printTopics(topic) { var topicLabel = topic.getLabel().replaceAll('\n', ' '); print(getIndent(depth) + topicLabel); printHyperlinks(topic) var topics = Java.from(topic.getChildren()); depth++; topics.forEach(function (topic) { if (topic.getType() == 'Topic') { printTopics(topic); } }); depth--; } function printHyperlinks(topic) { var links = Java.from(topic.getHyperlinks()); links.forEach(function (link) { if (link.isFile()) { printHyperlinkInfo('[file]', link); } else if(link.isURL()) { printHyperlinkInfo('[url]', link); } }); } function printHyperlinkInfo(title, link) { print(getIndent(depth) + title + 'name:' + link.getName() + INDENT_STR + 'path:' + link.getPath() + INDENT_STR + 'comment:' + link.getComment()); } function getIndent(depth) { var indent = ''; for (var i = 0; i < depth; i++) { indent += INDENT_STR; } return indent; }
Show edition of currently-running Astah
//This script shows how to check the edition of Astah. run(); function run() { if (!isSupportedAstah()) { print('This edition is not supported'); } //Use a special API here. //Ex: //TransactionManager.beginTransaction(); //Edit the astah model //TransactionManager.endTransaction(); } function isSupportedAstah() { with(new JavaImporter( com.change_vision.jude.api.inf.editor)) { var edition = astah.getAstahEdition(); print('Your edition is ' + edition); if (edition == 'professional' || edition == 'UML') { return true; } else { return false; } } }
Show a dialogue to use Java GUI
//This script shows a GUI by using AWT and Swing of Java. with(new JavaImporter( java.awt, java.awt.event, javax.swing)) { var frame = new JFrame("Frame title"); frame.setLayout(new FlowLayout()); var goButton = new JButton("Go!"); goButton.addActionListener(new ActionListener({ actionPerformed: function(event) { JOptionPane.showMessageDialog(frame, "Hello!"); } })); var closeButton = new JButton("Close"); closeButton.addActionListener(new ActionListener({ actionPerformed: function(event) { frame.setVisible(false); frame.dispose(); } })); frame.add(goButton); frame.add(closeButton); frame.setSize(150, 100); //We must not use JFrame.EXIT_ON_CLOSE frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //We can use 'astahWindow' instead of 'scriptWindow' here. frame.setLocationRelativeTo(scriptWindow); frame.setVisible(true); }
List up all the solutions in currently-open diagram
printSolutions() function printSolutions() { var project = projectAccessor.getProject() var models = project.getOwnedElements() for each(var model in models){ if(isSolution(model)){ print(model.getName()) } } } function isSolution(model) { if(model == null) { return false } var taggedValues = model.getTaggedValues() for each(var taggedValue in taggedValues) { if(taggedValue.getKey() === "jude.gsn.solution"){ if (taggedValue.getValue() === "true"){ return true } } } return false }
List up all the solutions in currently-open diagram with statements
var HashSet = Java.type('java.util.HashSet'); var Point2D = Java.type('java.awt.geom.Point2D'); run(); function run() { var solutionNodes = getSolutionsOnDiagram(); solutionNodes.forEach(function(node) { var solution = node.model; print(solution.name + ': ' + getStatement(solution)); }); } function getSolutionsOnDiagram() { var presentations = getAllPresentationsInDiagramEditor(); if (presentations.length === 0) return []; return presentations.filter(function(p) { return isSolution(p.model); }); } function getAllPresentationsInDiagramEditor() { var diagram = astah.viewManager.diagramViewManager.currentDiagram; if (diagram === null) { print('Please open a diagram.'); return []; } return Java.from(diagram.presentations); } function isSolution(model) { return getStereotype(model) == 'solution'; } function getStatement(model) { return model.getTaggedValue('jude.gsn.statement'); } function getStereotype(model) { if (model === null) return ''; var stereotypes = model.getStereotypes(); if (stereotypes !== null && stereotypes.length > 0) { return stereotypes[0]; } return ''; }
List up all the solutions with statements in a project file
var ModelFinder = Java.type('com.change_vision.jude.api.inf.project.ModelFinder'); var IClass = Java.type('com.change_vision.jude.api.inf.model.IClass'); run(); function run() { printSolutionsNames() } function printSolutionsNames() { Java.from(getSolutions()).forEach( function (solution) { print(solution.name + ': ' + getStatement(solution)); } ) } function getSolutions() { var solutionsFinder = new ModelFinder() { isTarget: function(namedElement) { return isSolution(namedElement) } } return astah.findElements(solutionsFinder) } function isSolution(model) { return model instanceof IClass && getStereotype(model) == 'solution'; } function getStereotype(model) { if (model === null) return ''; var stereotypes = model.getStereotypes(); if (stereotypes !== null && stereotypes.length > 0) { return stereotypes[0]; } return ''; } function getStatement(model) { return model.getTaggedValue('jude.gsn.statement') }
List up all the solutions do not have hyperlinks
var ModelFinder = Java.type('com.change_vision.jude.api.inf.project.ModelFinder'); var IClass = Java.type('com.change_vision.jude.api.inf.model.IClass'); run(); function run() { printSolutionsNames() } function printSolutionsNames() { Java.from(getSolutions()).forEach( function (solution) { if (solution.getHyperlinks().length > 0) { return; } print(solution.name + ': ' + getStatement(solution)); } ) } function getSolutions() { var solutionsFinder = new ModelFinder() { isTarget: function(namedElement) { return isSolution(namedElement) } } return astah.findElements(solutionsFinder) } function isSolution(model) { return model instanceof IClass && getStereotype(model) == 'solution'; } function getStereotype(model) { if (model === null) return ''; var stereotypes = model.getStereotypes(); if (stereotypes !== null && stereotypes.length > 0) { return stereotypes[0]; } return ''; } function getStatement(model) { return model.getTaggedValue('jude.gsn.statement') }
Generate Mind Map from GSN diagram
run(); var editor; function run() { var topGoalPresentations = getTopGoalOnDiagram(); if (topGoalPresentations.length == 1) { var topGoal = topGoalPresentations[0].model with(new JavaImporter(com.change_vision.jude.api.inf.editor)) { TransactionManager.beginTransaction(); editor = astah.getDiagramEditorFactory().getMindmapEditor(); var mmDiagram = editor.createMindmapDiagram(astah.getProject(), topGoal); TransactionManager.endTransaction(); print('New MindMap Diagram was created!'); } generateTopic(makeTopic(mmDiagram.getRoot(), getStatement(topGoal)), topGoal); } else { print("Please make a GSN with just one top-goal.") } } function makeTopic(parentTopic, topicName) { with(new JavaImporter(com.change_vision.jude.api.inf.editor)) { TransactionManager.beginTransaction(); var createdTopic = editor.createTopic(parentTopic, topicName); TransactionManager.endTransaction(); } return createdTopic; } function generateTopic(mmParent, goal) { getAllChildren(goal).forEach(function(childPresentation) { var child = childPresentation.model var idTopic = makeTopic(mmParent, child); var statementTopic = makeTopic(idTopic, getStatement(child)); generateTopic(statementTopic, child); }); } function getGoalsOnDiagram() { return getAllPresentationsInDiagramEditor().filter(function(p) { return isGoal(p.model); }); } function getTopGoalOnDiagram() { return getAllPresentationsInDiagramEditor().filter(function(p) { return isTopGoal(p.model); }); } function getAllChildren(parent) { return getAllPresentationsInDiagramEditor().filter(function(p) { return isChild(parent, p.model); }); } function isChild(parent, model) { return getAllArrows().filter(function(p) { return p.model.getClient() == parent.name && p.model.getSupplier() == model.name }).length > 0; } function getAllPresentationsInDiagramEditor() { var diagram = astah.viewManager.diagramViewManager.currentDiagram; if (diagram === null) { print('Please open a diagram.'); return []; } return Java.from(diagram.presentations); } function isTopGoal(model) { return isGoal(model) && getAllSupportedBy().filter(function(p) { return p.model.getSupplier() == model.name }).length == 0; } function getStereotype(model) { if (model === null) return ''; var stereotypes = model.getStereotypes(); if (stereotypes !== null && stereotypes.length > 0) { return stereotypes[0]; } else { return ''; } } function getAllSupportedBy() { return getAllPresentationsInDiagramEditor().filter(function(p) { return isSupportedBy(p.model); }); } function getAllArrows() { return getAllPresentationsInDiagramEditor().filter(function(p) { return isSupportedBy(p.model) || isInContextOf(p.model); }); } function isSupportedBy(model) { return getStereotype(model) == 'supportedBy'; } function isInContextOf(model) { return getStereotype(model) == 'inContextOf'; } function isGoal(model) { return getStereotype(model) == 'goal'; } function getStatement(model) { var statement = model.getTaggedValue('jude.gsn.statement'); return statement == '' ? '_' : statement; }
Let’s use these Scripts!
To run these scripts in Astah, you will need to install Script Plug-in first to Astah!