モデル / プレゼンテーションの参照

モデルおよびプレゼンテーションの参照方法について説明します。
対応モデル対応プレゼンテーションについてはこちらをご覧ください。

モデルの参照

以下に、モデルを参照する例を示します。

プロジェクトからモデルを検索する

プロジェクト全体からモデルを取得するにはProjectAccessorのfindElementsメソッドを用います。
ProjectAccessorAstahAPI#getProjectAccessor()から取得できます。

ModelFinderの実装例

ブロック定義図の抽出
public class BlockDefinitionsDiagramPicker implements ModelFinder {
    public boolean isTarget(INamedElement namedElement) {
        return namedElement instanceof IBlockDefinitionDiagram;
    }
}
入場点の抽出
public class EntryPointPicker implements ModelFinder {
    public boolean isTarget(INamedElement namedElement) {
        if (namedElement instanceof IPseudostate) {
            return ((IPseudostate) namedElement).isEntryPointPseudostate();
        }
        return false;
    }
}

構造ツリーで選択しているモデルを取得する

public IEntity[] printSelectedEntities(IViewManager viewManager) {
    IProjectViewManager projectViewManager = viewManager.getProjectViewManager();
    return projectViewManager.getSelectedEntities();
}

図上で選択しているプレゼンテーションからモデルを取得する

public List<IElement> getModelsOfSelectedPresentations(IViewManager viewManager) {
    List<IElement> models = new ArrayList<>();
    for (IPresentation selectedP : getSelectedPresentations(viewManager)) {
        models.add(selectedP.getModel());
    }
    return models;
}

private IPresentation[] getSelectedPresentations(IViewManager viewManager) {
    IDiagramViewManager diagramViewManager = viewManager.getDiagramViewManager();
    return diagramViewManager.getSelectedPresentations();
}

ステレオタイプを取得する

IElement#getStereotypes()からStringの配列として取得できます。 下記の例では、ブロックのステレオタイプ(block)を取得できます。

public String getBlockStereotype(IBlock block) {
  String[] stereotypes = block.getStereotypes();
  return stereotypes[0];
}

図 / プレゼンテーションの参照

図を取得する

public List<IActivityDiagram> getActivityDiagram(IPackage pkg) {
    List<IActivityDiagram> activityDiagrams = new ArrayList<>();

    IDiagram[] diagrams = pkg.getDiagrams();
    for (IDiagram diagram : diagrams) {
       if (diagram instanceof IActivityDiagram
           && !((IActivityDiagram )diagram).isFlowChart()) {
           activityDiagrams.add((IActivityDiagram)diagram);
       }
    }
    return activityDiagrams;
}

図内の全プレゼンテーションを取得する

IDiagram#getPresentations() を用いると図内の全プレゼンテーションを取得することができます。
IElement#getPresentations() とは異なる挙動をするため注意してください。

public IPresentation[] getOwnedPresentation(IDiagram diagram) throws InvalidUsingException {
    return diagram.getPresentations();
}

モデルからプレゼンテーションを取得する

IElement#getPresentations() を用いると IPresentation#getModel() == element となるプレゼンテーションをすべて取得できます。
IDiagram#getPresentations()は図内のプレゼンテーションを取得することとなりますので、下記例では例外処理をしています。

public IPresentation[] getPresentation(IElement element) throws InvalidUsingException {
    if (element instanceof IDiagram) {
        throw new IllegalArgumentException("element is IDiagram.");
    }
    return element.getPresentations();
}

図上で選択しているプレゼンテーションを取得する

IDiagramViewManager#getSelectedPresentations() を用いると、現在アクティブなダイアグラムエディタ上で選択されている図要素を取得できます。

public IPresentation[] getSelectedPresentations(IViewManager viewManager) {
    IDiagramViewManager diagramViewManager = viewManager.getDiagramViewManager();
    return diagramViewManager.getSelectedPresentations();
}