Sample

This chapter explains the sample applications included in the Astah and also sample scripts.

Sample application

When you install Astah, the sample application would be saved in that <Installed folder>\api\sample.

Program name Function
simpleRead Get package and class information.
simpleEdit Create models of package, class, relation.
simpleDgmRead Get information on diagrams and presentations.
simpleDgmEdit Create packages, classes, related models and create class diagrams.
csvexporter Get model information (class, attribute, operation, definition, generalization, realization) and output it in CSV format.

Sample composition

Most sample applications consist of the following files.

File Path Details
*.java It is the java source code before compilation (there may be more than one)
compile.bat Run at compile time on windows
compile.sh Run at non-windows compilation
run.bat Used to run on windows
run.sh Use for non-windows execution

Compile and running

To run the sample applications, please adjust the batch file or shell command for your environment accordingly.
You should be able to compile these sample applications with Command Prompt or Java IDE.

Example of running csvexporter

C:\Users\xxx>cd C:\Program Files\astah-professional\api\sample\csvexporter

C:\Program Files\astah-professional\api\sample\csvexporter>compile.bat

C:\Program Files\astah-professional\api\sample\csvexporter>run.bat ../../../Sample.asta Sample.csv
Done
Press any key to continue . . .

C:\Program Files\astah-professional\api\sample\csvexporter>
  • You may need to run the Command Prompt as an administrator if you are on Windows.

Samples on Blog

We have more sample snippets on Astah Blog!

More sample code

We’ll post some sample source code here that may help you understand how Astah API works better.

Create a Class

import java.io.File;
import java.io.IOException;

import com.change_vision.jude.api.inf.AstahAPI;
import com.change_vision.jude.api.inf.editor.BasicModelEditor;
import com.change_vision.jude.api.inf.editor.IModelEditorFactory;
import com.change_vision.jude.api.inf.editor.ITransactionManager;
import com.change_vision.jude.api.inf.exception.BadTransactionException;
import com.change_vision.jude.api.inf.exception.InvalidEditingException;
import com.change_vision.jude.api.inf.exception.LicenseNotFoundException;
import com.change_vision.jude.api.inf.exception.NonCompatibleException;
import com.change_vision.jude.api.inf.exception.ProjectLockedException;
import com.change_vision.jude.api.inf.exception.ProjectNotFoundException;
import com.change_vision.jude.api.inf.model.IModel;
import com.change_vision.jude.api.inf.project.ProjectAccessor;

public class CreateClassSample {

    private static final String PROJECT_PATH = "test.asta";
    private static final String CLASS_NAME = "class0";

    public static void main(String[] args) {

        // get projectAccessor
        ProjectAccessor projectAccessor;
        try {
            projectAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
        } catch (ClassNotFoundException e) {
            System.out.println("An unexpected error occurred");
            return;
        }

        try {

            // set project to projectAccessor
            if (new File(PROJECT_PATH).exists()) {
                System.out.println("Open " + PROJECT_PATH);
                try {
                    projectAccessor.open(PROJECT_PATH);
                } catch (ClassNotFoundException | LicenseNotFoundException
                        | ProjectNotFoundException | NonCompatibleException | IOException
                        | ProjectLockedException e) {
                    System.out.println("can not open " + PROJECT_PATH);
                    System.out.println("details : " + e.getLocalizedMessage());
                    return;
                }
            } else {
                System.out.println("Create " + PROJECT_PATH);
                try {
                    projectAccessor.create(PROJECT_PATH);
                } catch (IOException e) {
                    System.out.println("can not create " + PROJECT_PATH);
                    return;
                }
                try {
                    projectAccessor.save();
                } catch (LicenseNotFoundException | ProjectLockedException
                        | ProjectNotFoundException | IOException e) {
                    System.out.println("Saving failed");
                    System.out.println("details : " + e.getLocalizedMessage());
                    return;
                }
            }

            // get model
            IModel parent;
            try {
                parent = projectAccessor.getProject();
            } catch (ProjectNotFoundException e) {
                System.out.println("Please create or open a project");
                return;
            }

            // edit
            if ("community".equals(projectAccessor.getAstahEdition())) {
                System.out.println("Can not edit project in community edition");
                return;
            }
            System.out.println("Create Class \"" + CLASS_NAME + "\"");
            try {
                createClass(projectAccessor, parent, CLASS_NAME);
            } catch (Exception e) {
                System.out.println("Can not create \"" + CLASS_NAME + "\"");
                System.out.println("details : " + e.getLocalizedMessage());
            }

            // save
            if (!projectAccessor.isProjectModified()) {
                return;
            }
            try {
                projectAccessor.save();
            } catch (LicenseNotFoundException | ProjectLockedException | ProjectNotFoundException
                    | IOException e) {
                System.out.println("Saving failed");
                System.out.println("details : " + e.getLocalizedMessage());
                return;
            }
            System.out.println("saved the project");

        } finally {

            // Close the project
            projectAccessor.close();
            System.out.println("closed the project");

        }

    }

    static private void createClass(ProjectAccessor projectAccessor, IModel parent, String name) {
        ITransactionManager transactionManager = projectAccessor.getTransactionManager();
        IModelEditorFactory modelEditorFactory = projectAccessor.getModelEditorFactory();
        try {
            BasicModelEditor basicModelEditor = modelEditorFactory.getBasicModelEditor();
            transactionManager.beginTransaction();
            basicModelEditor.createClass(parent, name);
            transactionManager.endTransaction();
        } catch (BadTransactionException | InvalidEditingException e) {
            transactionManager.abortTransaction();
            throw new RuntimeException(e.getLocalizedMessage(), e);
        }
    }

}