Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » UML save to *.uml file
UML save to *.uml file [message #998692] Thu, 10 January 2013 14:15 Go to next message
Florian Wartenberg is currently offline Florian Wartenberg
Messages: 25
Registered: July 2012
Junior Member
I am currently using the following method to save my UML Model.

    public static void saveModel(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) {
        }
    }

It is working fine but I recently discovered that it is slightly different from *.uml files created with Papyrus or Topcased:

1. The File Header changes from
<?xml version="1.0" encoding="UTF-8"?>
<uml:Model 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" xmi:id="_qb8akM37EdqwVrslYOdUDA" name="eShopModel" viewpoint="">


to

<?xml version="1.0" encoding="ASCII"?>
<uml:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" name="eShopModel" viewpoint="">


2.
<subvertex xmi:type="uml:State" xmi:id="_mq2sED0qEeKGTJtY_dTOTg" name="Product Selection">

changes to
<subvertex xsi:type="uml:State" name="Product Selection"> 


Especially the lost id's and the different file encoding are problems.

Is there a chance to configure this?

regards

F. Wartenberg
Re: UML save to *.uml file [message #998706 is a reply to message #998692] Thu, 10 January 2013 14:34 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. Damus
Messages: 802
Registered: July 2009
Senior Member
Hi, Florian,

Does your code run in the context of an Eclipse instance? If so, then
you don't need to manage any resource factory registrations. If not,
then the UMLResourcesUtil::init(ResourceSet) method will set up the
proper resource factories and other configuration of your ResourceSet.
In particular, you are missing the registration of the UMLResource
implementation, which provides the UTF-8 encoding, XMI IDs, etc.

BTW, your code probably has a memory leak: You're putting the Package
into a resource in a resource set. These will all be retained in
memory by the static CacheAdapter, along with the entire contents of
the package. When working with UML models, you *must* always maintain
your UML resources explicitly in a ResourceSet and ensure that you
unload all of your UML resources and clear the adapters of your
ResourceSet when you have finished with them.

HTH,

Christian


On 2013-01-10 14:15:12 +0000, Florian Wartenberg said:

> I am currently using the following method to save my UML Model.
>
>
> public static void saveModel(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) {
> }
> }
>
> It is working fine but I recently discovered that it is slightly
> different from *.uml files created with Papyrus or Topcased:
>
> 1. The File Header changes from <?xml version="1.0" encoding="UTF-8"?>
> <uml:Model 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"
> xmi:id="_qb8akM37EdqwVrslYOdUDA" name="eShopModel" viewpoint="">
>
> to
> <?xml version="1.0" encoding="ASCII"?>
> <uml:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" name="eShopModel"
> viewpoint="">
>
> 2. <subvertex xmi:type="uml:State" xmi:id="_mq2sED0qEeKGTJtY_dTOTg"
> name="Product Selection">
> changes to
> <subvertex xsi:type="uml:State" name="Product Selection">
> Especially the lost id's and the different file encoding are problems.
>
> Is there a chance to configure this?
>
> regards
> F. Wartenberg
Re: UML save to *.uml file [message #999100 is a reply to message #998706] Fri, 11 January 2013 10:46 Go to previous messageGo to next message
Florian Wartenberg is currently offline Florian Wartenberg
Messages: 25
Registered: July 2012
Junior Member
I am running the following Eclipse Version: 4.3.0.v20120906-193403-7V7p-DQ72z-z-8U8IIz0Ennw4DFBv216MQ_3m27IU_Kz0
with the following UML SDK Version: 4.1.0.v20121217-0922

But I can't resolve the UMLResourcesUtil::init(ResourceSet) method? Any suggestions?

regards

Florian
Re: UML save to *.uml file [message #999110 is a reply to message #998692] Fri, 11 January 2013 11:10 Go to previous messageGo to next message
Florian Wartenberg is currently offline Florian Wartenberg
Messages: 25
Registered: July 2012
Junior Member
Ok I found the .jar added it to my build patch and it works. I guess I can use the init method to load my model as well?

regards

Florian
Re: UML save to *.uml file [message #999126 is a reply to message #999110] Fri, 11 January 2013 11:47 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4088
Registered: July 2009
Senior Member
Hi

The init method does no saving or loading. It just provides all (bar the
pathmap) declarations for a standalone config that would have been
available in an Eclipse config.

If you are going to accept UML from a diversity of namespaces, you need
the init for load too.

Regards

Ed Willink

On 11/01/2013 11:10, Florian Wartenberg wrote:
> Ok I found the .jar added it to my build patch and it works. I guess I
> can use the init method to load my model as well?
>
> regards
>
> Florian
Re: UML save to *.uml file [message #1000728 is a reply to message #999126] Tue, 15 January 2013 10:40 Go to previous messageGo to next message
Florian Wartenberg is currently offline Florian Wartenberg
Messages: 25
Registered: July 2012
Junior Member
I am now using this method to save the model as suggested here: http://www.eclipse.org/modeling/mdt/uml2/docs/articles/Getting_Started_with_UML2/article.html

    public static void saveModel(org.eclipse.uml2.uml.Package package_, URI uri) {
        ResourceSet resourceSet = new ResourceSetImpl();
        UMLResourcesUtil.init(resourceSet);		// MDT/UML2 4.0.0 (Juno)
        Resource resource = resourceSet.createResource(uri);
        resource.getContents().add(package_);

        try {
            resource.save(null);
        } catch (IOException ioe) {
        }
    }


But I always get this error:
Exception in thread "main" java.lang.NullPointerException
	at <packagename>.UMLModelSaver.saveModel(UMLModelSaver.java:36)


Line 36 is this line of Code:
resource.getContents().add(package_);


What am I not taking into account or overlooking?

Regards

Florian
Re: UML save to *.uml file [message #1000776 is a reply to message #1000728] Tue, 15 January 2013 12:19 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. Damus
Messages: 802
Registered: July 2009
Senior Member
Hi, Florian,

Your resource is null because the resource set couldn't create it.
Most likely it didn't find a suitable resource factory for the URI that
you specified. Because you initialized the resource set, probably the
URI doesn't have the *.uml file extension? What is the URI that you
are using?

HTH,

Christian


On 2013-01-15 10:40:35 +0000, Florian Wartenberg said:

> I am now using this method to save the model as suggested here:
> http://www.eclipse.org/modeling/mdt/uml2/docs/articles/Getting_Started_with_UML2/article.html
>
>
>
> public static void saveModel(org.eclipse.uml2.uml.Package package_,
> URI uri) {
> ResourceSet resourceSet = new ResourceSetImpl();
> UMLResourcesUtil.init(resourceSet); // MDT/UML2 4.0.0 (Juno)
> Resource resource = resourceSet.createResource(uri);
> resource.getContents().add(package_);
>
> try {
> resource.save(null);
> } catch (IOException ioe) {
> }
> }
>
>
> But I always get this error: Exception in thread "main"
> java.lang.NullPointerException
> at <packagename>.UMLModelSaver.saveModel(UMLModelSaver.java:36)
>
> Line 36 is this line of Code:
> resource.getContents().add(package_);
>
> What am I not taking into account or overlooking?
>
> Regards
>
> Florian
Re: UML save to *.uml file [message #1000788 is a reply to message #1000776] Tue, 15 January 2013 12:40 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4088
Registered: July 2009
Senior Member
Hi

Not necessarily. See
https://bugs.eclipse.org/bugs/show_bug.cgi?id=389542 and its attachment.

With 4.0.1, not even *.uml files can be created. Use a 4.0.2 maintenance
build. RC1 is now available.

Regards

Ed Willink

On 15/01/2013 12:19, Christian W. Damus wrote:
> Hi, Florian,
>
> Your resource is null because the resource set couldn't create it.
> Most likely it didn't find a suitable resource factory for the URI
> that you specified. Because you initialized the resource set,
> probably the URI doesn't have the *.uml file extension? What is the
> URI that you are using?
>
> HTH,
>
> Christian
>
>
> On 2013-01-15 10:40:35 +0000, Florian Wartenberg said:
>
>> I am now using this method to save the model as suggested here:
>> http://www.eclipse.org/modeling/mdt/uml2/docs/articles/Getting_Started_with_UML2/article.html
>>
>>
>>
>> public static void saveModel(org.eclipse.uml2.uml.Package
>> package_, URI uri) {
>> ResourceSet resourceSet = new ResourceSetImpl();
>> UMLResourcesUtil.init(resourceSet); // MDT/UML2 4.0.0
>> (Juno)
>> Resource resource = resourceSet.createResource(uri);
>> resource.getContents().add(package_);
>>
>> try {
>> resource.save(null);
>> } catch (IOException ioe) {
>> }
>> }
>>
>>
>> But I always get this error: Exception in thread "main"
>> java.lang.NullPointerException
>> at <packagename>.UMLModelSaver.saveModel(UMLModelSaver.java:36)
>>
>> Line 36 is this line of Code:
>> resource.getContents().add(package_);
>>
>> What am I not taking into account or overlooking?
>>
>> Regards
>>
>> Florian
>
>
Re: UML save to *.uml file [message #1000849 is a reply to message #1000788] Tue, 15 January 2013 14:39 Go to previous message
Florian Wartenberg is currently offline Florian Wartenberg
Messages: 25
Registered: July 2012
Junior Member
I load my model with this method:

    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 model = (Model) EcoreUtil.getObjectByType(r.getContents(), UMLPackage.Literals.MODEL);

        return model;
    }


Regards

Florian
Previous Topic:UML Statechart Flattening
Next Topic:How to save to .uml file using an older UML version?
Goto Forum:
  


Current Time: Wed Oct 22 00:07:29 GMT 2014

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

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