[jboss-svn-commits] JBL Code SVN: r27979 - in labs/jbossrules/trunk/drools-compiler/src/test: resources/org/drools/integrationtests and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jul 13 20:43:37 EDT 2009


Author: tirelli
Date: 2009-07-13 20:43:36 -0400 (Mon, 13 Jul 2009)
New Revision: 27979

Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_SimpleTimeWindow.drl
Log:
improving unit tests for idle time

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java	2009-07-14 00:09:28 UTC (rev 27978)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java	2009-07-14 00:43:36 UTC (rev 27979)
@@ -27,10 +27,12 @@
 import org.drools.common.EventFactHandle;
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalWorkingMemory;
 import org.drools.compiler.DrlParser;
 import org.drools.compiler.DroolsParserException;
 import org.drools.compiler.PackageBuilder;
 import org.drools.conf.EventProcessingOption;
+import org.drools.impl.StatefulKnowledgeSessionImpl;
 import org.drools.io.ResourceFactory;
 import org.drools.lang.descr.PackageDescr;
 import org.drools.rule.Package;
@@ -87,17 +89,18 @@
     public void testEventAssertion() throws Exception {
         // read in the source
         KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-        kbuilder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream( "test_CEP_SimpleEventAssertion.drl" ) ), 
+        kbuilder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream( "test_CEP_SimpleEventAssertion.drl" ) ),
                       ResourceType.DRL );
         KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
         kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
 
         KnowledgeSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
         conf.setOption( ClockTypeOption.get( "pseudo" ) );
-        StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession( conf, null );
-        
+        StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession( conf,
+                                                                              null );
+
         SessionPseudoClock clock = session.getSessionClock();
-        
+
         final List results = new ArrayList();
 
         session.setGlobal( "results",
@@ -121,13 +124,17 @@
                                          11000 );
 
         InternalFactHandle handle1 = (InternalFactHandle) session.insert( tick1 );
-        clock.advanceTime( 10, TimeUnit.SECONDS );
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS );
         InternalFactHandle handle2 = (InternalFactHandle) session.insert( tick2 );
-        clock.advanceTime( 30, TimeUnit.SECONDS );
+        clock.advanceTime( 30,
+                           TimeUnit.SECONDS );
         InternalFactHandle handle3 = (InternalFactHandle) session.insert( tick3 );
-        clock.advanceTime( 20, TimeUnit.SECONDS );
+        clock.advanceTime( 20,
+                           TimeUnit.SECONDS );
         InternalFactHandle handle4 = (InternalFactHandle) session.insert( tick4 );
-        clock.advanceTime( 10, TimeUnit.SECONDS );
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS );
 
         assertNotNull( handle1 );
         assertNotNull( handle2 );
@@ -153,7 +160,8 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
         final List results = new ArrayList();
 
@@ -233,7 +241,8 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
         final List results = new ArrayList();
 
@@ -301,11 +310,11 @@
         // read in the source
         final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_EventExpiration.drl" ) );
         final RuleBase ruleBase = loadRuleBase( reader );
-        
+
         final InternalRuleBase internal = (InternalRuleBase) ruleBase;
         final TimeIntervalParser parser = new TimeIntervalParser();
-        
-        assertEquals( parser.parse( "1h30m" )[0].longValue(), 
+
+        assertEquals( parser.parse( "1h30m" )[0].longValue(),
                       internal.getTypeDeclaration( StockTick.class ).getExpirationOffset() );
     }
 
@@ -319,7 +328,8 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
         final PseudoClockScheduler clock = (PseudoClockScheduler) wm.getSessionClock();
 
@@ -526,9 +536,10 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
-        final List<?> results = new ArrayList<Object>();
+        final List< ? > results = new ArrayList<Object>();
 
         wm.setGlobal( "results",
                       results );
@@ -577,9 +588,10 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
-        final List<?> results = new ArrayList<Object>();
+        final List< ? > results = new ArrayList<Object>();
 
         wm.setGlobal( "results",
                       results );
@@ -628,9 +640,10 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
-        final List<?> results = new ArrayList<Object>();
+        final List< ? > results = new ArrayList<Object>();
 
         wm.setGlobal( "results",
                       results );
@@ -670,7 +683,6 @@
                       results.get( 3 ) );
     }
 
