1

我希望通过使用函数来获取朋友列表。

(deffunction get-freinds-list (?age)

  (bind ?facts (find-all-facts ((?f userdata))
                               (and (eq ?f:name userdata)
                                    (>= ?f:age ?age))))

return ?facts)

(defrule getfriends
   (wantlist yes) 
=> 
   (printout t "under what age you want list for?" crlf)
   (bind ?age (read))
   (printout t "list is=" (get-freinds-list ?age ) crlf)) 


(defrule main-control
(initial-fact)
=>
(assert(wantlist yes))
)

(deffacts userfact 
(userdata(name "pranay" )(likes tea cricket badminton table farewell)(age 12)(location pakistan)(employer Oracle))
(userdata(name "rohan" )(likes lunch kabaddi tt khoko farewell)(age 10)(location china)(employer TCS))
(userdata(name "srinath" )(likes dinner kabaddi cricket farewell)(age 15)(location china)(employer TCS))
(userdata(name "prateek" )(likes dinner kabaddi cricket farewell drinks)(age 15)(location china)(employer TCS))
(userdata(name "sachin" )(likes drinks kabaddi cricket)(age 15)(location china)(employer TCS))
)

(deftemplate userdata "Knoweledge base"

(slot name)
(multislot likes)
(slot age)
(slot location)
(slot employer)
)
4

1 回答 1

0
CLIPS> 
(deftemplate userdata "Knowledge base"
   (slot name)
   (multislot likes)
   (slot age)
   (slot location)
   (slot employer))
CLIPS>    
(deffunction get-friends-list (?age)
  (bind ?list (create$))
  (do-for-all-facts ((?f userdata)) (>= ?f:age ?age)
     (bind ?list (create$ ?list ?f:name)))
  return ?list)
CLIPS> 
(defrule getfriends
   (wantlist yes) 
   => 
   (printout t "under what age you want list for? ")
   (bind ?age (read))
   (printout t "list is " (implode$ (get-friends-list ?age)) crlf)) 
CLIPS> 
(deffacts main-control
   (wantlist yes))
CLIPS> 
(deffacts userfact 
   (userdata (name "pranay") (likes tea cricket badminton table farewell) (age 12) (location pakistan) (employer Oracle))
   (userdata (name "rohan") (likes lunch kabaddi tt khoko farewell) (age 10) (location china) (employer TCS))
   (userdata (name "srinath") (likes dinner kabaddi cricket farewell) (age 15)(location china) (employer TCS))
   (userdata (name "prateek") (likes dinner kabaddi cricket farewell drinks) (age 15) (location china)(employer TCS))
   (userdata (name "sachin") (likes drinks kabaddi cricket) (age 15) (location china) (employer TCS)))
CLIPS> (reset)
CLIPS> (run)
under what age you want list for? 13
list is "srinath" "prateek" "sachin"
CLIPS> (reset)
CLIPS> (run)
under what age you want list for? 9
list is "pranay" "rohan" "srinath" "prateek" "sachin"
CLIPS> 
于 2013-05-01T19:22:53.123 回答