Drools學習

最近公司須要用到drools規則引擎, 找了一些資料, 將學習點記錄在此數據庫

  • Drools Fusion(CEP) 復瑣事件處理session

    1. 事件聲明app

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

      2. 開始時間@timestamp(attributeName)學習

      3. 持續時間@duration(attributeName)測試

      4. 過時時間@expires(timeOffset) 僅在stream模式生效this

    2. 會話時鐘lua

         實時時鐘(系統)spa

      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

相關文章
相關標籤/搜索