[rules-users] Trouble wrapping my head around window.length(1)
Wolfgang Laun
wolfgang.laun at gmail.com
Fri Jun 22 14:33:57 EDT 2012
The first firing of "Faults coincide" is a bug.
Rule "Faults don't coincide" doesn't do what I think you want it to do
- "or" is inclusive, not exclusive.
I'd recommend using this rule:
rule "Detect"
when
f1 : MyEvent( key == "faultType1", value == "ALARM" )
not MyEvent( key == "faultType1", this after f1 )
f2 : MyEvent( key == "faultType2", value == "ALARM" )
not MyEvent( key == "faultType2", this after f2 )
then
System.out.println( "************ BOTH FAULTS ARE ACTIVE! f1.key =
" + f1.getKey() + " f2.key = " + f2.getKey() );
end
-W
On 22/06/2012, Ladd <ladd at codemettle.com> wrote:
> 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.
> _______________________________________________
> 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