Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » add Package to Model
add Package to Model [message #474258] Mon, 16 July 2007 13:38 Go to next message
Jan Reimann is currently offline Jan ReimannFriend
Messages: 90
Registered: July 2009
Member
Hello NG,
it's me again ;)
I have a Package with some elements in it and a Model. Now I want to add
the Package to the Model and save it. That was my way:

private static Model createModel(Package pack, String name) {
Model model = umlFactory.createModel();
model.setName(name);
model.setNestingPackage(pack);
if (SAVE_MODELS) {
saveModel(name, model);
}

return model;
}

private static void saveModel(String name, Model model) {
String filename = outDir + name + "." + UMLResource.FILE_EXTENSION;
File file = new File(filename);
URI uri = URI.createFileURI(file.getAbsolutePath());
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(uri);
EList<EObject> contents = resource.getContents();

contents.add(model);

Iterator<Element> allContents = model.getOwnedElements().iterator();
while (allContents.hasNext()) {

EObject eObject = (EObject) allContents.next();

if (eObject instanceof Element) {
contents.addAll(((Element) eObject).getStereotypeApplications());
}
}
try {
resource.save(null);

} catch (IOException ioe) {
System.out.println("Saving failed!");
}
}

But in my method createModel the Package won't be added to the model
with setNestingPackage. The saved file only contains the model without
any elements. How can I do this?

greets
J.R.
Re: add Package to Model [message #474259 is a reply to message #474258] Mon, 16 July 2007 13:45 Go to previous messageGo to next message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Hi Jan,

I get confused by nesting and nested all the time too. I think the
solution is:

pack.setNestingPackage(model);


Regards,
Andrew.

Ar 16/07/2007 14:38, Scríobh Jan Reimann:
> Hello NG,
> it's me again ;)
> I have a Package with some elements in it and a Model. Now I want to add
> the Package to the Model and save it. That was my way:
>
> private static Model createModel(Package pack, String name) {
> Model model = umlFactory.createModel();
> model.setName(name);
> model.setNestingPackage(pack);
> if (SAVE_MODELS) {
> saveModel(name, model);
> }
>
> return model;
> }
>
> private static void saveModel(String name, Model model) {
> String filename = outDir + name + "." + UMLResource.FILE_EXTENSION;
> File file = new File(filename);
> URI uri = URI.createFileURI(file.getAbsolutePath());
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.createResource(uri);
> EList<EObject> contents = resource.getContents();
>
> contents.add(model);
>
> Iterator<Element> allContents = model.getOwnedElements().iterator();
> while (allContents.hasNext()) {
>
> EObject eObject = (EObject) allContents.next();
>
> if (eObject instanceof Element) {
> contents.addAll(((Element)
> eObject).getStereotypeApplications());
> }
> }
> try {
> resource.save(null);
>
> } catch (IOException ioe) {
> System.out.println("Saving failed!");
> }
> }
>
> But in my method createModel the Package won't be added to the model
> with setNestingPackage. The saved file only contains the model without
> any elements. How can I do this?
>
> greets
> J.R.
Re: add Package to Model [message #474260 is a reply to message #474258] Mon, 16 July 2007 13:44 Go to previous messageGo to next message
Philippe is currently offline PhilippeFriend
Messages: 100
Registered: July 2009
Senior Member
I don't have time to try it, but I believe this could work better

model.getNestedPackages().add(pack);

Regards
Philippe

Jan Reimann wrote:
> Hello NG,
> it's me again ;)
> I have a Package with some elements in it and a Model. Now I want to add
> the Package to the Model and save it. That was my way:
>
> private static Model createModel(Package pack, String name) {
> Model model = umlFactory.createModel();
> model.setName(name);
> model.setNestingPackage(pack);
> if (SAVE_MODELS) {
> saveModel(name, model);
> }
>
> return model;
> }
>
> private static void saveModel(String name, Model model) {
> String filename = outDir + name + "." + UMLResource.FILE_EXTENSION;
> File file = new File(filename);
> URI uri = URI.createFileURI(file.getAbsolutePath());
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.createResource(uri);
> EList<EObject> contents = resource.getContents();
>
> contents.add(model);
>
> Iterator<Element> allContents = model.getOwnedElements().iterator();
> while (allContents.hasNext()) {
>
> EObject eObject = (EObject) allContents.next();
>
> if (eObject instanceof Element) {
> contents.addAll(((Element)
> eObject).getStereotypeApplications());
> }
> }
> try {
> resource.save(null);
>
> } catch (IOException ioe) {
> System.out.println("Saving failed!");
> }
> }
>
> But in my method createModel the Package won't be added to the model
> with setNestingPackage. The saved file only contains the model without
> any elements. How can I do this?
>
> greets
> J.R.
Re: add Package to Model [message #474261 is a reply to message #474260] Mon, 16 July 2007 13:53 Go to previous message
Jan Reimann is currently offline Jan ReimannFriend
Messages: 90
Registered: July 2009
Member
Philippe schrieb:
> model.getNestedPackages().add(pack);

that's it. Thanks :)
Re: add Package to Model [message #623676 is a reply to message #474258] Mon, 16 July 2007 13:45 Go to previous message
Andrew Carton is currently offline Andrew CartonFriend
Messages: 104
Registered: July 2009
Senior Member
Hi Jan,

I get confused by nesting and nested all the time too. I think the
solution is:

pack.setNestingPackage(model);


Regards,
Andrew.

Ar 16/07/2007 14:38, Scríobh Jan Reimann:
> Hello NG,
> it's me again ;)
> I have a Package with some elements in it and a Model. Now I want to add
> the Package to the Model and save it. That was my way:
>
> private static Model createModel(Package pack, String name) {
> Model model = umlFactory.createModel();
> model.setName(name);
> model.setNestingPackage(pack);
> if (SAVE_MODELS) {
> saveModel(name, model);
> }
>
> return model;
> }
>
> private static void saveModel(String name, Model model) {
> String filename = outDir + name + "." + UMLResource.FILE_EXTENSION;
> File file = new File(filename);
> URI uri = URI.createFileURI(file.getAbsolutePath());
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.createResource(uri);
> EList<EObject> contents = resource.getContents();
>
> contents.add(model);
>
> Iterator<Element> allContents = model.getOwnedElements().iterator();
> while (allContents.hasNext()) {
>
> EObject eObject = (EObject) allContents.next();
>
> if (eObject instanceof Element) {
> contents.addAll(((Element)
> eObject).getStereotypeApplications());
> }
> }
> try {
> resource.save(null);
>
> } catch (IOException ioe) {
> System.out.println("Saving failed!");
> }
> }
>
> But in my method createModel the Package won't be added to the model
> with setNestingPackage. The saved file only contains the model without
> any elements. How can I do this?
>
> greets
> J.R.
Re: add Package to Model [message #623677 is a reply to message #474258] Mon, 16 July 2007 13:44 Go to previous message
Philippe is currently offline PhilippeFriend
Messages: 100
Registered: July 2009
Senior Member
I don't have time to try it, but I believe this could work better

model.getNestedPackages().add(pack);

Regards
Philippe

Jan Reimann wrote:
> Hello NG,
> it's me again ;)
> I have a Package with some elements in it and a Model. Now I want to add
> the Package to the Model and save it. That was my way:
>
> private static Model createModel(Package pack, String name) {
> Model model = umlFactory.createModel();
> model.setName(name);
> model.setNestingPackage(pack);
> if (SAVE_MODELS) {
> saveModel(name, model);
> }
>
> return model;
> }
>
> private static void saveModel(String name, Model model) {
> String filename = outDir + name + "." + UMLResource.FILE_EXTENSION;
> File file = new File(filename);
> URI uri = URI.createFileURI(file.getAbsolutePath());
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.createResource(uri);
> EList<EObject> contents = resource.getContents();
>
> contents.add(model);
>
> Iterator<Element> allContents = model.getOwnedElements().iterator();
> while (allContents.hasNext()) {
>
> EObject eObject = (EObject) allContents.next();
>
> if (eObject instanceof Element) {
> contents.addAll(((Element)
> eObject).getStereotypeApplications());
> }
> }
> try {
> resource.save(null);
>
> } catch (IOException ioe) {
> System.out.println("Saving failed!");
> }
> }
>
> But in my method createModel the Package won't be added to the model
> with setNestingPackage. The saved file only contains the model without
> any elements. How can I do this?
>
> greets
> J.R.
Re: add Package to Model [message #623678 is a reply to message #474260] Mon, 16 July 2007 13:53 Go to previous message
Jan Reimann is currently offline Jan ReimannFriend
Messages: 90
Registered: July 2009
Member
Philippe schrieb:
> model.getNestedPackages().add(pack);

that's it. Thanks :)
Previous Topic:add Package to Model
Next Topic:modeling object creation action
Goto Forum:
  


Current Time: Wed Apr 01 17:33:30 GMT 2020

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

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

Back to the top