Need guidance towards evaluative boolean logic tree

前端 未结 5 1353
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 16:29

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

相关标签:
5条回答
  • 2021-02-04 16:48

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

    0 讨论(0)
  • 2021-02-04 17:04

    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
    
    0 讨论(0)
  • 2021-02-04 17:06

    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.

    0 讨论(0)
  • 2021-02-04 17:09

    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 
    
    0 讨论(0)
  • 2021-02-04 17:11

    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.

    0 讨论(0)
提交回复
热议问题