Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Proper way to store and save EObjects(Design)
Proper way to store and save EObjects [message #1855300] Thu, 06 October 2022 17:38 Go to next message
Frank Conover is currently offline Frank ConoverFriend
Messages: 20
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 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 #1855302 is a reply to message #1855301] Thu, 06 October 2022 20:01 Go to previous messageGo to next message
Frank Conover is currently offline Frank ConoverFriend
Messages: 20
Registered: October 2022
Junior Member
Thank you.

I added the model to the resource immediately and that resolved the issue where I had to add the blocks to the resource before saving.

However, once I stopped using the StereotypeApplicationHelper the problem returned.
Without it, I can't seem to properly create a Block and set it's base class so that these lines show up in the xmi output.
I looked at the stereo type link and it didn't ring any bells.

I am now doing it this way and the model isn't being updated as it was with the helper.
		Class ownedBlockClass2 = model.createOwnedClass("Block2", false);
		Block owningAgregationBlock2 = BlocksFactoryImpl.eINSTANCE.createBlock();
		owningAgregationBlock2.setBase_Class(ownedBlockClass2);


I imagine, once I understand this step, I can cruise right along as everything will work similarly.
Re: Proper way to store and save EObjects [message #1855303 is a reply to message #1855302] Thu, 06 October 2022 20:10 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 #1855305 is a reply to message #1855303] Thu, 06 October 2022 22:36 Go to previous messageGo to next message
Frank Conover is currently offline Frank ConoverFriend
Messages: 20
Registered: October 2022
Junior Member
Thanks,

So I printed out the profiles from my model and they are there.

But when I try to apply a stereotype I get an error. Which led me down the other path I tried earlier.

I realize this is a different topic now.

		Class block1Class = model.createOwnedClass("Block1", false);
		Stereotype block1 = model.getAppliedProfile("SysML").createOwnedStereotype("Block", false);
		block1Class.applyStereotype(block1);


java.lang.IllegalArgumentException: stereotype "SysML::Block" has no Ecore definition
at org.eclipse.uml2.uml.internal.operations.ElementOperations.getDefinition(ElementOperations.java:1276)
at org.eclipse.uml2.uml.internal.operations.ElementOperations.applyStereotype(ElementOperations.java:1496)
at org.eclipse.uml2.uml.internal.impl.ElementImpl.applyStereotype(ElementImpl.java:504)
Re: Proper way to store and save EObjects [message #1855306 is a reply to message #1855305] Thu, 06 October 2022 23:46 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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
Re: Proper way to store and save EObjects [message #1855316 is a reply to message #1855306] Fri, 07 October 2022 16:22 Go to previous messageGo to next message
Frank Conover is currently offline Frank ConoverFriend
Messages: 20
Registered: October 2022
Junior Member
Thank you. I probably do need to load another resource but I've tried them all.
If I get my code cleaned up I can just post it here. There isn't much to it yet as I've just been stuck at this point.

Taking a step back.
I did discover through searching the forums previously that I had to load profiles and resources because I am running standalone.

It was the only way I got as far as loading the 'Standard Profile' and the 'SysML' profile into my model without error.

I do:
UMLResourcesUtil.init(resourceSet);
UMLPackage.eINSTANCE.getName(); // (not really sure this is needed)
// note that before I discovered that this method existed, I found a different thread that had me register the pathmaps myself and I got it working, but then that code all went away with the use of the method above.
It did come in handy when loading the SysML files though below.

Then the SysML files I had to load a bit differently.
// registerSysMLPathmaps locally, otherwise I received a pathmap error

// get local baseuri path to my project where I copied the resources to and then... URIConverter.URI_MAP.put(URI.createURI(SysMLResource.LIBRARIES_PATHMAP), baseUri.appendSegment("resources").appendSegment("library").appendSegment(""));
URIConverter.URI_MAP.put(URI.createURI(SysMLResource.PROFILES_PATHMAP), baseUri.appendSegment("resources").appendSegment("profile").appendSegment(""));
}

sysmlPackage.eINSTANCE.getName(); // (not sure this is needed)

Then after your last comment I loaded the sysml.ecore file and every other file I could find just to be safe but I still get the ecore error.

// Register the default resource factory -- only needed for stand-alone!
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( EcorePackage.eNAME, new EcoreResourceFactoryImpl());

// load the resource
URI fileURI = URI.createFileURI("resources/profile/sysml.ecore");
resourceSet.createResource(fileURI);

// Register the package -- only needed for stand-alone! (not really sure this is needed)
EcorePackage.eINSTANCE.getName();

I loaded every other file I could find into the resource set for good measure:

fileURI = URI.createFileURI("resources/profile/SysML.profile.uml");
resourceSet.createResource(fileURI);
//load(fileURI);

fileURI = URI.createFileURI("resources/library/QUDV.uml");
resourceSet.createResource(fileURI);

fileURI = URI.createFileURI("resources/library/SysML-Standard-Library.uml");
resourceSet.createResource(fileURI);

fileURI = URI.createFileURI("metamodels/Ecore.metamodel.uml");
resourceSet.createResource(fileURI);

fileURI = URI.createFileURI("metamodels/UML.metamodel.uml");
resourceSet.createResource(fileURI);

fileURI = URI.createFileURI("profiles/Ecore.profile.uml");
resourceSet.createResource(fileURI);

fileURI = URI.createFileURI("profiles/Standard.profile.uml");
resourceSet.createResource(fileURI);

fileURI = URI.createFileURI("profiles/UML2.profile.uml");
resourceSet.createResource(fileURI);

At this point I'm wondering if it is not possible to initiate the SysML profile properly. Or whichever Ecore files I am missing.
Re: Proper way to store and save EObjects [message #1855318 is a reply to message #1855316] Fri, 07 October 2022 19:24 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You should find many, and all recent, threads advocate UMLResourcesUtil.init.

You should find a SysML equivalent since SysML has the same, if not worse, problems of ensuring that everything is correctly initialized.

The XXXPackage.eINSTANCE.getName() idiom has the side effect of installing entries within the EMF global registries. It should indeed be redundant if you use UMLResourcesUtil.init(null) to init globally rather than locally.

Regards

Ed Willink
Re: Proper way to store and save EObjects [message #1855386 is a reply to message #1855318] Tue, 11 October 2022 16:21 Go to previous message
Frank Conover is currently offline Frank ConoverFriend
Messages: 20
Registered: October 2022
Junior Member
Ed it looks like you made a ticket for this a long while ago that hasn't had much traction. https://bugs.eclipse.org/bugs/show_bug.cgi?id=531840

I started a new thread in the Papyrus forums for those interested: Where is SysMLResourcesUtil.init (How to perform SysML initialization in a standalone application)
https://www.eclipse.org/forums/index.php?t=msg&th=1111773&goto=1855384&#msg_1855384
Previous Topic:Problem with Features of Type Class
Next Topic:Generating errors in java class from an xsd file that contains special character like "µ"
Goto Forum:
  


Current Time: Sat Apr 27 00:10:46 GMT 2024

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

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

Back to the top