Question

I have a wine defined like this:

(deftemplate wine
  (slot name)
  (slot color)
  (slot certainty (type NUMBER) (default 0)))

And the list dof wines defined like this:

(deffacts wines
  (wine (name "Chardonnay") (color white))
  (wine (name "Merlot") (color red))
  (wine (name "Cabernet sauvignon") (color red)))

Now, in case a rule gets triggered, I'd like to increase certainty value for items in a list which have a color slot set to "red".

Any ideas how to accomplish this?

Was it helpful?

Solution

I'm new to CLIPS so I'm sure there is a better way but the following rules do what you want:

(defrule inc-wines-with-color
  (increase-all-with color ?color ?amount)
  (wine (name ?name) (color ?color))
  =>
  (assert (increase-certainty ?name ?amount)))

(defrule retract-inc-all-with
  ?f <- (increase-all-with $?)
  =>
  (retract ?f))

(defrule increase-wine-certainty
  (not (increase-all-with $?))
  ?ic <-(increase-certainty ?name ?amount)
  ?wine <- (wine (name ?name) (certainty ?c))
  =>
  (printout t "Incrementing " ?name " from " ?c " to " (+ ?amount ?c) crlf)
  (modify ?wine (certainty (+ ?amount ?c)))
  (retract ?ic))

Here are the results of running it:

CLIPS> (reset)
CLIPS> (assert (increase-all-with color red 0.2))
<Fact-4>
CLIPS> (run)
Incrementing Merlot from 0 to 0.2
Incrementing Cabernet sauvignon from 0 to 0.2
CLIPS> (facts)
f-0     (initial-fact)
f-1     (wine (name "Chardonnay") (color white) (certainty 0))
f-7     (wine (name "Merlot") (color red) (certainty 0.2))
f-8     (wine (name "Cabernet sauvignon") (color red) (certainty 0.2))
For a total of 4 facts.

Note: You may need to set your conflict resolution strategy to LEX or MEA to guarantee proper ordering of the rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top