评估运行时生成的java中的布尔表达式
如何计算 Java 程序运行时生成的复杂布尔表达式?
示例:
(x 和 y 或 z) 和 s
以及 x、y、z 布尔变量...
谢谢
How to evaluate complex boolean expressions generated at runtime in a Java program?
Example:
(x and y or z) and s
with x, y, z boolean variables ...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 http://docs.codehaus.org/display/JANINO/Home 进行最少的工作。我能做的不仅仅是简单的表达。
Use http://docs.codehaus.org/display/JANINO/Home for minimum work. I can do much more than simple expressions.
简而言之,您需要布尔表达式的“中间表示”。这是一棵由
Node
对象组成的树。Node
具有子类AndNode
、OrNode
、NotNode
和VariableNode
。AndNode
有两个子Node
,OrNode
有两个子Node
,以及一个NotNode
有一个子Node
。VariableNode
只有一个变量名称字符串,例如“x”。您将拥有一个HashMap
,其中每个变量名称键都有一个关联的布尔值。每个 Node 类都有一个
eval()
方法,用于计算其表达式并返回一个boolean
。VariableNode.eval()
方法在HashMap
中查找变量的值并返回它。NotNode.eval()
返回!child.eval()
。AndNode.evaluate()
返回child1.eval() && child2.eval()
,而OrNode.evaluate()
返回child1.eval() || child2.eval()。要计算整个布尔表达式树,只需调用根节点的
eval()
方法。您可以使用 Java 构造函数等以编程方式构建这些布尔表达式树。
如果您想从字符串构建表达式树,则需要编写一个解析器来从字符串生成树。 Terence Parr 的语言实现模式对此进行了非常简单明了的介绍。
Very briefly, you need an "intermediate representation" of the Boolean expressions. This is a tree formed of
Node
objects.Node
has ths subclassesAndNode
,OrNode
,NotNode
, andVariableNode
. AnAndNode
has two childNode
s, anOrNode
has two childNode
s, and aNotNode
has one childNode
.A
VariableNode
has just a variable name String, eg, "x". You would have aHashMap<String, Boolean>
where each variable name key has an associated Boolean value.Each Node class has an
eval()
method that evaluates its expression and returns aboolean
. TheVariableNode.eval()
method looks up the value of the variable in yourHashMap
and returns it.NotNode.eval()
returns!child.eval()
.AndNode.evaluate()
returnschild1.eval() && child2.eval()
, whileOrNode.evaluate()
returnschild1.eval() || child2.eval()
. To evaluate an entire Boolean expression tree, just call the root node'seval()
method.You can build these Boolean expression trees programmatically, using Java constructors, etc.
If you want to build your expression trees from strings, you'll need to write a parser that produces a tree from a string. Terence Parr's Language Implementation Patterns is a very simple and clear introduction to this.
如何评估逻辑表达式?诸如此类的逻辑表达式可以作为语法树进行评估,我认为这个相关问题中有一些很好的信息逻辑表达式解析器
浮现在脑海中的另一件事是,您希望能够将逻辑表达式作为数据处理,这似乎更适合 Jython、JRuby、Groovy 或 Scala 等脚本语言(假设您'仅限于 JVM)。尽管我怀疑编写一个解析器来处理基本和/或/非逻辑表达式会非常困难。
How to evaluate a logical expression? Logical expressions such as those can be evaluated as a syntax tree, and I think that there's some good information in this related question Logic expression parser
The other thing that springs to mind is that you want to be able to handle logical expressions as data, which seems like something more suited to a scripting language like maybe Jython, JRuby, Groovy or Scala (assuming you're restricted to the JVM). Although I doubt it'd be very hard to write a parser to handle basic and/or/not logical expressions.
您必须生成一个表达式树并将每个叶子绑定到一个布尔值。要解析此表达式并生成 AST,请查看 Dijkstra 的 Shunting Yard 算法。一切都在那里解释并且相当容易实施。
You will have to generate an expression tree and bind each leaf to a boolean value. For parsing this expression and generating an AST take a look at Dijkstra's Shunting Yard algorithm. Everything is explained in there and is fairly straight forward to implement.