[rules-users] Trouble wrapping my head around window.length(1)

Ladd ladd at codemettle.com
Fri Jun 22 11:16:18 EDT 2012


I'm new to Drools and have what I think should be a simple use case.  But I'm
getting some puzzling results from my tests.

I have a simple class with just a key and value.  What I want to detect is
when the last occurrence with key "X" has a value of "ALARM" and the last
occurrence with key "Y" has a value of "ALARM".

Could someone please either educate me or point me to the documentation that
explains how this would be done?  Many thanks in advance!!

Here's my test code and results:

*The event class:*
public class MyEvent {
	
	String key;
	String value;
	
	public MyEvent( String key, String value ) {
		this.key = key;
		this.value = value;
	}
	
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

*The test runner:*
	@Test
	public void testWindowLen1() {
	
		StringBuilder sb = new StringBuilder();
		
		sb.append( "import drools.tests.MyEvent \n" );
		sb.append( " \n" );
		
		sb.append( "declare MyEvent @role(event) end \n" ); 
		sb.append( " \n" );
		
		sb.append( "rule \"Any event detected\" \n" );
		sb.append( " \n" );
		sb.append( "salience 10 \n" );
		sb.append( "when \n" );
		sb.append( "	f : MyEvent() \n" );
		sb.append( "then \n" );
		sb.append( "	System.out.println( \"************ fault seen with key \" +
f.getKey() + \" value \" + f.getValue() ); \n" );
		sb.append( "end \n" );
		sb.append( " \n" );
		
		sb.append( "rule \"Faults coincide\" \n" );
		sb.append( " \n" );
		sb.append( "when \n" );
		sb.append( "	f1 : MyEvent( key == \"faultType1\", value == \"ALARM\" )
over window:length( 1 ) \n" );
		sb.append( "	f2 : MyEvent( key == \"faultType2\", value == \"ALARM\" )
over window:length( 1 ) \n" );
		sb.append( "then \n" );
		sb.append( "	System.out.println( \"************ both faults are active!
f1.key = \" + f1.getKey() + \" f2.key = \" + f2.getKey() ); \n" );
		sb.append( "end \n" );
		sb.append( " \n" );
		
		sb.append( "rule \"Faults don't coincide\" \n" );
		sb.append( " \n" );
		sb.append( "when \n" );
		sb.append( "	f1 : MyEvent( key == \"faultType1\", value != \"ALARM\" )
over window:length( 1 ) \n" );
		sb.append( "		or \n" );
		sb.append( "	f2 : MyEvent( key == \"faultType2\", value != \"ALARM\" )
over window:length( 1 ) \n" );
		sb.append( "then \n" );
		sb.append( "	System.out.println( \"************ at least one fault isn't
active!\" ); \n" );
		sb.append( "end \n" );
		
		
		StatefulKnowledgeSession ksession = null;
		KnowledgeBase kbase = null;
		KnowledgeBaseConfiguration config =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
		config.setOption( EventProcessingOption.STREAM );
		
		kbase = KnowledgeBaseFactory.newKnowledgeBase( config );
		
		ksession = kbase.newStatefulKnowledgeSession();
		
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		
		kbuilder.add( ResourceFactory.newByteArrayResource(
sb.toString().getBytes() ), ResourceType.DRL );
		if( kbuilder.hasErrors() ) {
			System.err.println( kbuilder.getErrors().toString() );
		} else {
			kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
		}
		
		// Inject the first fault = ALARM
		MyEvent faultType1Alarm = new MyEvent( "faultType1", "ALARM" );
		System.out.println( "Firing fault1 = ALARM" );
		ksession.insert( faultType1Alarm );
		ksession.fireAllRules();
		
		// Inject the second fault = ALARM
		// Want this to trigger the "Faults concide" rule
		MyEvent faultType2Alarm = new MyEvent( "faultType2", "ALARM" );
		System.out.println( "Firing fault2 = ALARM" );
		ksession.insert( faultType2Alarm );
		ksession.fireAllRules();
		
		// Inject the first fault = OK
		// Want this to trigger the "Faults don't concide" rule
		MyEvent faultType1OK = new MyEvent( "faultType1", "OK" );
		System.out.println( "Firing fault1 = OK" );
		ksession.insert( faultType1OK );
		ksession.fireAllRules();
		
		ksession.dispose();
		
		System.out.println( "Done!" );
		
		assert( true );
	}

*And the results that have me scratching my head:*
Firing fault1 = ALARM
************ fault seen with key faultType1 value ALARM
************ at least one fault isn't active!
************ at least one fault isn't active!
************ both faults are active! f1.key = faultType1 f2.key = faultType1
Firing fault2 = ALARM
************ fault seen with key faultType2 value ALARM
************ at least one fault isn't active!
************ at least one fault isn't active!
************ both faults are active! f1.key = faultType2 f2.key = faultType2
************ both faults are active! f1.key = faultType2 f2.key = faultType1
Firing fault1 = OK
************ fault seen with key faultType1 value OK
************ at least one fault isn't active!
************ at least one fault isn't active!
************ both faults are active! f1.key = faultType1 f2.key = faultType1
************ both faults are active! f1.key = faultType1 f2.key = faultType2
Done!

--
View this message in context: http://drools.46999.n3.nabble.com/Trouble-wrapping-my-head-around-window-length-1-tp4018171.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list