The use case is already fully covered by PojoUtils that is part of Malhar. Please take a look and let me know if you have any questions how to use it.

Thank you,

Vlad

On 12/2/15 02:50, Chinmay Kolhatkar wrote:

Hi All,

We’re evaluating a expression evaluator for our use case.

*Example Use Case:*
The expressions needs to contain Java specific code for evaluating once and running the same for every tuple.
For e.g. a POJO has following definition:

|public class POJO { String firstname; // Firstname String lastname; // Lastname Date dob; // Date of birth } |

From this POJO, we need to generate fullname as concatenation of firstname & lastname and age which will be derived from dob field.
The expressions for those might look like following:
For full name : ${inp.firstname} + “ “ + ${inp.lastname}
For Age : new Date().getYear() - ${inp.dob}.getYear()

Currently, I have a implementation using Janino library for expression evaluation. Code (ExpressionEvaluator.java) and Test code (Main.java) attached. As performance is an important concern, we chose a custom evaluator using Janino’s fast script evaluator.

*Design of the custom expression evaluator:*

    /ExpressionEvaluator class is used for evaluating expressions
    which takes multiple parameter object and the result is returned
    for that expression./
    /
    /
    /The way to reference a variable in an object is
    ${placeholder.varname}./
    /The variable will be resolved to its accessible variable or
    getter method in order. After this the variable can be used as if
    its a Java variable./
    /
    /
    /ExpressionEvaluator also allows you to set extra imports that
    needs to be added over default is java.lang.*/
    /
    /
    /ExpressionEvaluator needs to be configured with following
    configurations as minimal configuration:/
    /1. Mapping of input object place holders to it corresponding types./
    /    This can be done with setInputObjectPlaceholders method./
    /2. Return type of of expression eveluation./
    /3. Expression to be evaluated. This is a standard java expression
    except for referencing the variable inside object JEL syntax needs
    to be used i.e. ${objectPlaceHolder.varName}/

*Example Use of custom expression evaluator:*

|ExpressionEveluator ee = new ExpressionEvaluator(); // Let expression evaluator know what are the object mappings present in expressions and their class types. ee.setInputObjectPlaceholders(new String[]{"input"}, new Class[]{Test.class}); // Generate expression for finding age from Date object. String expression = "${input.firstname} + \" \" + ${input.lastname}"; ExpressionEvaluator.DataGetter<String> getter4 = ee.createGetter(expression, String.class); inp1.firstname = "ABC"; inp1.lastname = "XYZ"; String fullname = getter4.get(inp1); System.out.println("Fullname is: " + fullname); |

*Output:*

|Fullname is: ABC XYZ |


Can you please suggest for any improvements in this OR is there a better option to achieve expression evaluation?

Can this code possibly go into Malhar library?

~ Chinmay.

​

Reply via email to