I searched a lot about reading UML diagrams from XMI using EMF Java Eclipse. I only found solutions to read class diagram but my task is to read Sequence diagram from XMI. Can anyone please help me out?
After a hectic efforts finally I found a working solution, You only need to put path of error-free UML diagram in the main class, Provided code will read all nodes and sub-nodes respectively. You can customize the code as per your requirements. This code can be used both for class diagram as well as sequence diagram.
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);
}
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++;
}
}
}
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);
}
}