Transform exiting model and save to .uml [message #897362] |
Mon, 23 July 2012 14:44  |
Eclipse User |
|
|
|
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 #897377 is a reply to message #897370] |
Mon, 23 July 2012 16:23  |
Eclipse User |
|
|
|
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
>
|
|
|
Powered by
FUDForum. Page generated in 0.25585 seconds