我正在尝试用 Drools 替换 Jess 作为我们项目中的向后链接规则引擎。我一直在寻找有关如何使用 Drools 完成反向链接的简单示例。有趣的是,只有 1 个相同example http://www.slideshare.net/MarkProctor/reactive-transititive-closures-with-drools-backward-chaining在每个网站上(我不明白BC是怎么回事,但让我们暂时忘记它)。
Jess 中 BC 的一个非常简单的例子:
//q is a fact template with a slot named 'n'
//when there's a q with n==8 print something
//I need a q with n==8 to fire a rule so I will insert it myself!
(deftemplate q (slot n))
(do-backward-chaining q)
(defrule printq (q (n 8)) => (printout t "n is eight! yeah!" crlf))
(defrule iNeedn8 (need-q (n 8)) => (assert (q (n 8))))
(reset)
(run 1)
//fires printq and prints to console...
相当于流口水:
package com.example;
declare Q
n : int
end
rule "print q"
when
Q(n == 8)
then
System.out.println("n is eight by drools!");
end
//I'M LOST HERE! HELP!
如何使用 Drools 实现相同的行为?
在Drools中,BC的总体思想是使用查询。除了规则“print q”之外,您还需要:
query noQ( int $num )
Goal(num==$num) and not Q(num == $num)
end
rule goal when
Goal( $n: num )
noQ($n;)
then
Q q = new Q($n);
insert( q );
end
rule go when
then
insert( new Goal( 8 ) );
end
没有办法让 Drools 自己检测缺失的事实;您必须提供目标和查询来“弥合差距”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)