apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chinmay Kolhatkar <chin...@datatorrent.com>
Subject Re: Java Expression Evaluator
Date Wed, 02 Dec 2015 13:28:24 GMT
Yes. There are be semi-complex expressions allowed.
For eg Condtional, and possibly simple Java statements as well.

~ Chinmay.

On Wed, Dec 2, 2015 at 6:26 PM, Priyanka Gugale <priyanka@datatorrent.com>
wrote:

> Your use case is always going to be as simple as using class fields to
> build the new field or you need to do any more processing?
> Like build output based on certain conditions etc? e.g. (age < 18, then set
> status: minor)
>
> -Priyanka
>
> On Wed, Dec 2, 2015 at 4:20 PM, Chinmay Kolhatkar <chinmay@datatorrent.com
> >
> 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.
> > ​
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message