最近公司須要用到drools規則引擎, 找了一些資料, 將學習點記錄在此數據庫
Drools Fusion(CEP) 復瑣事件處理session
事件聲明app
事件聲明@role(fact|event)ide
開始時間@timestamp(attributeName)學習
持續時間@duration(attributeName)測試
過時時間@expires(timeOffset) 僅在stream模式生效this
會話時鐘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 );
流(stream)支持, JMS 數據庫 純文本
時間(Temporal)推理
時間運算符
after $eventA : EventA( this after[ 3m30s, 2m ] $eventB ) 3m30s <= AS - BE <= 2m
before $eventA : EventA( this before[ 3m30s, 4m ] $eventB ) 3m30s <= BS - AE <= 4m
coincides $eventA : EventA( this coincides[15s, 10s] $eventB ) abs( AS - BS ) <= 15s && abs( AE - BE ) <= 10s
during $eventA : EventA( this during[ 2s, 6s, 4s, 10s ] $eventB ) 2s <= AS - BS <= 6s && 4s <= BE - AE <= 10s
finishes $eventA : EventA( this finishes[ 5s ] $eventB ) BS < AS && abs( $AE - BE ) <= 5s
finishedby $eventA : EventA( this finishedby[ 5s ] $eventB ) AS < BS && abs( AE - BE ) <= 5s
includes $eventA : EventA( this includes[ 2s, 6s, 4s, 10s ] $eventB ) 2s <= BS - AS <= 6s && 4s <= AE - BE <= 10s
meets $eventA : EventA( this meets[ 5s ] $eventB ) abs( BS - AE) <= 5s
metby $eventA : EventA( this metby[ 5s ] $eventB ) abs( AS - BE) <= 5s
overlaps $eventA : EventA( this overlaps[ 5s, 10s ] $eventB ) AS < BS < AE < BE && 5s <= AE - BS <= 10s
overlappedby $eventA : EventA( this overlappedby[ 5s, 10s ] $eventB ) BS < AS < BE < AE && 5s <= BE - AS <= 10s
starts $eventA : EventA( this starts[ 5s ] $eventB ) abs( AS - BS ) <= 5s && AE < BE
startedby $eventA : EventA( this startedby[ 5s ] $eventB ) abs( AS - BS ) <= 5s && AE > BE
事件處理模式
雲模式(無序, 無時間前後概念)
KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.CLOUD );
或配置 drools.eventProcessingMode = cloud
流模式(有序, 時間同步)
KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM );
或配置 drools.eventProcessingMode = stream
滑動窗口
兩分鐘內的事件 over window:time( 2m )
最近10個事件 over window:length( 10 )
知識庫分割(並行計算規則而非觸發動做)
啓用(默認禁用)
KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( MultithreadEvaluationOption.YES );
或配置 drools.multithreadEvaluation = true|false 默認false
線程池配置
KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( MaxThreadsOption.get(5) );
或配置 drools.maxThreads = -1|1...n, 默認3, -1爲不限制(危險)
內存管理(規則到期移出內存)
顯式到期偏移量
declare StockTick @expires( 30m ) end
推理到期偏移量
rule "correlate orders" when $bo : BuyOrderEvent( $id : id ) $ae : AckEvent( id == $id, this after[0,10s] $bo ) then // do something end