Reputation: 11
I'm having some trouble with this problem and would appreciate it if anyone could help me with it! My code asks the user for characteristics of a gem (hardness, density and colors) and returns its name. However, the user should be prompted until he or she responds with one of the colors listed here(red, pink, yellow, brown, green, blue, violet, black, white, colorless) and I don't know how to do that even after spending hours on this...
(deftemplate characteristics
(slot name)
(slot hardness)
(slot density)
(multislot colors)
(deffacts gems
(characteristics (name Diamond) (hardness 10) (density 3.52) (colors yellow brown green blue white colorless))
(characteristics (name Corundum) (hardness 9) (density 4) (colors red pink yellow brown green blue violet black white colorless))
(characteristics (name Chrysoberyl) (hardness 8.5) (density 3.72) (colors yellow brown green))
(characteristics (name Spinel) (hardness 8) (density 3.6) (colors red pink yellow brown green blue violet white colorless))
(characteristics (name Topaz) (hardness 8) (density 3.52) (colors red pink yellow brown blue violet white colorless))
(characteristics (name Beryl) (hardness 7.5) (density 2.7) (colors red pink yellow brown green blue white colorless))
(characteristics (name Zircon) (hardness 6) (density 4.7) (colors yellow brown green violet white colorless))
(characteristics (name Quartz) (hardness 7) (density 2.65) (colors yellow brown green blue violet white black colorless))
(characteristics (name Tourmaline) (hardness 7) (density 3.1) (colors red pink yellow brown green blue white black colorless))
(characteristics (name Peridot) (hardness 6.5) (density 3.3) (colors yellow brown green))
(characteristics (name Jadeite) (hardness 6.5) (density 3.3) (colors red pink yellow brown green blue violet white black colorless))
(characteristics (name Opal) (hardness 5.5) (density 2) (colors red pink yellow brown white black colorless))
(characteristics (name Nephrite) (hardness 5) (density 2.9) (colors green white black colorless))
(characteristics (name Turquoise) (hardness 5) (density 2.7) (colors blue)))
(defrule get-input
=>
(printout t "What is the hardness?")
(assert (hardness (read)))
(printout t "What is the density?")
(assert (density (read)))
(printout t "What is/are the color(s)?")
(assert (colors (read))))
(defrule what-gem-is-this
(hardness ?hardness)(density ?density)(colors ?colors)
(characteristics(name ?name1)(hardness ?hardness1)(density ?density1)(colors $?colors1))
(test (= ?hardness ?hardness1))
(test (= ?density ?density1))
(test (member$ ?colors ?colors1))
=>
(printout t "The gem described is a: " ?name1 crlf))
Upvotes: 1
Views: 253
Reputation: 10757
Define a function that you can pass a question and a set of allowed values:
CLIPS (6.31 4/1/19)
CLIPS>
(deffunction ask-question
(?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer)))
(while (not (member ?answer ?allowed-values)) do
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then
(bind ?answer (lowcase ?answer))))
?answer)
CLIPS> (ask-question "What is the color? " yellow brown green blue white colorless red pink violet black)
What is the color? orange
What is the color? purple
What is the color? red
red
CLIPS>
Here's how you can check values using rules:
CLIPS>
(defrule get-color
(not (color ?))
=>
(printout t "What is the color? ")
(assert (color (read))))
CLIPS>
(defrule bad-color
?f <- (color ~yellow&~brown&~green&~blue&~white&~colorless&~red&~pink&~violet&~black)
=>
(retract ?f))
CLIPS>
(defrule get-hardness
(not (hardness ?))
=>
(printout t "What is the hardness? ")
(assert (hardness (read))))
CLIPS>
(defrule bad-hardness
?f <- (hardness ?h)
(test (or (not (numberp ?h))
(< ?h 1)
(> ?h 10)))
=>
(retract ?f))
CLIPS> (run)
What is the hardness? very
What is the hardness? 20
What is the hardness? 10
What is the color? orange
What is the color? blue
CLIPS>
Upvotes: 1