Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » How to read UML elements from a loaded file
How to read UML elements from a loaded file [message #699012] Wed, 20 July 2011 16:21 Go to next message
wws_ques wws_ques is currently offline wws_ques wws_quesFriend
Messages: 64
Registered: July 2009
Member
I tried to read the model elements from the fowlloing .uml file:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML">
<uml:Model xmi:id="_6HP2ALLqEeC4NenKT57Ftw" name="MyFirstModel"/>
<uml:Class xmi:id="_6HP2AbLqEeC4NenKT57Ftw" name="University">
<ownedAttribute xmi:id="_6HP2ArLqEeC4NenKT57Ftw" name="dept_end" association="_6HP2BrLqEeC4NenKT57Ftw"/>
</uml:Class>
<uml:Class xmi:id="_6HP2A7LqEeC4NenKT57Ftw" name="Department">
<ownedAttribute xmi:id="_6HP2BLLqEeC4NenKT57Ftw" name="address" type="_6HP2A7LqEeC4NenKT57Ftw"/>
<ownedAttribute xmi:id="_6HP2BbLqEeC4NenKT57Ftw" association="_6HP2BrLqEeC4NenKT57Ftw"/>
</uml:Class>
<uml:Association xmi:id="_6HP2BrLqEeC4NenKT57Ftw" name="Owns" memberEnd="_6HP2ArLqEeC4NenKT57Ftw _6HP2BbLqEeC4NenKT57Ftw"/>
</xmi:XMI>

which was created from an in-memory model. To read the model, I have the following code:

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml", new UMLResourceFactoryImpl());
URI uri = URI.createURI("file:/C:/projects/Myself/MyUMLProject/src/out.uml");

Resource resource = resourceSet.getResource(uri,true);

System.out.println(((Model)resource.getContents().get(0)).toString());


But the last statment gives me the following runtime error:

Exception in thread "main" java.lang.ClassCastException: org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl
at ReadFile.main(ReadFile.java:30)

But when I save the out.uml using a program, the program calls resource.getContents().add(model) first. So, why I cannot get the first model element after loading out.uml. Also, is there any API which allows me to read all instance of a specific element such as all Class' instances and Asscoaition's instances in a .uml file after it is loaded. Thanks.
wuwei
Re: How to read UML elements from a loaded file [message #699020 is a reply to message #699012] Wed, 20 July 2011 16:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<ul>
<li class="toclevel-1"><a
href="http://wiki.eclipse.org/MDT-UML2-FAQ#What.27s_required_to_load_a_UML_.28.uml.29_resource_from_a_standalone_application.3F"><span
class="tocnumber">6</span> <span class="toctext">What's
required to load a UML (.uml) resource from a standalone
application?</span></a></li>
</ul>
<br>
<br>
On 20/07/2011 9:21 AM, Wuwei wrote:
<blockquote cite="mid:j06uno$sfe$1@news.eclipse.org" type="cite">I
tried to read the model elements from the fowlloing .uml file:
<br>
<br>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<br>
&lt;xmi:XMI xmi:version="2.1"
xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://schema.omg.org/spec/XMI/2.1">"http://schema.omg.org/spec/XMI/2.1"</a>
xmlns:uml=<a class="moz-txt-link-rfc2396E" href="http://www.eclipse.org/uml2/3.0.0/UML">"http://www.eclipse.org/uml2/3.0.0/UML"</a>&gt;
<br>
 &lt;uml:Model xmi:id="_6HP2ALLqEeC4NenKT57Ftw"
name="MyFirstModel"/&gt;
<br>
 &lt;uml:Class xmi:id="_6HP2AbLqEeC4NenKT57Ftw"
name="University"&gt;
<br>
   &lt;ownedAttribute xmi:id="_6HP2ArLqEeC4NenKT57Ftw"
name="dept_end" association="_6HP2BrLqEeC4NenKT57Ftw"/&gt;
<br>
 &lt;/uml:Class&gt;
<br>
 &lt;uml:Class xmi:id="_6HP2A7LqEeC4NenKT57Ftw"
name="Department"&gt;
<br>
   &lt;ownedAttribute xmi:id="_6HP2BLLqEeC4NenKT57Ftw"
name="address" type="_6HP2A7LqEeC4NenKT57Ftw"/&gt;
<br>
   &lt;ownedAttribute xmi:id="_6HP2BbLqEeC4NenKT57Ftw"
association="_6HP2BrLqEeC4NenKT57Ftw"/&gt;
<br>
 &lt;/uml:Class&gt;
<br>
 &lt;uml:Association xmi:id="_6HP2BrLqEeC4NenKT57Ftw" name="Owns"
memberEnd="_6HP2ArLqEeC4NenKT57Ftw _6HP2BbLqEeC4NenKT57Ftw"/&gt;
<br>
&lt;/xmi:XMI&gt;
<br>
<br>
which was created from an in-memory model. To read the model, I
have the following code:
<br>
<br>
ResourceSet resourceSet = new ResourceSetImpl();
<br>
       
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
new UMLResourceFactoryImpl());
<br>
        URI uri =
URI.createURI(<a class="moz-txt-link-rfc2396E" href="file:/C:/projects/Myself/MyUMLProject/src/out.uml">"file:/C:/projects/Myself/MyUMLProject/src/out.uml"</a>);
    
<br>
        Resource resource = resourceSet.getResource(uri,true);
<br>
        <br>
       
System.out.println(((Model)resource.getContents().get(0)).toString());
<br>
    
<br>
<br>
But the last statment gives me the following runtime error:
<br>
<br>
Exception in thread "main" java.lang.ClassCastException:
org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl
<br>
    at ReadFile.main(ReadFile.java:30)
<br>
<br>
But when I save the out.uml using a program, the program calls
resource.getContents().add(model) first. So, why I cannot get the
first model element after loading out.uml. Also, is there any API
which allows me to read all instance of a specific element such as
all Class' instances and Asscoaition's instances in a .uml file
after it is loaded. Thanks.
<br>
wuwei    <br>
</blockquote>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to read UML elements from a loaded file [message #699070 is a reply to message #699020] Wed, 20 July 2011 19:02 Go to previous messageGo to next message
wws_ques wws_ques is currently offline wws_ques wws_quesFriend
Messages: 64
Registered: July 2009
Member
Thanks for the update. I get confused about how to use that code.
what does the following statement do?

URI uri = URI.createURI("jar:file:/C:/eclipse/plugins/org.eclipse.uml2.uml.resources_2.0.0.v200606221411.jar!/");

In my previous program, it gives the file to be loaded? Where should I provide the .uml file to be loaded in this case.

Thanks.
Wuwei


Ed Merks wrote on Wed, 20 July 2011 12:28
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<ul>
<li class="toclevel-1"><a
href="http://wiki.eclipse.org/MDT-UML2-FAQ#What.27s_required_to_load_a_UML_.28.uml.29_resource_from_a_standalone_application.3F"><span
class="tocnumber">6</span> <span class="toctext">What's
required to load a UML (.uml) resource from a standalone
application?</span></a></li>
</ul>
<br>
<br>
On 20/07/2011 9:21 AM, Wuwei wrote:
<blockquote cite="mid:j06uno$sfe$1@news.eclipse.org" type="cite">I
tried to read the model elements from the fowlloing .uml file:
<br>
<br>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<br>
&lt;xmi:XMI xmi:version="2.1"
xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://schema.omg.org/spec/XMI/2.1">"http://schema.omg.org/spec/XMI/2.1"</a>
xmlns:uml=<a class="moz-txt-link-rfc2396E" href="http://www.eclipse.org/uml2/3.0.0/UML">"http://www.eclipse.org/uml2/3.0.0/UML"</a>&gt;
<br>
 &lt;uml:Model xmi:id="_6HP2ALLqEeC4NenKT57Ftw"
name="MyFirstModel"/&gt;
<br>
 &lt;uml:Class xmi:id="_6HP2AbLqEeC4NenKT57Ftw"
name="University"&gt;
<br>
   &lt;ownedAttribute xmi:id="_6HP2ArLqEeC4NenKT57Ftw"
name="dept_end" association="_6HP2BrLqEeC4NenKT57Ftw"/&gt;
<br>
 &lt;/uml:Class&gt;
<br>
 &lt;uml:Class xmi:id="_6HP2A7LqEeC4NenKT57Ftw"
name="Department"&gt;
<br>
   &lt;ownedAttribute xmi:id="_6HP2BLLqEeC4NenKT57Ftw"
name="address" type="_6HP2A7LqEeC4NenKT57Ftw"/&gt;
<br>
   &lt;ownedAttribute xmi:id="_6HP2BbLqEeC4NenKT57Ftw"
association="_6HP2BrLqEeC4NenKT57Ftw"/&gt;
<br>
 &lt;/uml:Class&gt;
<br>
 &lt;uml:Association xmi:id="_6HP2BrLqEeC4NenKT57Ftw" name="Owns"
memberEnd="_6HP2ArLqEeC4NenKT57Ftw _6HP2BbLqEeC4NenKT57Ftw"/&gt;
<br>
&lt;/xmi:XMI&gt;
<br>
<br>
which was created from an in-memory model. To read the model, I
have the following code:
<br>
<br>
ResourceSet resourceSet = new ResourceSetImpl();
<br>
       
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
new UMLResourceFactoryImpl());
<br>
        URI uri =
URI.createURI(<a class="moz-txt-link-rfc2396E" href="file:/C:/projects/Myself/MyUMLProject/src/out.uml">"file:/C:/projects/Myself/MyUMLProject/src/out.uml"</a>);
    
<br>
        Resource resource = resourceSet.getResource(uri,true);
<br>
        <br>
       
System.out.println(((Model)resource.getContents().get(0)).toString());
<br>
    
<br>
<br>
But the last statment gives me the following runtime error:
<br>
<br>
Exception in thread "main" java.lang.ClassCastException:
org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl
<br>
    at ReadFile.main(ReadFile.java:30)
<br>
<br>
But when I save the out.uml using a program, the program calls
resource.getContents().add(model) first. So, why I cannot get the
first model element after loading out.uml. Also, is there any API
which allows me to read all instance of a specific element such as
all Class' instances and Asscoaition's instances in a .uml file
after it is loaded. Thanks.
<br>
wuwei    <br>
</blockquote>
</body>
</html>

Re: How to read UML elements from a loaded file [message #699074 is a reply to message #699070] Wed, 20 July 2011 19:14 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Comments below.

On 20/07/2011 12:02 PM, Wuwei wrote:
> Thanks for the update. I get confused about how to use that code.
It's just initialization code.
> what does the following statement do?
>
> URI uri =
> URI.createURI("jar:file:/C:/eclipse/plugins/org.eclipse.uml2.uml.resources_2.0.0.v200606221411.jar!/");
It's the base URI for locating things that are needed within the jars.
>
> In my previous program, it gives the file to be loaded?
What you have is fine. You just need to be sure you've initialized the
environment properly.
> Where should I provide the .uml file to be loaded in this case.
>
> Thanks.
> Wuwei
>
>
> Ed Merks wrote on Wed, 20 July 2011 12:28
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> <html>
>> <head>
>> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
>> </head>
>> <body bgcolor="#ffffff" text="#000000">
>> <ul>
>> <li class="toclevel-1"><a
>> href="http://wiki.eclipse.org/MDT-UML2-FAQ#What.27s_required_to_load_a_UML_.28.uml.29_resource_from_a_standalone_application.3F"><span
>>
>> class="tocnumber">6</span> <span class="toctext">What's
>> required to load a UML (.uml) resource from a standalone
>> application?</span></a></li>
>> </ul>
>> <br>
>> <br>
>> On 20/07/2011 9:21 AM, Wuwei wrote:
>> <blockquote cite="mid:j06uno$sfe$1@news.eclipse.org" type="cite">I
>> tried to read the model elements from the fowlloing .uml file:
>> <br>
>> <br>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <br>
>> <xmi:XMI xmi:version="2.1"
>> xmlns:xmi=<a class="moz-txt-link-rfc2396E"
>> href="http://schema.omg.org/spec/XMI/2.1">"http://schema.omg.org/spec/XMI/2.1"</a>
>> xmlns:uml=<a class="moz-txt-link-rfc2396E"
>> href="http://www.eclipse.org/uml2/3.0.0/UML">"http://www.eclipse.org/uml2/3.0.0/UML"</a>>
>> <br>
>> <uml:Model xmi:id="_6HP2ALLqEeC4NenKT57Ftw"
>> name="MyFirstModel"/>
>> <br>
>> <uml:Class xmi:id="_6HP2AbLqEeC4NenKT57Ftw"
>> name="University">
>> <br>
>> <ownedAttribute xmi:id="_6HP2ArLqEeC4NenKT57Ftw"
>> name="dept_end" association="_6HP2BrLqEeC4NenKT57Ftw"/>
>> <br>
>> </uml:Class>
>> <br>
>> <uml:Class xmi:id="_6HP2A7LqEeC4NenKT57Ftw"
>> name="Department">
>> <br>
>> <ownedAttribute xmi:id="_6HP2BLLqEeC4NenKT57Ftw"
>> name="address" type="_6HP2A7LqEeC4NenKT57Ftw"/>
>> <br>
>> <ownedAttribute xmi:id="_6HP2BbLqEeC4NenKT57Ftw"
>> association="_6HP2BrLqEeC4NenKT57Ftw"/>
>> <br>
>> </uml:Class>
>> <br>
>> <uml:Association xmi:id="_6HP2BrLqEeC4NenKT57Ftw" name="Owns"
>> memberEnd="_6HP2ArLqEeC4NenKT57Ftw _6HP2BbLqEeC4NenKT57Ftw"/>
>> <br>
>> </xmi:XMI>
>> <br>
>> <br>
>> which was created from an in-memory model. To read the model, I
>> have the following code:
>> <br>
>> <br>
>> ResourceSet resourceSet = new ResourceSetImpl();
>> <br>
>>
>>
>> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
>> new UMLResourceFactoryImpl());
>> <br>
>> URI uri =
>> URI.createURI(<a class="moz-txt-link-rfc2396E"
>> href="file:/C:/projects/Myself/MyUMLProject/src/out.uml">"file:/C:/projects/Myself/MyUMLProject/src/out.uml"</a>);
>>
>> <br>
>> Resource resource = resourceSet.getResource(uri,true);
>> <br>
>> <br>
>>
>>
>> System.out.println(((Model)resource.getContents().get(0)).toString());
>> <br>
>>
>> <br>
>> <br>
>> But the last statment gives me the following runtime error:
>> <br>
>> <br>
>> Exception in thread "main" java.lang.ClassCastException:
>> org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl
>> <br>
>> at ReadFile.main(ReadFile.java:30)
>> <br>
>> <br>
>> But when I save the out.uml using a program, the program calls
>> resource.getContents().add(model) first. So, why I cannot get the
>> first model element after loading out.uml. Also, is there any API
>> which allows me to read all instance of a specific element such as
>> all Class' instances and Asscoaition's instances in a .uml file
>> after it is loaded. Thanks.
>> <br>
>> wuwei <br>
>> </blockquote>
>> </body>
>> </html>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to read UML elements from a loaded file [message #699084 is a reply to message #699074] Wed, 20 July 2011 19:34 Go to previous messageGo to next message
wws_ques wws_ques is currently offline wws_ques wws_quesFriend
Messages: 64
Registered: July 2009
Member
I still have a question. So to be sure, I want to verify the following statementS.
My comment starts with // in CAPITALS.

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
Map uriMap = resourceSet.getURIConverter().getURIMap();
URI uri = URI.createURI("jar:file:/D:/Eclipse_Related_SW/Eclipse_Plugins/UML2_v3.2/eclipse/plugins/org.eclipse.uml2.uml.source_3.2.0.v201105231350.jar!/");

// I CHANGE THE ABOVE STATEMENT GIVING THE LOCATON OF THE BASE URI, RIGHT ?

uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));

