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().

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) {
    IProjectViewManager projectViewManager = viewManager.getProjectViewManager();
    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)) {
        models.add(selectedP.getModel());
    }
    return models;
}

private IPresentation[] getSelectedPresentations(IViewManager viewManager) {
    IDiagramViewManager diagramViewManager = viewManager.getDiagramViewManager();
    return diagramViewManager.getSelectedPresentations();
}

Associations

Let’s get an association from Class0 in the sample diagram below.

01_view_diagram_editor

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.

02_instance_diagram

This is how Astah API works in this case.

03_diagram_showing_class_structure

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<>();
    IAttribute[] attributes = clazz.getAttributes();
    for (IAttribute iAttribute : attributes) {
        IAssociation association = iAttribute.getAssociation();
        if (association == null) {
            continue;
        }
        associations.add(association);
    }
    return associations;
}

Dependencies

Let’s get a dependency from Class0.

01_view_diagram_editor

This is the instance diagram.

02_instance_diagram

This is how API works in this case. INamedElement is a parent class of IClass.

03_diagram_showing_class_structure

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) {
     INamedElement[] iNamedElements = iPackage.getOwnedElements();
     for (INamedElement iNamedElement : iNamedElements) {
        if (iNamedElement instanceof IPackage) {
            iPackages.add(iNamedElement);
            getPackages((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();
    INamedElement[] iNamedElements = iPackage.getOwnedElements();
    for (INamedElement iNamedElement : iNamedElement) {
       if (iNamedElement instanceof IClass) {
           iClasses.add(iNamedElement);
        }
    }
    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();
    IElement owner = iClass.getOwner();
    while (owner != null && owner instanceof INamedElement && owner.getOwner() != null) {
       sb.insert(0, ((INamedElement) owner).getName() + "::");
       owner = owner.getOwner();
    }
    sb.append(iClass.getName());
    return sb.toString();
}

Activity Diagram Models

Sample Activity Diagram:

01_view_diagram_editor

This is the instance diagram.
Please note that there is a Dimension as a superPartition of Partition0.

02_instance_diagram

This is a structure of the API for the Activity Diagram models.

03_diagram_showing_class_structure

Sequence Diagram Models

Sample Sequence Diagram, its instance diagram and the structure of the API :

01_view_diagram_editor

02_instance_diagram

03_diagram_showing_class_structure

Statemachine Diagram Models

Sample Statemachine Diagram, its instance diagram and the structure of API.

01_view_diagram_editor

02_instance_diagram

03_diagram_showing_class_structure

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();

    IDiagram[] dgms = iPackage.getDiagrams();
    for (IDiaram dgm : dgms) {
       if (dgm instanceof IActivityDiagram
           && !((IActivityDiagram )dgm).isFlowChart()) {
           activityDiagrams.add(dgm);
       }
    }
    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) {
    IDiagramViewManager diagramViewManager = viewManager.getDiagramViewManager();
    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);
}