# astah* API User Guide

## [How to create models]

Please refer to astah* API JavaDoc for details of Interfaces and methods.
Creating models is supported in astah* UML and professional.

#### [Transaction Operation]

Transaction operation is required when creating/deleting models in astah* API. Please see the Transaction operation.

#### [Create models in Class Diagram]

Models in Class Diagram (e.g. Class) can be created by using methods in BasicModelEditor (e.g. createClass()).

#### [Create models in UseCase Diagram]

Models in UseCase Diagram (e.g. UseCase) can be created by using methods in UseCaseModelEditor (e.g. createUseCase()).

#### [Create models in Statemachine Diagram]

Models in Statemachine Diagram (e.g. State) can be created by using methods in StateMachineDiagramEditor (e.g. createState()).

#### [Create models in Activity Diagram]

Models in Activity Diagram (e.g. Action) can be created by using methods in ActivityDiagramEditor (e.g. createAction()).

#### [Create models in Sequence Diagram]

Models in Sequence Diagram (e.g. Sequence) can be created by using methods in SequenceModelEditor (e.g. createLifeline()).

#### [Create models in Composite Structure Diagram]

Models in Composite Structure Diagram (e.g. Class) can be created by using methods in CompositeStructureModelEditor (e.g. createClass()).

#### [Create models in ER Diagram](professional only)

Models in ER Diagram (e.g. ER Entity) can be created by using methods in ERModelEditor (e.g. createEREntity()).

#### [Create models in Mind Map](UML, professional only)

Topic in Mind Map can be created by using methods in MindmapEditor (e.g. createTopic()).

#### [Create models in Requirement Diagram](professional only)

Models in Requirement Diagram (e.g. Requirement) can be created by using methods in BasicModelEditor (e.g. createRequirement()).

#### [Example of creating models in Class Diagram]

Create a package and two classes, and then add an association between classes.

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

Home