JessTab:找到平均值

时间:2014-12-14 21:08:44

标签: protege jess

我试图通过以下JessTab规则找到在家庭本体中断言的人的平均年龄:

(defrule print_people_total_age 
   (object (https://wiki.csc.calpoly.edu/OntologyTutorial/family_example.owl#age ?a1)) 
   => 
   (bind ?s 0) 
   (bind ?num 0) 
   (foreach ?a (create$ ?a1) (+ ?s ?a) (++ ?num) (printout t "Total age " ?s " and average age is " (/ ?s ?num) " of persons" crlf)))

规则编译得很好,但是当激活错误时:

Jess reported an error in routine +
    while executing (+ ?s ?a)
    while executing (foreach ?a (create$ ?a1) (+ ?s ?a) (++ ?num) (printout t "Total age " ?s " and average age is " (/ ?s ?num) " of persons" crlf))
    while executing defrule MAIN::print_people_total_ageSSS
    while executing (run).
  Message: Not a number: "~@http://www.w3.org/2001/XMLSchema#integer 20".

我哪里错了?

1 个答案:

答案 0 :(得分:1)

您需要了解规则执行的基础知识,最值得注意的是,与规则匹配的每个事实(或事实集)都会导致执行此规则,并且 所有这些执行都独立于每个其他。 要合并几个事实中包含的数据,您可以使用累积CE;在更复杂的情况下,可能需要辅助事实。

(defrule sumofages
?res <- (accumulate (progn (bind ?s 0)(bind ?n))
                    (progn (bind ?s (+ ?s ?a)) (++ ?n))
                    (create$ ?n ?s)
                    (object (age ?a)))
=>
(bind ?s (nth$ 2 ?res))
(bind ?n (nth$ 1 ?res))
(printout t "Total age " ?s
            " and average age is " (/ ?s ?n) " of persons" crlf))

您还应该确保理解算术函数的基本工作原理。 (+ ?s ?a)?添加但不更改操作数。