[rules-users] Sliding window behavior in stream mode and realtime clock

MarcoMojana marco.mojana at exmachina.ch
Tue Dec 6 12:23:08 EST 2011


Hi,

The situation is the following: we need to process a stream of delayed
events. This means that the event timestamp is provided for each one
of them and that it doesn't generally correspond to the system time when
we insert them in the entry-point. The events delay may vary, but they
are always inserted in chronological order. My questions refer to the
enclosed implementation and are:
1) Given my source code, would the timestamps correctly read and used?
2) I would like that the "time window" behaves as if it is initially
   placed at t = -inf and, when a new event is inserted in the
   entry-point, it would slide to the time of the last event timestamp.
   In my code I have used the stream mode and a realtime clock: is it
   correct?
3) When I run the given application, I expect it to print only:
   Found TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011]
   because it is the only event of type TriggerEvent for which there
   exists a window of size 1h that contains no InhibitEvent. Instead,
   sometimes it prints nothing and sometimes:
   Found TriggerEvent [timestamp=Tue Dec 06 14:10:00 CET 2011]
   Found TriggerEvent [timestamp=Tue Dec 06 13:00:00 CET 2011]
   How is this possible? How can I achieve my goal?

(I'm currently using Drools 5.3.0 final.)

Cheers

Marco Mojana

------------------------------------------------------------------------

package com.sample;

import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.conf.EventProcessingOption;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.KnowledgeSessionConfiguration;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.conf.ClockTypeOption;
import org.drools.runtime.rule.WorkingMemoryEntryPoint;

public class DroolsTest {

	public static final void main(String[] args) {
		try {
			// load up the knowledge base
			KnowledgeBase kbase = readKnowledgeBase();
			
			final KnowledgeSessionConfiguration sessionConfig =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
			sessionConfig.setOption(ClockTypeOption.get("realtime"));
			StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession(sessionConfig, null);
			WorkingMemoryEntryPoint eventStream =
ksession.getWorkingMemoryEntryPoint("EventStream");
			KnowledgeRuntimeLogger logger =
KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
			
			// Insert events
			eventStream.insert(new InhibitEvent(new GregorianCalendar(2011,
Calendar.DECEMBER, 6, 12,  0, 0).getTime()));
			eventStream.insert(new TriggerEvent(new GregorianCalendar(2011,
Calendar.DECEMBER, 6, 13,  0, 0).getTime()));
			eventStream.insert(new InhibitEvent(new GregorianCalendar(2011,
Calendar.DECEMBER, 6, 14,  0, 0).getTime()));
			eventStream.insert(new TriggerEvent(new GregorianCalendar(2011,
Calendar.DECEMBER, 6, 14, 10, 0).getTime()));
			eventStream.insert(new InhibitEvent(new GregorianCalendar(2011,
Calendar.DECEMBER, 6, 14, 20, 0).getTime()));
			
			ksession.fireAllRules();
			ksession.dispose();
			logger.close();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("Rules.drl"),
ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error: errors) {
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		
		final KnowledgeBaseConfiguration kbConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
		kbConfig.setOption(EventProcessingOption.STREAM);
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConfig);
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
		
	}
}

------------------------------------------------------------------------

package com.sample;

import java.util.Date;

public class TriggerEvent {
	
	private final Date timestamp;
	
	public TriggerEvent(final Date timestamp) {
		this.timestamp = timestamp;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	@Override
	public String toString() {
		return "TriggerEvent [timestamp=" + timestamp + "]";
	}
	
}

------------------------------------------------------------------------

package com.sample;

import java.util.Date;

public class InhibitEvent {

	private final Date timestamp;
	
	public InhibitEvent(final Date timestamp) {
		this.timestamp = timestamp;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	@Override
	public String toString() {
		return "InhibitEvent [timestamp=" + timestamp + "]";
	}
	
}

------------------------------------------------------------------------

package com.sample

declare InhibitEvent
	@role(event)
	@timestamp(timestamp)
end

declare TriggerEvent
	@role(event)
	@timestamp(timestamp)
end

rule "EventNotInhibited" 
	dialect "mvel"
	when
		
		not( InhibitEvent() over window:time(1h) from entry-point EventStream )
		$e0 : TriggerEvent() over window:time(1h) from entry-point EventStream
		
	then
		System.err.println("Found " + $e0);
end

------------------------------------------------------------------------


--
View this message in context: http://drools.46999.n3.nabble.com/Sliding-window-behavior-in-stream-mode-and-realtime-clock-tp3564950p3564950.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list