Drools学习

最近公司需要用到drools规则引擎, 找了一些资料, 将学习点记录在此

  • Drools Fusion(CEP) 复杂事件处理

    1. 事件声明

      1. 事件声明@role(fact|event)

      2. 开始时间@timestamp(attributeName)

      3. 持续时间@duration(attributeName)

      4. 过期时间@expires(timeOffset) 仅在stream模式生效

    2. 会话时钟

         实时时钟(系统)

      KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); config.setOption( ClockTypeOption.get("realtime") );

         伪时钟(程序测试)

      KnowledgeSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); conf.setOption( ClockTypeOption.get( "pseudo" ) ); StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession( conf, null ); SessionPseudoClock clock = session.getSessionClock();

      FactHandle handle1 = session.insert( tick1 ); clock.advanceTime( 10, TimeUnit.SECONDS ); FactHandle handle2 = session.insert( tick2 ); clock.advanceTime( 30, TimeUnit.SECONDS ); FactHandle handle3 = session.insert( tick3 );

    3. 流(stream)支持, JMS 数据库 纯文本

    4. 时间(Temporal)推理

      1. 时间运算符

        1. after            $eventA : EventA( this after[ 3m30s, 2m ] $eventB )    3m30s <= AS - BE <= 2m

        2. before        $eventA : EventA( this before[ 3m30s, 4m ] $eventB )  3m30s <= BS - AE <= 4m

        3. coincides    $eventA : EventA( this coincides[15s, 10s] $eventB )    abs( AS - BS ) <= 15s && abs( AE - BE ) <= 10s

        4. during        $eventA : EventA( this during[ 2s, 6s, 4s, 10s ] $eventB )  2s <= AS - BS <= 6s && 4s <= BE - AE <= 10s

        5. finishes        $eventA : EventA( this finishes[ 5s ] $eventB )  BS < AS && abs( $AE - BE ) <= 5s

        6. finishedby    $eventA : EventA( this finishedby[ 5s ] $eventB )  AS < BS && abs( AE - BE ) <= 5s

        7. includes        $eventA : EventA( this includes[ 2s, 6s, 4s, 10s ] $eventB )   2s <= BS - AS <= 6s && 4s <= AE - BE <= 10s

        8. meets            $eventA : EventA( this meets[ 5s ] $eventB )  abs( BS - AE) <= 5s

        9. metby            $eventA : EventA( this metby[ 5s ] $eventB )  abs( AS - BE) <= 5s

        10. overlaps         $eventA : EventA( this overlaps[ 5s, 10s ] $eventB )   AS < BS < AE < BE && 5s <= AE - BS <= 10s

        11. overlappedby    $eventA : EventA( this overlappedby[ 5s, 10s ] $eventB )  BS < AS < BE < AE && 5s <= BE - AS <= 10s

        12. starts                $eventA : EventA( this starts[ 5s ] $eventB )   abs( AS - BS ) <= 5s && AE < BE

        13. startedby          $eventA : EventA( this startedby[ 5s ] $eventB )   abs( AS - BS ) <= 5s && AE > BE

    5. 事件处理模式

      1. 云模式(无序, 无时间先后概念)

        KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.CLOUD );

        或配置 drools.eventProcessingMode = cloud

      2. 流模式(有序, 时间同步)

        KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM );

        或配置 drools.eventProcessingMode = stream

    6. 滑动窗口

      1. 两分钟内的事件  over window:time( 2m )

      2. 最近10个事件  over window:length( 10 )

    7. 知识库分割(并行计算规则而非触发动作)

      1. 启用(默认禁用)

        1. KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( MultithreadEvaluationOption.YES );

        2. 或配置 drools.multithreadEvaluation = true|false 默认false

      2. 线程池配置

        1. KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( MaxThreadsOption.get(5) );

        2. 或配置 drools.maxThreads = -1|1...n, 默认3, -1为不限制(危险)

    8. 内存管理(规则到期移出内存)

      1. 显式到期偏移量

        1. declare StockTick @expires( 30m ) end

      2. 推理到期偏移量

        1. rule "correlate orders" when $bo : BuyOrderEvent( $id : id ) $ae : AckEvent( id == $id, this after[0,10s] $bo ) then // do something end

  •  



你可能感兴趣的:(Drools学习)