Menu

How to implement AND / OR logic

Help
Young Park
2010-03-08
2019-06-06
  • Young Park

    Young Park - 2010-03-08

    I am a newbee in PyKE, went over the tutorial, installed, ran examples.
    Now I am writing rulebase of my own, and encountered a missing hole.

    Suppose that I made a fact base consisting of simple facts.
    Then made rulebase of FC and BC rules composing from the elementary facts.
    How can i make rules of AND and OR conditions?
    For example, (A and B) or C or (D and E) in Prolog would be something like this:
    P :- (A, B); C; (D, E)

    I would appreciate much for such initial help.

    • Young Park
     
  • Bruce Frederiksen

    Facts are AND-ed be default in Pyke when you list more than one of them in a rule.

    Pyke does not have OR-ing within a rule.  Instead, you would use multiple rules.  Using your Prolog example:

    P :- A, B.
    P :- C.
    P :- D, E.
    

    or, in Pyke:

    rule1
        use P( )
        when
            A( )
            B( )
    rule2
        use P( )
        when
            C( )
    rule3
        use P( )
        when
            D( )
            E( )
    

    -Bruce

     
  • Young Park

    Young Park - 2010-03-10

    Thank you Bruce.
    Then how to implement rules like the following?
    P :- A, (B; C), (D; E).
    i.e. A and (B or C) and (D or E).

    Thank you in advance for your help.

    • Young Park
     
  • Bruce Frederiksen

    You can do this by creating subgoals:

    P :- A, P2, P3.
    P2 :- B.
    P2 :- C.
    P3 :- D.
    P3 :- E.

    -Bruce

     
  • Angle

    Angle - 2019-06-06

    You can, actually, make OR rules, though it's kind of a hack:

    if_C_or_D_then_F
        foreach
            $facts = ('C', 'D')
            python satisfied = False;
            forall
                $fact in $facts
                test.fact($fact)
                python satisfied = True;
            check satisfied is True
        assert
            test.fact(F)
    
     

    Last edit: Angle 2019-06-06

Log in to post a comment.