假设我定义了以下事实:
(deffacts MAIN::students
(student (student-name X) (student-id 1) (major CS) (nationality UK))
(student (student-name Y) (student-id 2) (major CS) (nationality USA))
(student (student-name Z) (student-id 3) (major CIS) (nationality FR))
(student (student-name W) (student-id 4) (major SE) (nationality FR))
(student (student-name Q) (student-id 5) (major CIS) (nationality USE))
(student (student-name U) (student-id 6) (major CS) (nationality UK)))
我想打印这样的学生信息:
姓名id主要国籍
我该怎么做呢?到目前为止,我知道我可以使用(facts)
,但它会打印插槽名称以及值。
我也读过printout
,但我不知道如何用它打印特定的事实(只有值,没有插槽名称)
如何打印事实值,以便每个事实都在一行上?
答案 0 :(得分:1)
从规则中有几种方法可以做到这一点。使用do-for-all-facts的规则是一种技术,也可以通过在命令提示符下输入do-for-all-facts命令来使用。如果您希望按照事实列表中出现的顺序打印事实,也可以使用该技术。在每个规则中,您还可以使用打印输出或格式化功能。
CLIPS>
(deftemplate student
(slot student-name)
(slot student-id)
(slot major)
(slot nationality))
CLIPS>
(deffacts students
(student (student-name X) (student-id 1) (major CS) (nationality UK))
(student (student-name Y) (student-id 2) (major CS) (nationality USA))
(student (student-name Z) (student-id 3) (major CIS) (nationality FR))
(student (student-name W) (student-id 4) (major SE) (nationality FR))
(student (student-name Q) (student-id 5) (major CIS) (nationality USE))
(student (student-name U) (student-id 6) (major CS) (nationality UK)))
CLIPS>
(defrule rule-1
(student (student-name ?sn) (student-id ?si) (major ?m) (nationality ?n))
=>
(printout t "rule-1: " ?sn " " ?si " " ?m " " ?n crlf))
CLIPS>
(defrule rule-2
?f <- (student)
=>
(printout t "rule-2: " (fact-slot-value ?f student-name) " "
(fact-slot-value ?f student-id) " "
(fact-slot-value ?f major) " "
(fact-slot-value ?f nationality) crlf))
CLIPS>
(defrule rule-3
=>
(do-for-all-facts ((?f student)) TRUE
(format t "rule-3: %s %d %s %s%n" ?f:student-name ?f:student-id ?f:major ?f:nationality)))
CLIPS> (reset)
CLIPS> (run)
rule-1: U 6 CS UK
rule-2: U 6 CS UK
rule-1: Q 5 CIS USE
rule-2: Q 5 CIS USE
rule-1: W 4 SE FR
rule-2: W 4 SE FR
rule-1: Z 3 CIS FR
rule-2: Z 3 CIS FR
rule-1: Y 2 CS USA
rule-2: Y 2 CS USA
rule-1: X 1 CS UK
rule-2: X 1 CS UK
rule-3: X 1 CS UK
rule-3: Y 2 CS USA
rule-3: Z 3 CIS FR
rule-3: W 4 SE FR
rule-3: Q 5 CIS USE
rule-3: U 6 CS UK
CLIPS>