Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Add an ID (hidden) in the metamodel associated with the xtext grammar
Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831467] Thu, 20 August 2020 11:28 Go to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
I have a grammar where I can add some elements and give them a name.
In case I have duplicated names I need also an ID to know to which real element I associate the name.

Which would be the best approach in xtext to add an ID (as technical element, is hidden)?
Thanks.
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831483 is a reply to message #1831467] Thu, 20 August 2020 14:37 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

This seems to be the same question you asked earlier...

You seem to be trying to get the best of both worlds: have an ID but not have an ID. I suspect that you may easily end up with the worst of both worlds continually losing/duplicating IDs when copies and pastes are performed.

Xtext normally treats your edited text as your persisted text and it is pretty dangerous to hide here. In this mode of operation you typically load as text, edit as text, save as text. Your editor may be any text editor, although using the Xtext editor gives you added value.

Xtext may also be used to give a pleasanter editing experience for an XML model. The OCLinEcore editor does this when editing a *.ecore file rather than a *.oclinecore file. I expect the Xcore editor also does this when editing a *.ecore file rather than a *.xcore file. I don't think this is supported out of the box, so you need to ensure that your tooling loads from XML, serializes XML to text, edits text updating XML, saves XML.

Once you are saving as XML you may have all sorts of information that is selectively available in the editor.

However this is all a bit hard. I strongly recommend that you tolerate the visible IDs for a few months until you have a better feel for what you are actually doing. Only once you are sure that your system works and you really really want to hide the IDs is it worth the effort. Till then it seems like a premature optimization

Regards

Ed Willink
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831484 is a reply to message #1831483] Thu, 20 August 2020 14:57 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Thanks!
I found some resources of extending the metamodel (using IXtext2EcorePostProcessor ) however this seems to be deprecated.
I can add the id in the grammar and handle later the hiding aspect.
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831485 is a reply to message #1831484] Thu, 20 August 2020 15:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can simply use an uncalled rule there

Greeting: "Hello" name=ID;

GreetingUncalled returns Greeting: id=ID;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831574 is a reply to message #1831485] Mon, 24 August 2020 07:15 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Wow, thanks. It is exactly what I need, works with Uncalled rule :)
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831616 is a reply to message #1831574] Tue, 25 August 2020 09:58 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Is there a way in xtext content assist to know which of the proposals was selected?

In case there are duplicated names I need to distinguish based on id, in content assist the name is filled (the name + id is display as display string), but I would need to set also the id on the element after choosing one in content assist.

Update
var textApplier = new IReplacementTextApplier () {
override apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException {
//todo
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}
}
var proposal = createCompletionProposal("\"" + getName(el) + "\"",
getLabel(el), null, context) as ConfigurableCompletionProposal
proposal.textApplier = textApplier
acceptor.accept(proposal);

[Updated on: Tue, 25 August 2020 10:40]

Report message to a moderator

Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831618 is a reply to message #1831616] Tue, 25 August 2020 10:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
maybe you can (ab)use org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal.setAdditionalData(String, Object)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831620 is a reply to message #1831618] Tue, 25 August 2020 11:32 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Hello Christian,

Thank you for your suggestion. I have try to override apply and use addAditionalData link below, the Participant object is set with the id inside apply but after the document is updated, the Participant object it loses the id already set.

var textApplier = new IReplacementTextApplier () {
override apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException {
var participant = proposal.getAdditionalData("participant") as Participant
participant.id = proposal.getAdditionalData("id") as String
document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), proposal.getReplacementString());
}
}
var proposal = createCompletionProposal("\"" + getName(el) + "\"",
getLabel(el), null, context) as ConfigurableCompletionProposal
proposal.setTextApplier(textApplier)
proposal.setAdditionalData("id", getId(el))
proposal.setAdditionalData("participant", context.currentModel)
acceptor.accept(proposal);

[Updated on: Tue, 25 August 2020 11:32]

Report message to a moderator

Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831622 is a reply to message #1831620] Tue, 25 August 2020 11:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
did you debug where it is lost?
maybe you can also use additionalProposalInfo


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831626 is a reply to message #1831622] Tue, 25 August 2020 13:50 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
It seems that xtext creates a new object after the proposal it is accepted, it doesn't update the existing object, so I cannot use this approch to pass the object and update it in apply. Ctrl-space proposes the name of the element but I try to update the id which is not visible, added with uncalled rule.

