【问题标题】:how to write dynamic temporal rules in drools-fusion如何在drools-fusion中编写动态时间规则
【发布时间】:2013-09-30 06:47:12
【问题描述】:

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

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

【问题讨论】:

    标签: drools drools-fusion


    【解决方案1】:

    您可以使用模板从 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);
    

    【讨论】:

      猜你喜欢
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 2014-06-25
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多