博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Drools学习
阅读量:7080 次
发布时间:2019-06-28

本文共 3263 字,大约阅读时间需要 10 分钟。

hot3.png

最近公司需要用到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

 

转载于:https://my.oschina.net/u/175048/blog/492866

你可能感兴趣的文章
深入理解this对象
查看>>
inodes 100%情况解决办法
查看>>
mysql 5.6 调优
查看>>
动态菜单树加载过程
查看>>
计算机存储单位及网络传输单位
查看>>
linux安装jdk
查看>>
Oracle JOB 用法小结
查看>>
自动生成的当前时间与实际时间相差8个小时
查看>>
How to generate UML Diagrams from Java code in Eclipse
查看>>
day14 Python百分号字符串拼接
查看>>
GPGPU之应用于Mapped Reduced
查看>>
简单的记录一下JavaScript 高级应用
查看>>
HTML简介
查看>>
bzoj2467 [中山市选2010]生成树
查看>>
自动流水号
查看>>
最大异或子序列问题
查看>>
[Python] IMG to Char
查看>>
SpringBoot 整合 Logback
查看>>
Learn Python 014: Funtions
查看>>
Centos7编译Hadoop-2.7.5遇到的坑
查看>>