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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Dec 19 14:18:33 EST 2008


Author: tirelli
Date: 2008-12-19 14:18:33 -0500 (Fri, 19 Dec 2008)
New Revision: 24442

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_AfterOperatorDates.drl
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_BeforeOperatorDates.drl
Removed:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java
Log:
JBRULES-1894: adding support to arbitrary date time for 'before' operator

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	2008-12-19 18:48:15 UTC (rev 24441)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java	2008-12-19 19:18:33 UTC (rev 24442)
@@ -500,9 +500,9 @@
 
     }
 
-    public void testTemporalOperatorsOnArbitraryDates() throws Exception {
+    public void testAfterOnArbitraryDates() throws Exception {
         // read in the source
-        final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_TemporalOperatorsDates.drl" ) );
+        final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_AfterOperatorDates.drl" ) );
         final RuleBaseConfiguration rbconf = new RuleBaseConfiguration();
         final RuleBase ruleBase = loadRuleBase( reader,
                                                 rbconf );
@@ -551,6 +551,57 @@
                       results.get( 3 ) );
     }
 
+    public void testBeforeOnArbitraryDates() throws Exception {
+        // read in the source
+        final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_BeforeOperatorDates.drl" ) );
+        final RuleBaseConfiguration rbconf = new RuleBaseConfiguration();
+        final RuleBase ruleBase = loadRuleBase( reader,
+                                                rbconf );
+
+        SessionConfiguration conf = new SessionConfiguration();
+        conf.setClockType( ClockType.PSEUDO_CLOCK );
+        StatefulSession wm = ruleBase.newStatefulSession( conf );
+
+        final List<?> results = new ArrayList<Object>();
+
+        wm.setGlobal( "results",
+                      results );
+
+        StockTick tick1 = new StockTick( 1,
+                                         "DROO",
+                                         50,
+                                         104000, // arbitrary timestamp
+                                         3 );
+        StockTick tick2 = new StockTick( 2,
+                                         "ACME",
+                                         10,
+                                         100000, // 4 seconds after DROO
+                                         3 );
+
+        InternalFactHandle handle1 = (InternalFactHandle) wm.insert( tick1 );
+        InternalFactHandle handle2 = (InternalFactHandle) wm.insert( tick2 );
+
+        assertNotNull( handle1 );
+        assertNotNull( handle2 );
+
+        assertTrue( handle1.isEvent() );
+        assertTrue( handle2.isEvent() );
+
+        //        wm  = SerializationHelper.serializeObject(wm);
+        wm.fireAllRules();
+
+        assertEquals( 4,
+                      results.size() );
+        assertEquals( tick1,
+                      results.get( 0 ) );
+        assertEquals( tick2,
+                      results.get( 1 ) );
+        assertEquals( tick1,
+                      results.get( 2 ) );
+        assertEquals( tick2,
+                      results.get( 3 ) );
+    }
+
     // @FIXME: we need to decide on the semantics of expiration
     public void FIXME_testSimpleTimeWindow() throws Exception {
         // read in the source

Copied: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_AfterOperatorDates.drl (from rev 24403, labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl)
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_AfterOperatorDates.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_AfterOperatorDates.drl	2008-12-19 19:18:33 UTC (rev 24442)
@@ -0,0 +1,48 @@
+package org.drools;
+
+import org.drools.StockTick;
+
+global java.util.List results;
+
+declare StockTick
+    @role( event )
+    @timestamp( time )
+end
+
+rule "after operator on date"
+    salience 10
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", dateTimestamp after[1s,11s] $a.dateTimestamp )
+then
+    results.add( $a );
+end
+
+rule "after operator on long"
+    salience 5
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", time after[1s,12s] $a.time )
+then
+    results.add( $b );
+end
+
+
+rule "after operator on mixed event and date"
+    salience 3
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", this after[1s,13s] $a.time )
+then
+    results.add( $a );
+end
+
+rule "after operator on mixed date and event"
+    salience 0
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", time after[1s,14s] $a )
+then
+    results.add( $b );
+end
+


Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_AfterOperatorDates.drl
___________________________________________________________________
Name: svn:mergeinfo
   + 

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_BeforeOperatorDates.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_BeforeOperatorDates.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_BeforeOperatorDates.drl	2008-12-19 19:18:33 UTC (rev 24442)
@@ -0,0 +1,48 @@
+package org.drools;
+
+import org.drools.StockTick;
+
+global java.util.List results;
+
+declare StockTick
+    @role( event )
+    @timestamp( time )
+end
+
+rule "after operator on date"
+    salience 10
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", dateTimestamp before[1s,11s] $a.dateTimestamp )
+then
+    results.add( $a );
+end
+
+rule "after operator on long"
+    salience 5
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", time before[1s,12s] $a.time )
+then
+    results.add( $b );
+end
+
+
+rule "after operator on mixed event and date"
+    salience 3
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", this before[1s,13s] $a.time )
+then
+    results.add( $a );
+end
+
+rule "after operator on mixed date and event"
+    salience 0
+when
+    $a : StockTick( company == "DROO" )
+    $b : StockTick( company == "ACME", time before[1s,14s] $a )
+then
+    results.add( $b );
+end
+

Deleted: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl	2008-12-19 18:48:15 UTC (rev 24441)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_TemporalOperatorsDates.drl	2008-12-19 19:18:33 UTC (rev 24442)
@@ -1,48 +0,0 @@
-package org.drools;
-
-import org.drools.StockTick;
-
-global java.util.List results;
-
-declare StockTick
-    @role( event )
-    @timestamp( time )
-end
-
-rule "after operator on date"
-    salience 10
-when
-    $a : StockTick( company == "DROO" )
-    $b : StockTick( company == "ACME", dateTimestamp after[1s,11s] $a.dateTimestamp )
-then
-    results.add( $a );
-end
-
-rule "after operator on long"
-    salience 5
-when
-    $a : StockTick( company == "DROO" )
-    $b : StockTick( company == "ACME", time after[1s,12s] $a.time )
-then
-    results.add( $b );
-end
-
-
-rule "after operator on mixed event and date"
-    salience 3
-when
-    $a : StockTick( company == "DROO" )
-    $b : StockTick( company == "ACME", this after[1s,13s] $a.time )
-then
-    results.add( $a );
-end
-
-rule "after operator on mixed date and event"
-    salience 0
-when
-    $a : StockTick( company == "DROO" )
-    $b : StockTick( company == "ACME", time after[1s,14s] $a.time )
-then
-    results.add( $b );
-end
-

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java	2008-12-19 18:48:15 UTC (rev 24441)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java	2008-12-19 19:18:33 UTC (rev 24442)
@@ -21,16 +21,19 @@
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.drools.RuntimeDroolsException;
 import org.drools.base.BaseEvaluator;
 import org.drools.base.ValueType;
+import org.drools.base.evaluators.AfterEvaluatorDefinition.AfterEvaluator;
 import org.drools.base.evaluators.EvaluatorDefinition.Target;
 import org.drools.common.EventFactHandle;
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalWorkingMemory;
+import org.drools.rule.VariableRestriction.LongVariableContextEntry;
 import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
 import org.drools.rule.VariableRestriction.VariableContextEntry;
 import org.drools.spi.Evaluator;
@@ -134,16 +137,16 @@
     public Evaluator getEvaluator(final ValueType type,
                                   final String operatorId,
                                   final boolean isNegated,
-                                  final String parameterText ) {
+                                  final String parameterText) {
         return this.getEvaluator( type,
                                   operatorId,
                                   isNegated,
                                   parameterText,
                                   Target.HANDLE,
                                   Target.HANDLE );
-        
+
     }
-    
+
     /**
      * @inheritDoc
      */
@@ -152,7 +155,7 @@
                                   final boolean isNegated,
                                   final String parameterText,
                                   final Target left,
-                                  final Target right ) {
+                                  final Target right) {
         if ( this.cache == Collections.EMPTY_MAP ) {
             this.cache = new HashMap<String, BeforeEvaluator>();
         }
@@ -163,7 +166,9 @@
             eval = new BeforeEvaluator( type,
                                         isNegated,
                                         params,
-                                        parameterText );
+                                        parameterText,
+                                        left == Target.FACT,
+                                        right == Target.FACT );
             this.cache.put( key,
                             eval );
         }
