0
votes

i am new to clips rule engine and have to do following in clips in mobile app. if a person buys x amount of product A give him y discount.

Following is the code i have written to first check if my productid is in orders.

(deftemplate Producttemp
(slot productid (type INTEGER))
(slot umid)
(slot quantity (type INTEGER))
)

(deffacts orders
(Producttemp (productid 123) (umid CG) (quantity 4))
(Producttemp (productid 456) (umid CG) (quantity 2))
)



(defrule checkorder
=>
 (printout t "Enter the productid: ")
  (bind ?p1 (readline))
   (printout t "Enter another quantity: ")
   (bind ?p2 (readline))

   (do-for-all-facts ((?o Producttemp)) 
       (and (eq ?p1 ?o:p1)
            (eq ?p2 ?o:p2))
       (printout t ?p1 " is a " ?o: productid" in order " ?p2 crlf)))
)

I am getting following error.

Defining defrule: checkorder 
[PRCCODE3] Undefined variable o: referenced in RHS of defrule.
1

1 Answers

2
votes

There's a space between ?o: and product in the last printout statement. There's also an extraneous right parenthesis at the end of the rule. This rule will load without any syntax errors:

(defrule checkorder
   =>
   (printout t "Enter the productid: ")
   (bind ?p1 (readline))
   (printout t "Enter another quantity: ")
   (bind ?p2 (readline))

   (do-for-all-facts ((?o Producttemp)) 
       (and (eq ?p1 ?o:p1)
            (eq ?p2 ?o:p2))
       (printout t ?p1 " is a " ?o:productid" in order " ?p2 crlf))
)

There are two other issues with your rule. Within the fact query you reference the p1 and p2 slots of the Producttemp fact with the references ?o:p1 and ?o:p2, but these slots do not exist. Perhaps you intend the productid and quantity slots. You also use the readline function to get input. This will return a string, but your fact slots contain symbols and integers, not strings, so any comparisons between these values using the eq function will fail since the types are not the same. You should use the read function instead.