Qdibs
Qdibs

Reputation: 11

CLIPS program testing strings

Trying to ask for a star's spectral class and return those that meet the requirement. I am getting a syntax error in my p1 rule.

(deftemplate star
   (slot name)
   (slot spectral)
   (slot magnitude)
   (slot distance))

(deffacts stars
   (star (name "Sirius") (spectral "A") (magnitude 1) (distance 8.8))
   (star (name "Canopus") (spectral "F") (magnitude -3) (distance 98)) 
   (star (name "Arcturus") (spectral "K") (magnitude 0) (distance 36)) 
   (star (name "Vega") (spectral "A") (magnitude 1) (distance 26)) 
   (star (name "Capella") (spectral "G") (magnitude -1) (distance 46)) 
   (star (name "Rigel") (spectral "B") (magnitude -7) (distance 880)) 
   (star (name "Procyon") (spectral "F") (magnitude 3) (distance 11)) 
   (star (name "Betelgeuse") (spectral "M") (magnitude -5) (distance 490)) 
   (star (name "Altair") (spectral "A") (magnitude 2) (distance 16)) 
   (star (name "Aldebaran") (spectral "K") (magnitude -1) (distance 68)) 
   (star (name "Spica") (spectral "B") (magnitude -3) (distance 300)) 
   (star (name "Antares") (spectral "M") (magnitude -4) (distance 250))
   (star (name "Pollux") (spectral "K") (magnitude 1) (distance 35))
   (star (name "Deneb") (spectral "A") (magnitude -7) (distance 1630)) )

(defrule start-up 
   ?i <- (initial-fact) 
   => 
   (printout t "Stars spectral class?: ") 
   (bind ?y (read)) 
   (assert (spectral ?y)) 
   (retract ?i))

(defrule P1
   ?char <- (star (name ?n) (spectral ?s) (magnitude ?m) (distance ?d))
   (spectral ?y)
   (test (eq ?s ?y)
   => 
   (printout t ?n ", " ?s ", " ?m ", " ?d crlf)
   (retract ?char)
   (assert (found)))

Defining deftemplate: star
Defining deffacts: stars
Defining defrule: start-up +j+j
Defining defrule: P1
[PRNTUTIL2] Syntax Error:  Check appropriate syntax for test conditional element.

ERROR:
(defrule MAIN::P1
   ?char <- (star (name ?n) (spectral ?s) (magnitude ?m) (distance ?d))
   (spectral ?y)
   (test (eq ?s ?y)=>
FALSE
CLIPS>

Upvotes: 1

Views: 47

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

There is a missing right parenthesis at the end of the test conditional element:

(defrule P1 
   ?char <- (star (name ?n) (spectral ?s) (magnitude ?m) (distance ?d))
   (spectral ?y) 
   (test (eq ?s ?y)) ;; <--
   => 
   (printout t ?n ", " ?s ", " ?m ", " ?d crlf) 
   (retract ?char)
   (assert (found)))

Your spectral classes are specified as strings in the deffacts rather than symbols, so you have to enter "A", "B", etc. when prompted by the read function in order for your P1 rule to work. Change the values from strings to symbols (e.g., "A" to A) in the deffacts and then you can just enter a single letter in response to the read.

Use of the initial-fact was deprecated in CLIPS 6.3 and is no longer supported in CLIPS 6.4, so you should remove it from your start-up rule:

(defrule start-up 
   => 
   (printout t "Stars spectral class?: ") 
   (bind ?y (read)) 
   (assert (spectral ?y)))

Upvotes: 0

Related Questions