根据变量显示事实

时间:2015-04-23 13:23:28

标签: clips

我的问题是,我想在步骤中保存答案,最后显示结果,但它不起作用。

我的代码:

(defglobal
    ?*name* = empty
    ?*country* = empty
    ?*age* = empty
)

(deffacts persons 
    (person John Chile 20 3000)
    (person Jacob Mexico 20 5500)
    (person Jacob Chile 20 3300)
    (person John Chile 20 8400)
)

(defrule Hello
=>
    (printout t "Name:" crlf)
    (bind ?*name* (read))
    (assert (step 1))
)

(defrule Step1
    (step 1)
=>
    (printout t "Country:" crlf)    
    (bind ?*country* (read))
    (assert (step 2))
)


(defrule Step2
    (step 2)
=>
    (printout t "Age:" crlf)
    (bind ?*age* (read))
    (assert (step 3))
)   

(defrule Step3
    (step 3)
    (person ?name ?country ?age ?money)
=>
    (printout t ?name "," ?country "," ?age "," ?money crlf)
)

我的解决方案:

(defrule Step3
        (step 3)
        (person ?name&?*name* ?country&?*country* ?age&?*age* ?money)
    =>
        (printout t ?name "," ?country "," ?age "," ?money crlf)
    )

但是这个解决方案会导致错误。

也许最好在每次选择后删除事实?

1 个答案:

答案 0 :(得分:1)

CLIPS> (clear)
CLIPS> 
(deftemplate person
   (slot name)
   (slot country)
   (slot age)
   (slot money))
CLIPS>    
(deffacts persons 
    (person (name John) (country Chile) (age 20) (money 3000))
    (person (name Jacob) (country  Mexico) (age 20) (money 5500))
    (person (name Jacob) (country Chile) (age 20) (money 3300))
    (person (name John) (country Chile) (age 20) (money 8400)))
CLIPS> 
(defrule get-input
   =>
   (printout t "Name: ")
   (assert (name (read)))
   (printout t "Country: ")    
   (assert (country (read)))
   (printout t "Age: ")
   (assert (age (read))))
CLIPS> 
(defrule print-match
   (person (name ?name) (country ?country) (age ?age) (money ?money))
   (name ?name)
   (country ?country)
   (age ?age)
   =>
   (printout t ?name ", " ?country ", " ?age ", " ?money crlf))
CLIPS> (reset)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (person (name John) (country Chile) (age 20) (money 3000))
f-2     (person (name Jacob) (country Mexico) (age 20) (money 5500))
f-3     (person (name Jacob) (country Chile) (age 20) (money 3300))
f-4     (person (name John) (country Chile) (age 20) (money 8400))
For a total of 5 facts.
CLIPS> (run)
Name: John
Country: Chile
Age: 20
John, Chile, 20, 3000
John, Chile, 20, 8400
CLIPS> (facts)
f-0     (initial-fact)
f-1     (person (name John) (country Chile) (age 20) (money 3000))
f-2     (person (name Jacob) (country Mexico) (age 20) (money 5500))
f-3     (person (name Jacob) (country Chile) (age 20) (money 3300))
f-4     (person (name John) (country Chile) (age 20) (money 8400))
f-5     (name John)
f-6     (country Chile)
f-7     (age 20)
For a total of 8 facts.
CLIPS>