Menu

Using logical operators in CLIPS

Help
methuselah
2012-11-25
2012-11-26
  • methuselah

    methuselah - 2012-11-25

    I am trying to define a less than/greater than rule in CLIPS but it doesn't seem to be working. Any idea on how I can fix it. The problem seems to be occurring at defrule btwn100and120.

    # (defrule part-credits
    #    (or (current-part "a")
    #        (current-part "b")
    #        (current-part "c"))
    #    =>
    #    (bind ?reply (get-text-from-user "How many points did you achieve?"))
    #    (assert (part-credits ?reply))
    # )
    #
    # (defrule btwn100and120
    #    (part-credits => 100)
    #    (part-credits <= 120)
    #    =>
    #    (bind ?reply (get-text-from-user "Did you Part A before the changes? (y/n)"))
    #    (assert (btwn100and120 ?reply))
    # )
    
     
  • Gary Riley

    Gary Riley - 2012-11-25
    (defrule btwn100and120
       (part-credits ?credits&:(and (numberp ?credits)
                                    (>= ?credits 100)
                                    (<= ?credits 120)))
       =>
       ...)    
    
     

    Last edit: Gary Riley 2012-11-25
  • methuselah

    methuselah - 2012-11-26

    Thanks for your reply Gary. This is my code in full. Unfortunately it still doesn't work:

    (defrule bsc-part-credits
        (or (bsc-current-part "a")
            (bsc-current-part "b")
            (bsc-current-part "c"))
        =>
        (bind ?reply (get-text-from-user "How many credits did you achieve?"))
        (assert (bsc-part-credits ?reply))
    )
    
    (defrule bsc-btwn100and120
       (bsc-part-credits ?credits&:(and (numberp ?credits)
                                    (>= ?credits 100)
                                    (<= ?credits 120)))
       =>
        (assert(conclusion "End."))
    )
    
    ; when all the reasoning has been done print out the conclusion(s)
    (defrule print-conclusion
        (declare (salience -10))
        (conclusion ?X)
        =>
        (message-box ?X)
    )
    
     

    Last edit: methuselah 2012-11-26
  • Gary Riley

    Gary Riley - 2012-11-26

    The rule portion appears to work OK. There is nothing in your code that would create a bsc-current-part fact, so perhaps that is your issue.

    CLIPS> 
    (defrule bsc-part-credits
        (or (bsc-current-part "a")
            (bsc-current-part "b")
            (bsc-current-part "c"))
        =>
        (bind ?reply 110)
        (assert (bsc-part-credits ?reply))
    )
    CLIPS> 
    (defrule bsc-btwn100and120
       (bsc-part-credits ?credits&:(and (numberp ?credits)
                                    (>= ?credits 100)
                                    (<= ?credits 120)))
       =>
        (assert(conclusion "End."))
    )
    CLIPS> 
    ; when all the reasoning has been done print out the conclusion(s)
    (defrule print-conclusion
        (declare (salience -10))
        (conclusion ?X)
        =>
        (printout t ?X crlf)
    )
    CLIPS> (assert (bsc-current-part "c"))
    <Fact-1>
    CLIPS> (agenda)
    0      bsc-part-credits: f-1
    For a total of 1 activation.
    CLIPS> (run 1)
    FIRE    1 bsc-part-credits: f-1
    CLIPS> (agenda)
    0      bsc-btwn100and120: f-2
    For a total of 1 activation.
    CLIPS> (run 1)
    FIRE    1 bsc-btwn100and120: f-2
    CLIPS> (agenda)
    -10    print-conclusion: f-3
    For a total of 1 activation.
    CLIPS> (run 1)
    FIRE    1 print-conclusion: f-3
    End.
    CLIPS> 
    
     

    Last edit: Gary Riley 2012-11-26

Log in to post a comment.