CLIPS如何总结事实

时间:2018-05-21 15:38:58

标签: clips

让我们说我有以下两个deftemplates:

(deftemplate total-crop-yield-quality (slot primary_key) (slot quality))
(deftemplate total-fertilizer-quality (slot primary_key) (slot quality))

以及以下deffacts:

   (deffacts tQuality 
    (total-crop-yield-quality (primary_key 1) (quality 10))
    (total-crop-yield-quality (primary_key 2) (quality 7))
    (total-crop-yield-quality (primary_key 3) (quality 9))
    (total-crop-yield-quality (primary_key 4) (quality -2)))

   (deffacts fQuality 
    (total-fertilizer-quality (primary_key 1) (quality 4))
    (total-fertilizer-quality (primary_key 2) (quality 0))
    (total-fertilizer-quality (primary_key 3) (quality 2))
    (total-fertilizer-quality (primary_key 4) (quality 5)))

如何将两个deffacts的所有质量的总和总结为一个变量?

(defrule determine-total-quality
  =>
 //code here to add quality to a variable)

1 个答案:

答案 0 :(得分:0)

         CLIPS (6.31 2/3/18)
CLIPS> (deftemplate total-crop-yield-quality (slot primary_key) (slot quality))
CLIPS> (deftemplate total-fertilizer-quality (slot primary_key) (slot quality))
CLIPS> 
(deffacts tQuality 
   (total-crop-yield-quality (primary_key 1) (quality 10))
   (total-crop-yield-quality (primary_key 2) (quality 7))
   (total-crop-yield-quality (primary_key 3) (quality 9))
   (total-crop-yield-quality (primary_key 4) (quality -2)))
CLIPS> 
(deffacts fQuality 
   (total-fertilizer-quality (primary_key 1) (quality 4))
   (total-fertilizer-quality (primary_key 2) (quality 0))
   (total-fertilizer-quality (primary_key 3) (quality 2))
   (total-fertilizer-quality (primary_key 4) (quality 5)))
CLIPS>    
(defrule determine-total-quality
  =>
  (bind ?sum 0)
  (do-for-all-facts ((?f total-crop-yield-quality)) TRUE
     (bind ?sum (+ ?sum ?f:quality)))
  (printout t "Crop Yield Quality: " ?sum crlf)
  (bind ?sum 0)
  (do-for-all-facts ((?f total-fertilizer-quality)) TRUE
     (bind ?sum (+ ?sum ?f:quality)))
  (printout t "Fertilizer Quality: " ?sum crlf))
CLIPS> (reset)
CLIPS> (run)
Crop Yield Quality: 24
Fertilizer Quality: 11
CLIPS>