需要对评估布尔逻辑树的指导 [英] Need guidance towards evaluative boolean logic tree

查看:108
本文介绍了需要对评估布尔逻辑树的指导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到正确方向的指针,我什至不确定我应该研究的术语是什么,但是无数小时的谷歌搜索似乎使我无所事事,所以希望堆栈溢出的智能可以提供帮助.

I can't seem to find a pointer in the right direction, I am not even sure what the terms are that I should be researching but countless hours of googling seem to be spinning me in circles, so hopefully the collective hive of intelligence of Stack Overflow can help.

问题是这样的,我需要一种方法来过滤我只能称为复合逻辑树的数据.当前,该系统实现了一个简单的AND过滤系统.例如,假设我们有一个人的数据集.您添加了一堆过滤器,以便向所有人显示(性别=女性)AND(年龄> 23)AND(年龄< 30)AND(状态=单身).很容易,遍历每个项目,仅在每个条件为真时才添加到有效的项目集合.

The problem is this, I need a way to filter data in what I can only call a compound logic tree. Currently the system implements a simple AND filtering system. For example, lets say we have a dataset of people. You add a bunch of filters such that show all the people where (Sex = Female) AND (Age > 23) AND (Age < 30) AND ( Status = Single). Easy enough, iterate through each item, add to a valid items collection only if every condition is true.

我遇到的问题是如何处理能够建立与and和or or有关的复杂查询的用户?我想到的是像树一样的树,其中每个节点代表一个表达式,并将其子级评估为true或false.一个简单的例子是-过滤到((性别==男性AND年龄== 25)或(性别==女性AND状态==单身))AND IQ>120.对不起,我想不出一个更好的例子此时此刻.但是,您将如何表示这种类型的表达式树,并针对这些过滤器评估集合中的项目.有哪些参考资料对您有帮助?地狱,有什么可恶的Google搜索可能会带来积极的方向?!

The problem I'm encountering is how do I handle the user being able to build complex queries involved and's and or's? I'm thinking of something like a tree where each node represents and expression evaluating its children to true or false. A simplistic example would be - filter down to ((Sex == Male AND Age == 25) OR (Sex == Female AND Status == Single)) AND IQ > 120. Sorry I can't think of a better example at the moment. But how would you go about representing this type of expression tree, and evaluating the items in a collection against these filters. What are some references that would help? Hell, what are some damn Google searching that might lead into a positive direction?!

感谢任何能提供帮助的人.

Thanks to anyone that can provide any help.

这是一个使用人员数据集的树状复合查询的示例

Here is an example of a compound query in tree form using a dataset of people

  • 查询-向我显示性别为男性,眼睛为绿色或性别为女性,眼睛为蓝色或状态为单身的所有人. 处于Paren形式(性别==男性&&&眼睛==绿色)|| (性别==女性&&(眼睛==蓝色||状态==单身))
  • Query - Show me all people where sex is male and eyes are green or sex is female, eyes are blue, or status is single. In Paren form (Sex==Male && Eyes == Green) || ( Sex == Female && ( Eyes == Blue || Status == Single))

所以我想的树状形式

o-Root Node
  - And - Sex = Male
     - And - Eyes = Blue
  - Or - Sex = Female
     - And Eyes = Blue
     - Or Status = Single

我相信解决方案是在诸如这样的数据结构中表示每个节点

I believe the solution is to represent each node such in a data structure like

Node
{
   OpType - AND or OR
   ExpressionField - The field to evaluate
   ExpressionOp -   =, !=, >, >=, <, <=
   ExpressionValue - the value to compare the field's value against

   Function Evaluate() - returns a bool
}

因此,对于给定的节点,请评估孩子,如果您是AND节点,则如果表达式的结果为true且所有AND子级的孩子都评估为true或任何OR子级的孩子都评估为true并递归,则返回true.

So for a given node, evaluate the chilren, if you are an AND node, then return true if your expression results in true and all your AND children evaluate to true or any OR child evaluates to true and recurse up.

似乎可以满足我可以提出的每一个概念性条件,但是自从我实现它之后,我们就会做到.我将在稍后的实际代码和工作图片中发布真实代码,以帮助其他人更好地描述此问题.

Seems to satisfy every conceptual condition I can throw at it, but we will since once I implement it. I will post the real code up later when its working and pictures to help describe this problem better for others.

推荐答案

您对表达式的解析((性别==男性AND年龄== 25)或(性别==女性AND状态==单身))AND IQ> 120看起来很奇怪.我将其解析为:

Your parsing of the expression ((Sex == Male AND Age == 25) OR (Sex == Female AND Status == Single)) AND IQ > 120 looks odd. I would parse it as:

* And
    * Or
        * And
            * ==
                * Sex
                * Male
            * ==
                * Eyes
                * Blue
        * And
            * ==
                * Sex
                * Female
            * ==
                * Status
                * Single
    * >
        * IQ
        * 120

树的类型为:

Node
{
    bool evaluate ()
}

AndNode : Node
{
    Node left
    Node right

    bool evaluate ()
    {
        return left.evaluate () && right.evaluate ()
    }
}

// OrNode is similar

EqualsNode : Node
{
    Field field
    Value value

    bool evaluate ()
    {
        return field.value () == value
    }
}

// Likewise for <, >, etc

这篇关于需要对评估布尔逻辑树的指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