Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to programmtically add EObject into Resource
How to programmtically add EObject into Resource [message #1830726] Sat, 01 August 2020 11:25 Go to next message
Eclipse UserFriend
Hi,
Sometimes the datamodel is incomplete, and I want to programmtically generate a default EObject and add it into Resource.
I know emf provide a Ecore Factory to new a EObject. I did and add that into resource.resourSet.

But, XText seems packaged more things into resource, when I'm trying to follow "The index and the containers", like:
def getVisibleContainers(EObject o) { val index = rdp.getResourceDescriptions(o.eResource) val rd = index.getResourceDescription(o.eResource.URI) cm.getVisibleContainers(rd, index) }...
to get the Visible Eobjects of the resource, none of the generated EObject was returned.
what should I do?
Re: How to programmtically add EObject into Resource [message #1830727 is a reply to message #1830726] Sat, 01 August 2020 12:09 Go to previous messageGo to next message
Eclipse UserFriend
i am not sure what you want to do with the visible containers
but maybe IDerivedStateComputer is the the thing you are looking for
http://xtextcasts.org/episodes/18-model-optimization
Re: How to programmtically add EObject into Resource [message #1830747 is a reply to message #1830727] Mon, 03 August 2020 01:37 Go to previous messageGo to next message
Eclipse UserFriend
thank you, I might didn't describe my problem correctly.
I programmitcally generated EObject into resource, and then I want to get it's IEObjectDescription in the resource.
I did below test to get all IEObjectDescriptions of same resource, but get different results. seems li1 only contains the initial EObjects of the model script and it only change when the model script changes. but li2 contains all the EObjects including the new generated one. I want to know the differences between those two methods. thank you!
//for Method1:
@Inject ResourceDescriptionsProvider rdp;
//for Method2:
@Inject IResourceServiceProvider.Registry rspr;

//Method1:
IResourceDescription description = rdp.getResourceDescriptions(resource)
.getResourceDescription(resource.getURI());
List<IEObjectDescription> li1 = new ArrayList<IEObjectDescription>();
for (IEObjectDescription eod : description.getExportedObjects()) {
li1.add(eod);
}
//Method2:
IResourceServiceProvider rdp2 = this.rspr.getResourceServiceProvider(resource.getURI());
IResourceDescription.Manager manager2 = rdp2.getResourceDescriptionManager();
IResourceDescription description2 = manager2.getResourceDescription(resource);
List<IEObjectDescription> li2 = new ArrayList<IEObjectDescription>();
for (IEObjectDescription eod : description2.getExportedObjects()) {
li2.add(eod);
}
Re: How to programmtically add EObject into Resource [message #1830748 is a reply to message #1830747] Mon, 03 August 2020 01:43 Go to previous messageGo to next message
Eclipse UserFriend
How do you add the stuff?
The first one reads the Xtext index
The second one calculates it new
The proper place to do such m2m is the iderivedstatecomputer
Re: How to programmtically add EObject into Resource [message #1830767 is a reply to message #1830748] Mon, 03 August 2020 10:52 Go to previous messageGo to next message
Eclipse UserFriend
I didn't mean adding new attribute for metamodel, I just want to generate the default content of the EMF model. I didn't see what's the meaning of using IDerivedStateComputer in my situation.
I add the stuff by using :
//Query is my emf class
Query q = PlatformFactory.eINSTANCE.createQuery();
resource.getResourceSet().getResources().get(0). add(q);
Re: How to programmtically add EObject into Resource [message #1830771 is a reply to message #1830767] Mon, 03 August 2020 12:42 Go to previous messageGo to next message
Eclipse UserFriend
Did you read the linked article ?
Iderivredstate computer allows you to derive state (aka create new objects)
Based on the parsed one.
In you case you would add a query
Re: How to programmtically add EObject into Resource [message #1830779 is a reply to message #1830771] Mon, 03 August 2020 15:15 Go to previous messageGo to next message
Eclipse UserFriend
HI

Like Christian I don't really understand what you want.

Another approach to adding extras to a model is to define a derived property and in your manual implementation of that property's getter you can create it and as much else that you like.

(In QVTd I could use this to autocreate the "this" variable, but in practice I prefer to exert more manual control, so you might look to do it in a controlled rather than automatic fashion. "automatic" can go adrift when you copy.)

Regards

Ed Willink
Re: How to programmtically add EObject into Resource [message #1831245 is a reply to message #1830779] Fri, 14 August 2020 22:00 Go to previous message
Eclipse UserFriend
that really helps, thank you.
Previous Topic:Filter the terminals list proposed in content assistant
Next Topic: XText Integration with Angular application
Goto Forum:
  


Current Time: Wed Jul 16 19:09:44 EDT 2025

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

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

Back to the top