@@ -209,6 +214,8 @@
         private long              initRange;
         private long              finalRange;
         private String            paramText;
+        private boolean           unwrapLeft;
+        private boolean           unwrapRight;
 
         public BeforeEvaluator() {
         }
@@ -216,10 +223,14 @@
         public BeforeEvaluator(final ValueType type,
                                final boolean isNegated,
                                final Long[] parameters,
-                               final String paramText) {
+                               final String paramText,
+                               final boolean unwrapLeft,
+                               final boolean unwrapRight) {
             super( type,
                    isNegated ? NOT_BEFORE : BEFORE );
             this.paramText = paramText;
+            this.unwrapLeft = unwrapLeft;
+            this.unwrapRight = unwrapRight;
             this.setParameters( parameters );
         }
 
@@ -229,6 +240,8 @@
             initRange = in.readLong();
             finalRange = in.readLong();
             paramText = (String) in.readObject();
+            unwrapLeft = in.readBoolean();
+            unwrapRight = in.readBoolean();
         }
 
         public void writeExternal(ObjectOutput out) throws IOException {
@@ -236,14 +249,21 @@
             out.writeLong( initRange );
             out.writeLong( finalRange );
             out.writeObject( paramText );
+            out.writeBoolean( unwrapLeft );
+            out.writeBoolean( unwrapRight );
         }
 
         @Override
         public Object prepareLeftObject(InternalFactHandle handle) {
-            return handle;
+            return unwrapLeft ? handle.getObject() : handle;
         }
 
         @Override
+        public Object prepareRightObject(InternalFactHandle handle) {
+            return unwrapRight ? handle.getObject() : handle;
+        }
+
+        @Override
         public boolean isTemporal() {
             return true;
         }
@@ -284,7 +304,24 @@
             if ( context.rightNull ) {
                 return false;
             }
