Twitter4JとDroolsでなんかリアルタイムな感じのもの その3 ルール編


Drools
Drools Rule Language (DRL) 

twitterRules1.drl
declare Status
    @role( event )
    @timestamp( createdAt )
    @expires( 2s )
end

POJOEventStatus1tweetTwitter4J
@timestampStatuscreatedAttweet稿
 @expires( 2s ) Event2 GCOutOfMemory
rule "Dump tweets"
when
    $s : Status() from entry-point "twitter"
then
    System.out.println( MessageFormat.format( "[{0,time,full}] @{1} - {2}",
                                              $s.getCreatedAt(),
                                              $s.getUser().getScreenName(),
                                              $s.getText() ) );
end



WHEN 
THEN 


 Status() Status $s $s


twitterRules2.drl
rule "Dump tweets from people laughing"
when
    $s : Status( text matches ".*lol.*" ) from entry-point "twitter"
then
...

textStatustweetmatchesDRLlol

twitterRules3.drl
rule "Dump tweets from US"
when
    $s : Status( ) from entry-point "twitter"
    $p : Place( countryCode == "US" ) from $s.place
then
...

Tweet使

twitterRules4.drl
rule "Dump tweets from user conversation"
when
    $s1 : Status( $name : user.screenName ) from entry-point "twitter"
    $s2 : Status( inReplyToScreenName == $name, this after[1ms,2m] $s1 ) from entry-point "twitter"
then
...


Status($s1, $s2)$s2$s1$s2$s122
Java
Tweet(after)(before)(coincides)3AB(meets)AB(finishes)
http://www.slideshare.net/ge0ffrey/applying-cep-drools-fusion-drools-jbpm-bootcamps-2011
 24-25 

twitterRules5.drl
rule "Count tweets in 10 seconds"
    duration( 10s )
when
    Number( $count : intValue ) from accumulate(
        Status() over window:time( 30s ) from entry-point "twitter",
        count(1) )
then
...

30tweetsampletweet
accumulate
duration( 10s ) 10


twitterRules6.drl
rule "Filter retweeted messages"
when
    $s : Status( retweetCount > 0 ) from entry-point "twitter"
then
    entryPoints["retweeted"].insert( $s );
end

rule "Filter non-retweeted messages"
when
    $s : Status( retweetCount == 0 ) from entry-point "twitter"
then
    entryPoints["non-retweeted"].insert( $s );
end

"retweeted", "non-retweeted"entryPoints["retweeted"]
rule "Dump retweeted"
when
    $s : Status( user.screenName matches "[a-g].*" ) from entry-point "retweeted"
then
...
end

rule "Dump non retweeted"
when
    $s : Status( user.screenName matches "[h-p].*" ) from entry-point "non-retweeted"
then
...



Javaif/thenDroolswhen
ABjBPM( http://www.jboss.org/jbpm )

4