Miltos Taramanidis
Miltos Taramanidis

Reputation: 23

CLIPS code no result

I am new to CLIPS and I can't figure out why my code doesn't run. Actually I get no errros, but when I press run the facts don't change and the goal is never found. The project is that we have 2 bottles. Bottle A with capacity 3 and B with capacity 2. The goal is A has 0 and B has 1. We can do it with these rules: fillA,fillB,emptyA,emptyB,moveAB,moveBA. Below is my code. Can someone help?thanks!

(deffacts problem
   (name A cap 3 water 0)
   (name B cap 2 water 0)
)

(defrule goal
 (declare (salience 100))
 (name A water 0)
 (name B water 1)
=>
 (printout t "FOUND" crlf)
 (halt)
)

(defrule start
 (initial-fact)
=>
 (set-strategy random)
)

(defrule emptyA
 ?x<-(name A water ?w)
=>
 (retract ?x)
 (assert (name A water 0))
)

(defrule emptyB
 ?x<-(name B water ?w)
=>
 (retract ?x)
 (assert (name B water 0))
)

(defrule fillA
 ?x<-(name A water ?w)
=>
 (retract ?x)
 (assert (name A water 3))
)

(defrule fillB
 ?x<-(name B water ?w)
=>
 (retract ?x)
 (assert (name B water 2))
)

(defrule moveAB
 ?x<-(name A water ?w)
 ?y<-(name B water ?water)
 (test (not (< ?w 0)))
 (test (not (> ?water 2)))
=>
 (retract ?x ?y)
 (assert (name B water ?w))
 (assert (name A water ?water))
)

(defrule moveBA
 ?x<-(name A water ?w)
 ?y<-(name B water ?water)
 (test (not (> ?w 3)))
 (test (not (< ?water 0)))
=>
 (retract ?x ?y)
 (assert (name A water ?water))
 (assert (name B water ?w))
)

Upvotes: 0

Views: 67

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

All of your fact patterns match name facts with three fields. Your name facts have five fields. There needs to be an exact match. For example, instead of the following pattern:

(name B water ?w)

You should use:

(name B cap ? water ?w)

or:

(name B cap ?c water ?w)

Alternately, use deftemplate facts so that you can specify only the slots of interest in your patterns:

(deftemplate bottle
   (slot name)
   (slot cap)
   (slot water (default 0)))

(deffacts problem
   (bottle (name A) (cap 3))
   (bottle (name B) (cap 2)))

(defrule fillA
   ?x <- (bottle (name A ) (water ?w))
   =>
   (modify ?x (water 3)))

Upvotes: 1

Related Questions