[rules-users] Prevent re-evaluation of events in stream mode

Mark Proctor mproctor at codehaus.org
Tue Dec 10 10:28:32 EST 2013


Logically insert the average, in a separate rule. This will ensure there is an “average” fact that is maintained n the TMS. As it’s logically inserted, you don’t need to delete it.

After any Transaction has been evaluated, you need to set a flag on it, so it’s not processed again.

Mark


On 9 Dec 2013, at 17:59, sumantp <sumantp at yahoo.com> wrote:

> Hi,
> 
> I've written a simple rule to illustrate the issue I'm running into.
> Transaction events representing amounts deposited into a bank account are
> inserted sequentially. The goal is to detect a transaction that is higher
> than the average from a rolling window. What I'm expecting is that only the
> most recent insertion is compared with the average from the window. However,
> I'm observing that all previous events in the window are re-evaluated each
> time a new event is inserted. So events that previously were below the
> average (at that point in time) may now be above the average since
> subsequent events brought the average down. I don't want those transactions
> to be re-evaluated.
> 
> Here's my rule:
> 	declare Transaction
> 		@role(event)
> 	end
> 	
> 	rule "Higher than average amount"
> 	when
> 		$transaction : Transaction($account : getAccount(), $amount : getAmount())
> 		Number($avg : doubleValue, $amount > $avg) from
> accumulate(Transaction(getAccount() == $account, $amt : getAmount()) over
> window:length(100), average($amt))
> 	then
> 		System.out.println("\t***ALERT***: Higher than average transaction amount
> (Average = " + $avg + "): " + $transaction.toString());
> 	end
> 
> 
> Here's a snippet from my Java class:
> 	public static final void main(String[] args) {
> 		try {
> 			// Load the knowledge base
> 			KieServices ks = KieServices.Factory.get();
> 			KieContainer kContainer = ks.getKieClasspathContainer();
> 			KieSession kSession = kContainer.newKieSession("ksession-rules");
> 
> 			// Insert events
> 			List<Transaction> list = getEvents();
> 			for (Transaction event : list) {
> 				printEvent(event);
> 				kSession.insert(event);
> 				kSession.fireAllRules();
> 			}
> 		} catch (Throwable t) {
> 			t.printStackTrace();
> 		}
> 	}
> 
> 	private static List<Transaction> getEvents() {
> 		List<Transaction> list = new Vector<Transaction>();
> 
> 		list.add(new Transaction(1, 600));
> 		list.add(new Transaction(1, 600));
> 		list.add(new Transaction(1, 800)); // This should trigger an alert
> 		list.add(new Transaction(1, 100)); // This should NOT re-evaluate previous
> transactions
> 
> 		return list;
> 	}
> 
> This is my kmodule.xml:
> 	<?xml version="1.0" encoding="UTF-8"?>
> 	<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
>    	<kbase name="rules" eventProcessingMode="stream" packages="rules">
>        	<ksession name="ksession-rules"/>
>    	</kbase>
> 	</kmodule>
> 
> I'm using 6.0.0.Final.
> 
> Any help is appreciated!
> 
> 
> 
> --
> View this message in context: http://drools.46999.n3.nabble.com/Prevent-re-evaluation-of-events-in-stream-mode-tp4027171.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users




More information about the rules-users mailing list