最近公司需要用到drools规则引擎, 找了一些资料, 将学习点记录在此
Drools Fusion(CEP) 复杂事件处理
事件声明
事件声明@role(fact|event)
开始时间@timestamp(attributeName)
持续时间@duration(attributeName)
过期时间@expires(timeOffset) 仅在stream模式生效
会话时钟
实时时钟(系统)
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