Search code examples
clips

Find all facts matching a condition in Defrule


I have two type of facts: state1 and state2, both are arrays of numbers. I want to check if a fact with tag 'state' with current value of state2 variable exists. How can I do it?

For example, if now facts are:
State 1 2 3
State2 4 5 6
State 0 8 9,
And current state2 is 1 2 3, I want to get into RHS because first row 'State 1 2 3' matches current value of State2.

I tried following solution, but I never enter RHS.

;;; END FUNCTION, EXECUTES WHEN SOLUTION WAS FOUND
(defrule complete (declare (salience 1000))
      (exists (state ?state2))
      => (printout t "Target state [1 2 3 8 0 4 7 6 5] reached" crlf) (halt)
);

Solution

  •          CLIPS (6.31 6/12/19)
    CLIPS> 
    (defrule complete 
       (exists (state $?state)
               (state2 $?state))
        =>
        (printout t "Target state [1 2 3 8 0 4 7 6 5] reached" crlf))
    CLIPS> 
    (assert
       (state 1 2 3)
       (state2 4 5 6)
       (state 0 8 9)
       (state2 1 2 3))
    <Fact-4>
    CLIPS> (agenda)
    0      complete: *
    For a total of 1 activation.
    CLIPS> (run)
    Target state [1 2 3 8 0 4 7 6 5] reached
    CLIPS>