Drools 6-访问ObjectInsertedEvent的LHS [英] Drools 6 - Accessing the LHS of an ObjectInsertedEvent

查看:82
本文介绍了Drools 6-访问ObjectInsertedEvent的LHS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Drools 5.5.0.Final升级到Drools 6.5.0.CR2,并且遇到一个自定义事件侦听器的问题,该侦听器侦听ObjectInsertedEvents并引用规则的LHS触发了插入。根据触发规则的LHS绑定的类型,侦听器会更新新插入的事实的属性。

I'm in the process of upgrading from Drools 5.5.0.Final to Drools 6.5.0.CR2, and I'm running into a problem with a custom event listener that listens for ObjectInsertedEvents and references the LHS of the rule that triggered the insertion. Depending on the types of the LHS bindings of the fired rule, the listener updates a property of the newly inserted fact.

在Drools 5中,我们使用自定义事件来完成此操作。扩展了DebugWorkingMemoryEventListener并实现了WorkingMemoryEventListener的侦听器:

In Drools 5, we did this with a custom event listener that extended DebugWorkingMemoryEventListener and implemented WorkingMemoryEventListener:

public void objectInserted(ObjectInsertedEvent event) {
    Rule firedRule = event.getPropagationContext().getRuleOrigin();

    if (firedRule !=  null) {
        PropagationContext pc = event.getPropagationContext();
        Tuple tuple = pc.getLeftTupleOrigin();
        Map<String, Declaration> declarationsMap = firedRule.getDeclarations();

        // Iterate through the LHS variable bindings
        for (Map.Entry<String, Declaration> entry : declarationsMap.entrySet()) {

            // Get the value, via the handle to the fact in working memory to which the variable is bound
            Declaration declaration = entry.getValue();
            FactHandle factHandle = tuple.get(declaration);

            // Get the object in working memory via the fact handle
            Object bindingObj = ss.getObject(factHandle)

            // do some other checks, update the inserted object if necessary
        }
    }
}

WorkingMemoryEventListener为在Drools 6中已弃用,因此我已更新为RuleRuntimeEventListener,但看来我无法再访问触发ObjectInsertedEvent的规则的LHS,而只能访问有关规则本身的一些基本信息(名称,包,元数据)。或者,我可以使用AgendaEventListener和AfterMatchFiredEvent访问触发规则的LHS,但是1)不会告诉我是否插入了对象,2)即使我可以,也无法给我更新该对象的方法确定是否已插入。

WorkingMemoryEventListener is deprecated in Drools 6, so I've updated to RuleRuntimeEventListener, but it looks like I can no longer access the LHS of the rule that fired the ObjectInsertedEvent, only some basic info about the rule itself (name, package, metadata). Alternatively, I could access the LHS of a fired rule with an AgendaEventListener and AfterMatchFiredEvent, but that 1) doesn't tell me if an object was inserted, and 2) doesn't give me a way to update that object even if I could determine if it was inserted.

如何通过这些Drools 6 API更改来复制Drools 5中的侦听器行为?谢谢。

How can I replicate the listener behavior that I had in Drools 5 with these Drools 6 API changes? Thanks.

推荐答案

如果实现了BeforeMatchFired,AfterMatchFired和ObjectInsertedEvent,则可以创建在触发规则期间插入的事实的集合。 Match对象为您提供了规则LHS的所有绑定。所有插入的对象都可用,并且应该在AfterMatchFired事件中进行处理。

If you implement BeforeMatchFired, AfterMatchFired and ObjectInsertedEvent you can create a Collection of facts inserted during the firing of a rule. The Match object gives you all the bindings from the rule's LHS. All inserted objects are available and should be processed at the AfterMatchFired event.

我看不到有任何阻止您将代码移植到6.x的东西,只是适度用于维护集合的其他代码。

I don't see anything that keeps you from porting your code to 6.x, with only moderate additional code for maintaining the Collection.

这篇关于Drools 6-访问ObjectInsertedEvent的LHS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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