Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
astah* API サンプルコード

astah* API サンプルコード

astah* API を使って書いたコードを適当に書き溜めています。

import java.awt.geom.Point2D;
import com.change_vision.jude.api.inf.AstahAPI;
import com.change_vision.jude.api.inf.exception.InvalidUsingException;
import com.change_vision.jude.api.inf.model.IActivity;
import com.change_vision.jude.api.inf.model.IActivityDiagram;
import com.change_vision.jude.api.inf.model.IActivityNode;
import com.change_vision.jude.api.inf.model.IDiagram;
import com.change_vision.jude.api.inf.model.IModel;
import com.change_vision.jude.api.inf.presentation.INodePresentation;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
public class APIForReadingActivityDiagramModelsSample {
public static void main(String[] args) {
String projectPath = args[0];
ProjectAccessor projectAccessor;
try {
projectAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
projectAccessor.open(projectPath, true, false, true);
IModel project = projectAccessor.getProject();
for (IDiagram diagram : project.getDiagrams()) {
if (diagram instanceof IActivityDiagram) {
printActivityDiagramInfo((IActivityDiagram) diagram);
continue;
}
System.out.println(diagram.getName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
projectAccessor.close();
}
}
private static void printActivityDiagramInfo(IActivityDiagram diagram)
throws InvalidUsingException {
System.out.println(String.format("ActivityDiagram Name: %s", diagram.getName()));
IActivity activity = diagram.getActivity();
IActivityNode[] activityNodes = activity.getActivityNodes();
for (IActivityNode activityNode : activityNodes) {
INodePresentation presentation = getPresentation(activityNode);
Point2D location = getLocation(presentation);
System.out.println(String.format(" %s (X:%f, Y:%f)", presentation.getLabel(),
location.getX(), location.getY()));
}
}
private static Point2D getLocation(INodePresentation presentation) {
if (presentation == null) {
throw new IllegalArgumentException("presentation is null.");
}
return presentation.getLocation();
}
private static INodePresentation getPresentation(IActivityNode activityNode)
throws InvalidUsingException {
if (activityNode == null) {
throw new IllegalArgumentException("action is null.");
}
return (INodePresentation) activityNode.getPresentations()[0];
}
}
/**
* The color of the presentation is changed.
*
* @param presentation
* presentation
* @param color
* The color of the hexadecimal (for example, #FFFFFF)
*/
public static void changeColor(IPresentation presentation, final String color) {
try {
TransactionManager.beginTransaction();
presentation.setProperty(Key.FILL_COLOR, color);
TransactionManager.endTransaction();
} catch (InvalidEditingException e) {
TransactionManager.abortTransaction();
}
}
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.ITransactionManager;
import com.change_vision.jude.api.inf.exception.InvalidEditingException;
import com.change_vision.jude.api.inf.model.IModel;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
public class CreatePackageSample {
public static void main(String[] args) {
String projectPath = args[0];
ProjectAccessor projectAccessor;
try {
projectAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
ITransactionManager transactionManager = projectAccessor.getTransactionManager();
try {
try {
projectAccessor.open(projectPath, true, false, true);
} catch (Exception e) {
e.printStackTrace();
return;
}
BasicModelEditor basicModelEditor;
try {
basicModelEditor = projectAccessor.getModelEditorFactory().getBasicModelEditor();
} catch (InvalidEditingException e) {
e.printStackTrace();
return;
}
try {
IModel project = projectAccessor.getProject();
transactionManager.beginTransaction();
basicModelEditor.createPackage(project, "Parent is project package");
transactionManager.endTransaction();
} catch (Exception e) {
transactionManager.abortTransaction();
e.printStackTrace();
}
try {
transactionManager.beginTransaction();
basicModelEditor.createPackage(null, "Parent is null package");
transactionManager.endTransaction();
} catch (Exception e) {
transactionManager.abortTransaction();
System.out.println("The first parameter can not be null.");
}
try {
projectAccessor.save();
} catch (Exception e) {
e.printStackTrace();
return;
}
} finally {
projectAccessor.close();
}
}
}
/**
* クラスを指定せずに、指定されたクラスにテンプレートパラメタを作成します。
*
* @param modelEditor
* モデルエディタ
* @param targetClass
* クラス
* @param name
* 名前
* @param defaultValue
* デフォルト値
* @return 作成されたテンプレートパラメタ
* @throws InvalidEditingException
*/
public IClassifierTemplateParameter createTemplateParameter(
BasicModelEditor modelEditor, IClass targetClass, String name, Object defaultValue)
throws InvalidEditingException {
throws InvalidEditingException {
if (targetClass == null) {
throw new InvalidEditingException(InvalidEditingException.INVALID_TEMPLATE_KEY,
InvalidEditingException.INVALID_TEMPLATE_MESSAGE);
}
IElement container = targetClass.getContainer();
if (container == null || !(container instanceof IPackage)) {
throw new InvalidEditingException(InvalidEditingException.INVALID_TEMPLATE_KEY,
InvalidEditingException.INVALID_TEMPLATE_MESSAGE);
}
IClass dummyClass = modelEditor.createClass(IPackage.class.cast(container), "dummyClass");
IClassifierTemplateParameter templateParameter = modelEditor.createTemplateParameter(
targetClass, name, dummyClass, defaultValue);
modelEditor.delete(dummyClass);
return templateParameter;
}
/**
* Search Actors in an Astah model.
*
* @param prjAccessor
* ProjectAccessor
* @return Actors
* @throws ProjectNotFoundException
* Project doesn't exist
*/
public List<IClass> findActors(ProjectAccessor prjAccessor)
throws ProjectNotFoundException {
return getActors(findClasses(prjAccessor));
}
/**
* Search Classes in an Astah model.
*
* @param prjAccessor
* ProjectAccessor
* @return Classes
* @throws ProjectNotFoundException
* Project doesn't exist
*/
public List<IClass> findClasses(ProjectAccessor prjAccessor)
throws ProjectNotFoundException {
List<IClass> classes = new ArrayList<IClass>();
for (INamedElement element : prjAccessor.findElements(IClass.class)) {
if (element instanceof IClass) {
classes.add(IClass.class.cast(element));
}
}
return classes;
}
/**
* Actors are picked out from a classes.
*
* @param classes
* Classes
* @return Actors
*/
public List<IClass> getActors(List<IClass> classes) {
List<IClass> actors = new ArrayList<IClass>();
for (IClass clazz : classes) {
if (isActor(clazz)) {
actors.add(clazz);
}
}
return actors;
}
/**
* Check if the Class is an Actor.
*
* @param clazz
* Class
* @return If true, it is an Actor. If false, it is not an Actor.
*/
public boolean isActor(IClass clazz) {
return Arrays.asList(clazz.getStereotypes()).contains("actor");
}
/**
* getting a list of physical entities.
*
* @param prjAccessor
* @param physicalName
* @return physical entities
* @throws ProjectNotFoundException
*/
public List<IEREntity> getEREntities(ProjectAccessor prjAccessor, final String physicalName)
throws ProjectNotFoundException {
ModelFinder finder = new ModelFinder() {
@Override
public boolean isTarget(INamedElement element) {
if (element instanceof IEREntity) {
IEREntity erEntity = (IEREntity) element;
return StringUtils.equals(physicalName, erEntity.getPhysicalName());
}
return false;
}
};
return Arrays.asList((IEREntity[]) prjAccessor.findElements(finder));
}
/**
* A diagram is acquired from presentation in a class.
*
* @param clazz
* Class
* @return Related diagrams
*/
public List<IDiagram> getRelatedDiagrams(IClass clazz) {
List<IDiagram> diagrams = new ArrayList<IDiagram>();
IPresentation[] presentations = new IPresentation[0];
try {
presentations = clazz.getPresentations();
} catch (InvalidUsingException e) {
return diagrams;
}
for (IPresentation presentation : presentations) {
diagrams.add(presentation.getDiagram());
}
return diagrams;
}
/**
* The method which judges a flow final node before Astah 6.7.
* IControlNode#isFlowFinalNode() was added by Astah 6.8.
*
* @param element
* Element
*/
public static boolean isFlowFinalNode(IElement element) {
if (!(element instanceof IControlNode)) {
return false;
}
String[] stereotypes = element.getStereotypes();
for (String stereotype : stereotypes) {
if ("flow_final_node".equals(stereotype)) {
return true;
}
}
return false;
}
import java.io.IOException;
import com.change_vision.jude.api.inf.AstahAPI;
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.IAction;
import com.change_vision.jude.api.inf.model.IModel;
import com.change_vision.jude.api.inf.model.INamedElement;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
/**
* すべてのアクションの名前を表示するサンプルプログラム
*/
public class PrintActionName {
private static final String PROJECT_PATH = "ActivityDiagram.asta";
public static void main(String[] args) {
System.out.println("Opening project...");
try {
ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
prjAccessor.open(PROJECT_PATH, true, false, true);
IModel project = prjAccessor.getProject();
System.out.println("project name : " + project.getName());
INamedElement[] actions = prjAccessor.findElements(IAction.class);
for (INamedElement action : actions) {
System.out.println(action.getName());
}
System.out.println("Finished");
prjAccessor.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (LicenseNotFoundException e) {
e.printStackTrace();
} catch (ProjectNotFoundException e) {
e.printStackTrace();
} catch (NonCompatibleException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ProjectLockedException e) {
e.printStackTrace();
}
}
}
/**
* prints the contents of the note from a class.
*
* @param clazz
* class
*/
public static void printCommentsInfo(IClass clazz) {
IComment[] comments = clazz.getComments();
for (IComment comment : comments) {
System.out.println(clazz.getName() + " : " + comment.getBody());
}
}
/**
* prints the contents of the comment connected with the note anchor.
*
* @param nodePs
* NodePresentation
*/
public void printConnectedCommentsInfo(INodePresentation nodePs) {
ILinkPresentation[] links = nodePs.getLinks();
for (IComment comment : getComments(links)) {
System.out.println(comment.getBody());
}
}
/**
* returns the comments in the both ends of LinkPresentations.
*
* @param links
* LinkPresentations
* @return comments
*/
private List<IComment> getComments(ILinkPresentation[] links) {
List<IComment> comments = new ArrayList<IComment>();
for (ILinkPresentation linkPs : links) {
IComment sourceComment = getComment(linkPs.getSource());
if (sourceComment != null) {
comments.add(sourceComment);
}
}
for (ILinkPresentation linkPs : links) {
IComment targetComment = getComment(linkPs.getTarget());
if (targetComment != null) {
comments.add(targetComment);
}
}
return comments;
}
/**
* return model of comment.
*
* @param nodePs
* NodePresentation
* @return model of comment
*/
private IComment getComment(INodePresentation nodePs) {
IElement model = nodePs.getModel();
if (model instanceof IComment) {
return IComment.class.cast(model);
}
return null;
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.change_vision.jude.api.inf.AstahAPI;
import com.change_vision.jude.api.inf.exception.InvalidUsingException;
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.IAttribute;
import com.change_vision.jude.api.inf.model.IDiagram;
import com.change_vision.jude.api.inf.model.IElement;
import com.change_vision.jude.api.inf.model.IInstanceSpecification;
import com.change_vision.jude.api.inf.model.IModel;
import com.change_vision.jude.api.inf.model.INamedElement;
import com.change_vision.jude.api.inf.model.ISlot;
import com.change_vision.jude.api.inf.presentation.IPresentation;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
/**
* プレゼンテーションからインスタンス仕様の属性と値を表示するサンプルプログラム
*/
public class PrintInstanceSpecificationInfo {
private static final String PROJECT_PATH = "SampleModel.asta";
public static void main(String[] args) {
System.out.println("Opening project...");
try {
ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
prjAccessor.open(PROJECT_PATH, true, false, true);
IModel project = prjAccessor.getProject();
System.out.println("project name : " + project.getName());
List<IPresentation> presentations = new ArrayList<IPresentation>();
IDiagram[] diagrams = project.getDiagrams();
for (IDiagram diagram : diagrams) {
presentations.addAll(Arrays.asList(diagram.getPresentations()));
}
System.out.println("Printing the InstanceSpecification...");
System.out.println("---");
for (IPresentation presentation : presentations) {
printPresentationInfo(presentation);
System.out.println("---");
}
System.out.println("Finished");
prjAccessor.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (LicenseNotFoundException e) {
e.printStackTrace();
} catch (ProjectNotFoundException e) {
e.printStackTrace();
} catch (NonCompatibleException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ProjectLockedException e) {
e.printStackTrace();
} catch (InvalidUsingException e) {
e.printStackTrace();
}
}
public static void printPresentationInfo(IPresentation presentation) {
IElement model = presentation.getModel();
if (model instanceof IInstanceSpecification) {
IInstanceSpecification instanceSpecification = IInstanceSpecification.class.cast(model);
printInstanceSpecificationInfo(instanceSpecification);
return;
}
if (model instanceof INamedElement) {
INamedElement namedElement = INamedElement.class.cast(model);
System.out.println(namedElement.getName() + " is Not InstanceSpecification.");
return;
}
System.out.println("This Presentation is Not InstanceSpecification.");
}
private static void printInstanceSpecificationInfo(IInstanceSpecification instanceSpecification) {
System.out.println("instanceSpecification name : " + instanceSpecification.getName());
ISlot[] slots = instanceSpecification.getAllSlots();
for (ISlot slot : slots) {
IAttribute attribute = slot.getDefiningAttribute();
String value = slot.getValue();
System.out.println("attribute : " + attribute + ", value : " + value);
}
}
}
/**
* The method which displays the type of all the presentations which exist in a figure.
*
* @param dgm
* Diagram
* @throws InvalidUsingException
* Invalid Using.
*/
public static void printPresentationsType(IDiagram dgm) throws InvalidUsingException {
IPresentation[] presentations = dgm.getPresentations();
for (IPresentation presentation : presentations) {
System.out.println(presentation.getType());
}
}
/**
* A class and the class tied with the relation are printed.
*
* @param clazz
* Class
*/
public void printToEndTarget(IClass clazz) {
for (IAttribute attribute : clazz.getAttributes()) {
System.out.println(clazz.getName() + " : "
+ getAssociationName(attribute.getAssociation()) + " : "
+ attribute.getType().getName());
}
}
public String getAssociationName(IAssociation association) {
if(association != null) {
return "";
}
return association.getName();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.