用户输入的公式解析(eval)
大家好,
我正在尝试弄清楚如何允许用户对某些指定数据调用方法。
我想提供一组预定义的函数:
- 移动平均线, moving_ave(x,5) ..将是 x 上的 5 天移动平均线。
- 3*x+y...等等...
所以基本上,我将为用户提供各种数据系列(x,y,z ....)和一组< em>functions moving_ave, + - / * ....并且他们应该能够编写简单的公式(仅限于我提供的函数)。
这怎么办?
我将在 App Engine for Java 上部署它。
所以我已经找到了 JSR-223...但我不确定它是否合适?我想我可以使用 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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你想要的是一个简单语法的解释器。对 Aerosteak 建议的方法要非常警惕;允许用户直接调用代码中的函数是危险的,并且在沙箱中很容易出错,从而导致安全漏洞。它还要求您编写自己的解析器。
最简单的方法可能是使用现有的语言 - Javascript 可能非常适合,您可以使用 Rhino ,一个用 Java 编写的 Javascript 解释器。
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.
您将需要使用反射来调用未知方法。查看 Apache BeanUtil。
您可以有一个带有值的文本框:1,2,3,将这些值转换为对象数组。
拥有另一个组合框,其中包含您可以调用的所有可能的方法。
然后使用 Bean Util 来调用对象数组的方法。
例如:
class MyMathManager{
public void doCalculationType1(Object args...){..}
公共无效 doCalculationType2(对象参数...){..}
public void doCalculationType3(Object args...){..}
然后看BeanUtil的Java调用这些Method。
祝你好运。
You will need to use Reflection to call unknow Method. Look a Apache BeanUtil.
You can have a TextBox with the value: 1,2,3, Convert these values to Object Array.
Have another ComBo Box with all you possible Method you can call.
Then use Bean Util to call the method with the Object Array.
For Exemple:
class MyMathManager{
public void doCalculationType1(Object args...){..}
public void doCalculationType2(Object args...){..}
public void doCalculationType3(Object args...){..}
Then Look at the Java of BeanUtil to call these Method.
Good Luck.
这听起来像是可以在客户端而不是服务器上完成的事情。您可以编写一些方便的 JavaScript 函数,这些函数调用服务器上的 Restful API,以向一些更方便的 JavaScript 函数提供所需的数据,这些函数执行有用的计算。允许用户评估自己的客户端几乎总是安全的......他们在任何情况下都可以这样做。
This sounds like something that could probably be done on the client, rather than the server. You could write a few handy javascript functions that call a restful API on the server to provide the needed data to a few more handy javascript functions that do the useful calculations. It's almost always safe to allow users to eval on their own clients.. they can do it in any case.