// NO CHANGES here, right?

/* resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml", new UMLResourceFactoryImpl());
URI uri = URI.createURI("file:/D:/projects/Myself/MyUMLProject/src/Superstructure.uml");
*/
Resource resource = resourceSet.createResource(uri);

// SINCE I COMMENT OUT THE TWO STATEMENTS ABOVE TO LOAD THE UML FILE, HOW CAN I DO HERE?

Thanks.



Ed Merks wrote on Wed, 20 July 2011 15:14
Comments below.

On 20/07/2011 12:02 PM, Wuwei wrote:
> Thanks for the update. I get confused about how to use that code.
It's just initialization code.
> what does the following statement do?
>
> URI uri =
> URI.createURI("jar:file:/C:/eclipse/plugins/org.eclipse.uml2.uml.resources_2.0.0.v200606221411.jar!/");
It's the base URI for locating things that are needed within the jars.
>
> In my previous program, it gives the file to be loaded?
What you have is fine. You just need to be sure you've initialized the
environment properly.
> Where should I provide the .uml file to be loaded in this case.
>
> Thanks.
> Wuwei
>
>
> Ed Merks wrote on Wed, 20 July 2011 12:28
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> <html>
>> <head>
>> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
>> </head>
>> <body bgcolor="#ffffff" text="#000000">
>> <ul>
>> <li class="toclevel-1"><a
>> href="http://wiki.eclipse.org/MDT-UML2-FAQ#What.27s_required_to_load_a_UML_.28.uml.29_resource_from_a_standalone_application.3F"><span
>>
>> class="tocnumber">6</span> <span class="toctext">What's
>> required to load a UML (.uml) resource from a standalone
>> application?</span></a></li>
>> </ul>
>> <br>
>> <br>
>> On 20/07/2011 9:21 AM, Wuwei wrote:
>> <blockquote cite="mid:j06uno$sfe$1@news.eclipse.org" type="cite">I
>> tried to read the model elements from the fowlloing .uml file:
>> <br>
>> <br>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <br>
>> <xmi:XMI xmi:version="2.1"
>> xmlns:xmi=<a class="moz-txt-link-rfc2396E"
>> href="http://schema.omg.org/spec/XMI/2.1">"http://schema.omg.org/spec/XMI/2.1"</a>
>> xmlns:uml=<a class="moz-txt-link-rfc2396E"
>> href="http://www.eclipse.org/uml2/3.0.0/UML">"http://www.eclipse.org/uml2/3.0.0/UML"</a>>
>> <br>
>> <uml:Model xmi:id="_6HP2ALLqEeC4NenKT57Ftw"
>> name="MyFirstModel"/>
>> <br>
>> <uml:Class xmi:id="_6HP2AbLqEeC4NenKT57Ftw"
>> name="University">
>> <br>
>> <ownedAttribute xmi:id="_6HP2ArLqEeC4NenKT57Ftw"
>> name="dept_end" association="_6HP2BrLqEeC4NenKT57Ftw"/>
>> <br>
>> </uml:Class>
>> <br>
>> <uml:Class xmi:id="_6HP2A7LqEeC4NenKT57Ftw"
>> name="Department">
>> <br>
>> <ownedAttribute xmi:id="_6HP2BLLqEeC4NenKT57Ftw"
>> name="address" type="_6HP2A7LqEeC4NenKT57Ftw"/>
>> <br>
>> <ownedAttribute xmi:id="_6HP2BbLqEeC4NenKT57Ftw"
>> association="_6HP2BrLqEeC4NenKT57Ftw"/>
>> <br>
>> </uml:Class>
>> <br>
>> <uml:Association xmi:id="_6HP2BrLqEeC4NenKT57Ftw" name="Owns"
>> memberEnd="_6HP2ArLqEeC4NenKT57Ftw _6HP2BbLqEeC4NenKT57Ftw"/>
>> <br>
>> </xmi:XMI>
>> <br>
>> <br>
>> which was created from an in-memory model. To read the model, I
>> have the following code:
>> <br>
>> <br>
>> ResourceSet resourceSet = new ResourceSetImpl();
>> <br>
>>
>>
>> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
>> new UMLResourceFactoryImpl());
>> <br>
>> URI uri =
>> URI.createURI(<a class="moz-txt-link-rfc2396E"
>> href="file:/C:/projects/Myself/MyUMLProject/src/out.uml">"file:/C:/projects/Myself/MyUMLProject/src/out.uml"</a>);
>>
>> <br>
>> Resource resource = resourceSet.getResource(uri,true);
>> <br>
>> <br>
>>
>>
>> System.out.println(((Model)resource.getContents().get(0)).toString());
>> <br>
>>
>> <br>
>> <br>
>> But the last statment gives me the following runtime error:
>> <br>
>> <br>
>> Exception in thread "main" java.lang.ClassCastException:
>> org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl
>> <br>
>> at ReadFile.main(ReadFile.java:30)
>> <br>
>> <br>
>> But when I save the out.uml using a program, the program calls
>> resource.getContents().add(model) first. So, why I cannot get the
>> first model element after loading out.uml. Also, is there any API
>> which allows me to read all instance of a specific element such as
>> all Class' instances and Asscoaition's instances in a .uml file
>> after it is loaded. Thanks.
>> <br>
>> wuwei <br>
>> </blockquote>
>> </body>
>> </html>
>
>

