JESS vs DROOLS:向后链接

时间:2015-06-10 13:28:18

标签: drools jess

我试图用Drools替换Jess作为我们项目中的反向链接规则引擎。我正在寻找关于如何使用Drools进行反向链接的简单示例。有趣的是,每个网站上只有1个example(我不知道它是怎样的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...

Drools中的等价物:

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实现相同的行为?

2 个答案:

答案 0 :(得分:1)

在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独自检测到遗漏的事实;你必须提供目标和查询以弥补差距"。

答案 1 :(得分:1)

受Jess功能的启发,有一个实验性的开发中功能可以为您提供类似的行为。以下是测试的样子:

@Test
public void testFindQ8() {
    String droolsSource =
        " package org.drools.abductive.test;                 " +
        "                                                    " +
        " import " + Abducible.class.getName() + ";          " +
        " global java.util.List list;                     \n " +
        "                                                    " +
        " declare Q                                          " +
        "    @Abducible                                      " +
        "    id : int @key                                   " +
        " end                                             \n " +
        "                                                    " +
        " query foo( int $x )                                " +
        "    @Abductive( target=Q.class )                    " +
        "    not Q( $x; )                                    " +
        " end                                             \n " +
        "                                                    " +
        " rule R1                                            " +
        " when                                               " +
        "    $x := foo( 8 ; )                                " +
        " then                                               " +
        "    System.out.println( 'R1 returned ' + $x );      " +
        " end                                             \n " +
        "                                                    " +
        " rule R2                                            " +
        " when                                               " +
        "    $q : Q( 8; )                                    " +
        " then                                               " +
        "    System.out.println( 'We have 8!' );             " +
        " end                                                ";
    /////////////////////////////////////

    KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( droolsSource, ResourceType.DRL ).build().newKieSession().fireAllRules();


}