-    // @FIXME: we need to decide on the semantics of expiration
     public void testSimpleTimeWindow() throws Exception {
         // read in the source
         final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_SimpleTimeWindow.drl" ) );
@@ -681,7 +693,8 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
         List results = new ArrayList();
 
@@ -814,7 +827,8 @@
 
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.REALTIME_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
         final List results = new ArrayList();
 
@@ -898,33 +912,43 @@
                                                 rbconf );
 
         final Rule rule = ruleBase.getPackage( "org.drools" ).getRule( "Delaying Not" );
-        assertEquals( 10000,  rule.getDuration().getDuration( null ) );
-        
+        assertEquals( 10000,
+                      rule.getDuration().getDuration( null ) );
+
         SessionConfiguration conf = new SessionConfiguration();
         conf.setClockType( ClockType.PSEUDO_CLOCK );
-        StatefulSession wm = ruleBase.newStatefulSession( conf, null );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
 
         final List results = new ArrayList();
 
         wm.setGlobal( "results",
                       results );
-        
+
         SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
-        
-        clock.advanceTime( 10, TimeUnit.SECONDS );
 
-        EventFactHandle st1 = (EventFactHandle) wm.insert( new StockTick( 1, "DROO", 100, clock.getCurrentTime() ) );
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS );
 
+        EventFactHandle st1 = (EventFactHandle) wm.insert( new StockTick( 1,
+                                                                          "DROO",
+                                                                          100,
+                                                                          clock.getCurrentTime() ) );
+
         wm.fireAllRules();
 
         // should not fire, because it must wait 10 seconds
         assertEquals( 0,
                       results.size() );
-        
-        clock.advanceTime( 5, TimeUnit.SECONDS );
 
-        EventFactHandle st2 = (EventFactHandle) wm.insert( new StockTick( 1, "DROO", 80, clock.getCurrentTime() ) );
+        clock.advanceTime( 5,
+                           TimeUnit.SECONDS );
 
+        EventFactHandle st2 = (EventFactHandle) wm.insert( new StockTick( 1,
+                                                                          "DROO",
+                                                                          80,
+                                                                          clock.getCurrentTime() ) );
+
         wm.fireAllRules();
 
         // should still not fire, because it must wait 5 more seconds, and st2 has lower price (80)
@@ -933,14 +957,15 @@
         // assert new data
         wm.fireAllRules();
 
-        clock.advanceTime( 6, TimeUnit.SECONDS );
-        
+        clock.advanceTime( 6,
+                           TimeUnit.SECONDS );
+
         wm.fireAllRules();
 
         // should fire, because waited for 10 seconds and no other event arrived with a price increase
         assertEquals( 1,
                       results.size() );
-        
+
         assertEquals( st1.getObject(),
                       results.get( 0 ) );
 
@@ -960,4 +985,185 @@
     //
     //    }
 
