Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Is it possible to write ValueConverter for EClass elements?
Is it possible to write ValueConverter for EClass elements? [message #1795907] Tue, 02 October 2018 08:28 Go to next message
Venkata Chittem is currently offline Venkata ChittemFriend
Messages: 13
Registered: October 2018
Junior Member
Hi buddies,
I am able to follow the procedure explained in the link below and write successfully a Value Converter for a ecore::EInt datatype.
_ttp://www.scispike.com/blog/xtext-valueconverter/

However i have a case in which I would like to call a backend converter to convert string in the rule to the EMF EClass Object.

CustomEClass is EClass from my custom EMF Ecore model.

Below are the Xtext Rules:
CustomEClass_RULE returns CustomEClass:
{CustomEClass}
('BoolVal' VALID_STRING ('true'|'false')) |
('FloatVal' VALID_STRING REAL_NUM) |
('IntVal' VALID_STRING SIGNED_INT)
;

terminal VALID_STRING: (('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*);
UNSIGNED_INT: (('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9')+);
SIGNED_INT: ('-'?UNSIGNED_INT);
REAL_NUM: (SIGNED_INT '.' UNSIGNED_INT);

Class Registering converter (MyTerminalConverters.java):
public class MyTerminalConverters
extends DefaultTerminalConverters
implements IValueConverterService {

@Inject
private CustomEClassConverter customEClassConverter;
@ValueConverter(rule = "CustomEClass_RULE")
public IValueConverter<CustomEClass> CustomEClassConverter() {
return customEClassConverter;
}
...
...
}

Value Converter class (customEClassConverter.java)
@Singleton
public class customEClassConverter implements IValueConverter<customEClass> {

@Override
public customEClass toValue(String string, INode node) throws ValueConverterException {
System.exit(1);
if (Strings.isEmpty(string)) {
throw new ValueConverterException("Numerical value can not be empty string", node, null);
} else {
try {
customEClass retVal = myEmfFactory.eINSTANCE.createcustomEClass();
//TODO: retVal.setType(...) based on input "string"
//TODO: retVal.setValue(...) based on input "string"
return retVal;
} catch (NumberFormatException e1) {
throw new ValueConverterException("Couldn't convert '"+string+"' to Double/Floating Point Value ", node, null);
}
}
}

@Override
public String toString(customEClass value) throws ValueConverterException {
return null;
}
}


There is no problem in xtext generation and also in building the code, however In the runtime customEClassConverter is not being called for converting the user input string to customEClass type object.

Best Regards,
Venkata
Re: Is it possible to write ValueConverter for EClass elements? [message #1795930 is a reply to message #1795907] Tue, 02 October 2018 13:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
What do you actually want to achieve

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Is it possible to write ValueConverter for EClass elements? [message #1795945 is a reply to message #1795930] Tue, 02 October 2018 16:11 Go to previous messageGo to next message
Venkata Chittem is currently offline Venkata ChittemFriend
Messages: 13
Registered: October 2018
Junior Member
@Christian: thanks for your reply!
Context: I have a huge legacy xsd file, which I have used to create my ecore model and emf plugins. This xsd, and ecore model derived from it, have many EClasses and a typical xmi file created would have longest path of the tree (of EObjects) running into 20~50 Eclasses (one Contained in another).
I have started writing grammar file (.xtext) for this ecore model. It is actually going good; however i find that the number of rules (one per EClass or EDataType) in my grammar file already more than ~50 (with only <0.1% of ecore classes covered so far).

What I am trying to achieve: to reduce the number of rules and the complexity of grammar file, i though I can write the grammar to accept input for current EClass and also for one or more "contained" EClasses in one grammar rule and pass this string (with one or more enum values/attributes/keywords in a format specified in the rule) to a Value Converter. This Value Converter will parse this string and create instance of EClass, along with its contained EClasses instantiated and assigned to respective attributes, along with other attributes of EDataTypes and Enums initialized appropriately.

In a nutshell, i would like to a rule (and associated callbacks such as Value Converter) that sets attributes of a EClass, instantiates "Contained" Eclasses and sets their attributes also based on formatted text in the rule input.
Re: Is it possible to write ValueConverter for EClass elements? [message #1795948 is a reply to message #1795945] Tue, 02 October 2018 16:39 Go to previous messageGo to next message
Venkata Chittem is currently offline Venkata ChittemFriend
Messages: 13
Registered: October 2018
Junior Member
I have written the rule and respective value converter implementing IValueConverter<T>, however this callback is not being called (i checked by putting break point). however similar callbacks written for Data Types such as FeatureMap.Entry, String, EInt are being called correctly.
Based on xtext documentation and also based on the online references or conversation threads, I am thinking ValueConverter works only for EDataTypes. I might be wrong! I am looking for other approaches for EClasses.
Re: Is it possible to write ValueConverter for EClass elements? [message #1795950 is a reply to message #1795948] Tue, 02 October 2018 17:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
There is nothing that would allow this easily

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Is it possible to write ValueConverter for EClass elements? [message #1795951 is a reply to message #1795950] Tue, 02 October 2018 19:50 Go to previous message
Venkata Chittem is currently offline Venkata ChittemFriend
Messages: 13
Registered: October 2018
Junior Member
ok thanks
Previous Topic:Is it possible to write ValueConverter for EClass elements?
Next Topic:Xtext Runtime exceptions
Goto Forum:
  


Current Time: Sat Apr 20 03:56:54 GMT 2024

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

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

Back to the top