复杂的条件元素在Jess规则中累积

时间:2014-12-27 08:26:32

标签: rules protege jess

我试图在JessTab中找到温度观测值的平均值,这需要加入来自多个类的事实。以下规则:

(defrule averageOfObsValue  
?res <- 
(accumulate  
    (progn (bind ?s 0)(bind ?n 0)) 
    (progn (bind ?s (+ ?s ?v)) (++ ?n)) 
    (create$ ?n ?s ?qo) 
    (and  
        (object (is-a http..#ObservationValue) 
                (OBJECT ?ov)
                (http..#hasDataValue ?v)
        )
        (object (is-a http..#SensorOutput) 
                (OBJECT ?so) 
                (http..#hasValue ?ov)
        )
        (object (is-a http..#Observation)
                (OBJECT ?o)
                (http..#observationResult ?so)
                (http..#qualityOfObservation ?qo)
        )   
    )
)
=>  
(bind ?q (nth$ 3 ?res))  
(bind ?s (nth$ 2 ?res))  
(bind ?n (nth$ 1 ?res))  
(if (= (?q getURI) "http..#Temperature") then
(printout t "Average value is " (/ ?s ?n) " of " ?n " temperature observations." crlf)))
WM中的

具有以下形式:

(defrule MAIN::averageOfObsValue 
   (or 
     (and 
       (object (is-a http..#ObservationValue) 
               (OBJECT ?ov) 
               (http..#isValueOf ?so) 
               (http..#hasDataValue ?v))) 
     (and 
       (object (is-a http..#SensorOutput) 
               (OBJECT ?so) (http..#isObservationResultOf ?o))) 
     (and 
       (object (is-a http..#Observation) 
               (OBJECT ?o) (http..#qualityOfObservation ?qo)))) 
   => 
   (bind ?q (nth$ 3 ?res)) 
   (bind ?s (nth$ 2 ?res)) 
   (bind ?n (nth$ 1 ?res)) 
   (if (= (?q getURI) "http..#Temperature") then
   (printout t "Average value is " (/ ?s ?n) " of " ?n " temperature observations." crlf)))

并在运行时出现以下错误:

  

Jess报告了例程Context.getVariable中的错误       执行时(n $ 3?res)       执行时(bind?q(nth $ 3?res))       执行默认MAIN :: averageOfObsValue655时       执行(运行)时。     消息:没有这样的变量res。     计划文本:(运行)第137行。

1 个答案:

答案 0 :(得分:0)

似乎(并且您的最新观察证实)累积不能应用于具有连接的CE。最好的方法是创建临时事实,其中包含需要计算平均值的值,但必须注意创建不同的临时事实

(deftemplate Value (slot v)(slot s)

(defrule createValueFacts
    (declare (salience 100))
    (object (is-a http..#ObservationValue) 
            (OBJECT ?ov)
            (http..#hasDataValue ?v)
    )
?s<-(object (is-a http..#SensorOutput) 
            (OBJECT ?so) 
            (http..#hasValue ?ov)
    )
    (object (is-a http..#Observation)
            (OBJECT ?o)
            (http..#observationResult ?so)
            (http..#qualityOfObservation ?qo)
    )
=>
    (assert (Value (v ?v)(s ?s))   ; using slot s to make fact unique
)

这些Value事实很容易累积:

(defrule averageOfObsValue 
?res <- (accumulate  
    (progn (bind ?s 0)(bind ?n 0)) 
    (progn (bind ?s (+ ?s ?v)) (++ ?n)) 
    (create$ ?n ?s) 
    (Value (v ?v)))
=>
(bind ?s (nth$ 2 ?res))  
(bind ?n (nth$ 1 ?res))  
(printout t "Average value is " (/ ?s ?n) " of " ?n " temperature observations." crlf))