[jboss-svn-commits] JBL Code SVN: r24344 - in labs/jbossrules/trunk: drools-compiler/src/main/java/org/drools/lang/descr and 7 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Dec 10 19:14:04 EST 2008


Author: tirelli
Date: 2008-12-10 19:14:04 -0500 (Wed, 10 Dec 2008)
New Revision: 24344

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_EventExpiration.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/SlidingWindowDescr.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
Log:
JBRULES-1891: Add support to fact expiration, change expiration offset to take into account not only session clock current time but also the actual event timestamp.

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -42,6 +42,7 @@
 import org.drools.base.ClassFieldAccessor;
 import org.drools.base.ClassFieldAccessorCache;
 import org.drools.base.ClassFieldAccessorStore;
+import org.drools.base.evaluators.TimeIntervalParser;
 import org.drools.builder.DecisionTableConfiguration;
 import org.drools.builder.ResourceType;
 import org.drools.builder.ResourceConfiguration;
@@ -139,6 +140,8 @@
     private Resource                      resource;
 
     private List<DSLTokenizedMappingFile> dslFiles;
+    
+    private TimeIntervalParser            timeParser;
 
     /**
      * Use this when package is starting from scratch.
@@ -1004,7 +1007,13 @@
                                                                   duration,
                                                                   type.new DurationAccessorSetter() );
             }
-
+            String expiration = typeDescr.getMetaAttribute( TypeDeclaration.ATTR_EXPIRE );
+            if ( expiration != null ) {
+                if( timeParser == null ) {
+                    timeParser = new TimeIntervalParser();
+                }
+                type.setExpirationOffset( timeParser.parse( expiration )[0].longValue() );
+            }
             pkgRegistry.getPackage().addTypeDeclaration( type );
         }
     }

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/SlidingWindowDescr.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/SlidingWindowDescr.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/SlidingWindowDescr.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -18,13 +18,18 @@
 
 package org.drools.lang.descr;
 
+import org.drools.base.evaluators.TimeIntervalParser;
+
 /**
  * A descriptor for sliding windows
  * 
  * @author etirelli
  */
 public class SlidingWindowDescr extends BehaviorDescr {
+    private static final TimeIntervalParser parser = new TimeIntervalParser();
+    
     private String parameters;
+    
 
     public SlidingWindowDescr() {
         super();
@@ -54,8 +59,7 @@
 
     
     public long getLength() {
-        // TODO: need to improve the framework to support units, like seconds, minutes, hours, etc
-        return Long.parseLong( this.parameters );
+        return parser.parse( this.parameters )[0].longValue();
     }
 
 

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-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/CepEspTest.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -19,8 +19,10 @@
 import org.drools.StatefulSession;
 import org.drools.StockTick;
 import org.drools.RuleBaseConfiguration.EventProcessingMode;
+import org.drools.base.evaluators.TimeIntervalParser;
 import org.drools.common.EventFactHandle;
 import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalRuleBase;
 import org.drools.compiler.DrlParser;
 import org.drools.compiler.DroolsParserException;
 import org.drools.compiler.PackageBuilder;
@@ -278,6 +280,18 @@
 
     }
 
+    public void testEventExpiration() throws Exception {
+        // 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(), 
+                      internal.getTypeDeclaration( StockTick.class ).getExpirationOffset() );
+    }
+
     public void testTimeRelationalOperators() throws Exception {
         // read in the source
         final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_CEP_TimeRelationalOperators.drl" ) );

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_EventExpiration.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_EventExpiration.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_CEP_EventExpiration.drl	2008-12-11 00:14:04 UTC (rev 24344)
@@ -0,0 +1,18 @@
+package org.drools;
+
+import org.drools.StockTick;
+
+global java.util.List results;
+
+declare StockTick 
+    @role( event )
+    @timestamp( dateTimestamp )
+    @expires( 1h30m )
+end
+
+rule "Check event"
+when
+    $st : StockTick( company == "ACME" )
+then
+    results.add( $st );
+end

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -42,7 +42,7 @@
     private static final long serialVersionUID = 400L;
 
     /** Java object class. */
-    protected Class           cls;
+    protected Class<?>        cls;
 
     protected String          clsName;
 
@@ -63,7 +63,7 @@
      * @param objectTypeClass
      *            Java object class.
      */
-    public ClassObjectType(final Class objectTypeClass) {
+    public ClassObjectType(final Class<?> objectTypeClass) {
         this( objectTypeClass,
               false );
     }
@@ -74,7 +74,7 @@
      * @param objectTypeClass the class represented by this class object type
      * @param isEvent true if it is an event class, false otherwise
      */
-    public ClassObjectType(final Class objectTypeClass,
+    public ClassObjectType(final Class<?> objectTypeClass,
                            final boolean isEvent) {
         this.cls = objectTypeClass;
         this.isEvent = isEvent;
@@ -118,7 +118,7 @@
      *
      * @return The Java object class.
      */
-    public Class getClassType() {
+    public Class<?> getClassType() {
         return this.cls;
     }
 
@@ -126,7 +126,7 @@
         return this.clsName;
     }
 
-    public void setClassType(Class cls) {
+    public void setClassType(Class<?> cls) {
         this.cls = cls;
         this.valueType = ValueType.determineValueType( cls );
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -24,6 +24,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutput;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -598,6 +599,10 @@
         return this.classTypeDeclaration.get( clazz );
     }
 
+    public Collection<TypeDeclaration> getTypeDeclarations() {
+        return this.classTypeDeclaration.values();
+    }
+    
     public synchronized void addRule(final Package pkg,
                                      final Rule rule) throws InvalidPatternException {
         this.eventSupport.fireBeforeRuleAdded( pkg,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -17,6 +17,7 @@
  */
 
 import java.io.IOException;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -125,6 +126,13 @@
     public TypeDeclaration getTypeDeclaration(Class<?> clazz);
 
     /**
+     * Returns a collection with all TypeDeclarations in this rulebase
+     * 
+     * @return
+     */
+    public Collection<TypeDeclaration> getTypeDeclarations();
+
+    /**
      * Creates and allocates a new partition ID for this rulebase
      * 
      * @return
@@ -136,4 +144,5 @@
      * @return
      */
     List<RuleBasePartitionId> getPartitionIds();
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -26,6 +26,7 @@
 import org.drools.common.AbstractRuleBase;
 import org.drools.common.BaseNode;
 import org.drools.common.DroolsObjectInputStream;
+import org.drools.common.EventFactHandle;
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.common.NodeMemory;
@@ -190,7 +191,8 @@
                                                                                       this );
             TimerService clock = workingMemory.getTimerService();
 
-            long nextTimestamp = clock.getCurrentTime() + this.expirationOffset;
+            long nextTimestamp = Math.max( clock.getCurrentTime() + this.expirationOffset,
+                                           ((EventFactHandle)factHandle).getStartTimestamp() + this.expirationOffset );
             JobContext jobctx = new ExpireJobContext( expire,
                                                       workingMemory );
             JobHandle handle = clock.scheduleJob( job,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/PatternBuilder.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -37,6 +37,7 @@
 import org.drools.rule.Pattern;
 import org.drools.rule.PatternSource;
 import org.drools.rule.RuleConditionElement;
+import org.drools.rule.TypeDeclaration;
 import org.drools.spi.AlphaNodeFieldConstraint;
 import org.drools.spi.Constraint;
 import org.drools.spi.ObjectType;
@@ -224,7 +225,16 @@
                                                  objectType,
                                                  context );
         if( objectType.isEvent() && EventProcessingMode.STREAM.equals( context.getRuleBase().getConfiguration().getEventProcessingMode() ) ) {
-            otn.setExpirationOffset( context.getTemporalDistance().getExpirationOffset( pattern ) );
+            long expirationOffset = 0;
+            for( TypeDeclaration type : context.getRuleBase().getTypeDeclarations() ) {
+                if( type.getObjectType().isAssignableFrom( objectType ) ) {
+                    expirationOffset = Math.max( type.getExpirationOffset(), expirationOffset );
+                }
+                
+            }
+            if( expirationOffset == 0) {
+                otn.setExpirationOffset( context.getTemporalDistance().getExpirationOffset( pattern ) );
+            }
         }
 
         context.setObjectSource( (ObjectSource) utils.attachNode( context,

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java	2008-12-10 19:18:55 UTC (rev 24343)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java	2008-12-11 00:14:04 UTC (rev 24344)
@@ -24,12 +24,15 @@
 import java.io.ObjectOutput;
 import java.io.Serializable;
 
+import org.drools.base.ClassObjectType;
 import org.drools.definition.KnowledgeDefinition;
 import org.drools.factmodel.ClassDefinition;
 import org.drools.facttemplates.FactTemplate;
+import org.drools.facttemplates.FactTemplateObjectType;
 import org.drools.io.Resource;
 import org.drools.spi.AcceptsReadAccessor;
 import org.drools.spi.InternalReadAccessor;
+import org.drools.spi.ObjectType;
 
 /**
  * The type declaration class stores all type's metadata
@@ -46,6 +49,7 @@
     public static final String ATTR_TEMPLATE  = "template";
     public static final String ATTR_DURATION  = "duration";
     public static final String ATTR_TIMESTAMP = "timestamp";
+    public static final String ATTR_EXPIRE    = "expires";
 
     public static enum Role {
         FACT, EVENT;
@@ -88,7 +92,10 @@
     private FactTemplate         typeTemplate;
     private ClassDefinition      typeClassDef;
     private Resource             resource;
-    
+
+    private transient ObjectType objectType;
+    private long                 expirationOffset;
+
     public TypeDeclaration() {
     }
 
@@ -114,7 +121,8 @@
         this.typeClassDef = (ClassDefinition) in.readObject();
         this.durationExtractor = (InternalReadAccessor) in.readObject();
         this.timestampExtractor = (InternalReadAccessor) in.readObject();
-        this.resource = ( Resource ) in.readObject();
+        this.resource = (Resource) in.readObject();
+        this.expirationOffset = in.readLong();
     }
 
     public void writeExternal(ObjectOutput out) throws IOException {
@@ -129,6 +137,7 @@
         out.writeObject( durationExtractor );
         out.writeObject( timestampExtractor );
         out.writeObject( this.resource );
+        out.writeLong( expirationOffset );
     }
 
     /**
@@ -291,15 +300,23 @@
         this.timestampExtractor = timestampExtractor;
     }
 
-    public class DurationAccessorSetter implements AcceptsReadAccessor, Serializable {
+    public class DurationAccessorSetter
+        implements
+        AcceptsReadAccessor,
+        Serializable {
         private static final long serialVersionUID = 1429300982505284833L;
+
         public void setReadAccessor(InternalReadAccessor readAccessor) {
             setDurationExtractor( readAccessor );
         }
     }
-    
-    public class TimestampAccessorSetter implements AcceptsReadAccessor, Serializable {
+
+    public class TimestampAccessorSetter
+        implements
+        AcceptsReadAccessor,
+        Serializable {
         private static final long serialVersionUID = 8656678871125722903L;
+
         public void setReadAccessor(InternalReadAccessor readAccessor) {
             setTimestampExtractor( readAccessor );
         }
@@ -312,6 +329,24 @@
     public void setResource(Resource resource) {
         this.resource = resource;
     }
-    
-    
+
+    public ObjectType getObjectType() {
+        if ( this.objectType == null ) {
+            if ( this.getFormat() == Format.POJO ) {
+                this.objectType = new ClassObjectType( this.getTypeClass() );
+            } else {
+                this.objectType = new FactTemplateObjectType( this.getTypeTemplate() );
+            }
+        }
+        return this.objectType;
+    }
+
+    public long getExpirationOffset() {
+        return this.expirationOffset;
+    }
+
+    public void setExpirationOffset(final long expirationOffset) {
+        this.expirationOffset = expirationOffset;
+    }
+
 }




More information about the jboss-svn-commits mailing list