[jboss-svn-commits] JBL Code SVN: r15934 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/reteoo and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Oct 19 01:59:14 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-10-19 01:59:14 -0400 (Fri, 19 Oct 2007)
New Revision: 15934

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TimeMachine.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/RuleTest.java
Log:
JBRULES-1285 Time machine (and some minor refactoring)

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-10-19 05:20:29 UTC (rev 15933)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-10-19 05:59:14 UTC (rev 15934)
@@ -2,13 +2,13 @@
 
 /*
  * Copyright 2005 JBoss Inc
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -53,6 +53,7 @@
 import org.drools.reteoo.LIANodePropagation;
 import org.drools.rule.Declaration;
 import org.drools.rule.Rule;
+import org.drools.rule.TimeMachine;
 import org.drools.ruleflow.common.core.Process;
 import org.drools.ruleflow.common.instance.ProcessInstance;
 import org.drools.ruleflow.core.RuleFlowProcess;
@@ -74,7 +75,7 @@
 
 /**
  * Implementation of <code>WorkingMemory</code>.
- * 
+ *
  * @author <a href="mailto:bob at werken.com">bob mcwhirter </a>
  * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
  * @author <a href="mailto:simon at redhillconsulting.com.au">Simon Harris </a>
@@ -151,13 +152,15 @@
 
     private int                                     processCounter;
 
+    private TimeMachine 							timeMachine = new TimeMachine();
+
     // ------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------
 
     /**
      * Construct.
-     * 
+     *
      * @param ruleBase
      *            The backing rule-base.
      */
@@ -189,7 +192,7 @@
             this.identityMap.setComparator( new IdentityAssertMapComparator() );
         }
 
