简体   繁体   English

决策树和规则引擎(Drools)

[英]Decision trees and rule engines (Drools)

In the application that I'm working on right now, I need to periodically check eligibility of tens of thousands of objects for some kind of a service. 在我正在进行的应用程序中,我需要定期检查成千上万个对象的资格,以获得某种服务。 The decision diagram itself is in the following form, just way larger: 决策图本身采用以下形式,只是更大: 决策图

In each of the end nodes (circles), I need to run an action (change an object's field, log information etc). 在每个端节点(圆圈)中,我需要运行一个动作(更改对象的字段,日志信息等)。 I tried using Drool Expert framework, but in that case I'd need to write a long rule for every path in the diagram leading to an end node. 我尝试使用Drool Expert框架,但在这种情况下,我需要为图中的每个路径编写一条长规则,从而导致结束节点。 Drools Flow doesn't seem to be built for such a use case either - I take an object and then, depending on the decisions along the way, I end up in one of the end nodes; Drools Flow似乎也没有为这样的用例构建 - 我拿一个对象,然后,根据一路上的决定,我最终进入一个终端节点; and then again for another object. 然后又为另一个对象。 Or is it? 或者是吗? Could you give me some examples/links to such solutions? 你能给我一些这些解决方案的例子/链接吗?

UPDATE: 更新:

Drools Flow calls might look like this: Drools Flow调用可能如下所示:

// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Map<String, Object> params = new HashMap<String, Object>();

for(int i = 0; i < 10000; i++) {

    Application app = somehowGetAppById(i);

    // insert app into working memory
    FactHandle appHandle = ksession.insert(app);

    // app variable for action nodes
    params.put("app", app);

    // start a new process instance
    ProcessInstance instance = ksession.startProcess("com.sample.ruleflow", params);
    while(true) {
        if(instance.getState() == instance.STATE_COMPLETED) {
            break;
        }
    }

  // remove object from working memory
    ksession.retract(appHandle);
}

That is: I'd take an Application object, start a new process for it, when the process is finished (the final, action node would modify the application somehow), I'd remove the object from working memory and repeat the process for a new App object. 那就是:我接受一个Application对象,为它启动一个新进程,当进程完成时(最终,动作节点会以某种方式修改应用程序),我将从工作内存中删除该对象并重复该进程一个新的App对象。 What do you think about this solution? 您对此解决方案有何看法?

SOLUTION: 解:
I've ended up using Drools Flow and it has been working quite fine. 我最终使用了Drools Flow,它一直很好用。 My decision process isn't as straightforward as Drools Expert asks for and depending on where in the decision tree the process is it needs to load lists of objects from the database, transform them, make decisions, log everything etc. I use a Process object that is passed to the process as a parameter and stores all my global variables (for the process) and some convenience methods that are repeated at different points in the tree (as writing Java code in the Script Task nodes isn't very convenient itself). 我的决策过程并不像Drools Expert所要求的那样简单,并且取决于决策树中的位置,它需要从数据库加载对象列表,转换它们,做出决策,记录所有内容等等。我使用Process对象作为参数传递给流程并存储我的所有全局变量(对于流程)以及在树中不同点重复的一些便捷方法(因为在Script Task节点中编写Java代码本身不是很方便) 。 I also ended up using Java to make decisions (and not mvel or rules) - it's faster and I'd say easier to control. 我最终也使用Java来做决定(而不是mvel或规则) - 它更快,我说更容易控制。 All objects that I work with are passed as parameters and used as normal Java variables in the code. 我使用的所有对象都作为参数传递,并在代码中用作普通的Java变量。

Drools expert is definitely the way to go. Drools专家肯定是要走的路。

If you want to avoid repeating yourself for the higher nodes, then the trick is to use insertLogical (or just insert if you're in a stateless session) and to understand that rules can trigger rules (It's not your father's SQL query). 如果你想避免为更高的节点重复自己,那么诀窍是使用insertLogical (或者如果你处于无状态会话中就insert )并理解规则可以触发规则(这不是你父亲的SQL查询)。 For example: 例如:

// we just insert Customer objects in the WM

rule "evaluateRetired"
when
    $c : Customer(age > 65)
then
    insertLogical(new Retiree($c));
end

rule "evaluteRetireeIsFemale"
when
    $r : Retiree(customer.gender == Gender.FEMALE, $c : customer)
then
    ...
end

If the decision diagram frequently changes (and you want non-programmers to edit it), take a look at the documentation on decision tables (and DSL ). 如果决策图经常更改(并且您希望非程序员编辑它),请查看有关决策表 (和DSL )的文档。 In that case you'll probably repeat the entire path for each rule, but that's actually ok in most cases. 在这种情况下,您可能会重复每个规则的整个路径,但在大多数情况下这实际上是正常的。

I had a similiar problem and used Neo4J node database as a simple and very flexible rules engine. 我有一个类似的问题,并使用Neo4J节点数据库作为一个简单而非常灵活的规则引擎。 You can use is it with a REST service interface so it is independent from the main application. 您可以使用它与REST服务接口,因此它独立于主应用程序。 You can also have a separate application to configure the rules (even by end users). 您还可以使用单独的应用程序来配置规则(即使是最终用户)。

您可以尝试iLog框架兼规则引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM