Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 15:25 Go to next message
Qiran Cong is currently offline Qiran CongFriend
Messages: 26
Registered: November 2019
Junior Member
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 16:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
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


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to programmtically add EObject into Resource [message #1830747 is a reply to message #1830727] Mon, 03 August 2020 05:37 Go to previous messageGo to next message
Qiran Cong is currently offline Qiran CongFriend
Messages: 26
Registered: November 2019
Junior Member
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 05:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
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


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to programmtically add EObject into Resource [message #1830767 is a reply to message #1830748] Mon, 03 August 2020 14:52 Go to previous messageGo to next message
Qiran Cong is currently offline Qiran CongFriend
Messages: 26
Registered: November 2019
Junior Member
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 16:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
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


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to programmtically add EObject into Resource [message #1830779 is a reply to message #1830771] Mon, 03 August 2020 19:15 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6874
Registered: July 2009
Senior Member
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] Sat, 15 August 2020 02:00 Go to previous message
Qiran Cong is currently offline Qiran CongFriend
Messages: 26
Registered: November 2019
Junior Member
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: Thu Oct 22 13:26:47 GMT 2020

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

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

Back to the top