How to assign values to attributes[RESOLVED] [message #628912] |
Fri, 24 September 2010 17:03 |
Sébastien Gandon 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 #629133 is a reply to message #628932] |
Mon, 27 September 2010 10:31 |
Sébastien Gandon 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 #630001 is a reply to message #628912] |
Thu, 30 September 2010 10:05 |
Sébastien Gandon 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[RESOLVED] [message #631140 is a reply to message #628912] |
Wed, 06 October 2010 10:18 |
Sébastien Gandon 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
[Updated on: Wed, 06 October 2010 10:19] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.05765 seconds