简体   繁体   English

在Java的布尔表达式树中找到所有可能的组合

[英]Find all possible combinations in a boolean expression tree in java

I was trying to find all possible combination of expressions in a given condition. 我试图找到给定条件下所有可能的表达式组合。

For example, consider the following condition. 例如,请考虑以下情况。

((b > 5) | (c > 4)) & ((c < 6) | (b < 2))) 

I need the final combination like 我需要像这样的最终组合

[(b > 5) , (c < 6)]
[(b > 5) , (b < 2)]
[(c > 4) , (c < 6)]
[(c > 4) , (b < 2)]

The logic is just like (a|b)&(c|d) -> a&c,a&d,b&c,b&d . 逻辑就像(a|b)&(c|d) -> a&c,a&d,b&c,b&d The comma (,) above can be considered as an AND operator. 上面的comma (,)可被视为AND运算符。

Since a tree is the best solution to parse expressions, I managed to build an Abstract Syntax Tree using a Recursive Descent Parser using java with the tree looking like the following. 由于树是解析表达式的最佳解决方案,因此我设法使用递归下降解析器(使用Java)使用递归下降解析器构建了抽象语法树,树的结构如下所示。

布尔表达式树

I use a modified version of parser as described in this blog. 我使用了博客中描述的解析器的修改版本。 This is where I am stuck as I don't how to extract the final expressions. 这是我被困住的地方,因为我不提取最终表达式。

Is there any inputs to which direction I could try. 是否有我可以尝试的输入。

Other examples consider the following condition. 其他示例考虑以下条件。

((b > 5) & ((c < 6) | (b < 2))) 

Result: 结果:

[(b > 5) , (c < 6)]
[(b > 5) , (b < 2)]

consider the following condition. 考虑以下情况。

((b > 5) | ((c < 6) & (b < 2))) 

Result: 结果:

[(b > 5)]
[(c < 6) , (b < 2)]

No need for a tree here. 这里不需要树。 You can begin by making a 2D matrix of the conditions. 您可以先创建条件的2D矩阵。 so for 因此对于

        (b > 5) | (c > 4)) & ((c < 6) | (b < 2)))

The matrix would be 2X2 and look like: 矩阵为2X2,如下所示:

(b > 5) (c > 4)
(c < 6) (b < 2)

In any row the condition between the neighbors would be OR and in any column the condition between neighbors would be AND. 在任何行中,邻居之间的条件将为OR,在任何列中邻居之间的条件将为AND。 Now all you have to do generate all possible combinations of this 2D matrix. 现在您要做的就是生成此2D矩阵的所有可能组合。 For that you can follow How to get 2D array possible combinations 为此,您可以按照如何获得2D数组的可能组合进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM