This page guides you on how to create models by using Astah SysML API.

section divider

Transaction process

Transaction operation is required before creating/editing/deleting models and presentations.

Create a diagram

Sample Code: Creating a Block Diagram

public IDiagram createBlockDefinitionDiagram(IModel project, String diagramName) 
        throws InvalidUsingException, ClassNotFoundException {
    ProjectAccessor projectAccessor =  AstahAPI.getAstahAPI().getProjectAccessor();
    BlockDefinitionDiagramEditor bdde = projectAccessor.getDiagramEditorFactory().getBlockDefinitionDiagramEditor();
    try {
        //To begin the transaction
        TransactionManager.beginTransaction();
        IDiagram iBlockDefinitionDiagram = bdde.createBlockDefinitionDiagram(project, diagramName);
        //To Close the transaction
        TransactionManager.endTransaction();
        return iBlockDefinitionDiagram;
    } catch (InvalidEditingException e) {
        e.printStackTrace();
        //To abort the transaction
        TransactionManager.abortTransaction();
    }
}

Delete a diagram

Sample Code A: Deleting diagram

public void deleteDiagram(IDiagram diagram) 
        throws ClassNotFoundException, InvalidUsingException {
    ProjectAccessor projectAccessor =  AstahAPI.getAstahAPI().getProjectAccessor();
    BlockDefinitionDiagramEditor bdde = projectAccessor.getDiagramEditorFactory().getBlockDefinitionDiagramEditor();
    try {
        //To begin the transaction
        TransactionManager.beginTransaction();
        bdde.setDiagram(diagram);
        bdde.deleteDiagram();
        //To Close the transaction
        TransactionManager.endTransaction();
    } catch (InvalidEditingException e) {
        e.printStackTrace();
        //To abort the transaction
        TransactionManager.abortTransaction();
    }
}
section divider

Create/Delete models by modelEditors

Models can be created by using methods in modelEditors.
For example, Models in Block Definition Diagram (e.g. Block) and Sequence Diagram (e.g. Lifeline) can be created by using methods in SysmlModelEditor (e.g. createBlock()) and SequenceDiagramEditor (e.g. createLifeline()).
Models can be deleted by the delete method in SysmlModelEditor.

DiagramModel Editor
Requirement DiagramSysmlModelEditor (e.g. createRequirement())
Mind MapMindmapEditor(e.g. createTopic())
Block Definition DiagramSysmlModelEditor (e.g. createBlock())
UseCase DiagramUseCaseModelEditor (e.g. createUseCase())
Sequence DiagramSequenceDiagramEditor (e.g. createLifeline())
Activity DiagramActivityDiagramEditor (e.g. createPartition())
Internal Block DiagramSysmlModelEditor (e.g. CreateFullPort())
Parametric DiagramSysmlModelEditor (e.g. createConstraintProperty())
section divider

Sample codes

Create a package and two blocks, and then add an association between blocks in Block Definition Diagram.

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.asml");
            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 block definition diagram
            SysmlModelEditor sysmlModelEditor = ModelEditorFactory.getSysmlModelEditor();

            // Create a package
            IPackage packageA = sysmlModelEditor.createPackage(project, "PackageA");

            // Create a block in the specified package
            IBlock blockA = sysmlModelEditor.createBlock(packageA, "BlockA");
            // Add a definition to the class
            blockA.setDefinition("Definition of ClassA");
            // Add an operation to the block
            sysmlModelEditor.createOperation(blockA, "operation0", "void");

            // Create a class in the specified package
            IBlock blockB = sysmlModelEditor.createBlock(packageA, "BlockB");

            // Add an association between classes
            sysmlModelEditor.createAssociation(blockA, blockB, "association name",
                    "blockA end", "blockB 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();
        }
    }
}
section divider

Edit models

Transaction operation is required when editing models.

Sample: Edit block name and its definition

  try {
      TransactionManager.beginTransaction();
      SysmlModelEditor bme = ModelEditorFactory.getSysmlModelEditor();
      // Create a block
      IBlock block0 = bme.createBlock(project, "block0");
      // Set name
      block0.setName("new block0 name");
      // Set definition
      block0.setDefinition("Definition of the block0");
      TransactionManager.endTransaction();
  } catch (InvalidEditingException e) {
      TransactionManager.abortTransaction();
  }
section divider

Create a diagram

Transaction operation is required when creating a diagram.

Sample Code: Creating a Block Diagram

public IDiagram createBlockDefinitionDiagram(IModel project, String diagramName) 
        throws InvalidUsingException, ClassNotFoundException {
    ProjectAccessor projectAccessor =  AstahAPI.getAstahAPI().getProjectAccessor();
    BlockDefinitionDiagramEditor bdde = projectAccessor.getDiagramEditorFactory().getBlockDefinitionDiagramEditor();
    try {
        TransactionManager.beginTransaction();
        IDiagram iBlockDefinitionDiagram = bdde.createBlockDefinitionDiagram(project, diagramName);
        TransactionManager.endTransaction();
        return iBlockDefinitionDiagram;
    } catch (InvalidEditingException e) {
        e.printStackTrace();
        TransactionManager.abortTransaction();
    }
}