Examples of Astah API

How to get a Model of project accessor

All model information in an Astah Project file is stored under the model of project accessor (Project model) in a tree structure. First, get the ProjectAccessor object and open Astah project file, then get the Project Model
(IModel Class object).

ClassDefinitionBuilder.java
//Open project file and get project model
 ProjectAccessor prjAccessor = ProjectAccessorFactory.getProjectAccessor();
 prjAccessor.open(inputFile);
 IModel iModel = prjAccessor.getProject();

How to get packages under Package recursively

It is defined that Package we call here would include Subsystems and Models that inherit iPackage. You can get all model elements directly under the package (iPackage) in an array by calling getOwnedElements() and get only packages abstracted from them. By doing so, you can get all packages recursively.

ClassDefinitionBuilder.java
/**
 * Get packages under Package recursively.
 *
 * @param iPackage
 * Selected Package
 * @param iPackages
 * The list of all stored packages
 * @return The list of all stored packages
 */
 private List getPackages(IPackage iPackage, List iPackages) {
     INamedElement[] iNamedElements = iPackage.getOwnedElements();
     for (int i = 0; i < iNamedElements.length; i++) {
         INamedElement iNamedElement = iNamedElements[i];
         if (iNamedElement instanceof IPackage) {
             iPackages.add(iNamedElement);
             getPackages((IPackage)iNamedElement, iPackages);
         }
     }
     return iPackages;
 }

How to get Full Path Name of class

You can 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, you can get the Full Path name from the project model.

ClassDefinitionBuilder.java
/**
* Get the class name with Full Path
*
* @param iClass
* Class
* @return Class name (Full Path)
*/
private 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();
}