SysML

Get SysML facet model

All SysML models and presentations exist in the SysML facet Model which can be accessed by calling getFacet(String symbolicName)of ProjectAccessor.

public IFacet getProjectSysMLFacet(ProjectAccessor projectAccessor) throws ProjectNotFoundException {
    return projectAccessor.getFacet(ISysmlFacet.FACET_SYMBOLIC_NAME);
}

Get Models and Presentations

Get Models

Associations

Let’s get an association from Block0 in the sample block definition diagram below.

common_view_diagram_editor

This is the instance diagram. As you can see, there is no direct way to get associations from Block. So you need to get it through the AssociationEnd.

common_instance_diagram

This is how Astah API works in this case.

common_diagram_showing_class_structure

You can get Attributes and AssociationEnds from Blocks with IBlock#getReferences().
You can get Associations from Associations with IAttribute#getAssociation(). (It returns null if you call IAttribute#getAssociation() from attributes.)

public List<IAssociation> getAssociations(IBlock block) {
    List<IAssociation> associations = new ArrayList<>();
    IAttribute[] attributes = block.getReferences();
    for (IAttribute attribute : attributes) {
        IAssociation association = attribute.getAssociation();
        if (association == null) {
            continue;
        }
        associations.add(association);
    }
    return associations;
}

Dependencies

Let’s get a dependency from Block0.

common_view_diagram_editor

This is the instance diagram.

common_instance_diagram

This is how API works in this case.
INamedElement is parent of IBlock.

common_diagram_showing_class_structure

You can get the dependency from Block0 with INamedElement#getClientDependencies().
If you want to get it from Block1, you can do so with INamedElement#getSupplierDependencies().

Packages - recursively

Packages and Models that inherit packages (IPackage) are also included in the Package.

public List<IPackage> getPackages(IPackage pkg, List<IPackage> packages) {
    INamedElement[] namedElements = pkg.getOwnedElements();
    for (INamedElement namedElement : namedElements) {
       if (namedElement instanceof IPackage) {
           packages.add((IPackage)namedElement);
           getPackages((IPackage)namedElement, packages);
       }
     }
     return packages;
}

Blocks under Packages

Get all the model elements under package (IPackage) and pick classes.

public List<IBlock> getIBlocks(IPackage pkg) {
    List<IBlock> blocks = new ArrayList<>();
    INamedElement[] namedElements = pkg.getOwnedElements();
    for (INamedElement namedElement : namedElements) {
       if (namedElement instanceof IBlock) {
           blocks.add((IBlock)namedElement);
       }
    }
    return blocks;
}

Namespace of Block

Get the name of Block by calling getName() in case they are subclasses of INamedElement (i.g. IBlock).

public String getFullName(IBlock block) {
    return block.getFullName("::");
}

In addition, by calling getOwner() that obtains the owned model elements, the Full Path name from the project model can be obtained.

public String getFullName(IBlock block) {
    IElement owner = block.getOwner();
    StringJoiner sj = new StringJoiner("::");
    while (owner != null && owner instanceof INamedElement && owner.getOwner() != null) {
        sj.add(((INamedElement) owner).getName());
        owner = owner.getOwner();
    }
    sj.add(block.getName());
    return sj.toString();
}

Activity Diagram Models

Sample Activity Diagram:

common_view_diagram_editor

This is the instance diagram.
Please note that there is a Dimension as a superPartition of Partition0.

common_instance_diagram

This is a structure of the API for the Activity Diagram models.

common_diagram_showing_class_structure

Sequence Diagram Models

Sample Sequence Diagram:

common_view_diagram_editor

Instance diagram of the sample Sequence Diagram:

common_instance_diagram

The structure of the API:

common_diagram_showing_class_structure

Statemachine Diagram Models

Sample State Machine Diagram, its instance diagram and the structure of API.

common_view_diagram_editor

common_instance_diagram

common_diagram_showing_class_structure

Use IState#getSubvertexes() to get all the models inside the state0.
Use IState#getSubvertexes(int regionIndex) if you want to get models per region.

Get Diagrams and Presentations

Get Internal Block Diagram

public List<IInternalBlockDiagram> getInternalBlockDiagrams(IBlock block) {
    List<IInternalBlockDiagram> internalBlockDiagrams = new ArrayList<>();
    IDiagram[] diagrams = block.getDiagrams();
    for (IDiagram diagram : diagrams) {
        if (diagram instanceof IInternalBlockDiagram) {
            internalBlockDiagrams.add((IInternalBlockDiagram) diagram);
        }
    }
    return internalBlockDiagrams;
}

Get part property presentations on the Internal Block Diagram

public List<INodePresentation> getPartProperty(IDiagram diagram) {
    List<INodePresentation> partPropertyPresentations = new ArrayList<>();
    if (diagram instanceof IInternalBlockDiagram) {
        try {
             INodePresentation[] childrenPresentations = ((IInternalBlockDiagram) diagram)
                         .getStructuredBlockPresentation().getChildren();
             for (INodePresentation nodePresentation : childrenPresentations) {
                  IElement model = nodePresentation.getModel();
                  if (model instanceof IAttribute && ((IAttribute) model).isComposite()) {
                      partPropertyPresentations.add(nodePresentation);
                  }
             }
        } catch (InvalidUsingException e) {
            e.printStackTrace();
        }
    }
    return partPropertyPresentations;
}

Edit Models and Presentations

In order to edit models or presentations using API, you need the transaction process. Also an exception will happen if:

  • The project is not registered in the project accessor or
  • The edition for the API is not correct or
  • target models/presentations you are trying to edit are non-editable

