决策树和规则引擎 (Drools)
在我现在正在开发的应用程序中,我需要定期检查数万个对象对于某种服务的资格。决策图本身采用以下形式,只是更大:
在每个末端节点(圆圈)中,我需要运行一个操作(更改对象的字段、日志信息等)。我尝试使用 Drool Expert 框架,但在这种情况下,我需要为图中通向结束节点的每条路径编写一条很长的规则。 Drools Flow 似乎也不是为这样的用例构建的 - 我获取一个对象,然后根据一路上的决策,我最终到达一个终端节点;然后再针对另一个对象。或者是吗?您能给我一些此类解决方案的示例/链接吗?
更新:
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);
}
即:我将获取一个 Application 对象,为其启动一个新进程,当该进程完成时(最后一个操作节点将修改该应用程序)不知何故),我会从工作内存中删除该对象,并为新的 App 对象重复该过程。您对此解决方案有何看法?
解决方案:
我最终使用了 Drools Flow,它运行得很好。我的决策过程并不像 Drools Expert 要求的那么简单,并且取决于决策树中的过程需要从数据库加载对象列表、转换它们、做出决策、记录所有内容等。我使用 Process 对象它作为参数传递给进程,并存储我的所有全局变量(用于进程)和一些在树中不同点重复的便捷方法(如在 Script Task
节点中编写 Java 代码)本身不是很方便)。我最终还使用 Java 来做出决策(而不是 mvel
或规则)——它更快,而且我想说更容易控制。我使用的所有对象都作为参数传递,并在代码中用作普通 Java 变量。
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. 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; 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:
// 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. What do you think about this solution?
SOLUTION:
I've ended up using Drools Flow and it has been working quite fine. 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). I also ended up using Java to make decisions (and not mvel
or rules) - it's faster and I'd say easier to control. All objects that I work with are passed as parameters and used as normal Java variables in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Drools Expert绝对是您的最佳选择。
如果您想避免在更高的节点上重复,那么技巧是使用
insertLogical
(或者如果您处于无状态会话,则仅使用insert
)并了解这一点规则可以触发规则(这不是你父亲的SQL查询)。例如:如果决策图经常更改(并且您希望非程序员对其进行编辑),请查看有关决策表(和DSL)的文档。在这种情况下,您可能会为每个规则重复整个路径,但这在大多数情况下实际上是可以的。
Drools expert is definitely the way to go.
If you want to avoid repeating yourself for the higher nodes, then the trick is to use
insertLogical
(or justinsert
if you're in a stateless session) and to understand that rules can trigger rules (It's not your father's SQL query). For example: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). In that case you'll probably repeat the entire path for each rule, but that's actually ok in most cases.
您可以尝试 iLog 框架兼规则引擎。
You can try the iLog framework cum rules engine.
我遇到了类似的问题,并使用 Neo4J 节点数据库作为简单且非常灵活的规则引擎。
您可以将其与 REST 服务接口一起使用,因此它独立于主应用程序。
您还可以使用单独的应用程序来配置规则(甚至由最终用户配置)。
I had a similiar problem and used Neo4J node database as a simple and very flexible rules engine.
You can use is it with a REST service interface so it is independent from the main application.
You can also have a separate application to configure the rules (even by end users).