Hello,

 

I have modified the stockTicker fusion example to keep some running stats, you can see from the rule snippet below that it injects a stats object based on the symbol then matches them as updates come in later.  You can see for now I am just updating the running counts and outputting the readings count on each stock tick and this works fine.

 

What I would like to do however is only have the running averages,stats object reflect stock ticks that are still in memory….essentiall only stock tick items that have not expired.  As it is now the count just keeps growing and growing, I want the count to only reflect the stock ticks within the expiration time in the past for stock ticks, but I cannot figure out how to make this happen?    Could anyone give me a pointer on how to do this?  How to make the stats object only reflect those stock ticks that have not expired?  I do not know the strategy for this.

 

Thanks,

Chris

 

# tells the engine that a StockTick instance will assume the

# role (semantics) of events and that the default retention

# policy will be 2 minutes

declare StockTick

    @role( event )

    @expires( 1m )

end

 

# One can even declare helper facts to make rules easier and

# simpler to write and maintain

declare Statistics

    symbol : String @key()

    average : double

    readings : int

    total : double

end

 

rule "Setup statistics"

when

   $c : Company( $s : symbol )

   not( Statistics( symbol == $s ) )

then

   Statistics s = new Statistics();

   s.symbol = $s;

   s.readings = s.readings + 1;

   insert( s );

 

end

 

 

 

# a simple rule to show that it is possible to join

# events from an entry-point (stream) with facts

# present in the working memory

rule "Update stock stats"

    agenda-group "evaluation"

    lock-on-active

when

     

    $cp : Company( $sb : symbol )

    $st : StockTick( symbol == $sb, $pr : price ) from entry-point "StockTick stream"

 

      $stats : Statistics( symbol == $sb  )

 

then

 

      modify( $stats ) { readings = readings + 1};

      System.err.println($stats.symbol + "readings: " + $stats.readings);

    // This shows an update on working memory facts with data from joined events

    //modify( $cp ) { currentPrice = $pr }

   

    // Although events are considered immutable, a common pattern is to use a class

    // to represent an event and enrich that event instance with data derived from other facts/events.

    // Bellow we "enrich" the event instance with the percentual change in the price,

    // based on the previous price

    //modify( $st ) { delta = $cp.delta }

    //modify( $st ) { readings = 5 }

    //System.out.println($st.delta)

end