Search code examples
javagoogle-app-enginejsr223

User inputed formula parsing (eval)


Good day all,

I am trying to figure out how to allow users to call a method on some specified data.

I would like to provide a predefined set of functions:

  • Moving average, moving_ave(x,5) ..would be a 5 day moving average on x.
  • 3*x+y....and so on...

So basically, i will provide the users with various data series (x,y,z....) and a set of functions moving_ave, + - / * ....and they should be able to write simple formulas (restricted to the functions i provide).

how can this be done?

I will be deploying this on App Engine for Java.

so for i have found out about JSR-223...but i'm not sure if its appropriate? I am thinking i can use the Eval function.

Thanks,


Solution

  • It sounds like what you want is an interpreter for a simple grammar. Be very wary of approaches such as that suggested by Aerosteak; allowing your user to call functions in your code directly is dangerous, and it's easy to make mistakes sandboxing it, resulting in security vulnerabilities. It'll also require you to write your own parser.

    The easiest approach is probably to use an existing language - Javascript probably fits very well, and you can use Rhino, a Javascript interpreter written in Java.