在流口水中,我可以从AgendaFilter中访问工作记忆(或任意事实)吗? [英] In drools can I access the working memory (or arbitrary facts) from within an AgendaFilter?

查看:123
本文介绍了在流口水中,我可以从AgendaFilter中访问工作记忆(或任意事实)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AgendaFilter来决定是否应该执行规则激活。
作为工作记忆事实的一部分,我为每个规则插入一个规则配置事实,其中包含允许我的规则执行的频率(以及相应的计数器)。
我注意到 Match.getFactHandles()仅返回创建此匹配项的事实(根据Java文档)。
是否有一种方法可以访问WorkingMemory及其所有事实,或者我是否必须根据自身条件声明规则配置事实?

I'm using an AgendaFilter to decide whether a rule activation should be executed or not. As part of my working memory's facts I insert one "rule configuration" fact per rule which contains how often my rule is allowed to be executed (and a corresponding counter). I noticed that Match.getFactHandles() only returns the facts that 'created this match' (as per java doc). Is there a way to access the WorkingMemory and all its facts or do I basically have to declare my "rule configuration" fact as part of my condition?

举例说明:现在,我做下面的事情(下图),我想知道是否可以绕过规则中声明 $ ruleConfig 事实,但仍然能够在我的AgendaFilter中查找它。

Example to illustrate: right now I do something like this (below) and I wonder if I can get around having to declare the $ruleConfig fact in the rule but still be able to look it up in my AgendaFilter.

rule "abc"
@uid("1234")

    when
        $ruleConfig : RuleConfig(uid="1234")
        // insert the actual rule conditions 

...

谢谢!
(我知道我可以完全在规则内使用控制事实来解决此问题,但是出于架构上的原因,我想将其尽可能地排除在实际规则代码之外,因此避免使用AgendaFilter)

Thanks! (I know I could solve this with control facts entirely inside the rule, but for architectural reasons I want to keep this out of the actual rule code as much as possible, hence the AgendaFilter)

推荐答案

AgendaFilter是Pojo,您可以通过将KieSession作为参数传递来创建它。根据API文档,这使您可以访问所有WM事实。因此,每个规则都没有必要具有适当的RuleConfig作为附加条件。这种方法的唯一优点是,您可以在匹配事实列表中随时使用此对象。

An AgendaFilter is a Pojo, and you can create it with the KieSession being passed as an argument. This lets you access all the WM facts, according to the API documentation. Thus, it isn't necessary to have the appropriate RuleConfig as an extra condition with each rule. The only advantage of your approach is that you have this object readily available in the List of matched facts.

但是,传递<$ c $一样简单c> Map< String,RuleConfig> 到AgendaFilter,而没有将所有RuleConfig对象都添加到工作内存中。

But it would be just as simple to pass a Map<String,RuleConfig> to te AgendaFilter, without all of the RuleConfig objects being added to the Working Memory.

略微多一点

rule "abc"
@uid("1234")
when
    $ruleConfig : RuleConfig(uid="1234", counter > 0)
    // ...

,每次触发规则时计数器都会递减-不一定在每个RHS上,而是在中心位置:
RuleRuntimeEventListener

with the counter being decremented with each rule firing - not necessarily on each RHS but in a central place: a RuleRuntimeEventListener.

这篇关于在流口水中,我可以从AgendaFilter中访问工作记忆(或任意事实)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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