add Package to Model [message #474258] |
Mon, 16 July 2007 09:38  |
Eclipse User |
|
|
|
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 #623676 is a reply to message #474258] |
Mon, 16 July 2007 09:45  |
Eclipse User |
|
|
|
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 09:44  |
Eclipse User |
|
|
|
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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03709 seconds