Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to assign values to attributes[RESOLVED]
How to assign values to attributes[RESOLVED] [message #628912] Fri, 24 September 2010 17:03 Go to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
Hi, i know this is probably a dum question.
I use external emf metamodel and do not generate any model with XText.
I need to create an instance of ElementParameterType
with one attribute set by the user and the others automatically set.
But I can't set them automatically.
Here is what I tried.
---------------------------------
UniqueNameType returns talendFile::ElementParameterType :
value=EString
name="UNIQUE_NAME"
field="TEXT"
;
------------------------
name and filed attributes need to be with the UNIQUE_NAME and TEXT string but they are not. I alway get null for both of them.

any one as an idea on how to do that ?

thank you.

[Updated on: Wed, 06 October 2010 10:20]

Report message to a moderator

Re: How to assign values to attributes [message #628932 is a reply to message #628912] Fri, 24 September 2010 18:48 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi,

this sounds strange to me. Do you get any errors in the grammar editor
or when generating the language? How does you input look like?
One would usually set such default values in the ecore model in the
attribute instead of poluting the files and the grammar. Another option
would be to subclass the DefaultEcoreElementFactory and assign such
defaults when the objects is created.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.09.10 19:04, schrieb seb.fr:
> Hi, i know this is probably a dum question.
> I use external emf metamodel and do not generate any model with XText.
> I need to create an instance of ElementParameterType
> with one attribute set by the user and the others automatically set.
> But I can't set them automatically.
> Here is what I tried.
> ---------------------------------
> UniqueNameType returns talendFile::ElementParameterType :
> {talendFile::ElementParameterType}
> value=EString
> name="UNIQUE_NAME"
> field="TEXT"
> ;
> ------------------------
> name and filed attributes need to be with the UNIQUE_NAME and TEXT
> string but they are not. I alway get null for both of them.
>
> any one as an idea on how to do that ?
>
> thank you.
>
Re: How to assign values to attributes [message #629133 is a reply to message #628932] Mon, 27 September 2010 10:31 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
What exactly sounds strange to you.
I have an external model with a class and many attributes that can be set but for my grammar definition I want the user to bother only with one attribute not the others.
Basically these are default values only for this grammar definition.
This does not seem that strange to me ?

The problem is that the main usage of Xtext is to generate the metamodel from the grammar and most peaple tend to think of xtext this way, but I have a metamodel that I need to restrict in the grammar.

Do you mean strange because my grammar should work and create default values for these attributes ?

I'll look at the DefaultEcoreElementFactory but indeed I really would like to have this information in the grammar definition.

If it is not possible today I shall create a new bug for the Xtext project.

PS : I do not have any errors, it is just that these attributes are null instead of the values that I set in the grammar when inspecting the emf memory model.

[Updated on: Mon, 27 September 2010 10:34]

Report message to a moderator

Re: How to assign values to attributes [message #629141 is a reply to message #629133] Mon, 27 September 2010 10:59 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi,

please see below.

Am 27.09.10 12:32, schrieb seb.fr:
> What exactly sounds strange to you.

This part of your initial post sounds strange to me:
attributes need to be with the UNIQUE_NAME and TEXT string but they are
not. I alway get null for both of them.

> I have an external model with a class and many attributes that can be
> set but for my grammar definition I want the user to bother only with
> one attribute not the others.

You'll have to use the suggested customized EcoreElementFactory if you
want to do this.

> Basically these are default values only for this grammar definition.
> This does not seem that strange to me ?
>
> The problem is that the main usage of Xtext is to generate the metamodel
> from the grammar

Not it is not.

> and most peaple tend to think of xtext this way,

I'm afraid that's not true.

> but I have a metamodel that I need to restrict in the grammar.

You are free to do this but it will polute your input files if you use
the approach that you describe in your initial post.

>
> Do you mean strange because my grammar should work and create default
> values for these attributes ?

No, it will not create default values for these attributes but if you
input is

"foo" UNIQUE_NAME TEXT

you'll end up with ElementParameterType with value=foo name=UNIQUE_NAME
and field=TEXT.

>
> I'll look at the DefaultEcoreElementFactory but indeed I really would
> like to have this information in the grammar definition.
>

The grammar is not about defining metamodel defaults and stuff. The
EPackage generation is meant to be used for early prototyping. A typical
usecase is to migrate the initially generated package to an imported
package after the ideas are somewhat clearer and the frequency of
changes decreases during a project livecylce.

> If it is not possible today I shall create a new bug for the Xtext project.

Fee free to do this and describe your ideas / usecase.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: How to assign values to attributes [message #629188 is a reply to message #629141] Mon, 27 September 2010 12:59 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
Tahnks Sebastian for your answers,
I create a bugzilla on this here : https://bugs.eclipse.org/bugs/show_bug.cgi?id=326298
I still have to figure out how to use the DefaultEcoreElementFactory because I did not see any reference to it in the xtext generated code ???

SeB.
Re: How to assign values to attributes [message #629745 is a reply to message #628912] Wed, 29 September 2010 13:04 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
Actually the DefaultEcoreElementFactory is not a workaround for my personnal case, because the same class is instanciated in different places in the grammar, and with different attributes values, which are different from the default values.
Which means I cannot intercept the Object creation to set some attributes by means of code because their values depends on the grammar context.
I there any other workaround please.

Thanks.
Re: How to assign values to attributes [message #629763 is a reply to message #628912] Wed, 29 September 2010 13:50 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
You can always use a post-processing linker to manage things like that.

I think, a good solution to this in Xtext would be to extend the Action possibilities like :
{current.name = 'UNIQUE_NAME'} {current.field = 'TEXT'}
Re: How to assign values to attributes [message #629769 is a reply to message #629745] Wed, 29 September 2010 14:11 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 9/29/10 3:04 PM, schrieb seb.fr:
> Actually the DefaultEcoreElementFactory is not a workaround for my
> personnal case, because the same class is instanciated in different
> places in the grammar, and with different attributes values, which are
> different from the default values.
> Which means I cannot intercept the Object creation to set some
> attributes by means of code because their values depends on the grammar
> context.
> I there any other workaround please.

define concrete subclasses for each set of default values.

Sven

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: How to assign values to attributes [message #629772 is a reply to message #629769] Wed, 29 September 2010 14:16 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
Sven Efftinge wrote on Wed, 29 September 2010 10:11

define concrete subclasses for each set of default values.

I think he's working on an existing meta-model so that won't work.
Re: How to assign values to attributes [message #629776 is a reply to message #629769] Wed, 29 September 2010 14:20 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
exactly, this would mean altering the existing metamodel or extending it which I cannot do.
what about using value converter for this purpose ?

[Updated on: Wed, 29 September 2010 14:25]

Report message to a moderator

Re: How to assign values to attributes [message #629781 is a reply to message #629772] Wed, 29 September 2010 14:18 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 9/29/10 4:16 PM, schrieb Sylvain EVEILLARD:
> Sven Efftinge wrote on Wed, 29 September 2010 10:11
>> define concrete subclasses for each set of default values.
>
> I think he's working on an existing meta-model so that won't work.
>
That doesn't mean you can't create a new ecore model with EClasses,
which subclass preexisting EClasses from another ecore model.

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: How to assign values to attributes [message #629805 is a reply to message #629776] Wed, 29 September 2010 14:26 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 9/29/10 4:20 PM, schrieb seb.fr:
> exactly, this would mean altering the existing metamodel or extending it
> which I cannot do.

Extending is non-invasive. All the objects would full-fill the orginal
contract. What's the problem?

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: How to assign values to attributes [message #629810 is a reply to message #629805] Wed, 29 September 2010 15:32 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
Sven Efftinge wrote on Wed, 29 September 2010 10:26

Extending is non-invasive. All the objects would full-fill the orginal
contract. What's the problem?

The xmi serialization will use the extended meta-model though
Re: How to assign values to attributes [message #630001 is a reply to message #628912] Thu, 30 September 2010 10:05 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
I have tried value converter as I thougth this might be a workaround but I cannot make it work

Is there anything wrong with this definition please ?
---------xtext grammar------
UniqueNameType returns talendFile::ElementParameterType :STRING;


------- java Runtime module
/**
 * Use this class to register components to be used at runtime / without the Equinox extension registry.
 */
public class JobScriptRuntimeModule extends  org.talend.metalanguage.jobscript.AbstractJobScriptRuntimeMo dule {

    /*
     * (non-Javadoc)
     * 
     * @see  org.eclipse.xtext.service.DefaultRuntimeModule#bindIValueCon verterService()
     */
    @Override
    public Class<? extends IValueConverterService> bindIValueConverterService() {
        return JobScriptValueConverters.class;
    }

    static public class JobScriptValueConverters extends DefaultTerminalConverters {

        @ValueConverter(rule = "UniqueNameType")
        public IValueConverter<ElementParameterType> UniqueNameType() {
            return new IValueConverter<ElementParameterType>() {

                public ElementParameterType toValue(String string, AbstractNode node) throws ValueConverterException {
                    ElementParameterType elementParameterType = TalendFileFactory.eINSTANCE.createElementParameterType();
                    elementParameterType.setField("TEXT");
                    elementParameterType.setName("UNIQUE_NAME");
                    elementParameterType.setValue(string);
                    return elementParameterType;
                }

                public String toString(ElementParameterType value) throws ValueConverterException {
                    return value.getValue();
                }
            };
        }
    }
}

---------------end of runtime module

The convertion methods never get called and I do not know why ?

[Updated on: Thu, 30 September 2010 13:36]

Report message to a moderator

Re: How to assign values to attributes [message #630105 is a reply to message #630001] Thu, 30 September 2010 16:08 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
afaik, the type must be an EMF data type and not any EObject (but I
could be wrong). Your sample does not say if that is the case.

One reason for the rule not being called could be if your input does not
trigger the rule you expect to be triggered by your grammar.

What do you get instead of the expected result? A String data type?

Regards
- henrik

On 9/30/10 12:05 PM, seb.fr wrote:
> I have tried value converter as I thougth this might be a workaround but
> I cannot make it work
>
> Is there anything wrong with this definition please ?
> ---------xtext grammar------
> UniqueNameType returns talendFile::ElementParameterType :STRING;
>
> ------- java Runtime module
> /**
> * Use this class to register components to be used at runtime / without
> the Equinox extension registry.
> */
> public class JobScriptRuntimeModule extends
> org.talend.metalanguage.jobscript.AbstractJobScriptRuntimeMo dule {
>
> /*
> * (non-Javadoc)
> * * @see org.eclipse.xtext.service.DefaultRuntimeModule#bindIValueCon
> verterService()
> */
> @Override
> public Class<? extends IValueConverterService>
> bindIValueConverterService() {
> return JobScriptValueConverters.class;
> }
>
> static public class JobScriptValueConverters extends
> DefaultTerminalConverters {
>
> @ValueConverter(rule = "UniqueNameType")
> public IValueConverter<ElementParameterType> UniqueNameType() {
> return new IValueConverter<ElementParameterType>() {
>
> public ElementParameterType toValue(String string, AbstractNode node)
> throws ValueConverterException {
> ElementParameterType elementParameterType =
> TalendFileFactory.eINSTANCE.createElementParameterType();
> elementParameterType.setField("TEXT");
> elementParameterType.setName("UNIQUE_NAME");
> elementParameterType.setValue(string);
> return elementParameterType;
> }
>
> public String toString(ElementParameterType value) throws
> ValueConverterException {
> return value.getValue();
> }
> };
> }
> }
> }
>
> ---------------end of runtime module
>
> The convertion methods never get called and I do not know why ?
Re: How to assign values to attributes [message #630115 is a reply to message #628912] Thu, 30 September 2010 16:47 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
yes your are probably right, my class is a simple EObject.
As a result I get an instance of my class with everything set to null.

Is there any reason why convertion can only be applied to EDataType ?
Re: How to assign values to attributes [message #631109 is a reply to message #629810] Wed, 06 October 2010 08:52 Go to previous messageGo to next message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
Sylvain EVEILLARD wrote on Wed, 29 September 2010 17:32
Sven Efftinge wrote on Wed, 29 September 2010 10:26

Extending is non-invasive. All the objects would full-fill the orginal
contract. What's the problem?

The xmi serialization will use the extended meta-model though


Yes exactly my case, I use xmi serialization to give the model back to some other application.

So I still do not know how to solve this issue.
Sylvain mentionned the use of post-processing linker but I do not know what it is exactly.
Can somebody give me a hint please, like the class to extends and the runtime module method to overload ?

thanks.

Re: How to assign values to attributes [message #631134 is a reply to message #628912] Wed, 06 October 2010 09:55 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
Code your own linker that extends LazyLinker (you'll have to bind it in your runtime module). There, you override afterModelLinked. That method gets called once the model is linked.
There you can specify your own 'post-processing' mechanisms.

Then, you can iterate on the elements of the model and code the modifications you want to do.
Maybe the easiest way to do that is to use the EMF generated switch for your metamodel and code the caseXXXX methods for the elements you want to post-process.
You can link each EObject to it's corresponding grammar element and execute modifications depending on that.
private static AbstractElement getGrammarElement(EObject eObj){
	AbstractNode node = NodeUtil.getNode(eObj);
	AbstractElement grammarElement = (AbstractElement) node.getGrammarElement();
	return grammarElement;
}
Re: How to assign values to attributes[RESOLVED] [message #631140 is a reply to message #628912] Wed, 06 October 2010 10:18 Go to previous message
Sébastien  Gandon is currently offline Sébastien GandonFriend
Messages: 184
Registered: July 2009
Senior Member
Thanks for this quick answer.
I have found a more elegant way I think.

I have created a specyfic rule for the attribute I need to set
UniqueNameType returns talendFile::ElementParameterType : 
{talendFile::ElementParameterType} 
  		value=UNIQUE_NAME_RULE
//  		name="UNIQUE_NAME"
//  		field="TEXT"
;

UNIQUE_NAME_RULE : STRING;


and then I have overiden the AstFactory set method instead of the creation method this way

public class JobScriptRuntimeModule extends org.talend.metalanguage.jobscript.AbstractJobScriptRuntimeModule {

    @Override
    public Class<? extends IAstFactory> bindIAstFactory() {
        return JobScriptAstFactory.class;
    }

    static public class JobScriptAstFactory extends DefaultEcoreElementFactory {

        @Override
        public void set(EObject object, String feature, Object value, String ruleName, AbstractNode node)
                throws ValueConverterException {
            super.set(object, feature, value, ruleName, node);
            if ("UNIQUE_NAME_RULE".equals(ruleName)) {
                ElementParameterType elemParamType = (ElementParameterType) object;
                elemParamType.setName("UNIQUE_NAME");
                elemParamType.setField("TEXT");
            }
        }
    }
}

And this is working fine, I can set other features of my emf instance when setting this value.

pfuii, it took some thinking but I think that is a valid workaround Smile

[Updated on: Wed, 06 October 2010 10:19]

Report message to a moderator

Previous Topic:Form feed character in Lexer
Next Topic:Complex language example?
Goto Forum:
  


Current Time: Thu Apr 25 08:14:58 GMT 2024

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

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

Back to the top