| How to read UML elements from a loaded file [message #699012] |
Wed, 20 July 2011 12:21  |
wws_ques wws_ques 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
|
|
|