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 #1795903] Tue, 02 October 2018 07:48
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
Previous Topic:Testing: Different start symbol for parsing
Next Topic:Is it possible to write ValueConverter for EClass elements?
Goto Forum:
  


Current Time: Mon Sep 27 11:25:02 GMT 2021

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

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

Back to the top