找到最大的树

时间:2017-03-07 08:12:49

标签: clips

我的结果太多了。怎么了?如prolog中那样剪切结果不存在?

(deffacts mytree
    (below birch poplar)
    (above linden maple)
    (below pine fir)
    (below linden birch)
    (above pine poplar))


(defrule high-low-tree
    (below ?tree1  ?tree2)
    (not (above ?tree1  ?tree2))    

    (or (above ?tree2  ?tree1)
(not (above ?tree2  ?tree1)))
    =>
    (printout t "The tallest tree " ?tree2 crlf)
    (printout t "The lowest tree " ?tree1 crlf))

1 个答案:

答案 0 :(得分:1)

找到最高和最低的:

CLIPS> 
(deffacts mytree
    (tree birch)
    (tree poplar)
    (tree linden)
    (tree maple)
    (tree pine)
    (tree fir)
    (below birch poplar)
    (above linden maple)
    (below pine fir)
    (below linden birch)
    (above pine poplar))
CLIPS> 
(defrule high-low-tree
   (tree ?tallest)
   (tree ?lowest)
   (not (below ?tallest ?))
   (not (above ? ?tallest))
   (not (below ? ?lowest))
   (not (above ?lowest ?))
   =>        
   (printout t "The tallest tree " ?tallest crlf)
   (printout t "The lowest tree " ?lowest crlf))
CLIPS> (reset)
CLIPS> (run) 
The tallest tree fir
The lowest tree maple
CLIPS>

对所有人进行排名:

CLIPS> (clear)
CLIPS>    
(deffacts mytree
    (tree birch)
    (tree poplar)
    (tree linden)
    (tree maple)
    (tree pine)
    (tree fir)
    (below birch poplar)
    (above linden maple)
    (below pine fir)
    (below linden birch)
    (above pine poplar)
    (tree-list))
CLIPS> 
(defrule order-trees
   (tree ?tallest)
   ?t <- (tree-list $?list)
   (test (not (member$ ?tallest ?list)))
   (not (above ?above&:(not (member$ ?above ?list)) ?tallest))
   (not (below ?tallest ?below&:(not (member$ ?below ?list))))
   =>        
   (retract ?t)
   (assert (tree-list ?list ?tallest)))
CLIPS>    
(defrule print-list
   (declare (salience -10))
   (tree-list $?list)
   =>
   (printout t "Trees (tallest to lowest): " (implode$ ?list) crlf))
CLIPS> (reset)
CLIPS> (run)
Trees (tallest to lowest): fir pine poplar birch linden maple
CLIPS>