2015-04-23 27 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