【问题标题】:JESS vs DROOLS : Backward chainingJESS vs DROOLS:反向链接
【发布时间】:2015-08-25 19:01:15
【问题描述】:

我正在尝试用 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 实现相同的行为?

【问题讨论】:

  • Drools BW 链接与 Jess 的 BW 链接不同,我认为两者都做不到 Prolog 能够做的事情。
  • 我不能找到要在 Drools 中触发的规则缺少哪个事实 (Q(n==8)) 吗? (它们的功能不同,jess 在 BWC 中的能力更强?)
  • 我同意@laune 的观点,这两个引擎实现 BWC 的方式完全不同,而且 afaik 在这个意义上都没有与 Prolog 相同的功能。但是,该功能正在开发中。它将在稳定后尽快正式记录。
  • @DavideSottara 这个“正式记录为稳定”听起来不错,但问题是:它会被记录吗?

标签: drools jess


【解决方案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 自己检测缺失的事实;您必须提供目标和查询以“缩小差距”。

【讨论】:

    【解决方案2】:

    受 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();
    
    
    }
    

    【讨论】:

    • 谢谢,以这种方式实施可能不会那么痛苦。是否有任何关于 @Abductive 的文档(它是如何工作的),我没有看到任何文档?
    • 对于@Abducible 也是如此?
    • 如前所述,该功能是非常实验性的,因此可能会发生变化。任何正式记录的内容都需要与 API 相同级别的稳定性。已经讨论了在“稳定”和“实验”功能之间拆分文档以允许此类事情的选项
    • 这表示,目前,“@Abductive”查询将尝试为每个结果元组插入“目标”类的实例,如果 WM 中尚未存在该实例。该实例是使用结果元组本身构造的。除此之外,TMS 并不真正支持溯因推理。您可以在 JIRA 票证issues.jboss.org/browse/DROOLS-371 和单元测试github.com/droolsjbpm/drools/blob/master/drools-compiler/src/… 上关注更新和新功能
    猜你喜欢
    • 2011-08-22
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2016-03-29
    • 2020-07-21
    • 2015-08-31
    相关资源
    最近更新 更多