0

我试图通过以下 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".

我哪里错了?

4

1 回答 1

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)?添加但不更改任何操作数。

于 2014-12-15T06:51:03.687 回答