Re: How to read UML elements from a loaded file [message #699097 is a reply to message #699084] Wed, 20 July 2011 20:08 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Comments below.

On 20/07/2011 12:34 PM, Wuwei wrote:
> I still have a question. So to be sure, I want to verify the following
> statementS.
> My comment starts with // in CAPITALS.
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE);
>
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION,
> UMLResource.Factory.INSTANCE);
> Map uriMap = resourceSet.getURIConverter().getURIMap();
> URI uri =
> URI.createURI("jar:file:/D:/Eclipse_Related_SW/Eclipse_Plugins/UML2_v3.2/eclipse/plugins/org.eclipse.uml2.uml.source_3.2.0.v201105231350.jar!/");
>
> // I CHANGE THE ABOVE STATEMENT GIVING THE LOCATON OF THE BASE URI,
> RIGHT ?
Yes, you'd better find that jar's location/version on your machine.
>
> uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP),
> uri.appendSegment("libraries").appendSegment(""));
> uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP),
> uri.appendSegment("metamodels").appendSegment(""));
> uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP),
> uri.appendSegment("profiles").appendSegment(""));
>
> // NO CHANGES here, right?
Yep.
>
> /*
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
> new UMLResourceFactoryImpl());
> URI uri =
> URI.createURI("file:/D:/projects/Myself/MyUMLProject/src/Superstructure.uml");
> */
> Resource resource = resourceSet.createResource(uri);
>
> // SINCE I COMMENT OUT THE TWO STATEMENTS ABOVE TO LOAD THE UML FILE,
> HOW CAN I DO HERE?
What you had before should work nicely:

