使用 EMF 从 XMI 读取序列图
Read Sequence Diagram from XMI using EMF
我搜索了很多有关使用 EMF 从 XMI 读取 UML 图的信息 Java
蚀。我只找到了阅读 class 图的解决方案,但我的任务是
从 XMI 读取序列图。谁能帮帮我?
经过一番努力终于找到了一个可行的解决方案,你只需要将无错误的UML图路径放在主class,提供的代码将读取所有节点和子- 节点分别。您可以根据您的要求自定义代码。此代码可用于 class 图和序列图。
时序图模型Reader(主要Class)
public static void main(String[] args) {
MododelLoader umlModel= new ModelLoader();
// sequence diagram XMI
String umlFilePath = "SequenceDiagramUML\sequenceDiagram.uml";
String uri = null;
try {
uri = umlModel.getFileURI(umlFilePath);
} catch (Exception e) {
e.printStackTrace();
}
Object objModel = umlModel.loadModel(uri);
Model sourceModel;
EList<PackageableElement> sourcePackagedElements = null;
if (objModel instanceof Model) {
sourceModel = (Model) objModel;
sourcePackagedElements = sourceModel.getPackagedElements();
} else if (objModel instanceof Package) {
Package sourcePackage = (Package) objModel;
sourcePackagedElements = sourcePackage.getPackagedElements();
}
for (PackageableElement element : sourcePackagedElements){
//for nested package
if(element.eClass() == UMLPackage.Literals.PACKAGE){
org.eclipse.uml2.uml.Package nestedPackage =
(org.eclipse.uml2.uml.Package) element;
EList<PackageableElement> nestedPackagedElements =
nestedPackage.getPackagedElements();
for (PackageableElement nestedElement : nestedPackagedElements){
printModelDetails(nestedElement);
}
}
else
printModelDetails(element);
}
模型详细信息提取器(助手 Class)
private static void printModelDetails(PackageableElement element){
int counterEnd=1;
String classfierName="Test";
boolean isClass=false;
if(element.eClass()==UMLPackage.Literals.COLLABORATION){
Collaboration collab=(Collaboration)element;
System.out.println("Collaboration Name: "+collab.getName());
EList<Behavior> behaviours = collab.getOwnedBehaviors();
System.out.println("Behavious: ");
if(!behaviours.isEmpty()){
for (Behavior obj: behaviours)
{
Interaction interaction=(Interaction)obj;
EList<Lifeline> lifelines=interaction.getLifelines();
EList<InteractionFragment> fragments=interaction.getFragments();
EList<Message> messages=interaction.getMessages();
if(!lifelines.isEmpty()) {
for(Lifeline lifln:lifelines) {
EList<EAnnotation> inter=lifln.getEAnnotations();
}
}
if(!fragments.isEmpty()) {
for(InteractionFragment frag:fragments) {
EList<Element> elements= frag.getOwnedElements();
}
}
if(!messages.isEmpty()) {
for(Message msg:messages) {
EList<ConnectorEnd>
connectorEnds=msg.getConnector().getEnds();
String conEnd1=connectorEnds.get(0).getRole().getName();
String conEnd2=connectorEnds.get(1).getRole().getName();
System.out.println("Messages: "+msg.getName());
System.out.println("Message_Receive_event:"+msg.getMessageSort());
System.out.println("ConEnd1: "+conEnd1);
System.out.println("ConEnd2: "+conEnd2);
}
}
System.out.println("Interaction Name: "+obj.getName());
EList<Connector> connectors = obj.getOwnedConnectors();
System.out.println("Connectors: ");
if(!connectors.isEmpty()){
for (Connector behav: connectors)
{
EList<ConnectorEnd> property = behav.getEnds();
if(!property.isEmpty()) {
System.out.println("Connector Ends "+counterEnd);
for(ConnectorEnd ends:property) {
System.out.println("EndName:"+ends.getRole().getName());
}
counterEnd++;
}
}
}
模型加载器Class(助手Class)
public class ModelLoader {
private final ResourceSet RESOURCE_SET;
public ModelLoader() {
RESOURCE_SET = new ResourceSetImpl();
}
/**
* A method that converts input path to uri and return uri.
*
* @param path
* @return uri
* @throws Exception
*/
public String getFileURI(String path) throws Exception {
File f = new File(path);
String uri = f.toURI().toString();
return uri;
}
/**
* A method that loads UML/Ecore model from input URI.
*
* @param uri:String
* @return model:Object
*/
public Object loadModel(String uri){
URI modelUri = URI.createURI(uri);
registerPackages(RESOURCE_SET);
registerResourceFactories();
String relPath = null;
try {
relPath = new File(".").getCanonicalPath();
} catch (IOException e1) {
e1.printStackTrace();
}
URIConverter.URI_MAP.put(URI.createURI("platform:/plugin/org.eclipse.uml2.uml/"),
URI.createURI("jar:file:"+
relPath+"/EMF_Lib/org.eclipse.uml2.uml_3.1.2.v201010261927.jar! /"));
Resource resource = null;
try {
resource = RESOURCE_SET.getResource(modelUri, true);
}
catch (Exception e) {
e.printStackTrace();
}
Object result;
Model _model = (Model) EcoreUtil.getObjectByType(resource.getContents(),
UMLPackage.Literals.MODEL);
result = _model;
// org.eclipse.uml2.uml.Package _model =
//
(org.eclipse.uml2.uml.Package)EcoreUtil.getObjectByType(resource.getContents(),
// UMLPackage.Literals.PACKAGE);
if (_model == null) {
result = resource.getContents().get(0);
}
return result;
}
public void loadResources(){
registerPackages(RESOURCE_SET);
registerResourceFactories();
String relPath = null;
try {
relPath = new File(".").getCanonicalPath();
} catch (IOException e1) {
e1.printStackTrace();
}
URIConverter.URI_MAP.put(URI.createURI("platform:/plugin/org.eclipse.uml2.uml/"),
URI.createURI("jar:file:"+
relPath+"/EMF_Lib/org.eclipse.uml2.uml_3.1.2.v201010261927.jar!/"));
}
private void registerResourceFactories()
{
Map extensionFactoryMap =
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap();
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(Ecore2XMLResource.FILE_EXTENSION,
Ecore2XMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UML22UMLResource.FILE_EXTENSION,
UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,new
XMIResourceFactoryImpl());
}
private void registerPackages(ResourceSet resourceSet)
{
Map packageRegistry = resourceSet.getPackageRegistry();
packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
packageRegistry.put(Ecore2XMLPackage.eNS_URI, Ecore2XMLPackage.eINSTANCE);
packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
packageRegistry .put(UML212UMLResource.UML_METAMODEL_NS_URI,
UMLPackage.eINSTANCE);
//for RSA
packageRegistry.put("http://www.eclipse.org/uml2/2.0.0/UML",UMLPackage.eINSTANCE);
//for Papyrus
packageRegistry.put("http://www.eclipse.org/uml2/5.0.0/UML",UMLPackage.eINSTANCE);
}
}
我搜索了很多有关使用 EMF 从 XMI 读取 UML 图的信息 Java 蚀。我只找到了阅读 class 图的解决方案,但我的任务是 从 XMI 读取序列图。谁能帮帮我?
经过一番努力终于找到了一个可行的解决方案,你只需要将无错误的UML图路径放在主class,提供的代码将读取所有节点和子- 节点分别。您可以根据您的要求自定义代码。此代码可用于 class 图和序列图。
时序图模型Reader(主要Class)
public static void main(String[] args) {
MododelLoader umlModel= new ModelLoader();
// sequence diagram XMI
String umlFilePath = "SequenceDiagramUML\sequenceDiagram.uml";
String uri = null;
try {
uri = umlModel.getFileURI(umlFilePath);
} catch (Exception e) {
e.printStackTrace();
}
Object objModel = umlModel.loadModel(uri);
Model sourceModel;
EList<PackageableElement> sourcePackagedElements = null;
if (objModel instanceof Model) {
sourceModel = (Model) objModel;
sourcePackagedElements = sourceModel.getPackagedElements();
} else if (objModel instanceof Package) {
Package sourcePackage = (Package) objModel;
sourcePackagedElements = sourcePackage.getPackagedElements();
}
for (PackageableElement element : sourcePackagedElements){
//for nested package
if(element.eClass() == UMLPackage.Literals.PACKAGE){
org.eclipse.uml2.uml.Package nestedPackage =
(org.eclipse.uml2.uml.Package) element;
EList<PackageableElement> nestedPackagedElements =
nestedPackage.getPackagedElements();
for (PackageableElement nestedElement : nestedPackagedElements){
printModelDetails(nestedElement);
}
}
else
printModelDetails(element);
}
模型详细信息提取器(助手 Class)
private static void printModelDetails(PackageableElement element){
int counterEnd=1;
String classfierName="Test";
boolean isClass=false;
if(element.eClass()==UMLPackage.Literals.COLLABORATION){
Collaboration collab=(Collaboration)element;
System.out.println("Collaboration Name: "+collab.getName());
EList<Behavior> behaviours = collab.getOwnedBehaviors();
System.out.println("Behavious: ");
if(!behaviours.isEmpty()){
for (Behavior obj: behaviours)
{
Interaction interaction=(Interaction)obj;
EList<Lifeline> lifelines=interaction.getLifelines();
EList<InteractionFragment> fragments=interaction.getFragments();
EList<Message> messages=interaction.getMessages();
if(!lifelines.isEmpty()) {
for(Lifeline lifln:lifelines) {
EList<EAnnotation> inter=lifln.getEAnnotations();
}
}
if(!fragments.isEmpty()) {
for(InteractionFragment frag:fragments) {
EList<Element> elements= frag.getOwnedElements();
}
}
if(!messages.isEmpty()) {
for(Message msg:messages) {
EList<ConnectorEnd>
connectorEnds=msg.getConnector().getEnds();
String conEnd1=connectorEnds.get(0).getRole().getName();
String conEnd2=connectorEnds.get(1).getRole().getName();
System.out.println("Messages: "+msg.getName());
System.out.println("Message_Receive_event:"+msg.getMessageSort());
System.out.println("ConEnd1: "+conEnd1);
System.out.println("ConEnd2: "+conEnd2);
}
}
System.out.println("Interaction Name: "+obj.getName());
EList<Connector> connectors = obj.getOwnedConnectors();
System.out.println("Connectors: ");
if(!connectors.isEmpty()){
for (Connector behav: connectors)
{
EList<ConnectorEnd> property = behav.getEnds();
if(!property.isEmpty()) {
System.out.println("Connector Ends "+counterEnd);
for(ConnectorEnd ends:property) {
System.out.println("EndName:"+ends.getRole().getName());
}
counterEnd++;
}
}
}
模型加载器Class(助手Class)
public class ModelLoader {
private final ResourceSet RESOURCE_SET;
public ModelLoader() {
RESOURCE_SET = new ResourceSetImpl();
}
/**
* A method that converts input path to uri and return uri.
*
* @param path
* @return uri
* @throws Exception
*/
public String getFileURI(String path) throws Exception {
File f = new File(path);
String uri = f.toURI().toString();
return uri;
}
/**
* A method that loads UML/Ecore model from input URI.
*
* @param uri:String
* @return model:Object
*/
public Object loadModel(String uri){
URI modelUri = URI.createURI(uri);
registerPackages(RESOURCE_SET);
registerResourceFactories();
String relPath = null;
try {
relPath = new File(".").getCanonicalPath();
} catch (IOException e1) {
e1.printStackTrace();
}
URIConverter.URI_MAP.put(URI.createURI("platform:/plugin/org.eclipse.uml2.uml/"),
URI.createURI("jar:file:"+
relPath+"/EMF_Lib/org.eclipse.uml2.uml_3.1.2.v201010261927.jar! /"));
Resource resource = null;
try {
resource = RESOURCE_SET.getResource(modelUri, true);
}
catch (Exception e) {
e.printStackTrace();
}
Object result;
Model _model = (Model) EcoreUtil.getObjectByType(resource.getContents(),
UMLPackage.Literals.MODEL);
result = _model;
// org.eclipse.uml2.uml.Package _model =
//
(org.eclipse.uml2.uml.Package)EcoreUtil.getObjectByType(resource.getContents(),
// UMLPackage.Literals.PACKAGE);
if (_model == null) {
result = resource.getContents().get(0);
}
return result;
}
public void loadResources(){
registerPackages(RESOURCE_SET);
registerResourceFactories();
String relPath = null;
try {
relPath = new File(".").getCanonicalPath();
} catch (IOException e1) {
e1.printStackTrace();
}
URIConverter.URI_MAP.put(URI.createURI("platform:/plugin/org.eclipse.uml2.uml/"),
URI.createURI("jar:file:"+
relPath+"/EMF_Lib/org.eclipse.uml2.uml_3.1.2.v201010261927.jar!/"));
}
private void registerResourceFactories()
{
Map extensionFactoryMap =
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap();
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(Ecore2XMLResource.FILE_EXTENSION,
Ecore2XMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UML22UMLResource.FILE_EXTENSION,
UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(UMLResource.FILE_EXTENSION,
UML22UMLResource.Factory.INSTANCE);
extensionFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,new
XMIResourceFactoryImpl());
}
private void registerPackages(ResourceSet resourceSet)
{
Map packageRegistry = resourceSet.getPackageRegistry();
packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
packageRegistry.put(Ecore2XMLPackage.eNS_URI, Ecore2XMLPackage.eINSTANCE);
packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
packageRegistry .put(UML212UMLResource.UML_METAMODEL_NS_URI,
UMLPackage.eINSTANCE);
//for RSA
packageRegistry.put("http://www.eclipse.org/uml2/2.0.0/UML",UMLPackage.eINSTANCE);
//for Papyrus
packageRegistry.put("http://www.eclipse.org/uml2/5.0.0/UML",UMLPackage.eINSTANCE);
}
}