Question

How do you test for fact existence in the RHS of rules in Clips? I'm trying to design a rule that will "score" the current state by counting how many goal facts exist. It's obvious how to test for facts in the LHS, but I can't find how to do the equivalent in the RHS.

I want to do something like this, albeit this isn't valid Clips code:

(defrule score-state ""
    ?score <- (score (value ?value))
    =>
    (modify ?score (value (+ (exists (goal-1)) (exists (goal-2)) (exists (goal-3))))))

So that if none of the goal states exist, then there would exist the fact (score (value 0)). Conversely, if all the goal states existed, then there would exist the fact (score (value 3)).

The reference manual mentions the function fact-existp, but this seems to require it's passed a fact address. I tried using it like (fact-existp (goal-1)), but Clips gives me a syntax error.

Was it helpful?

Solution

(deftemplate score
   (slot value))

(deftemplate goal-1)

(deftemplate goal-2)

(deftemplate goal-3)

(deffacts start
   (score (value undefined))
   (goal-1)
   (goal-3))

(deffunction ecount (?g)
   (if (any-factp ((?f ?g)) TRUE)
      then (return 1)
      else (return 0)))

(defrule score-state ""
    ?score <- (score (value undefined))
    =>
    (modify ?score (value (+ (ecount goal-1) (ecount goal-2) (ecount goal-3)))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top