0

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

在 Jess 中映射 A 类:

(mapclass http...#A)

由于没有 A 的直接实例,因此计数实例的规则给了我 0:

起初:

(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 的子类的实例。

修改版:

(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 在执行规则 LHS (TEQ) 时执行 (instanceof ?_20_x(0,2,-1) http...#A) 时报告例程 instanceof 错误,同时执行规则 LHS (TECT)。消息:找不到类:http...#A。程序文本: ( defrule countAinstances ?c <- (accumulate ( bind ?count 0 ) ( bind ?count ( + ?count 1 ) ) ?count ( object ( OBJECT ?x & : ( instanceof ?x http...#A ) ) ) ) = > ( printout t ?c "A 类实例的数量。" crlf ) ) 在第 20 行。

嵌套异常是:http...#A

4

2 回答 2

1

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

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

如果未导入 A,则使用完整的类名。

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

之后

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

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))
于 2015-01-03T10:15:47.853 回答
0

完整的解决方案规则如下:

(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))
于 2015-01-03T14:21:55.963 回答