修改触发规则的当前事实

时间:2014-06-07 13:01:11

标签: rule clips

说我已经定义了一个模板和一些事实,如下所示:

(deftemplate student
    (slot name (type SYMBOL) (default ?NONE))
    (slot grade (type SYMBOL) (default C) (allowed-symbols A B C D))
    (slot graduated (type SYMBOL) (default no) (allowed-symbols yes no))
)

(deffacts insert-facts
    (student (name George) (grade A))
    (student (name Nick) (grade C))
    (student (name Bob))
    (student (name Mary) (grade B))
)

假设我想创建一个规则来检查每个学生的成绩 将相应的分级变量设置为符号“是”'。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

这是您为解决问题而提出的规则略微冗长的版本:

CLIPS> 
(deftemplate student
    (slot name (type SYMBOL) (default ?NONE))
    (slot grade (type SYMBOL) (default C) (allowed-symbols A B C D))
    (slot graduated (type SYMBOL) (default no) (allowed-symbols yes no)))
CLIPS> 
(deffacts insert-facts
    (student (name George) (grade A))
    (student (name Nick) (grade C))
    (student (name Bob))
    (student (name Mary) (grade B)))
CLIPS> 
(defrule rule-1
   ?s <- (student (grade A|B) (name ?n) (graduated ~yes))
   =>
   (modify ?s (graduated yes))
   (printout t "Congratulations " ?n "!" crlf))
CLIPS> (reset)
CLIPS> (run)
Congratulations Mary!
Congratulations George!
CLIPS> 

答案 1 :(得分:0)

我已经解决了这个问题。我把它留在这里以防其他人需要它。

我已按以下方式创建规则

(defrule rule-1
    ?s <- (student (grade ?g&A|B) (name ?n) (graduated ?gr) (classified ?c&no))
=>
    (retract ?s)
    (assert (student (name ?n)(grade ?g) (graduated yes) (classified yes)))
    (printout t "Congratulations " ?n "!" crlf)
)

并以下列方式修改了学生模板,以防止程序陷入无限循环。

(deftemplate student
    (slot name (type SYMBOL) (default ?NONE))
    (slot grade (type SYMBOL) (default C) (allowed-symbols A B C D))
    (slot graduated (type SYMBOL) (default no) (allowed-symbols yes no))
    (slot classified (type SYMBOL) (default no) (allowed-symbols yes no))
)