Drools fusion "Group By" 解决方案

Drools fusion "Group By" 解决方案

问题:

         只有一个AccessLog事件插入在工作内存WorkingMemory中,AccessLog事件中包含ip、time等属性。实现,在AccessLog事件的1s的滑动时间窗口中,同一ip出现的次数大于2次,我们就输出的该ip和次数。

 

解决方案:

         1.在规则文件中,我们自定义一个新的AccessLogType事实,用来存储AccessLog事件中,我们所需要分组的类型,如代码清单1-1所示。

代码清单1-1

1 declare AccessLogType 
2     type : String @key
3 end


         2. 将我们定义的AccessLogType事实中的属性,根据AccessLog事件传过来的ip属性赋值,如代码清单1-2所示。        

代码清单1-2

1 rule "register types" 
2     no-loop true
3     when 
4         AccessLog( $type : remoteip ) 
5         not AccessLogType( type == $type )
6     then 
7         insert( new AccessLogType( $type ) ); 
8 end

 

         3. 接下来将我们定义的AccessLogType事实,应用到AccessLog事件的滑动时间窗口中,如代码清单1-3所示。 

代码清单1-3

 1 // log
 2 rule "rule0"
 3     salience 2
 4     when
 5         AccessLogType($remoteip : type) 
 6         $count : Number( intValue > 2 ) from accumulate (
 7             $temp : AccessLog( $remoteip == remoteip) over window:time( 1s ),
 8             count( $temp )            
 9         )
10     then
11         System.out.println(" | " + $remoteip + " | " + $count + " | ");
12 end
posted @ 2015-12-31 15:57  S_Kirito  阅读(503)  评论(1编辑  收藏  举报