[Updated on: Tue, 25 August 2020 13:58]

Report message to a moderator

Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831634 is a reply to message #1831626] Tue, 25 August 2020 17:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I don't get it. I thought you implement text applier

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831635 is a reply to message #1831634] Tue, 25 August 2020 17:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Of course xtext reparses the file all the time so if
Your info is nowhere in the file it will be lost on each reparses


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831645 is a reply to message #1831635] Wed, 26 August 2020 06:47 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Ok, there is a way to do a post processing after a proposal is accepted and the model in re-parsed?
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831646 is a reply to message #1831645] Wed, 26 August 2020 07:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i am still not clear about your usecase
- standalone vs eclipse
- session vs restart vs version control
....

if have some extra information somewhere and no information about where to store it.
i also dont know what kind of information you store and how it is calculated


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831648 is a reply to message #1831646] Wed, 26 August 2020 07:13 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
I have a rule that accepts a name and an id, but the id is hidden (uncalled rule).
On ctrl+space I propose the names, but they may be duplicated so I also want to set the id (which is hidden).
I override apply on IReplacementTextApplier where I can update the name (with document.replace), but I don't know how to also update the id, as a new object is created after changing the name.
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831649 is a reply to message #1831648] Wed, 26 August 2020 07:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yes but why this hidden id. what is it used for. what happens if eclipse is restarted. what happens if file is shared via git.
=> what is your actual usecase


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831650 is a reply to message #1831649] Wed, 26 August 2020 07:33 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
The model is not persisted, is kept in memory so is regenerated at each restart.
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831651 is a reply to message #1831650] Wed, 26 August 2020 07:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the you need to have a separate store to save this information and have to recover it after parse e.g.
via IDerivedStateComputer.

for storage you need to find a place. if you have a physical file you can set it as (non persistent) property
or you can introduce a global singleton cache and inject it


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831653 is a reply to message #1831651] Wed, 26 August 2020 07:49 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
I have the store, to recover the info, but I don't know how to set on ctrl-space the hidden info (is which is the id and I can recover it from my persistance but I need to be able to set it).
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831654 is a reply to message #1831653] Wed, 26 August 2020 07:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
if you have a text applier you can set it. cant you?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831655 is a reply to message #1831654] Wed, 26 August 2020 08:05 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Yes,

Participant:
keyword = 'participant' name=STRING
;

ParticipantUncalled returns Participant: id=ID;

var textApplier = new IReplacementTextApplier () {
override apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException {
var participant = proposal.getAdditionalData("participant") as Participant
participant.id = proposal.getAdditionalData("id") as String //if i set here the id- it is lost because on document.replace - a new object is created
participant.name = "A"
document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), proposal.getReplacementString());

// todo - I will try to obtain here the new object created in the model

}
}

[Updated on: Wed, 26 August 2020 08:07]

Report message to a moderator

Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831657 is a reply to message #1831655] Wed, 26 August 2020 08:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont understand.

in the text applier you can store to the global cache
and in a derivedStateComputer (or custom parser)
recover it


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831662 is a reply to message #1831657] Wed, 26 August 2020 08:38 Go to previous messageGo to next message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Thanks.
Ok, i'll try to use IDerivedStateComputer.
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831663 is a reply to message #1831662] Wed, 26 August 2020 08:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
http://xtextcasts.org/episodes/18-model-optimization

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add an ID (hidden) in the metamodel associated with the xtext grammar [message #1831677 is a reply to message #1831663] Wed, 26 August 2020 12:01 Go to previous message
Georgiana Ecobici is currently offline Georgiana EcobiciFriend
Messages: 23
Registered: July 2019
Junior Member
Thanks for the suggestions.
It worked to set the id like below, using IDerivedStateComputer.

class DslDerivedStateComputer implements IDerivedStateComputer {

override discardDerivedState(DerivedStateAwareResource resource) {
resource.allContents.filter(Participant).forEach [
id = "456"
]
}

override installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
if (!preLinkingPhase)
resource.allContents.filter(Participant).forEach [ participant |
participant.id = "123"
]
}

}
Previous Topic:Autocomplete suggestion files in project
Next Topic:Unable to fetch data type information if defined in an imported AADL file:
Goto Forum:
  


Current Time: Fri Apr 19 05:47:57 GMT 2024

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

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

Back to the top