-            long dist = ((EventFactHandle) left).getStartTimestamp() - ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getEndTimestamp();
+            long rightTS;
+            if ( this.unwrapRight ) {
+                if ( context instanceof ObjectVariableContextEntry ) {
+                    if ( ((ObjectVariableContextEntry) context).right instanceof Date ) {
+                        rightTS = ((Date) ((ObjectVariableContextEntry) context).right).getTime();
+                    } else {
+                        rightTS = ((Number) ((ObjectVariableContextEntry) context).right).longValue();
+                    }
+                } else {
+                    rightTS = ((LongVariableContextEntry) context).right;
+                }
+            } else {
+                rightTS = ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getEndTimestamp();
+            }
+            long leftTS = this.unwrapLeft ? context.declaration.getExtractor().getLongValue( workingMemory,
+                                                                                             left ) : ((EventFactHandle) left).getStartTimestamp();
+
+            long dist = leftTS - rightTS;
             return this.getOperator().isNegated() ^ (dist >= this.initRange && dist <= this.finalRange);
         }
 
@@ -295,8 +332,25 @@
                                                 right ) ) {
                 return false;
             }
-            long dist = ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp() - ((EventFactHandle) right).getEndTimestamp();
+            long rightTS = this.unwrapRight ? context.extractor.getLongValue( workingMemory,
+                                                                              right ) : ((EventFactHandle) right).getEndTimestamp();
 
+            long leftTS;
+            if ( this.unwrapLeft ) {
+                if ( context instanceof ObjectVariableContextEntry ) {
+                    if ( ((ObjectVariableContextEntry) context).left instanceof Date ) {
+                        leftTS = ((Date) ((ObjectVariableContextEntry) context).left).getTime();
+                    } else {
+                        leftTS = ((Number) ((ObjectVariableContextEntry) context).left).longValue();
+                    }
+                } else {
+                    leftTS = ((LongVariableContextEntry) context).left;
+                }
+            } else {
+                leftTS = ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp();
+            }
+            long dist = leftTS - rightTS;
+
             return this.getOperator().isNegated() ^ (dist >= this.initRange && dist <= this.finalRange);
         }
 
@@ -309,7 +363,14 @@
                                          object1 ) ) {
                 return false;
             }
-            long dist = ((EventFactHandle) object2).getStartTimestamp() - ((EventFactHandle) object1).getEndTimestamp();
+            long rightTS = this.unwrapRight ? extractor1.getLongValue( workingMemory,
+                                                                       object1 ) : ((EventFactHandle) object1).getEndTimestamp();
+
+            long leftTS = this.unwrapLeft ? extractor2.getLongValue( workingMemory,
+                                                                     object2 ) : ((EventFactHandle) object2).getStartTimestamp();
+
+            long dist = leftTS - rightTS;
+
             return this.getOperator().isNegated() ^ (dist >= this.initRange && dist <= this.finalRange);
         }
 
@@ -326,6 +387,9 @@
             int result = super.hashCode();
             result = PRIME * result + (int) (finalRange ^ (finalRange >>> 32));
             result = PRIME * result + (int) (initRange ^ (initRange >>> 32));
+            result = PRIME * result + ((paramText == null) ? 0 : paramText.hashCode());
+            result = PRIME * result + (unwrapLeft ? 1231 : 1237);
+            result = PRIME * result + (unwrapRight ? 1231 : 1237);
             return result;
         }
 
@@ -337,8 +401,15 @@
             if ( this == obj ) return true;
             if ( !super.equals( obj ) ) return false;
             if ( getClass() != obj.getClass() ) return false;
-            final BeforeEvaluator other = (BeforeEvaluator) obj;
-            return finalRange == other.finalRange && initRange == other.initRange;
+            BeforeEvaluator other = (BeforeEvaluator) obj;
+            if ( finalRange != other.finalRange ) return false;
+            if ( initRange != other.initRange ) return false;
+            if ( paramText == null ) {
+                if ( other.paramText != null ) return false;
+            } else if ( !paramText.equals( other.paramText ) ) return false;
+            if ( unwrapLeft != other.unwrapLeft ) return false;
+            if ( unwrapRight != other.unwrapRight ) return false;
+            return true;
         }
 
         /**

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java	2008-12-19 18:48:15 UTC (rev 24441)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PatternExtractor.java	2008-12-19 19:18:33 UTC (rev 24442)
@@ -9,6 +9,7 @@
 import org.drools.RuntimeDroolsException;
 import org.drools.base.ClassObjectType;
 import org.drools.base.ValueType;
+import org.drools.common.EventFactHandle;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.facttemplates.Fact;
 import org.drools.util.ClassUtils;
@@ -147,6 +148,9 @@
                              final Object object) {
         if ( this.objectType.getValueType().isNumber() ) {
             return ((Number) object).longValue();
+        } else if( object instanceof EventFactHandle ) {
+            // special case for handling event timestamps
+            return ((EventFactHandle)object).getStartTimestamp();
         }
         throw new RuntimeDroolsException( "Conversion to long not supported for type: " + object.getClass() );
     }




More information about the jboss-svn-commits mailing list