查找片段中匹配的最佳规则

时间:2018-06-23 20:19:47

标签: clips expert-system

我是CLIPS的新手,在个人项目的开发过程中,我想实现一项功能,使我能够在事实插入后查看具有最多匹配模式的规则。 为了更好的理解:

>(defrule one
        (fact a)
        (fact b)
=>
        (assert (fact i)))

>(defrule two
        (fact b)
        (fact c)
=>
        (assert (fact d) (fact f)))

>(defrule three
        (fact a)
        (fact d)
        (fact c)
=>
        (assert (fact g)))


> (assert (fact a) (fact c))
> (trace-rule)
rule three 
Matches for Pattern 1
f-1
Matches for Pattern 2
None
Matches for Pattern 3
f-2

也许我可以使用matchs命令,但是我不知道如何进行。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

         CLIPS (6.30 3/17/15)
CLIPS> 
(deffunction most-matches ()
   (bind ?rules (create$))
   (bind ?most -1)
   (foreach ?r (get-defrule-list)
      (bind ?matches (nth$ 1 (matches ?r terse)))
      (if (= ?matches ?most)
         then
         (bind ?most ?matches)
         (bind ?rules (create$ ?rules ?r))
         else
         (if (> ?matches ?most)
            then
            (bind ?most ?matches)
            (bind ?rules (create$ ?r)))))
   ?rules)
CLIPS>    
(defrule one
   (fact a)
   (fact b)
   =>
   (assert (fact i)))
CLIPS> 
(defrule two
   (fact b)
   (fact c)
   =>
   (assert (fact d)
           (fact f)))
CLIPS> 
(defrule three
   (fact a)
   (fact d)
   (fact c)
   =>
   (assert (fact g)))
CLIPS> (assert (fact a) (fact c))
<Fact-2>
CLIPS> (most-matches)
(three)
CLIPS> (reset)
CLIPS> (assert (fact b))
<Fact-1>
CLIPS> (most-matches)
(one two)
CLIPS> (reset)
CLIPS> (most-matches)
(one two three)
CLIPS>