Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363012

Summary: I just got null model
Product: [Modeling] MDT.UML2 Reporter: Amy <xiyan_cao>
Component: CoreAssignee: UML2 Inbox <mdt-uml2-inbox>
Status: RESOLVED WORKSFORME QA Contact:
Severity: normal    
Priority: P2 CC: Kenn.Hussey
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Amy CLA 2011-11-07 00:23:23 EST
Build Identifier: Eclipse v3.3

Anyone can help me? I spent a lot time on this. I am still confused.
I used to use EMF to parse xmi from RSA 7.0, which used uml2.2.0.0. It works when I used Model model = (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.eINSTANCE.getModel()); But now the RSA is updated to 7.5. So the uml2 used in RSA is uml2.1.0. Now I just got null when I use Model model = (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.eINSTANCE.getModel()); 

Reproducible: Always
Comment 1 Amy CLA 2011-11-07 00:24:46 EST
This is the code for parsing xmi, which use uml 2.1.0.
public void processXMI2USE(String inputFile, String outputFile,String batFile){
		try {
			UMLPackage.eINSTANCE.getName();
			System.out.println(inputFile);
			//register XML resource factory
			ResourceSet resourceSet = new ResourceSetImpl(); 
			
	System.out.println("resourceSet "+resourceSet);
			
			resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
					UMLPackage.eINSTANCE);
			System.out.println("UMLPackage.eNS_URI "+UMLPackage.eNS_URI+" UMLPackage.eINSTANCE "+UMLPackage.eINSTANCE);		
			Map extensionToFactoryMap = resourceSet.getResourceFactoryRegistry()
					.getExtensionToFactoryMap();
			extensionToFactoryMap.put(UMLResource.FILE_EXTENSION,
					UMLResource.Factory.INSTANCE);
			extensionToFactoryMap.put("ecore", new EcoreResourceFactoryImpl());
			extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,
					new XMIResourceFactoryImpl());
		
			
			 Iterator it = extensionToFactoryMap.entrySet().iterator();  
			 while (it.hasNext()) {    
				 Map.Entry pairs = (Map.Entry)it.next();     
				 System.out.println(pairs.getKey() + " = " + pairs.getValue());  
			    } 
			
			// Load a profile from XMI
			Resource inputResource = resourceSet.createResource(URI
					.createFileURI(inputFile));
			try {
				inputResource.load(null);
			} catch (IOException exception) {
				//exception.printStackTrace();
			}
			
			System.out.println("inputResource "+inputResource);
			// Load a model from XMI
		

			URI uri = URI.createFileURI(inputFile);
			Resource resource = resourceSet.getResource(uri, true);
			//getObjectsByType Returns a collection of objects of the specified type.
			// Returns the registry used for looking up a package based namespace.
			//the registry is like a map, so it can use put(key, value)
			System.out.println(resource);
			//resource.getContents() is a collection to check
			//the type of object to find
			//the returned value of method getObjectByType is the first object or a collection
			//of object of the specified type
		/*	Iterator <EObject> iter = resource.getContents().iterator();
			while(iter.hasNext()){
				Object obj = iter.next();
				
				System.out.println("obj "+obj.getClass().getName());
			}*/
			
			//output:resource contents [org.eclipse.uml2.uml.internal.impl.PackageImpl@181ed9e
			//(name: Blank Package, visibility: <unset>)]
			System.out.println("resource contents "+(ModelImpl)resource.getContents());
			
			System.out.println(UMLPackage.eINSTANCE.getModel());
			
			Model model = (Model) EcoreUtil.getObjectByType(resource.getContents(),
					UMLPackage.eINSTANCE.getModel());

			System.out.println(model);
			performTransform(model, outputFile, batFile, "xmi21");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
Comment 2 Ed Merks CLA 2011-11-07 03:54:42 EST
I'll assign this to the UML2 project
Comment 3 Kenn Hussey CLA 2011-11-07 09:17:52 EST
Are you trying to deserialize a model that was created with the older version of UML2? Are you able to open that model using the (open source) UML editor?
Comment 4 Kenn Hussey CLA 2012-06-21 11:44:45 EDT
Note that there are new utilities in UML2 4.0 which should make it much easier to programmatically load resources based on older versions of UML. Try using UMLUtil.init(ResourceSet) or UMLResourcesUtil.init(ResourceSet) to initialize a new resource set and then just load your resource, something like this:

ResourceSet resourceSet = UMLResourcesUtil.init(new ResourceSetImpl());
Resource resource = resourceSet.getResource(URI.createFileURI(inputFile), true);

...

Given that I and others have successfully used these utilities (and the open source UML editor) to open models from a myriad of older UML and XMI versions, I'm going to mark this bug as "works for me" unless/until you can verify that these utilities don't work for you and/or provide a sample resource that demonstrates the problem.