如何在drools-fusion中编写动态时间规则

时间:2013-09-30 06:47:12

标签: drools drools-fusion

我正在尝试为一个事件编写规则,该事件需要检查在发生其他事情之后某个时间窗口内是否发生了某些事情。目前代码看起来像这样(它可以正常工作):

    rule "Detect BPM reseed not starting when requested from Mart"
        when
            $martDailyRefreshRequestedEvent: MessageSentEvent(
                $correlationId: correlationId,
                $when: timestamp,
                messageTypeName == "MartDailyRefreshCompletedEvent")
                    from entry-point "mart"
            not ( MessageHandleStartedEvent(
                    this after[0ms, 30s] $martDailyRefreshRequestedEvent, 
                    correlationId == $correlationId,
                    messageTypeName == "MartDailyRefreshCompletedEvent") 
                            from entry-point "bpm")
        then
            notifier.notify("BPM not responding to MartDailyRefreshCompletedEvent quick enough", 
                String.format(
                    "At **%s** Mart sent out a **MartDailyRefreshCompletedEvent**.\n\n**BPM** was supposed to react to it within **30 seconds**.",
                    $when));
    end

目前30s实际上是硬编码的。我读过,如果你想参数化规则,你需要使用在KB中声明的其他事实,但我无法弄清楚如何为时间规则做这件事。

那么:如何在此规则中“配置”30s,以便我可以更改应用程序之外的值?这样的事情:MessageHandleStartedEvent(this after [ $duration ] ...

1 个答案:

答案 0 :(得分:0)

您可以使用模板从Drools外部提供硬编码。

template dynamicTimer
rule "Detect BPM reseed not starting when requested from Mart"
    when
        $martDailyRefreshRequestedEvent: MessageSentEvent(
            $correlationId: correlationId,
            $when: timestamp,
            messageTypeName == "MartDailyRefreshCompletedEvent")
                from entry-point "mart"
        not ( MessageHandleStartedEvent(
                this after[0ms, @{timeout}s] $martDailyRefreshRequestedEvent, 
                correlationId == $correlationId,
                messageTypeName == "MartDailyRefreshCompletedEvent") 
                        from entry-point "bpm")
    then
        notifier.notify("BPM not responding to MartDailyRefreshCompletedEvent quick enough", 
            String.format(
                "At **%s** Mart sent out a **MartDailyRefreshCompletedEvent**.\n\n**BPM** was supposed to react to it within **@{timeout} seconds**.",
                $when));
end
end template

然后,您只需要提供30作为模板参数:

ObjectDataCompiler converter = new ObjectDataCompiler();
InputStream templateStream = getClass().getResourceAsStream(resource.getFilePath());
Collection<Map<String, String>> paramMaps = new ArrayList<>();
Map<String,String> param = new HashMap<>();
param.put("timeout", "30");
paramMaps.add(param);
String drl = converter.compile(paramMaps, templateStream);
Reader rdr = new StringReader(drl);
kbuilder.add(ResourceFactory.newReaderResource(rdr), ResourceType.DRL);