rule being refired repeatedly

Anonymous
2009-05-04
2012-11-23
  • Anonymous - 2009-05-04

    If the LHS of a rule fires, say it matches an object, and the RHS modifies one of the fields in the object, then the state of the object is modified and the LHS may match again causing the RHS to fire adfinitum. How do you prevent this? 

    Example
    (defclass Man (is-a USER)
      (slot age (create-accessor read-write)))

    (defrule rule1 (is-a Man)
       (object (is-a A man) (age ?age)(name ?name))
       =>
        (printout "We have a man." crlf)
        (send ?name put-age (+ 1 ?age)))

    How do I stop this rule from firing again and again?

     
    • Gary Riley

      Gary Riley - 2009-05-04

      For object patterns, if an attribute isn't used as part of the condition of the rule, changes to that attribute won't retrigger the rule:

      CLIPS> (defclass MAN (is-a USER) (slot age))
      CLIPS>
      (defrule rule1
         ?m <- (object (is-a MAN))
         =>
         (send ?m put-age (+ (send ?m get-age) 1)))
      CLIPS> (make-instance jack of MAN (age 33))
      [jack]
      CLIPS> (send [jack] get-age)
      33
      CLIPS> (run)
      CLIPS> (send [jack] get-age)
      34
      CLIPS>

       
    • Anonymous - 2009-05-05

      How about two rules,each one referring to the other.

      (defclass Thing (is-a USER) (slot size) (slot age))

      (defrule rule1
         (object (is-a Thing) (size ?size)(name ?name))
         =>
         (send ?name set-age (+ (send ?name get-age) 1)))

      defrule rule2
         (object (is-a Thing) (age ?age) (name ?name))
         =>
         (send ?name set-size (+ (send ?name get-size) 1)))

      How do you prevent this from firing infinitely often?

       
    • Gary Riley

      Gary Riley - 2009-05-05

      You consider it a bug and fix the program behavior so that it doesn't loop. It's the same type of issue that you have with procedural programs where a function can recurse infinitely or a loop can never terminate.

       

Log in to post a comment.