开发者

Counting Existing Facts in the RHS of Clips Rules

开发者 https://www.devze.com 2023-01-14 11:47 出处:网络
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

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 synta开发者_StackOverflowx error.


(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)))))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号