JESS VS滴料:反向链接(JESS vs DROOLS : Backward chaining)

2019-10-23 08:19发布

我试图通过Drools在我们的项目反向链接规则引擎来取代杰西。 我一直在寻找如何反向链接与Drools的做简单的例子。 有趣的是,这里只有1同样的例子在每一个网站(我不明白它是如何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的相同的行为?

Answer 1:

在Drools中,BC的总体思路是使用查询。 除了您的规则“打印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通过自身所有检测丢失的事实; 你必须提供目标和查询以“缩小差距”。



Answer 2:

从杰斯功能的启发,有一个实验,在开发的功能,给你一个类似的行为。 下面是测试会是什么样子:

@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();


}


文章来源: JESS vs DROOLS : Backward chaining
标签: drools jess