需要评估布尔逻辑树的指导

发布于 2024-08-13 07:51:29 字数 1411 浏览 10 评论 0原文

我似乎找不到正确方向的指针,我什至不确定我应该研究的术语是什么,但无数个小时的谷歌搜索似乎让我在转圈,所以希望 Stack 的集体智慧蜂巢溢出可以提供帮助。

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

我遇到的问题是如何处理用户能够构建涉及 and 和 or 的复杂查询?我正在考虑类似树的东西,其中每个节点代表和表达式评估其子节点的真或假。一个简单的例子是 - 过滤为 ((Sex == Male AND Age == 25) OR (Sex == Female AND St​​atus == Single)) AND IQ > 120. 抱歉,我现在想不出更好的例子。但是,您将如何表示这种类型的表达式树,并根据这些过滤器评估集合中的项目。有哪些参考资料可以提供帮助?天哪,谷歌搜索有哪些可能会带来积极方向的搜索?!

感谢任何可以提供帮助的人。

以下是使用人物数据集的树形复合查询示例

  • 查询 - 向我显示性别为男性且眼睛为绿色的所有人,或性别为女性、眼睛为蓝色或状态为单身的人。 以Paren形式(性别==男性&&眼睛==绿色)|| (性别 == 女性 && (眼睛 == 蓝色 || 状态 == Single))

所以以树的形式我认为

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

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

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。

似乎满足了我可以提出的每一个概念条件,但一旦我实现了它,我们就会满足。我稍后会在其工作时发布真实的代码和图片,以帮助其他人更好地描述这个问题。

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.

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.

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

  • 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))

So In tree form im Thinking

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
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〃安静 2024-08-20 07:51:29

您对表达式 ((Sex == Male AND Age == 25) OR (Sex == Female AND St​​atus == Single)) AND IQ > 的解析120看起来很奇怪。我将其解析为:

* 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

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

The tree type would be :

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
鯉魚旗 2024-08-20 07:51:29

这些类型的查询通常以由 ANDed 子句组成的 OR 数组表示。也就是说,这是一种表格格式,您可以在其中将多个条件AND组合在一起读取,然后向下读取以OR它们。这会导致一些条件重复,但对于用户来说很容易阅读、编写和理解。您的样本 ((性别 == 男性 AND 年龄 == 25) OR (性别 == 女性 AND 状态 == 单身)) AND IQ > 120 看起来像

Sex == Male   & Age == 25        & IQ > 120 
Sex == Female & Status == Single & IQ > 120 

These kinds of queries are often presented as an ORed array of ANDed clauses. That is, a tabular format in which you read across multiple conditions ANDed together, and then read down to OR them. That leads to some repetition of conditions, but is easy for users to read, write, and understand. Your sample ((Sex == Male AND Age == 25) OR (Sex == Female AND Status == Single)) AND IQ > 120 would look like

Sex == Male   & Age == 25        & IQ > 120 
Sex == Female & Status == Single & IQ > 120 
嗳卜坏 2024-08-20 07:51:29

您可能需要在 Google 上搜索“谓词演算”和“合取范式”等术语。

You might want to Google for terms such as 'predicate calculus' and 'conjunctive normal form'.

七禾 2024-08-20 07:51:29

不得不说,这就是数据库引擎诞生的原因。你可以用设定的逻辑完成你需要的所有事情,甚至可能得到你正在寻找的结果,但这些都是数据库和 SQL 解决的标准问题。您还可以查看 linq 的代码解决方案。

I have to say that this is why database engines are built. You can do all that you require with set logic and you may even arrive at the result you are looking for, but theses are standard problems solved by databases and SQL. You can also look at linq for a in code solution.

原来是傀儡 2024-08-20 07:51:29

听起来您需要创建一个允许创建简单解析树的用户界面。当按下 GO 时,您可以遍历树并从该用户界面结构创建 LINQ 表达式树。执行 LINQ 查询,然后根据需要处理结果。因此,我建议您阅读 LINQ 表达式树。

Sounds like you need to create a user interface that allows the creation of a simple parse tree. When the presses GO you can then walk the tree and create a LINQ expression tree from that user interface structure. Execute the LINQ query and then process the results as needed. I would therefore recommend you read up on LINQ expression trees.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文