1

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

    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 ] ...

4

1 回答 1

0

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

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);
于 2015-02-19T16:53:06.140 回答