-        // Only takes effect if are using idententity behaviour for assert        
+        // Only takes effect if are using idententity behaviour for assert
         if ( conf.getLogicalOverride() == LogicalOverride.DISCARD ) {
             this.discardOnLogicalOverride = true;
         } else {
@@ -199,7 +202,7 @@
 
     // ------------------------------------------------------------
     // Instance methods
-    // ------------------------------------------------------------    
+    // ------------------------------------------------------------
 
     void setRuleBase(final InternalRuleBase ruleBase) {
         this.ruleBase = ruleBase;
@@ -440,7 +443,7 @@
         // If we're already firing a rule, then it'll pick up
         // the firing for any other assertObject(..) that get
         // nested inside, avoiding concurrent-modification
-        // exceptions, depending on code paths of the actions.          
+        // exceptions, depending on code paths of the actions.
         this.halt = false;
 
         if ( isSequential() ) {
@@ -507,8 +510,8 @@
 
     //
     //        MN: The following is the traditional fireAllRules (without otherwise).
-    //            Purely kept here as this implementation of otherwise is still experimental.    
-    //    
+    //            Purely kept here as this implementation of otherwise is still experimental.
+    //
     //    public synchronized void fireAllRules(final AgendaFilter agendaFilter) throws FactException {
     //        // If we're already firing a rule, then it'll pick up
     //        // the firing for any other assertObject(..) that get
@@ -530,19 +533,19 @@
     //                this.firing = false;
     //            }
     //        }
-    //    }    
+    //    }
 
     /**
      * Returns the fact Object for the given <code>FactHandle</code>. It
      * actually attemps to return the value from the handle, before retrieving
      * it from objects map.
-     * 
+     *
      * @see WorkingMemory
-     * 
+     *
      * @param handle
      *            The <code>FactHandle</code> reference for the
      *            <code>Object</code> lookup
-     * 
+     *
      */
     public Object getObject(final FactHandle handle) {
         try {
@@ -585,7 +588,7 @@
         }
     }
 
-    /** 
+    /**
      * This is an internal method, used to avoid java.util.Iterator adaptors
      */
     public ObjectHashMap getFactHandleMap() {
@@ -1192,7 +1195,7 @@
     /**
      * modify is implemented as half way retract / assert due to the truth
      * maintenance issues.
-     * 
+     *
      * @see WorkingMemory
      */
     public void update(final FactHandle factHandle,
@@ -1315,10 +1318,10 @@
     /**
      * Retrieve the <code>JoinMemory</code> for a particular
      * <code>JoinNode</code>.
-     * 
+     *
      * @param node
      *            The <code>JoinNode</code> key.
-     * 
+     *
      * @return The node's memory.
      */
     public Object getNodeMemory(final NodeMemory node) {
@@ -1353,7 +1356,7 @@
     /**
      * Sets the AsyncExceptionHandler to handle exceptions thrown by the Agenda
      * Scheduler used for duration rules.
-     * 
+     *
      * @param handler
      */
     public void setAsyncExceptionHandler(final AsyncExceptionHandler handler) {
@@ -1438,7 +1441,7 @@
     }
 
     /**
-     * Helper method 
+     * Helper method
      */
     public Map getActivationParameters(Activation activation) {
         Map result = new HashMap();
@@ -1454,4 +1457,19 @@
         return result;
     }
 
+    /**
+     * The time machine tells you what time it is.
+     */
+	public TimeMachine getTimeMachine() {
+		return timeMachine;
+	}
+
+	/**
+	 * The time machine defaults to returning the current time when asked. However, you can use tell it to go back in time.
+	 * @param timeMachine
+	 */
+	public void setTimeMachine(TimeMachine timeMachine) {
+		this.timeMachine = timeMachine;
+	}
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java	2007-10-19 05:20:29 UTC (rev 15933)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java	2007-10-19 05:59:14 UTC (rev 15934)
@@ -8,6 +8,7 @@
 import org.drools.event.WorkingMemoryEventSupport;
 import org.drools.reteoo.LIANodePropagation;
 import org.drools.rule.Rule;
+import org.drools.rule.TimeMachine;
 import org.drools.spi.Activation;
 import org.drools.spi.FactHandleFactory;
 import org.drools.spi.PropagationContext;
@@ -16,11 +17,11 @@
 
 public interface InternalWorkingMemory
     extends
-    WorkingMemory {      
-    public long getId();    
-    
+    WorkingMemory {
+    public long getId();
+
     public void setWorkingMemoryEventSupport(WorkingMemoryEventSupport workingMemoryEventSupport);
-    
+
     public ObjectHashMap getAssertMap();
 
     public void setAgendaEventSupport(AgendaEventSupport agendaEventSupport);
@@ -38,7 +39,7 @@
     public TruthMaintenanceSystem getTruthMaintenanceSystem();
 
     public void executeQueuedActions();
-    
+
     public void queueWorkingMemoryAction(final WorkingMemoryAction action);
 
     public FactHandleFactory getFactHandleFactory();
@@ -46,17 +47,19 @@
     public void removeLogicalDependencies(final Activation activation,
                                           final PropagationContext context,
                                           final Rule rule) throws FactException;
-    
+
     void retract(final FactHandle factHandle,
                        final boolean removeLogical,
                        final boolean updateEqualsMap,
                        final Rule rule,
                        final Activation activation) throws FactException;
 
-    public Lock getLock();    
-    
+    public Lock getLock();
+
     public boolean isSequential();
-    
+
     public void addLIANodePropagation(LIANodePropagation liaNodePropagation);
-    
+
+	public TimeMachine getTimeMachine();
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2007-10-19 05:20:29 UTC (rev 15933)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2007-10-19 05:59:14 UTC (rev 15934)
@@ -2,13 +2,13 @@
 
 /*
  * Copyright 2005 JBoss Inc
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -48,9 +48,9 @@
 /**
  * Leaf Rete-OO node responsible for enacting <code>Action</code> s on a
  * matched <code>Rule</code>.
- * 
+ *
  * @see org.drools.rule.Rule
- * 
+ *
  * @author <a href="mailto:bob at eng.werken.com">bob mcwhirter </a>
  */
 public final class RuleTerminalNode extends BaseNode
@@ -63,14 +63,14 @@
     // ------------------------------------------------------------
 
     private int sequence;
-    
+
     /**
-     * 
+     *
      */
     private static final long  serialVersionUID = 400L;
     /** The rule to invoke upon match. */
     private final Rule         rule;
-    /** 
+    /**
      * the subrule reference is needed to resolve declarations
      * because declarations may have different offsets in each subrule
      */
@@ -79,16 +79,16 @@
 
     private TupleSinkNode      previousTupleSinkNode;
     private TupleSinkNode      nextTupleSinkNode;
-    
-    protected boolean          tupleMemoryEnabled;    
 
+    protected boolean          tupleMemoryEnabled;
+
     // ------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------
 
     /**
      * Construct.
-     * 
+     *
      * @param inputSource
      *            The parent tuple source.
      * @param rule
@@ -112,13 +112,13 @@
 
     /**
      * Retrieve the <code>Action</code> associated with this node.
-     * 
+     *
      * @return The <code>Action</code> associated with this node.
      */
     public Rule getRule() {
         return this.rule;
     }
-    
+
     public void setSequence(int seq) {
         this.sequence = seq;
     }
@@ -126,7 +126,7 @@
     public int getSequence() {
         return this.sequence;
     }
-    
+
     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     // org.drools.impl.TupleSink
     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -143,7 +143,7 @@
 
     /**
      * Assert a new <code>Tuple</code>.
-     * 
+     *
      * @param tuple
      *            The <code>Tuple</code> being asserted.
      * @param workingMemory
@@ -157,7 +157,7 @@
                             final boolean fireActivationCreated) {
 
         //check if the rule is effective
-        if ( !this.rule.isEffective() ) {
+        if ( !this.rule.isEffective(workingMemory.getTimeMachine()) ) {
             return;
         }
 
@@ -195,7 +195,7 @@
 
             agenda.scheduleItem( item );
             tuple.setActivation( item );
-            
+
             if ( this.tupleMemoryEnabled ) {
                 memory.getTupleMemory().add( tuple );
             }
@@ -236,7 +236,7 @@
                                                     context,
                                                     this.rule,
                                                     this.subrule );
-            
+
             if ( this.tupleMemoryEnabled ) {
                 item.setSequenence( this.sequence );
             }
@@ -254,13 +254,13 @@
                 // No RuleFlowNode so add  it directly to  the Agenda
 
                 // do not add the activation if the rule is "lock-on-active" and the AgendaGroup is active
-                // we must check the context to determine if its a new tuple or an exist re-activated tuple as part of the retract                
+                // we must check the context to determine if its a new tuple or an exist re-activated tuple as part of the retract
                 if ( context.getType() == PropagationContext.MODIFICATION ) {
                     if ( this.rule.isLockOnActive() && agendaGroup.isActive() ) {
                         Activation justifier = context.removeRetractedTuple( this.rule,
                                                                              tuple );
                         if ( justifier == null ) {
-                            // This rule is locked and active, do not allow new tuples to activate 
+                            // This rule is locked and active, do not allow new tuples to activate
                             return;
                         } else if ( this.rule.hasLogicalDependency() ) {
                             copyLogicalDependencies( context,
@@ -291,13 +291,13 @@
                 }
 
                 // do not add the activation if the rule is "lock-on-active" and the RuleFlowGroup is active
-                // we must check the context to determine if its a new tuple or an exist re-activated tuple as part of the retract                
+                // we must check the context to determine if its a new tuple or an exist re-activated tuple as part of the retract
                 if ( context.getType() == PropagationContext.MODIFICATION ) {
                     if ( this.rule.isLockOnActive() && rfg.isActive() ) {
                         Activation justifier = context.removeRetractedTuple( this.rule,
                                                                              tuple );
                         if ( justifier == null ) {
-                            // This rule is locked and active, do not allow new tuples to activate 
+                            // This rule is locked and active, do not allow new tuples to activate
                             return;
                         } else if ( this.rule.hasLogicalDependency() ) {
                             copyLogicalDependencies( context,
@@ -454,7 +454,7 @@
                                                                                           null );
                 workingMemory.getTruthMaintenanceSystem().removeLogicalDependencies( activation,
                                                                                      propagationContext,
-                                                                                     this.rule );                                
+                                                                                     this.rule );
             }
 
             workingMemory.executeQueuedActions();
@@ -462,7 +462,7 @@
         }
 
         removeShare();
-        
+
         this.tupleSource.remove( this,
                                  workingMemories );
     }
@@ -470,14 +470,14 @@
     public Object createMemory(final RuleBaseConfiguration config) {
         return new TerminalNodeMemory();
     }
-    
+
     public boolean isTupleMemoryEnabled() {
         return tupleMemoryEnabled;
     }
 
     public void setTupleMemoryEnabled(boolean tupleMemoryEnabled) {
         this.tupleMemoryEnabled = tupleMemoryEnabled;
-    }     
+    }
 
     /**
      * Returns the next node
@@ -489,7 +489,7 @@
     }
 
     /**
-     * Sets the next node 
+     * Sets the next node
      * @param next
      *      The next TupleSinkNode
      */
@@ -507,7 +507,7 @@
     }
 
     /**
-     * Sets the previous node 
+     * Sets the previous node
      * @param previous
      *      The previous TupleSinkNode
      */

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java	2007-10-19 05:20:29 UTC (rev 15933)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java	2007-10-19 05:59:14 UTC (rev 15934)
@@ -2,13 +2,13 @@
 
 /*
  * Copyright 2005 JBoss Inc
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -34,9 +34,7 @@
  * The <code>Test</code>s describe the circumstances that representrepresent
  * a match for this rule. The <code>Consequence</code> gets fired when the
  * Conditions match.
- * 
- * @see Eval
- * @see Consequence
+ *
  * @author <a href="mailto:bob at eng.werken.com"> bob mcwhirter </a>
  * @author <a href="mailto:simon at redhillconsulting.com.au"> Simon Harris </a>
  * @author <a href="mailto:mproctor at codehaus.org"> mark proctor </a>
@@ -45,7 +43,7 @@
     implements
     Serializable {
     /**
-     * 
+     *
      */
     private static final long serialVersionUID = 400L;
 
@@ -68,7 +66,7 @@
     private Declaration[]     declarationArray;
 
     private GroupElement      lhsRoot;
-    
+
     private String            dialect;
 
     private String            agendaGroup;
@@ -93,7 +91,7 @@
     private String            ruleFlowGroup;
 
     private boolean           lockOnActive;
-    
+
     private boolean           hasLogicalDependency;
 
     /** indicates that the rule is semantically correct. */
@@ -147,9 +145,9 @@
               null,
               AgendaGroup.MAIN );
     }
-    
-    
 
+
+
     public String getDialect() {
         return dialect;
     }
@@ -162,15 +160,15 @@
      * Set the truthness duration. This causes a delay before the firing of the
      * <code>Consequence</code> if the rule is still true at the end of the
      * duration.
-     * 
+     *
      * <p>
      * This is merely a convenience method for calling
      * {@link #setDuration(Duration)}with a <code>FixedDuration</code>.
      * </p>
-     * 
+     *
      * @see #setDuration(Duration)
      * @see FixedDuration
-     * 
+     *
      * @param seconds -
      *            The number of seconds the rule must hold true in order to
      *            fire.
@@ -183,7 +181,7 @@
      * Set the truthness duration object. This causes a delay before the firing
      * of the <code>Consequence</code> if the rule is still true at the end of
      * the duration.
-     * 
+     *
      * @param duration
      *            The truth duration object.
      */
@@ -193,7 +191,7 @@
 
     /**
      * Retrieve the truthness duration object.
-     * 
+     *
      * @return The truthness duration object.
      */
     public Duration getDuration() {
@@ -204,13 +202,13 @@
      * Determine if this rule is internally consistent and valid.
      * This will include checks to make sure the rules semantic components (actions and predicates)
      * are valid.
-     * 
+     *
      * No exception is thrown.
      * <p>
      * A <code>Rule</code> must include at least one parameter declaration and
      * one condition.
      * </p>
-     * 
+     *
      * @return <code>true</code> if this rule is valid, else
      *         <code>false</code>.
      */
@@ -232,7 +230,7 @@
 
     /**
      * Retrieve the name of this rule.
-     * 
+     *
      * @return The name of this rule.
      */
     public String getName() {
@@ -241,7 +239,7 @@
 
     /**
      * Retrieve the <code>Rule</code> salience.
-     * 
+     *
      * @return The salience.
      */
     public Salience getSalience() {
@@ -270,19 +268,20 @@
     }
 
     /**
-     * This returns true is the rule is effective. 
+     * This returns true is the rule is effective.
      * If the rule is not effective, it cannot activate.
-     * 
+     *
      * This uses the dateEffective, dateExpires and enabled flag to decide this.
      */
-    public boolean isEffective() {
+    public boolean isEffective(TimeMachine tm) {
         if ( !this.enabled ) {
             return false;
         }
+        final Calendar now = tm.getNow();
+
         if ( this.dateEffective == null && this.dateExpires == null ) {
             return true;
         } else {
-            final Calendar now = Calendar.getInstance();
             if ( this.dateEffective != null && this.dateExpires != null ) {
                 return (now.after( this.dateEffective ) && now.before( this.dateExpires ));
             } else if ( this.dateEffective != null ) {
@@ -324,10 +323,10 @@
 
     /**
      * Retrieve a parameter <code>Declaration</code> by identifier.
-     * 
+     *
      * @param identifier
      *            The identifier.
-     * 
+     *
      * @return The declaration or <code>null</code> if no declaration matches
      *         the <code>identifier</code>.
      */
@@ -340,7 +339,7 @@
         return (Declaration) this.declarations.get( identifier );
     }
 
-    /** 
+    /**
      * This field is updated at runtime, when the first logical assertion is done. I'm currently not too happy about having this determine at runtime
      * but its currently easier than trying to do this at compile time, although eventually this should be changed
      * @return
@@ -348,11 +347,11 @@
     public boolean hasLogicalDependency() {
         return this.hasLogicalDependency;
     }
-    
+
     public void setHasLogicalDependency(boolean hasLogicalDependency) {
         this.hasLogicalDependency = hasLogicalDependency;
     }
-    
+
     public boolean isLockOnActive() {
         return this.lockOnActive;
     }
@@ -360,11 +359,11 @@
     public void setLockOnActive(final boolean lockOnActive) {
         this.lockOnActive = lockOnActive;
     }
-    
+
     /**
      * Retrieve the set of all <i>root fact object </i> parameter
      * <code>Declarations</code>.
-     * 
+     *
      * @return The Set of <code>Declarations</code> in order which specify the
      *         <i>root fact objects</i>.
      */
@@ -379,11 +378,11 @@
 
     /**
      * Add a pattern to the rule. All patterns are searched for bindings which are then added to the rule
-     * as declarations 
-     * 
+     * as declarations
+     *
      * @param condition
      *            The <code>Test</code> to add.
-     * @throws InvalidRuleException 
+     * @throws InvalidRuleException
      */
     public void addPattern(final RuleConditionElement element) {
         this.dirty = true;
@@ -393,7 +392,7 @@
     /**
      * Retrieve the <code>List</code> of <code>Conditions</code> for this
      * rule.
-     * 
+     *
      * @return The <code>List</code> of <code>Conditions</code>.
      */
     public GroupElement getLhs() {
@@ -411,7 +410,7 @@
      * it will return an And element for each possible logic branch. The
      * processing uses as a clone of the Rule's patterns, so they are not
      * changed.
-     * 
+     *
      * @return
      * @throws InvalidPatternException
      */
@@ -450,7 +449,7 @@
     /**
      * Set the <code>Consequence</code> that is associated with the successful
      * match of this rule.
-     * 
+     *
      * @param consequence
      *            The <code>Consequence</code> to attach to this
      *            <code>Rule</code>.
@@ -462,7 +461,7 @@
     /**
      * Retrieve the <code>Consequence</code> associated with this
      * <code>Rule</code>.
-     * 
+     *
      * @return The <code>Consequence</code>.
      */
     public Consequence getConsequence() {
@@ -507,7 +506,7 @@
         this.semanticallyValid = valid;
     }
 
-    /** 
+    /**
      * This will return if the semantic actions or predicates in the rules
      * are valid.
      * This is provided so that lists of rules can be provided even if their semantic actions
@@ -532,8 +531,8 @@
     public void setDateExpires(final Calendar expiresDate) {
         this.dateExpires = expiresDate;
     }
-        
 
+
     public Calendar getDateEffective() {
         return this.dateEffective;
     }
@@ -548,7 +547,7 @@
     public void setEnabled(final boolean b) {
         this.enabled = b;
     }
-    
+
     public boolean isEnabled() {
         return this.enabled;
     }

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TimeMachine.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TimeMachine.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TimeMachine.java	2007-10-19 05:59:14 UTC (rev 15934)
@@ -0,0 +1,20 @@
+package org.drools.rule;
+
+import java.io.Serializable;
+import java.util.Calendar;
+
+/**
+ * This class allows the time for "now" to be defined outside of the Rule.
+ * Mainly for external testing tools (testing of rules, not drools).
+ *
+ * @author Michael Neale
+ */
+public class TimeMachine implements Serializable {
+
+    private static final long serialVersionUID = 400L;
+
+	public Calendar getNow() {
+		return Calendar.getInstance();
+	}
+
+}


Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TimeMachine.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/RuleTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/RuleTest.java	2007-10-19 05:20:29 UTC (rev 15933)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/RuleTest.java	2007-10-19 05:59:14 UTC (rev 15934)
@@ -12,14 +12,14 @@
     public void testDateEffective() {
         final Rule rule = new Rule( "myrule" );
 
-        assertTrue( rule.isEffective() );
+        assertTrue( rule.isEffective(new TimeMachine()) );
 
         final Calendar earlier = Calendar.getInstance();
         earlier.setTimeInMillis( 10 );
 
         rule.setDateEffective( earlier );
 
-        assertTrue( rule.isEffective() );
+        assertTrue( rule.isEffective(new TimeMachine()) );
 
         final Calendar later = Calendar.getInstance();
         later.setTimeInMillis( later.getTimeInMillis() + 100000000 );
@@ -27,27 +27,27 @@
         assertTrue( later.after( Calendar.getInstance() ) );
 
         rule.setDateEffective( later );
-        assertFalse( rule.isEffective() );
+        assertFalse( rule.isEffective(new TimeMachine()) );
 
     }
 
     public void testDateExpires() throws Exception {
         final Rule rule = new Rule( "myrule" );
 
-        assertTrue( rule.isEffective() );
+        assertTrue( rule.isEffective(new TimeMachine()) );
 
         final Calendar earlier = Calendar.getInstance();
         earlier.setTimeInMillis( 10 );
 
         rule.setDateExpires( earlier );
 
-        assertFalse( rule.isEffective() );
+        assertFalse( rule.isEffective(new TimeMachine()) );
 
         final Calendar later = Calendar.getInstance();
         later.setTimeInMillis( later.getTimeInMillis() + 100000000 );
 
         rule.setDateExpires( later );
-        assertTrue( rule.isEffective() );
+        assertTrue( rule.isEffective(new TimeMachine()) );
 
     }
 
@@ -63,31 +63,55 @@
         rule.setDateEffective( past );
         rule.setDateExpires( future );
 
-        assertTrue( rule.isEffective() );
+        assertTrue( rule.isEffective(new TimeMachine()) );
 
         rule.setDateExpires( past );
-        assertFalse( rule.isEffective() );
+        assertFalse( rule.isEffective(new TimeMachine()) );
 
         rule.setDateExpires( future );
         rule.setDateEffective( future );
 
-        assertFalse( rule.isEffective() );
 
+
+        assertFalse( rule.isEffective(new TimeMachine()) );
+
     }
 
     public void testRuleEnabled() {
         final Rule rule = new Rule( "myrule" );
         rule.setEnabled( false );
-        assertFalse( rule.isEffective() );
+        assertFalse( rule.isEffective(new TimeMachine()) );
 
         final Calendar past = Calendar.getInstance();
         past.setTimeInMillis( 10 );
 
         rule.setDateEffective( past );
-        assertFalse( rule.isEffective() );
+        assertFalse( rule.isEffective(new TimeMachine()) );
         rule.setEnabled( true );
 
-        assertTrue( rule.isEffective() );
+        assertTrue( rule.isEffective(new TimeMachine()) );
     }
 
+    public void testTimeMachine() {
+        final Rule rule = new Rule( "myrule" );
+        rule.setEnabled( true );
+        assertTrue(rule.isEffective(new TimeMachine()));
+
+        final Calendar future = Calendar.getInstance();
+        future.setTimeInMillis( future.getTimeInMillis() + 100000000 );
+        rule.setDateEffective(future);
+        assertFalse(rule.isEffective(new TimeMachine()));
+
+        assertTrue(rule.isEffective(new TimeMachine() {
+        	public Calendar getNow() {
+        		Calendar loveYouLongTime = Calendar.getInstance();
+        		loveYouLongTime.setTimeInMillis(future.getTimeInMillis() + 1000000000000L);
+        		return loveYouLongTime;
+        	}
+        }));
+
+
+
+    }
+
 }




More information about the jboss-svn-commits mailing list