+    public void testIdleTime() throws Exception {
+        // read in the source
+        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+        kbuilder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream( "test_CEP_SimpleEventAssertion.drl" ) ),
+                      ResourceType.DRL );
+        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+
+        KnowledgeSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
+        conf.setOption( ClockTypeOption.get( "pseudo" ) );
+        StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession( conf,
+                                                                              null );
+        InternalWorkingMemory iwm = ((StatefulKnowledgeSessionImpl) session).session;
+
+        SessionPseudoClock clock = session.getSessionClock();
+
+        final List results = new ArrayList();
+
+        session.setGlobal( "results",
+                           results );
+
+        StockTick tick1 = new StockTick( 1,
+                                         "DROO",
+                                         50,
+                                         10000 );
+        StockTick tick2 = new StockTick( 2,
+                                         "ACME",
+                                         10,
+                                         10010 );
+        StockTick tick3 = new StockTick( 3,
+                                         "ACME",
+                                         10,
+                                         10100 );
+        StockTick tick4 = new StockTick( 4,
+                                         "DROO",
+                                         50,
+                                         11000 );
+
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+        InternalFactHandle handle1 = (InternalFactHandle) session.insert( tick1 );
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS );
+        assertEquals( 10000,
+                      iwm.getIdleTime() );
+        InternalFactHandle handle2 = (InternalFactHandle) session.insert( tick2 );
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+        clock.advanceTime( 15,
+                           TimeUnit.SECONDS );
+        assertEquals( 15000,
+                      iwm.getIdleTime() );
+        clock.advanceTime( 15,
+                           TimeUnit.SECONDS );
+        assertEquals( 30000,
+                      iwm.getIdleTime() );
+        InternalFactHandle handle3 = (InternalFactHandle) session.insert( tick3 );
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+        clock.advanceTime( 20,
+                           TimeUnit.SECONDS );
+        InternalFactHandle handle4 = (InternalFactHandle) session.insert( tick4 );
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS );
+
+        assertNotNull( handle1 );
+        assertNotNull( handle2 );
+        assertNotNull( handle3 );
+        assertNotNull( handle4 );
+
+        assertTrue( handle1.isEvent() );
+        assertTrue( handle2.isEvent() );
+        assertTrue( handle3.isEvent() );
+        assertTrue( handle4.isEvent() );
+
+        assertEquals( 10000,
+                      iwm.getIdleTime() );
+        session.fireAllRules();
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+
+        assertEquals( 2,
+                      ((List) session.getGlobal( "results" )).size() );
+
+    }
+
+    public void testIdleTime2() throws Exception {
+        // read in the source
+        final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_SimpleTimeWindow.drl" ) );
+        final RuleBaseConfiguration rbconf = new RuleBaseConfiguration();
+        rbconf.setEventProcessingMode( EventProcessingOption.STREAM );
+        final RuleBase ruleBase = loadRuleBase( reader,
+                                                rbconf );
+
+        SessionConfiguration conf = new SessionConfiguration();
+        conf.setClockType( ClockType.PSEUDO_CLOCK );
+        StatefulSession wm = ruleBase.newStatefulSession( conf,
+                                                          null );
+
+        List results = new ArrayList();
+
+        wm.setGlobal( "results",
+                      results );
+        InternalWorkingMemory iwm = (InternalWorkingMemory) wm;
+
+        // how to initialize the clock?
+        // how to configure the clock?
+        SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
+        clock.advanceTime( 5,
+                           TimeUnit.SECONDS ); // 5 seconds
+        wm.insert( new OrderEvent( "1",
+                                   "customer A",
+                                   70 ) );
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+
+        wm.fireAllRules();
+        assertEquals( 1,
+                      results.size() );
+        assertEquals( 70,
+                      ((Number) results.get( 0 )).intValue() );
+
+        // advance clock and assert new data
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS ); // 10 seconds
+        wm.insert( new OrderEvent( "2",
+                                   "customer A",
+                                   60 ) );
+        wm.fireAllRules();
+
+        assertEquals( 2,
+                      results.size() );
+        assertEquals( 65,
+                      ((Number) results.get( 1 )).intValue() );
+
+        // advance clock and assert new data
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS ); // 10 seconds
+        wm.insert( new OrderEvent( "3",
+                                   "customer A",
+                                   50 ) );
+        wm.fireAllRules();
+        assertEquals( 3,
+                      results.size() );
+        assertEquals( 60,
+                      ((Number) results.get( 2 )).intValue() );
+
+        // advance clock and assert new data
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS ); // 10 seconds
+        // advancing clock time will cause events to expire
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+
+        wm.insert( new OrderEvent( "4",
+                                   "customer A",
+                                   25 ) );
+        wm.fireAllRules();
+
+        // first event should have expired, making average under the rule threshold, so no additional rule fire
+        assertEquals( 3,
+                      results.size() );
+
+        // advance clock and assert new data
+        clock.advanceTime( 10,
+                           TimeUnit.SECONDS ); // 10 seconds
+
+        wm.insert( new OrderEvent( "5",
+                                   "customer A",
+                                   70 ) );
+        assertEquals( 0,
+                      iwm.getIdleTime() );
+
+        //        wm  = SerializationHelper.serializeObject(wm);
+        wm.fireAllRules();
+
+        // still under the threshold, so no fire
+        assertEquals( 3,
+                      results.size() );
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_SimpleTimeWindow.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_SimpleTimeWindow.drl	2009-07-14 00:09:28 UTC (rev 27978)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_SimpleTimeWindow.drl	2009-07-14 00:43:36 UTC (rev 27979)
@@ -9,7 +9,7 @@
 rule "average over threshold"
 when
     Number( $avg : intValue >= 50 ) from accumulate(
-              OrderEvent( $amt : total ) over window:time(30000),
+              OrderEvent( $amt : total ) over window:time(30s),
               average( $amt ) )
 then
     //System.out.println( "Over threshold: "+$avg );




More information about the jboss-svn-commits mailing list