JESS vs DROOLS:向后链接 [英] JESS vs DROOLS : Backward chaining

查看:345
本文介绍了JESS vs DROOLS:向后链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我试图用Drools代替Jess作为向后链接规则引擎.我一直在寻找有关如何使用Drools进行反向链接的简单示例.有趣的是,每个实例上只有一个相同的示例网站(我不知道它在BC的情况如何,但现在让我们忘记它).

I'm trying to replace Jess with Drools as backward chaining rule engine in our project. I was looking for simple examples about how backward chaining is done with Drools. Interestingly, there's only 1 same example on every site (which I don't get how it's BC but let's forget it for now).

Jess中BC的一个非常简单的例子:

Very trivial example of BC in Jess:

//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中等效:

Equivalent in 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达到相同的行为?

How can I achieve same behaviour with Drools?

推荐答案

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

Inspired from the Jess feature, there is an experimental, in-development feature that gives you a similar behavior. Here is how a test would look like:

@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:向后链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