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
  • target models/presentations you are trying to edit are non-editable

Transaction process

A transaction is a unit of update processing performed on a Astah project.

Use beginTransaction to start a transaction.
Use endTransaction to commit the transaction.
This is one update process. undo / redo is done in this processing unit.

Use abortTransaction to cancel a transaction.
If you cancel a transaction, the project returns to the state it was in before the transaction started.
When any exception happens while working on the projects with API, you need to call the abortTransaction method, then start again.

ITransactionManager transactionManager = projectAccessor.getTransactionManager();
try {

    transactionManager.beginTransaction();

    // editting

    transactionManager.endTransaction();

} catch (BadTransactionException e) {

    transactionManager.abortTransaction();

    // processing

}

Edit model

Model Editor

The Model Editor is used when you edit model with API.
Each facet has its own Model Editor. For example, SysmlModelEditor can be used to create SysML models.
If you edit a model that can have multiple presentations, you can create/edit from each Model Editor.

Editing Model Viewpoint

There are some models that are automatically created when you create presentations. For details, please refer to Get Diagrams and Presentations.

Model Editors Model elements you can create in the Model Editor
BasicModelEditor Class, Artifact, Association, Association Class, Attribute, Component, Constraint, Copy, Dependency, DeriveReqt, Generalization, Interface, Model, Node, Operation, Package, Parameter, Port, Qualifier, Realization, Refine dependency, Requirement, Satisfy dependency, Subsystem, Tagged value, Template binding, Template parameter, TestCase, Trace dependency, Usage, Verify dependency

Create a Package under model

// Transaction processing is required
public IClass createPackage(ProjectAccessor projectAccessor, IModel parent, String name)
        throws InvalidEditingException {
    IModelEditorFactory modelEditorFactory = projectAccessor.getModelEditorFactory();
    SysmlModelEditor sysmlModelEditor = modelEditorFactory.getSysmlModelEditor();
    return sysmlModelEditor.createPackage(parent, name);
}

Create TaggedValues

// Transaction processing is required
public void createTaggedValue(SysmlModelEditor sysmlModelEditor, IElement element, String tag, String value)
        throws InvalidEditingException {
    sysmlModelEditor.createTaggedValue(element, tag, value);
}

Delete Models

// Transaction processing is required
public void deleteElement(SysmlModelEditor sysmlModelEditor, IElement element)
        throws InvalidEditingException {
    sysmlModelEditor.delete(element);
}

Add Definition

This is an example to add Definition to a Block./

// Transaction processing is required
public void setDefinition(IBlock block, String definition)
        throws InvalidEditingException {
    block.setDefinition(definition);
}

Add Stereotypes

// Transaction processing is required
public void addStereotype(IElement element, String stereotype) throws InvalidEditingException {
    element.addStereotype(stereotype);
}

Edit Presentations

Diagram Editor

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

A target diagram needs to be set for DiagramEditor before you start editing.
When you create a diagram, the diagram type will be automatically set to DiagramEditor. But when you edit any existing diagrams, you need to call DiagramEditor#setDiagram(IDiagram diagram) and set the target diagram.

Editing Presentation Viewpoint

Diagram Editor Editable diagrams
BlockDefinitionDiagramEditor Block Definition Diagram
RequirementDiagramEditor Requirement Diagram
GsnDiagramEditor GSN / D-Case Diagram
StampDiagramEditor Control Structure Diagram

Create a Block Definition diagram

// Transaction processing is required
public IBlockDefinitionDiagram createBlockDefinitionDiagram(BlockDefinitionDiagramEditor editor, INamedElement owner,
        String name) throws InvalidEditingException {
    return editor.createBlockDefinitionDiagram(owner, name);
}

Create a Block Presentation

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

Create a presentation that requires a Model as 1:1

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 createStatePresentation(StateMachineDiagramEditor editor, String name,
        INodePresentation parent, Point2D location) throws InvalidEditingException {
    return editor.createState(name, parent, location);
}

Delete presentations

// Transaction processing is required
public void deletePresentation(DiagramEditor editor, IPresentation presentation)
        throws InvalidEditingException {
    editor.deletePresentation(presentation);
}

Change Property of presentation

To change the presentation such as background color, etc. use PresentationPropertyConstants or PresentationPropertyUtil.
For correspondence between model and presentation, please see Supported presentation list.

Presentation Interface

Change Color

// Transaction processing is required
public void changeColor(IPresentation presentation, final String color)
        throws InvalidEditingException {
    presentation.setProperty(Key.FILL_COLOR, color);
}

Relocate

// Transaction processing is required
public void setLocation(INodePresentation presentation, Point2D location) throws InvalidEditingException {
        presentation.setLocation(location);
}