Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-matsuda/7c9174bf49bdf92f367b to your computer and use it in GitHub Desktop.
Save y-matsuda/7c9174bf49bdf92f367b to your computer and use it in GitHub Desktop.
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 java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.change_vision.jude.api.inf.AstahAPI;
import com.change_vision.jude.api.inf.editor.SequenceDiagramEditor;
import com.change_vision.jude.api.inf.editor.TransactionManager;
import com.change_vision.jude.api.inf.exception.BadTransactionException;
import com.change_vision.jude.api.inf.exception.InvalidEditingException;
import com.change_vision.jude.api.inf.exception.LicenseNotFoundException;
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.ICommunicationDiagram;
import com.change_vision.jude.api.inf.model.IElement;
import com.change_vision.jude.api.inf.model.ILifeline;
import com.change_vision.jude.api.inf.model.IMessage;
import com.change_vision.jude.api.inf.model.INamedElement;
import com.change_vision.jude.api.inf.presentation.ILinkPresentation;
import com.change_vision.jude.api.inf.presentation.INodePresentation;
import com.change_vision.jude.api.inf.presentation.IPresentation;
import com.change_vision.jude.api.inf.presentation.PresentationPropertyConstants.Key;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
public class CommunicationDiagramToSequenceDiagramConverter {
private final static int X_OF_LIFELINE = 10;
private final static int SPACING_OF_LIFELINE = 150;
private final static int Y_OF_MESSAGE = 100;
private final static int SPACING_OF_MESSAGE = 35;
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("The project path is required as an argument.");
return;
}
String projectPath = args[0];
ProjectAccessor prjAccessor;
try {
prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
prjAccessor.open(projectPath);
final INamedElement[] communicationDiagrams = prjAccessor
.findElements(ICommunicationDiagram.class);
SequenceDiagramEditor diagramEditor = prjAccessor.getDiagramEditorFactory()
.getSequenceDiagramEditor();
for (INamedElement communicationDiagram : communicationDiagrams) {
final IPresentation[] presentations = communicationDiagram.getPresentations();
List<IMessage> messages = new ArrayList<>();
for (IPresentation presentation : presentations) {
if (presentation.getModel() instanceof IMessage) {
messages.add((IMessage) presentation.getModel());
}
}
messages.sort((o1, o2) -> {
final String[] o1Indexs = String.format("%s.0", o1.getIndex()).split("\\.");
final String[] o2Indexs = String.format("%s.0", o2.getIndex()).split("\\.");
for (int i = 0; i < Math.min(o1Indexs.length, o2Indexs.length); i++) {
final int difference = Integer.valueOf(o1Indexs[i])
- Integer.valueOf(o2Indexs[i]);
if (difference == 0) {
continue;
}
return difference;
}
return 0;
});
try {
TransactionManager.beginTransaction();
diagramEditor.createSequenceDiagram(
(INamedElement) communicationDiagram.getOwner(),
communicationDiagram.getName());
// create lifeline
Map<INodePresentation, INodePresentation> lifelinePs = new HashMap<>();
int x = X_OF_LIFELINE;
for (IPresentation presentation : presentations) {
final IElement model = presentation.getModel();
if (model instanceof ILifeline) {
ILifeline original = (ILifeline) model;
final INodePresentation lifelineP = diagramEditor
.createLifeline(original.getName(), x);
copyColor(presentation, lifelineP);
final ILifeline lifeline = (ILifeline) lifelineP.getModel();
lifeline.setBase(original.getBase());
lifelinePs.put((INodePresentation) presentation, lifelineP);
x += SPACING_OF_LIFELINE;
}
}
// create message
int y = Y_OF_MESSAGE;
Map<IMessage, INodePresentation> messagePs = new HashMap<>();// originalMessage,
// lastExecutionSpecification
for (IMessage original : messages) {
INodePresentation sender = lifelinePs
.get(original.getSource().getPresentations()[0]);
if (original.getActivator() == null) {
if (y != Y_OF_MESSAGE) {
y += SPACING_OF_MESSAGE;
}
} else {
final INodePresentation sourceP = messagePs
.get(original.getActivator());
if (sourceP != null) {
sender = sourceP;
}
}
final INodePresentation receiver = lifelinePs
.get(original.getTarget().getPresentations()[0]);
final ILinkPresentation messageP = diagramEditor
.createMessage(original.getName(), sender, receiver, y);
copyColor(original.getPresentations()[0], messageP);
final Point2D[] allPoints = messageP.getAllPoints();
y += SPACING_OF_MESSAGE;
if (allPoints.length == 4) {
y += allPoints[3].getY() - allPoints[0].getY();
}
messagePs.put(original, messageP.getTarget());
final IMessage message = (IMessage) messageP.getModel();
message.setOperation(original.getOperation());
message.setReturnValue(original.getReturnValue());
message.setReturnValueVariable(original.getReturnValueVariable());
message.setGuard(original.getGuard());
}
TransactionManager.endTransaction();
} catch (BadTransactionException e) {
TransactionManager.abortTransaction();
System.err.print(e.getMessage());
e.printStackTrace();
}
}
prjAccessor.save();
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.print(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
} finally {
prjAccessor.close();
}
}
private static void copyColor(IPresentation originalPresentation,
final IPresentation presentation) throws InvalidEditingException {
if (presentation instanceof INodePresentation) {
copyProperty(Key.FILL_COLOR, originalPresentation, presentation);
}
copyProperty(Key.FONT_COLOR, originalPresentation, presentation);
copyProperty(Key.LINE_COLOR, originalPresentation, presentation);
}
private static void copyProperty(final String key, final IPresentation originalPresentation,
final IPresentation presentation) throws InvalidEditingException {
presentation.setProperty(key, originalPresentation.getProperty(key));
}
}
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