JessTab:我们可以统计所有的子类实例吗?

JessTab: Can we count all subclass instances?

我有一个class A,它由两个子classes B 和C 组成。Class B 有三个实例,而C 有两个。我可以编写一个 JessTab 规则来计算 A 的所有隐式实例,即给我 5 个吗?

映射 class Jess 中的 A:

(mapclass http...#A)

计算实例的规则给出了 0,因为没有 A 的直接实例:

原来:

(defrule countAinstances 
?c <- (accumulate (bind ?count 0) 
(bind ?count (+ ?count 1)) 
?count 
(object (ís-a http...#A))
) 
=> 
(printout t ?c " number of class A instances." crlf))

这不计算 A.class 的子实例

修改版本:

(defrule countAinstances 
?c <- (accumulate (bind ?count 0) 
(bind ?count (+ ?count 1)) 
?count 
(object (OBJECT ?x&:(instanceof ?x http...#A)))
) 
=> 
(printout t ?c " number of class A instances." crlf))

出现以下错误:

Jess reported an error in routine instanceof while executing (instanceof ?_20_x(0,2,-1) http...#A) while executing rule LHS (TEQ) while executing rule LHS (TECT). Message: Class not found: http...#A. Program text: ( defrule countAinstances ?c <- ( accumulate ( bind ?count 0 ) ( bind ?count ( + ?count 1 ) ) ?count ( object ( OBJECT ?x & : ( instanceof ?x http...#A ) ) ) ) = > ( printout t ?c " number of class A instances." crlf ) ) at line 20.

Nested exception is: http...#A

访问对象依赖Jess自带的函数instanceof,不是protege slot is-a:

(object (OBJECT ?x&:(instanceof ?x A))) 

如果未导入 A,请使用完整的 class 名称。

如果您没有 Java class 名称,则必须测试该模式中的所有 classes 和 subclasses,使用(或)函数。比较乏味。

以后

看到 is-a 包含 class 可以与 class 名称进行比较的引用,这将是编写规则的最简单方法:

defrule countAinstances
?c <- (accumulate (bind ?count 0) 
(bind ?count (+ ?count 1)) 
?count 
(object (is-a ?x&B|C))
      )
=> 
(printout t ?c " number of subclass instances." crlf))

完整解法如下:

(defrule countAinstances
?c <- (accumulate (bind ?count 0) 
(bind ?count (+ ?count 1)) 
?count 
(object (is-a ?x&:(or (= ?x B)(= ?x C))))
)
=> 
(printout t ?c " number of subclass instances." crlf))