Edit model

Model Editor

The SysmlModelEditor is to used when you edit SysML model with API.
For example, A requirement can be created from SysmlModelEditor.
If you edit a model that can have multiple presentations, you can create/edit from each Model Editor.

Editting Model Viewpoint

Model Editors Model elements you can create in the Model Editor
SysMLModelEditor Block,InterfaceBlock,ConstraintBlock,ConstraintParameter,ValueType,Unit,QuantityKind,Properties like FlowProperty,ItemFlow,Port,Requirement,Testcase,Part,BindingConnector,Association

Create a FlowProperty in a block

// Transaction processing is required
public IFlowProperty createFlowProperty(ProjectAccessor projectAccessor)
        throws InvalidEditingException, ProjectNotFoundException {
    IModelEditorFactory modelEditorFactory = projectAccessor.getModelEditorFactory();
    SysmlModelEditor sysmlModelEditor = modelEditorFactory.getSysmlModelEditor();
    IModel projectModel = projectAccessor.getProject();
    IBlock parent = sysmlModelEditor.createBlock(projectModel, "parentBlock");
    IBlock type = sysmlModelEditor.createBlock(projectModel, "typeBlock");
    return sysmlModelEditor.createFlowProperty(parent, "Flow Property", type);
}

Create a requirement

// Transaction processing is required
public IRequirement createRequirement(ProjectAccessor projectAccessor)
        throws InvalidEditingException, ProjectNotFoundException {
    IModelEditorFactory modelEditorFactory = projectAccessor.getModelEditorFactory();
    SysmlModelEditor sysmlModelEditor = modelEditorFactory.getSysmlModelEditor();
    IModel model = projectAccessor.getProject();
    IRequirement requirement = sysmlModelEditor.createRequirement(model, "requirement");
    requirement.setRequirementID("id");
    return requirement;
}

Edit Presentations

Diagram Editor

You will use DiagramEditor to create, edit and delete presentations.

Diagram Editor Editable diagrams
BlockDefinitionDiagramEditor Block Definition Diagram
InternalBlockDiagramEditor Interanl Block Diagram
ToDo Parametric Diagram
UseCaseDiagramEditor UseCase Diagram
StateMachineDiagramEditor StateMachine Diagram
ActivityDiagramEditor Activity Diagram
SequenceDiagramEditor Sequence Diagram
RequirementDiagramEditor Requirement Diagram
MindmapEditor Mindmap

Create a Requirement Diagram

// Transaction processing is required
public IRequirementDiagram createRequirementDiagram(RequirementDiagramEditor editor, INamedElement owner,
        String name) throws InvalidEditingException {
    return editor.createRequirementDiagram(owner, name);
}

Create a Requirement Presentation

// Transaction processing is required
public INodePresentation createRequirementPresentation(RequirementDiagramEditor editor, IRequirement model,
        Point2D location) throws InvalidEditingException {
    return createNodePresentation(editor, model, location);
}

// Transaction processing is required
public INodePresentation createNodePresentation(BlockDefinitionDiagramEditor editor, IElement model,
        Point2D location) throws InvalidEditingException {
    return editor.createNodePresentation(model, location);
}

Create an Action in Activity Diagram

Here’s an example to create an Action in Activity Diagram. When you create a presentation that requires a Model as 1:1, the model would be automatically created as you create a presentation.

// Transaction processing is required
public INodePresentation createActionPresentation(ActivityDiagramEditor editor, String name,
        Point2D location) throws InvalidEditingException {
    return editor.createAction(name, location);
}

Supported model / Presentation list

A part of the class structure of Astah API is different from the one of SysML metamodel.
Astah API has a simplified structure. Some of abstract model elements in the SysML metamodel inheritance structure are eliminated because they would not be instantiated as model elements. See Structure details: SysML Overview in Javadoc

Supported Models in Each Diagram

Summary of supported models of each SysML diagram. See model details:SysmlModelEditor.
To find out how to create diagram, please refer to the DiagramEditor, like BlockDefinitionDiagramEditor.

Diagram Class Related Models Get Model Create Model
BlockDefinitionDiagram IBlockDefinitionDiagram BlockDefinitionDiagram
InternalBlockDiagram IInternalBlockDiagram InternalBlockDiagram
ParametricDiagram ToDo - ToDo ToDo
RequirementDiagra and Requirement Table IRequirementDiagram,IRequirementTable RequirementDiagra and Requirement Table
UseCaseDiagram IUseCaseDiagram UseCaseDiagram
ActivityDiagram IActivityDiagram ActivityDiagram
SequenceDiagram ISequenceDiagram SequenceDiagram
StateMachineDiagram IStateMachineDiagram StateMachineDiagram

Supported Presentations in Each Diagram

This is a list of Presentation in SysML. To find out which presentation is editable or not, please refer to the DiagramEditor, like BlockDefinitionDiagramEditor.

Diagram Class DiagramEditor INodePresentation ILinkPresentation
BlockDefinitionDiagram IBlockDefinitionDiagram BlockDefinitionDiagramEditor
InternalBlockDiagram IInternalBlockDiagram InternalBlockDiagramEditor
ParameticDiagram ToDo - ToDo ToDo
Requirement Diagam and Requirement Table IRequirementDiagram,IRequirementTable RequirementDiagramEditor
UseCaseDiagram IUseCaseDiagram UseCaseDiagramEditor
ActivityDiagram IActivityDiagram ActivityDiagramEditor
SequenceDiagram ISequenceDiagram SequenceDiagramEditor
StateMachineDiagram IStateMachineDiagram StateMachineDiagramEditor