Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Transform exiting model and save to .uml
Transform exiting model and save to .uml [message #897362] Mon, 23 July 2012 18:44 Go to next message
Florian Wartenberg is currently offline Florian WartenbergFriend
Messages: 25
Registered: July 2012
Junior Member
Hey guys I am currently working on a project where I try to generate Code from existing UML Models which contain class diagrams and state machines. Therefore I need to flatten and resolve history states at first. I get the Models as an XMI file (created with Topcased). Now I want to load the model transform it and save it again. The loading and transforming works fine but I have trouble saving my model I always get a NullPointerException

Exception in thread "main" java.lang.NullPointerException
	at Main.save(Main.java:54)
	at Main.main(Main.java:19)


I followed the Getting started with UML2 guide. Here is my code so far:

import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.mapping.ecore2xml.util.Ecore2XMLResource;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.internal.impl.StateImpl;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.uml2.uml.resource.XMI2UMLResource;

public class Main {
	
	public static void main(String[] args){
		Model m=new Main().getModel("Some Path");
        save(m, URI.createURI("Some Path").appendSegment("newModel").appendFileExtension(UMLResource.FILE_EXTENSION));
		//System.out.println(m.getName());
        System.out.println("Done.");
	}
	
	public Model getModel(String pathToModel) {
		URI typesUri = URI.createFileURI(pathToModel);
		ResourceSet set = new ResourceSetImpl();
	
		set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
		set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
		set.createResource(typesUri);
		Resource r = set.getResource(typesUri, true);
		
        Model m = (Model) EcoreUtil.getObjectByType(r.getContents(), UMLPackage.Literals.MODEL);
		
		return m;
	}		


	// Do some transformations on model


	protected static void save(org.eclipse.uml2.uml.Package package_, URI uri)
	{    	
        Resource resource = new ResourceSetImpl().createResource(uri);
        resource.getContents().add(package_);
        try {
            resource.save(null);
        } catch (IOException ioe)  {}
	}

}


I am thankful for any input regarding my problem!

regards

Florian
Re: Transform exiting model and save to .uml [message #897367 is a reply to message #897362] Mon, 23 July 2012 19:46 Go to previous messageGo to next message
Florian Wartenberg is currently offline Florian WartenbergFriend
Messages: 25
Registered: July 2012
Junior Member
Somehow I solved the problem I simply added a few lines inside the save method It now looks like this:

protected static void save(org.eclipse.uml2.uml.Package package_, URI uri)
	{    	
		Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		Map<String, Object> m = reg.getExtensionToFactoryMap();
		m.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
        Resource resource = new ResourceSetImpl().createResource(uri);
        resource.getContents().add(package_);
        try {
            resource.save(null);
        } catch (IOException ioe)  {}
	}


Cheers
Re: Transform exiting model and save to .uml [message #897370 is a reply to message #897367] Mon, 23 July 2012 19:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Florian,

If you're saving UML instances you should use UML2's resource factory.

On 23/07/2012 9:46 PM, Florian Wartenberg wrote:
> Somehow I solved the problem I simply added a few lines inside the
> save method It now looks like this:
>
>
> protected static void save(org.eclipse.uml2.uml.Package package_, URI
> uri)
> {
> Resource.Factory.Registry reg =
> Resource.Factory.Registry.INSTANCE;
> Map<String, Object> m = reg.getExtensionToFactoryMap();
> m.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new
> XMIResourceFactoryImpl());
> Resource resource = new ResourceSetImpl().createResource(uri);
> resource.getContents().add(package_);
> try {
> resource.save(null);
> } catch (IOException ioe) {}
> }
>
>
> Cheers


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Transform exiting model and save to .uml [message #897377 is a reply to message #897370] Mon, 23 July 2012 20:23 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Correctly configuring UML is really hard.

As of Juno just use UMLResourcesUtil.init().

Regards

Ed Willink

On 23/07/2012 20:59, Ed Merks wrote:
> Florian,
>
> If you're saving UML instances you should use UML2's resource factory.
>
> On 23/07/2012 9:46 PM, Florian Wartenberg wrote:
>> Somehow I solved the problem I simply added a few lines inside the
>> save method It now looks like this:
>>
>>
>> protected static void save(org.eclipse.uml2.uml.Package package_, URI
>> uri)
>> {
>> Resource.Factory.Registry reg =
>> Resource.Factory.Registry.INSTANCE;
>> Map<String, Object> m = reg.getExtensionToFactoryMap();
>> m.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new
>> XMIResourceFactoryImpl());
>> Resource resource = new ResourceSetImpl().createResource(uri);
>> resource.getContents().add(package_);
>> try {
>> resource.save(null);
>> } catch (IOException ioe) {}
>> }
>>
>>
>> Cheers
>
Previous Topic:EMF/XSD invalid xml
Next Topic:Definition and data editor
Goto Forum:
  


Current Time: Fri Apr 26 03:14:26 GMT 2024

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

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

Back to the top