URI uri =
URI.createURI("file:/C:/projects/Myself/MyUMLProject/src/out.uml");
Resource resource = resourceSet.getResource(uri,true);

With the UML package properly registered, it should create UML instances
for you.
>
> Thanks.
>
>
>
> Ed Merks wrote on Wed, 20 July 2011 15:14
>> Comments below.
>>
>> On 20/07/2011 12:02 PM, Wuwei wrote:
>> > Thanks for the update. I get confused about how to use that code.
>> It's just initialization code.
>> > what does the following statement do?
>> >
>> > URI uri = >
>> URI.createURI("jar:file:/C:/eclipse/plugins/org.eclipse.uml2.uml.resources_2.0.0.v200606221411.jar!/");
>> It's the base URI for locating things that are needed within the jars.
>> >
>> > In my previous program, it gives the file to be loaded? What you
>> have is fine. You just need to be sure you've initialized the
>> environment properly.
>> > Where should I provide the .uml file to be loaded in this case.
>> >
>> > Thanks.
>> > Wuwei
>> >
>> >
>> > Ed Merks wrote on Wed, 20 July 2011 12:28
>> >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> >> <html>
>> >> <head>
>> >> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
>> >> </head>
>> >> <body bgcolor="#ffffff" text="#000000">
>> >> <ul>
>> >> <li class="toclevel-1"><a
>> >>
>> href="http://wiki.eclipse.org/MDT-UML2-FAQ#What.27s_required_to_load_a_UML_.28.uml.29_resource_from_a_standalone_application.3F"><span
>> >>
>> >> class="tocnumber">6</span> <span class="toctext">What's
>> >> required to load a UML (.uml) resource from a standalone
>> >> application?</span></a></li>
>> >> </ul>
>> >> <br>
>> >> <br>
>> >> On 20/07/2011 9:21 AM, Wuwei wrote:
>> >> <blockquote cite="mid:j06uno$sfe$1@news.eclipse.org" type="cite">I
>> >> tried to read the model elements from the fowlloing .uml file:
>> >> <br>
>> >> <br>
>> >> <?xml version="1.0" encoding="UTF-8"?>
>> >> <br>
>> >> <xmi:XMI xmi:version="2.1"
>> >> xmlns:xmi=<a class="moz-txt-link-rfc2396E" >>
>> href="http://schema.omg.org/spec/XMI/2.1">"http://schema.omg.org/spec/XMI/2.1"</a>
>> >> xmlns:uml=<a class="moz-txt-link-rfc2396E" >>
>> href="http://www.eclipse.org/uml2/3.0.0/UML">"http://www.eclipse.org/uml2/3.0.0/UML"</a>>
>> >> <br>
>> >> <uml:Model xmi:id="_6HP2ALLqEeC4NenKT57Ftw"
>> >> name="MyFirstModel"/>
>> >> <br>
>> >> <uml:Class xmi:id="_6HP2AbLqEeC4NenKT57Ftw"
>> >> name="University">
>> >> <br>
>> >> <ownedAttribute xmi:id="_6HP2ArLqEeC4NenKT57Ftw"
>> >> name="dept_end" association="_6HP2BrLqEeC4NenKT57Ftw"/>
>> >> <br>
>> >> </uml:Class>
>> >> <br>
>> >> <uml:Class xmi:id="_6HP2A7LqEeC4NenKT57Ftw"
>> >> name="Department">
>> >> <br>
>> >> <ownedAttribute xmi:id="_6HP2BLLqEeC4NenKT57Ftw"
>> >> name="address" type="_6HP2A7LqEeC4NenKT57Ftw"/>
>> >> <br>
>> >> <ownedAttribute xmi:id="_6HP2BbLqEeC4NenKT57Ftw"
>> >> association="_6HP2BrLqEeC4NenKT57Ftw"/>
>> >> <br>
>> >> </uml:Class>
>> >> <br>
>> >> <uml:Association xmi:id="_6HP2BrLqEeC4NenKT57Ftw" name="Owns"
>> >> memberEnd="_6HP2ArLqEeC4NenKT57Ftw _6HP2BbLqEeC4NenKT57Ftw"/>
>> >> <br>
>> >> </xmi:XMI>
>> >> <br>
>> >> <br>
>> >> which was created from an in-memory model. To read the model, I
>> >> have the following code:
>> >> <br>
>> >> <br>
>> >> ResourceSet resourceSet = new ResourceSetImpl();
>> >> <br>
>> >>
>> >> >>
>> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("uml",
>> >> new UMLResourceFactoryImpl());
>> >> <br>
>> >> URI uri =
>> >> URI.createURI(<a class="moz-txt-link-rfc2396E" >>
>> href="file:/C:/projects/Myself/MyUMLProject/src/out.uml">"file:/C:/projects/Myself/MyUMLProject/src/out.uml"</a>);
>> >>
>> >> <br>
>> >> Resource resource = resourceSet.getResource(uri,true);
>> >> <br>
>> >> <br>
>> >>
>> >> >>
>> System.out.println(((Model)resource.getContents().get(0)).toString());
>> >> <br>
>> >>
>> >> <br>
>> >> <br>
>> >> But the last statment gives me the following runtime error:
>> >> <br>
>> >> <br>
>> >> Exception in thread "main" java.lang.ClassCastException:
>> >> org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl
>> >> <br>
>> >> at ReadFile.main(ReadFile.java:30)
>> >> <br>
>> >> <br>
>> >> But when I save the out.uml using a program, the program calls
>> >> resource.getContents().add(model) first. So, why I cannot
>> get the
>> >> first model element after loading out.uml. Also, is there
>> any API
>> >> which allows me to read all instance of a specific element
>> such as
>> >> all Class' instances and Asscoaition's instances in a .uml file
>> >> after it is loaded. Thanks.
>> >> <br>
>> >> wuwei <br>
>> >> </blockquote>
>> >> </body>
>> >> </html>
>> >
>> >
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to read UML elements from a loaded file [message #699106 is a reply to message #699097] Wed, 20 July 2011 20:41 Go to previous messageGo to next message
wws_ques wws_ques is currently offline wws_ques wws_quesFriend
Messages: 64
Registered: July 2009
Member
Thanks, Ed. It works. Now, is there any easy way to get
all instances of an element such as Class and Association
in the UML model? Right I iterate each element to check its
type via instanceof. Thanks.
Wuwei
Re: How to read UML elements from a loaded file [message #699113 is a reply to message #699106] Wed, 20 July 2011 21:13 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
I'm not so familiar with the UML2 model to know all the places that
Class and Association can be contained. Looking at the model definition
should help you answer that.


On 20/07/2011 1:41 PM, Wuwei wrote:
> Thanks, Ed. It works. Now, is there any easy way to get
> all instances of an element such as Class and Association
> in the UML model? Right I iterate each element to check its
> type via instanceof. Thanks.
> Wuwei


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:how to use the two parameters in validate methods
Next Topic:Adding a Double value to a Slot
Goto Forum:
  


Current Time: Thu Mar 28 18:56:20 GMT 2024

Powered by FUDForum. Page generated in 0.03261 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top