Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Load and save a model with references
Load and save a model with references [message #478428] Thu, 06 August 2009 09:41 Go to next message
No real name is currently offline No real nameFriend
Messages: 61
Registered: July 2009
Member
Hi,

I have two UML2-models:
- "main.uml"
- contains package "foo" which contains class "SimpleClass" which
contains property "age" which is of PrimitiveType "int" (which is not
contained in "main.uml")
- "references.uml"
- contains only the PrimitiveType "int"

Now I want to load these models and save them in a different folder. The
way I do it now messes up the reference from "main.uml" to
"references.uml", because "main.uml" contains still the old path to the
old "references.uml".

Here is how I load the models in a List of Model:

@Override
protected List<Model> createModels() throws Exception
{
List<Model> models = new Vector<Model>();

ResourceSet rSet = new ResourceSetImpl();

// load the models

Model model = loadModel(rSet, "main.uml");
models.add(model);
Model refmodel = loadModel(rSet, "references.uml");
models.add(refmodel);

// return the models

return models;
}

private Model loadModel( ResourceSet rSet, String name ) throws Exception
{
XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();
EObject sourceModel = null;

rSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put( "*",
_xmiFac);

URL modelURL = FileLocator.find(Activator.getDefault().getBundle(),
new Path("completeTestTestData/model/" + name), null);
modelURL = FileLocator.toFileURL(modelURL);
String absolutePath = modelURL.getPath();
URI uri = URI.createFileURI(absolutePath);

// register meta-model
rSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
rSet.getPackageRegistry().put(OtmlPackage.eNS_URI,
OtmlPackage.eINSTANCE);

Resource resource = rSet.getResource(uri, true);
if( resource != null )
{
if( resource.getContents().size() > 0 )
{
sourceModel = (EObject) resource.getContents().get(0);
}
}

return (Model) sourceModel;
}

Here is how I save them (without changing anything) in a different folder:

public void saveModels( List<Model> models, IFolder modelFolder )
throws Exception
{
ResourceSet resourceSet = new ResourceSetImpl();

List<Resource> resources = new Vector<Resource>();

for( Model model : models )
{
String fileName = model.getName() + ".uml";
IFile file = modelFolder.getFile(fileName);
String path = file.getLocation().toFile().getAbsolutePath();
URI fileURI = URI.createFileURI(path);
Resource aResource = resourceSet.createResource(fileURI);
aResource.getContents().add(model);
resources.add(aResource);
}

for( Resource resource : resources )
{
resource.save(Collections.EMPTY_MAP);
}
}

One thing I stumbled upon: If I navigate to the reference in the loaded
EMF-model (after loading, before saving), then the saving works. So if I
give the "main-"model to the following method, then the saving is OK:

private void forDebugging( Model model )
{
for( org.eclipse.uml2.uml.Package pack : model.getNestedPackages() )
{
String name = pack.getName();

for( Type type : pack.getOwnedTypes() )
{
String typeName = type.getName();

if( type instanceof Classifier )
{
Classifier classifier = (Classifier) type;

for( Property prop : classifier.getAllAttributes() )
{
String propName = prop.getName();

Type propType = prop.getType();
String propTypeName = propType.getName();
}
}
}
}
}

So how can I save the models, so that the reference is "updated" ?

thank you
Re: Load and save a model with references [message #478699 is a reply to message #478428] Thu, 06 August 2009 12:37 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30686
Registered: July 2009
Senior Member
Usul

Maybe I should wait for a never mind? :-P

More comments below.

Usul wrote:
> Hi,
>
> I have two UML2-models:
> - "main.uml"
> - contains package "foo" which contains class "SimpleClass" which
> contains property "age" which is of PrimitiveType "int" (which is not
> contained in "main.uml")
> - "references.uml"
> - contains only the PrimitiveType "int"
>
> Now I want to load these models and save them in a different folder.
> The way I do it now messes up the reference from "main.uml" to
> "references.uml", because "main.uml" contains still the old path to
> the old "references.uml".
You might need to ensure all proxies are resolved. EcoreUtil.resolveAll
is useful for that.
>
> Here is how I load the models in a List of Model:
>
> @Override
> protected List<Model> createModels() throws Exception
> {
> List<Model> models = new Vector<Model>();
Vector. Yuck.
>
> ResourceSet rSet = new ResourceSetImpl();
>
> // load the models
>
> Model model = loadModel(rSet, "main.uml");
> models.add(model);
> Model refmodel = loadModel(rSet, "references.uml");
> models.add(refmodel);
>
> // return the models
>
> return models;
> }
>
> private Model loadModel( ResourceSet rSet, String name ) throws
> Exception
> {
> XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();
> EObject sourceModel = null;
>
>
> rSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put( "*",
> _xmiFac);
>
> URL modelURL =
> FileLocator.find(Activator.getDefault().getBundle(),
> new Path("completeTestTestData/model/" + name), null);
> modelURL = FileLocator.toFileURL(modelURL);
EMF should be able to process any URL directly.
> String absolutePath = modelURL.getPath();
> URI uri = URI.createFileURI(absolutePath);
>
> // register meta-model
> rSet.getPackageRegistry().put(UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE);
> rSet.getPackageRegistry().put(OtmlPackage.eNS_URI,
> OtmlPackage.eINSTANCE);
>
> Resource resource = rSet.getResource(uri, true);
> if( resource != null )
> {
> if( resource.getContents().size() > 0 )
> {
> sourceModel = (EObject) resource.getContents().get(0);
> }
> }
>
> return (Model) sourceModel;
> }
>
> Here is how I save them (without changing anything) in a different
> folder:
>
> public void saveModels( List<Model> models, IFolder modelFolder )
> throws Exception
> {
> ResourceSet resourceSet = new ResourceSetImpl();
>
> List<Resource> resources = new Vector<Resource>();
>
> for( Model model : models )
> {
> String fileName = model.getName() + ".uml";
> IFile file = modelFolder.getFile(fileName);
> String path = file.getLocation().toFile().getAbsolutePath();
> URI fileURI = URI.createFileURI(path);
> Resource aResource = resourceSet.createResource(fileURI);
> aResource.getContents().add(model);
> resources.add(aResource);
> }
Yes, be sure all the proxies are resolved. You can just change the URI
of the resources you've already loaded and save those...
>
> for( Resource resource : resources )
> {
> resource.save(Collections.EMPTY_MAP);
> }
> }
>
> One thing I stumbled upon: If I navigate to the reference in the
> loaded EMF-model (after loading, before saving), then the saving works.
Indeed, unresolved proxies are an issue then. They record the original
resource location and saving does not resolve proxies.
> So if I give the "main-"model to the following method, then the saving
> is OK:
>
> private void forDebugging( Model model )
> {
> for( org.eclipse.uml2.uml.Package pack :
> model.getNestedPackages() )
> {
> String name = pack.getName();
>
> for( Type type : pack.getOwnedTypes() )
> {
> String typeName = type.getName();
>
> if( type instanceof Classifier )
> {
> Classifier classifier = (Classifier) type;
>
> for( Property prop : classifier.getAllAttributes() )
> {
> String propName = prop.getName();
>
> Type propType = prop.getType();
> String propTypeName = propType.getName();
> }
> }
> }
> }
> }
>
> So how can I save the models, so that the reference is "updated" ?
>
> thank you
>
>
Previous Topic:EMF References using Generics, Proxy are created
Next Topic:"Interface Name Pattern" not considered during ecore model reload?
Goto Forum:
  


Current Time: Mon Dec 09 03:57:35 GMT 2019

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

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

Back to the top