2015-04-23 55 views
0

我的问题是我想保存答案的步骤和结束显示结果,但它不起作用。根据变量显示事实

我的代码:

(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
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> 
+0

你能告诉我为什么加载时返回False吗? //但代码有效 – Enteee

+0

文件中有错误。可能是构造之间的额外字符。该文件应该只包含deftemplates,deffacts和defrules。不知道你放在文件中的内容或加载时输出的内容,我只能推测出问题所在。 –

+0

我必须在加载之前使用(清除)? – Enteee