Jess规则中累积的复杂条件元素 [英] Complex conditional element in accumulate in Jess rules

查看:82
本文介绍了Jess规则中累积的复杂条件元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JessTab中找到温度观测值的平均值,这需要结合多个类别的事实.以下规则:

I am trying to find average value of temperature observations in JessTab which requires joining facts from multiple classes. The following rule:

(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中的

具有以下格式:

in the WM has the following form:

(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)))

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

and while running it the following error appears:

Jess报告了例行Context.getVariable中的错误 执行时(第3个$ res) 在执行时(绑定?q(nth $ 3?res)) 在执行defrule MAIN :: averageOfObsValue655时 在执行(运行)时. 消息:没有这样的可变分辨率. 程序文本:(运行)在第137行.

Jess reported an error in routine Context.getVariable while executing (nth$ 3 ?res) while executing (bind ?q (nth$ 3 ?res)) while executing defrule MAIN::averageOfObsValue655 while executing (run). Message: No such variable res. Program text: ( run ) at line 137.

推荐答案

似乎(并且您的最新观察证实了这一点),累加无法应用于带连词的CE.最好的方法是创建包含需要计算平均值的值的临时事实,但必须注意创建独特的临时事实.

It seems (and your latest observation confirms it) that accumulate cannot be applied to a CE with a conjunction. The best way is to create temporary facts containing the values over which the average needs to be computed, but care must be taken to create distinct temporary fact.

(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))

这篇关于Jess规则中累积的复杂条件元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