Traversing a EA Model via Java

Traversing an Enterprise Architect model from Sparx Systems is relatively simple. This blog introduces the concept of model traversal via the Java API. For information on Java Set-Up see this blog entry.

The first step is to obtain the repository object as it provides the anchor from which all other operations are derived.

In the example below, a repository is held on the file system. The variable "repoLocation" holds the file system location of the "eap" file.

  rep = new Repository();  // get a repository
  rep.OpenFile(repoLocation);  //open the repository file

At the top of a model is a root node and it is typically where you want to start when traversing a model. From the root node you can then obtain a collection of packages.

At this point, it is easy to traverse the collection pulling in the packages via a loop and in this example printing out their names.

To see the complete example, go to GitHub.


  org.sparx.Package myModel = rep.GetModels().GetAt((short) 0);

  Collection<org.sparx.Package> views = myModel.GetPackages();

  for (short i = 0; i < views.GetCount(); i++) {
                org.sparx.Package currentView = views.GetAt(i);

                System.out.println("View: " + currentView.GetName());
   }

Comments

Popular posts from this blog

Creating Items in Sparx EA via Java

Module Management - Part 1 - getModule() Pattern