Reputation: 5533
Let's say I have the following facts defined:
(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)))
and I want to print the students information like this:
name id major nationality
How do I go about doing that? So far, I know that I can use (facts)
but it'll print the slot-name as well as the value.
I've also read about printout
but I don't know how to print specific facts with it (only value, no slot name)
How do I do print the facts values such that each fact goes on a single line?
Upvotes: 0
Views: 4761
Reputation: 10757
There are several ways to do this from a rule. The rule using do-for-all-facts is a technique that can also be used by entering the do-for-all-facts command at the command prompt. It's also the technique to use if you want the facts printed in the order they occur in the fact list. In each of the rules you could also use either the printout or format function.
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>
Upvotes: 2