Proper way to store and save EObjects [message #1855300] |
Thu, 06 October 2022 17:38 |
Frank Conover Messages: 18 Registered: October 2022 |
Junior Member |
|
|
Hello, I am just getting started with EMF for the use of saving SysML from a standalone desktop application. I have created a test class where I manually populate a model. I added Class objects, Associations, Properties, etc from the samples I found.
I figured out how to manipulate the XMIResourse to produce the XMI format I expect.
My question is, where do I store EObjects so that I can easily use them when saving?
For example, I create a model and I add a few owned classes to it representing my blocks. I then create Block (EObject) where the base classes are set to the Classes I made.
Model model = SysMLResource.createSysMLModel(resourceSet);
Class owningAgregationBlockClass1 = model.createOwnedClass("Block1", false);
Block owningAgregationBlock1 = (Block)stereotypeApplicationHelper.applyStereotype(owningAgregationBlockClass1, BlocksPackage.eINSTANCE.getBlock(), null);
When I save my resource, I can simple add the model. But then I have to add each Block separately? Is there a different way to do this? Or do I have to maintain my own list of EObjects? Or add them to the resource as I go?
xmiResourceImpl.getContents().addAll(model.eContents()); // or xmiResourceImpl.getContents().add(model);
xmiResourceImpl.getContents().add(owningAgregationBlock1);
xmiResourceImpl.getContents().add(owningAgregationBlock2);
[Updated on: Thu, 06 October 2022 18:36] Report message to a moderator
|
|
|
Re: Proper way to store and save EObjects [message #1855301 is a reply to message #1855300] |
Thu, 06 October 2022 18:56 |
|
Hi, Frank,
The Eclipse UML2 project's UML API, on which Papyrus's SysML implementation is based (I assume this is what you are using), manages the stereotype-application objects automatically. You should not be using the StereotypeApplicationHelper.
The Element interface provides an applyStereotype(Stereotype) method that instantiates the stereotype and puts it in the resource in the proper way. Do be sure that the first thing you do with a new Model is to add it to the resource so that the API can then find the resource in which to add the stereotype applications.
The code snippets in the introduction to profiles article may be helpful:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles#Applying_Stereotypes
HTH,
Christian
|
|
|
|
Re: Proper way to store and save EObjects [message #1855303 is a reply to message #1855302] |
Thu, 06 October 2022 20:10 |
|
Hi, Frank,
You shouldn't have to explicitly create the stereotype application object and set its base_Class reference, either. This is all handled by the Element::applyStereotype(Stereotype) operation.
Did you forget to apply the SysML profile to your model?
Cheers,
Christian
|
|
|
|
Re: Proper way to store and save EObjects [message #1855306 is a reply to message #1855305] |
Thu, 06 October 2022 23:46 |
|
Ah! Now that's explaining a few things.
IIRC, this profile is statically generated, so at run-time the UML API needs to be able to locate the Ecore definition via the org.eclipse,uml2.uml.generated_package extension point.
Theres's a convenience API to find and load these registrations from the plugin.xml manifests (I assume your Java program is not an Eclipse application). Look for a UMLResourcesUtil.initialize(ResourceSet) API to set up your resource set with all of the required registrations. The name may not exactly be that, sorry, I'm AFK. But that should get you close to it.
This includes other registrations of run-time services that you'll most likely need, too. But if your program is an Eclipse application, then it wouldn't be necessary and I would be baffled as to what is the cause of this issue.
HTH,
Christian
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02319 seconds