public class APIForEditingModelsSample {
public static void main(String[] args) {
try {
System.out.println("Creating new project...");
// Create a project and get a root model
ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
prjAccessor.create("./SampleModel.asta");
IModel project = prjAccessor.getProject();
System.out.println("Creating new elements in the project...");
// Begin transaction when creating or editing models
TransactionManager.beginTransaction();
// Get model editor to create models in a class diagram
BasicModelEditor basicModelEditor = ModelEditorFactory.getBasicModelEditor();
// Create a package
IPackage packageA = basicModelEditor.createPackage(project, "PackageA");
// Create a class in the specified package
IClass classA = basicModelEditor.createClass(packageA, "ClassA");
// Add a definition to the class
classA.setDefinition("Definition of ClassA");
// Add an attribute to the class
basicModelEditor.createAttribute(classA, "attribute0", "int");
// Add an operation to the class
basicModelEditor.createOperation(classA, "operation0", "void");
// Create a class in the specified package
IClass classB = basicModelEditor.createClass(packageA, "ClassB");
// Add an association between classes
basicModelEditor.createAssociation(classA, classB, "association name",
"classA end", "classB end");
// End transaction
TransactionManager.endTransaction();
// Save project
prjAccessor.save();
// Close project
prjAccessor.close();
System.out.println("Finished");
} catch (LicenseNotFoundException e) {
e.printStackTrace();
} catch (ProjectNotFoundException e) {
e.printStackTrace();
} catch (ProjectLockedException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvalidEditingException e) {
// Abort transaction
TransactionManager.abortTransaction();
// Get an exception message
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
|