Get Models and Presentations
You can learn how to get Models and Presentations by using Astah API. You can find a list of available Models and Presentations to get from here.
Get Models
Find models in a project
Call ProjectAccessor#findElements() to get models in a project. ProjectAccessor can get from AstahAPI#getProjectAccessor().
- findElements(Class elementKind)
- findElements(Class elementKind, String name)
- findElements(ModelFinder picker)
Sample with ModelFinder
Class Diagram Picker
public class ClassDiagramPicker implements ModelFinder {
public boolean isTarget(INamedElement namedElement) {
return namedElement instanceof IClassDiagram;
}
}
Entry Point Picker
public class EntryPointPicker implements ModelFinder {
public boolean isTarget(INamedElement namedElement) {
if (namedElement instanceof IPseudostate) {
return ((IPseudostate) namedElement).isEntryPointPseudostate();
}
return false;
}
}
Get a model selected in the structure tree
public IEntity[] getSelectedEntities(IViewManager viewManager) {
= viewManager.getProjectViewManager();
IProjectViewManager projectViewManager return projectViewManager.getSelectedEntities();
}
Get a model selected on the diagram
public List<IElement> getModelsOfSelectedPresentations(IViewManager viewManager) {
List<IElement> models = new ArrayList<>();
for (IPresentation selectedP : getSelectedPresentations(viewManager)) {
.add(selectedP.getModel());
models}
return models;
}
private IPresentation[] getSelectedPresentations(IViewManager viewManager) {
= viewManager.getDiagramViewManager();
IDiagramViewManager diagramViewManager return diagramViewManager.getSelectedPresentations();
}
Associations
Let’s get an association from Class0 in the sample diagram below.
This is the instance diagram. As you can see, there is no direct way to get associations from Class. So you need to get it through the AssociationEnd.
This is how Astah API works in this case.
You can get Attributes and AssociationEnds from Classes with IClass#getAttributes().
You can get Associations from Associations with IAttribute#getAssociation(). (It returns null if you call IAttribute#getAssociation() from attributes.)
public List<IAssociation> getAssociations(IClass clazz) {
List<IAssociation> associations = new ArrayList<>();
[] attributes = clazz.getAttributes();
IAttributefor (IAttribute iAttribute : attributes) {
= iAttribute.getAssociation();
IAssociation association if (association == null) {
continue;
}
.add(association);
associations}
return associations;
}
Dependencies
Let’s get a dependency from Class0.
This is the instance diagram.
This is how API works in this case. INamedElement is a parent class of IClass.
You can get the dependency from Class0 with INamedElement#getClientDependencies().
If you want to get it from Class 1, you can do so with INamedElement#getSupplierDependencies().
Template Classes
You can get an array of templateBindings (ITemplateBinding) between the target Class and its Template Class with IClass#getTemplateBindings().
Then you can get the Template Class with ITemplateBinding#getTemplate().
If you’d like to get a Template Parameter, you can get an array of the class’s template parameter (IClassifierTemplateParameter) with IClass#getTemplateParameters().
Packages - recursively
Subsystems and Models that inherit packages (IPackage) are also included in the Package.
public List getPackages(IPackage iPackage, List iPackages) {
[] iNamedElements = iPackage.getOwnedElements();
INamedElementfor (INamedElement iNamedElement : iNamedElements) {
if (iNamedElement instanceof IPackage) {
.add(iNamedElement);
iPackagesgetPackages((IPackage)iNamedElement, iPackages);
}
}
return iPackages;
}
Classes under Packages
Get all the model elements under package (IPackage) and pick classes.
public List getIClasses(IPackage iPackage) {
List iClasses = new ArrayList();
[] iNamedElements = iPackage.getOwnedElements();
INamedElementfor (INamedElement iNamedElement : iNamedElement) {
if (iNamedElement instanceof IClass) {
.add(iNamedElement);
iClasses}
}
return iClasses;
}
Namespace of Class
Get the name of class by calling getName() in case they are subclasses of INamedElement (i.g. IClass).
In addition, by calling getOwner() that obtains the owned model elements, the Full Path name from the project model can be obtained.
public String getFullName(IClass iClass) {
StringBuffer sb = new StringBuffer();
= iClass.getOwner();
IElement owner while (owner != null && owner instanceof INamedElement && owner.getOwner() != null) {
.insert(0, ((INamedElement) owner).getName() + "::");
sb= owner.getOwner();
owner }
.append(iClass.getName());
sbreturn sb.toString();
}
Activity Diagram Models
Sample Activity Diagram:
This is the instance diagram.
Please note that there is a Dimension as a superPartition of Partition0.
This is a structure of the API for the Activity Diagram models.
Sequence Diagram Models
Sample Sequence Diagram, its instance diagram and the structure of the API :
Statemachine Diagram Models
Sample Statemachine Diagram, its instance diagram and the structure of API.
Use IState#getSubvertexes() to get all the models inside the state0.
Use IState#getSubvertexes(int regionIndex) if you want to get models per regision.
TaggedValues
Use ITaggedValue to get TaggedValues.
public String getTaggedValueValue(IElement element, String taggedValueKey) {
for (ITaggedValue taggedValue : element.getTaggedValues()) {
if (taggedValueKey.equals(taggedValue.getKey())) {
return taggedValue.getValue();
}
}
return null;
}
Stereotypes
You can get Stereotypes as an array of Strings with IElement#getStereotypes().
Alias
Alias are stored as TaggedValues.
Key of Alias1 is jude.multi_language.alias1 and jude.multi_language.alias2 is for Alias2. TaggedValues or Alias from presentation
Get Diagrams and Presentations
Get Diagrams
public List getActivityDiagram(IPackage iPackage) {
List activityDiagrams = new ArrayList();
[] dgms = iPackage.getDiagrams();
IDiagramfor (IDiaram dgm : dgms) {
if (dgm instanceof IActivityDiagram
&& !((IActivityDiagram )dgm).isFlowChart()) {
.add(dgm);
activityDiagrams}
}
return activityDiagrams;
}
Get all presentations in the diagram
With IDiagram#getPresentations() you can get all the presentations in the diagram.
Please note that IElement#getPresentations() behaves differently.
public IPresentation[] getOwnedPresentation(IDiagram diagram) throws InvalidUsingException {
return diagram.getPresentations();
}
Get presentation from model
With IElement#getPresentations() you can get all the presentations that will be “IPresentation#getModel() == element”.
Since IDiagram#getPresentations() will get the presentation in the diagram, exception handling is done in the example below.
public IPresentation[] getPresentation(IElement element) throws InvalidUsingException {
if (element instanceof IDiagram) {
throw new IllegalArgumentException("element is IDiagram.");
}
return element.getPresentations();
}
Get presentation of selected model on the diagram
You can get presentation of a model that is currently selected on the diagram with IDiagramViewManager#getSelectedPresentations()
public IPresentation[] getSelectedPresentations(IViewManager viewManager) {
= viewManager.getDiagramViewManager();
IDiagramViewManager diagramViewManager return diagramViewManager.getSelectedPresentations();
}
Alias from presentation
You can get Alias by passing PresentationPropertyConstants#Key#ALIAS1 or PresentationPropertyConstants#Key#ALIAS2 to IPresentation#getProperty(java.lang.String)
public String getAlias1(IPresentation presentation) {
return presentation.getProperty(Key.ALIAS1);
}