首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >剪辑初学者:剪辑规则如何解释事实顺序?

剪辑初学者:剪辑规则如何解释事实顺序?
EN

Stack Overflow用户
提问于 2021-05-17 18:34:27
回答 1查看 121关注 0票数 0

我想知道我每天做哪些活动,所以我构造了以下代码:

代码语言:javascript
复制
(deftemplate schedule
(slot activity)
(slot starthour)
(slot endhour)
)

(defrule r1
(schedule (activity ?a) (starthour ?start) (endhour ?end))
(not (busy ?start ?a))
=>
(assert (busy ?start ?a))
)

(defrule r2
(busy ?d ?a)
(schedule (activity ?a) (starthour ?start) (endhour ?end))
(test (< ?d ?end))
=>
(assert (busy ( + ?d 1) ?a))
)
CLIPS> (assert (schedule (activity reading) (starthour 3) (endhour 5)))
<Fact-1>
CLIPS> (assert (schedule (activity music) (starthour 4) (endhour 7)))
<Fact-2>

对于我插入的事实,我得到了一个不按天排序的结果。剪辑如何解释事实的顺序?有没有办法让我在一天内组合超过1个活动?

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-18 03:29:47

默认情况下,CLIPS使用深度优先策略来确定下一个要执行的规则,因此通常下一个执行的规则将由最后断言或收回的事实激活。基础编程指南中的5.3节“冲突解决策略”更详细地描述了此过程。

只要规则生成了正确的事实,您就不应该特别关心它们在事实列表中的放置顺序,因为1)使用事实列表来显示程序的输出是不实际的,2)强制特定的放置顺序可能是困难和过于复杂的。

相反,要对程序输出中的事实进行排序,请使用其中一个事实查询函数收集所有相关事实,然后在打印之前使用带有自定义比较器的排序函数对事实进行排序:

代码语言:javascript
复制
         CLIPS (6.4 2/9/21)
CLIPS> 
(deftemplate schedule
   (slot activity)
   (slot starthour)
   (slot endhour))
CLIPS>    
(deftemplate busy
   (slot activity)
   (slot hour))
CLIPS> 
(defrule r1
   (schedule (activity ?a) (starthour ?start) (endhour ?end))
   (not (busy (activity ?a) (hour ?start)))
   =>
   (assert (busy (activity ?a) (hour ?start))))
CLIPS> 
(defrule r2
   (busy (activity ?a) (hour ?d))
   (schedule (activity ?a) (starthour ?start) (endhour ?end))
   (test (< ?d ?end)) 
   =>
   (assert (busy (activity ?a) (hour (+ ?d 1)))))
CLIPS>    
(deffacts schedules
   (schedule (activity reading) (starthour 3) (endhour 5))
   (schedule (activity music) (starthour 4) (endhour 7)))
CLIPS>    
(deffunction busy-compare (?f1 ?f2)
    ;; Sort by hour
    (if (> (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
       then (return FALSE))
    (if (< (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
       then (return TRUE))
    ;; And then sort by activity
    (if (> (str-compare (fact-slot-value ?f2 activity) 
                        (fact-slot-value ?f1 activity)) 0)
       then (return FALSE)
       else (return TRUE)))
CLIPS>            
 (defrule print
    (declare (salience -10))
    =>
    (bind ?schedule (find-all-facts ((?f busy)) TRUE))
    (bind ?schedule (sort busy-compare ?schedule))
    (foreach ?s ?schedule
       (format t "%2d %s%n" (fact-slot-value ?s hour) (fact-slot-value ?s activity))))
CLIPS> (reset)
CLIPS> (run)
 3 reading
 4 music
 4 reading
 5 music
 5 reading
 6 music
 7 music
CLIPS> (facts)
f-1     (schedule (activity reading) (starthour 3) (endhour 5))
f-2     (schedule (activity music) (starthour 4) (endhour 7))
f-3     (busy (activity music) (hour 4))
f-4     (busy (activity music) (hour 5))
f-5     (busy (activity music) (hour 6))
f-6     (busy (activity music) (hour 7))
f-7     (busy (activity reading) (hour 3))
f-8     (busy (activity reading) (hour 4))
f-9     (busy (activity reading) (hour 5))
For a total of 9 facts.
CLIPS> 

这是另一种方法,在每个繁忙的事实中存储多个活动:

代码语言:javascript
复制
CLIPS> (clear)
CLIPS> 
(deftemplate schedule
   (slot activity)
   (slot starthour)
   (slot endhour))
CLIPS> 
(deftemplate busy
   (multislot activity)
   (slot hour))
CLIPS> 
(defrule r1
   (schedule (activity ?a) (starthour ?start) (endhour ?end))
   =>
   (loop-for-count (?hour ?start ?end)
      (assert (busy (activity ?a) (hour ?hour)))))
CLIPS> 
(defrule combine
   ?b1 <- (busy (activity $?a) (hour ?d))
   ?b2 <- (busy (activity ?n&:(not (member$ ?n ?a))) (hour ?d))
   =>
   (modify ?b1 (activity ?a ?n))
   (retract ?b2))
CLIPS> 
(deffacts schedules
   (schedule (activity reading) (starthour 3) (endhour 5))
   (schedule (activity music) (starthour 4) (endhour 7)))
CLIPS> 
(deffunction busy-compare (?f1 ?f2)
    (if (> (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
       then (return FALSE))
    (if (< (fact-slot-value ?f2 hour) (fact-slot-value ?f1 hour))
       then (return TRUE))
    (return FALSE))
CLIPS>     
 (defrule print
    (declare (salience -10))
    =>
    (bind ?schedule (find-all-facts ((?f busy)) TRUE))
    (bind ?schedule (sort busy-compare ?schedule))
    (foreach ?s ?schedule
       (format t "%2d %s%n" (fact-slot-value ?s hour) (implode$ (fact-slot-value ?s activity)))))
CLIPS> (reset)
CLIPS> (run)
 3 reading
 4 reading music
 5 reading music
 6 music
 7 music
CLIPS> (facts)
f-1     (schedule (activity reading) (starthour 3) (endhour 5))
f-2     (schedule (activity music) (starthour 4) (endhour 7))
f-5     (busy (activity music) (hour 6))
f-6     (busy (activity music) (hour 7))
f-7     (busy (activity reading) (hour 3))
f-8     (busy (activity reading music) (hour 4))
f-9     (busy (activity reading music) (hour 5))
For a total of 7 facts.
CLIPS> 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67568019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档