Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 223   Methods: 9
NCLOC: 79   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Scheduler.java 0% 80% 77.8% 74.2%
coverage coverage
 1    package org.drools.reteoo;
 2   
 3    /*
 4    * $Id: Scheduler.java,v 1.16 2005/02/02 00:23:22 mproctor Exp $
 5    *
 6    * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved.
 7    *
 8    * Redistribution and use of this software and associated documentation
 9    * ("Software"), with or without modification, are permitted provided that the
 10    * following conditions are met:
 11    *
 12    * 1. Redistributions of source code must retain copyright statements and
 13    * notices. Redistributions must also contain a copy of this document.
 14    *
 15    * 2. Redistributions in binary form must reproduce the above copyright notice,
 16    * this list of conditions and the following disclaimer in the documentation
 17    * and/or other materials provided with the distribution.
 18    *
 19    * 3. The name "drools" must not be used to endorse or promote products derived
 20    * from this Software without prior written permission of The Werken Company.
 21    * For written permission, please contact bob@werken.com.
 22    *
 23    * 4. Products derived from this Software may not be called "drools" nor may
 24    * "drools" appear in their names without prior written permission of The Werken
 25    * Company. "drools" is a trademark of The Werken Company.
 26    *
 27    * 5. Due credit should be given to The Werken Company. (http://werken.com/)
 28    *
 29    * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 30    * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 31    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 32    * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 33    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 34    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 35    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 36    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 37    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 38    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 39    * POSSIBILITY OF SUCH DAMAGE.
 40    *
 41    */
 42   
 43    import java.util.Date;
 44    import java.util.HashMap;
 45    import java.util.Map;
 46    import java.util.Timer;
 47    import java.util.TimerTask;
 48   
 49    import org.drools.spi.AsyncExceptionHandler;
 50    import org.drools.spi.ConsequenceException;
 51   
 52    /**
 53    * Scheduler for rules requiring truth duration.
 54    *
 55    * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
 56    */
 57    final class Scheduler
 58    {
 59    // ------------------------------------------------------------
 60    // Class members
 61    // ------------------------------------------------------------
 62   
 63    /** Singleton instance. */
 64    private static final Scheduler INSTANCE = new Scheduler( );
 65   
 66    // ------------------------------------------------------------
 67    // Class methods
 68    // ------------------------------------------------------------
 69   
 70    /**
 71    * Retrieve the singleton instance.
 72    *
 73    * @return The singleton instance.
 74    */
 75  4 static Scheduler getInstance()
 76    {
 77  4 return INSTANCE;
 78    }
 79   
 80    // ------------------------------------------------------------
 81    // Instance members
 82    // ------------------------------------------------------------
 83   
 84    /** Alarm manager. */
 85    private final Timer scheduler;
 86   
 87    /** Scheduled tasks. */
 88    private final Map tasks;
 89   
 90    private AsyncExceptionHandler exceptionHandler;
 91    // ------------------------------------------------------------
 92    // Constructors
 93    // ------------------------------------------------------------
 94   
 95    /**
 96    * Construct.
 97    */
 98  1 private Scheduler()
 99    {
 100  1 this.scheduler = new Timer( true );
 101   
 102  1 this.tasks = new HashMap( );
 103    }
 104   
 105    /**
 106    * Schedule an agenda item.
 107    *
 108    * @param item
 109    * The item to schedule.
 110    * @param workingMemory
 111    * The working memory session.
 112    */
 113  2 void scheduleAgendaItem(AgendaItem item,
 114    WorkingMemoryImpl workingMemory)
 115    {
 116  2 Date now = new Date( );
 117   
 118  2 Date then = new Date( now.getTime( ) + (item.getRule( ).getDuration( ).getDuration( item.getTuple( ) ) * 1000) );
 119   
 120  2 TimerTask task = new AgendaItemFireListener( item,
 121    workingMemory );
 122   
 123  2 this.scheduler.schedule( task,
 124    then );
 125   
 126  2 this.tasks.put( item,
 127    task );
 128    }
 129   
 130    /**
 131    * Cancel an agenda item.
 132    *
 133    * @param item
 134    * The item to cancle.
 135    */
 136  0 void cancelAgendaItem(AgendaItem item)
 137    {
 138  0 TimerTask task = (TimerTask) this.tasks.remove( item );
 139   
 140  0 if ( task != null )
 141    {
 142  0 task.cancel( );
 143    }
 144    }
 145   
 146  1 void setAsyncExceptionHandler(AsyncExceptionHandler handler)
 147    {
 148  1 this.exceptionHandler = handler;
 149    }
 150   
 151  1 AsyncExceptionHandler getAsyncExceptionHandler()
 152    {
 153  1 return this.exceptionHandler;
 154    }
 155   
 156  0 public int size()
 157    {
 158  0 return this.tasks.size();
 159    }
 160   
 161    /**
 162    * Fire listener.
 163    *
 164    * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a>
 165    */
 166   
 167    class AgendaItemFireListener extends TimerTask
 168    {
 169    // ------------------------------------------------------------
 170    // Instance members
 171    // ------------------------------------------------------------
 172   
 173    /** The agenda item. */
 174    private AgendaItem item;
 175   
 176    /** The working-memory session. */
 177    private WorkingMemoryImpl workingMemory;
 178   
 179    // ------------------------------------------------------------
 180    // Constructors
 181    // ------------------------------------------------------------
 182   
 183    /**
 184    * Construct.
 185    *
 186    * @param item
 187    * The agenda item.
 188    * @param workingMemory
 189    * The working memory session.
 190    */
 191  2 AgendaItemFireListener(AgendaItem item,
 192    WorkingMemoryImpl workingMemory)
 193    {
 194  2 this.item = item;
 195  2 this.workingMemory = workingMemory;
 196    }
 197   
 198    // ------------------------------------------------------------
 199    // Instance methods
 200    // ------------------------------------------------------------
 201   
 202    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 203    // fr.dyade.jdring.AlarmListener
 204    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 205   
 206    /**
 207    * Handle the firing of an alarm.
 208    */
 209  2 public void run()
 210    {
 211  2 try
 212    {
 213  2 this.item.fire( this.workingMemory );
 214  1 Scheduler.this.tasks.remove( item );
 215    }
 216    catch ( ConsequenceException e )
 217    {
 218   
 219  1 Scheduler.getInstance().getAsyncExceptionHandler().handleException( this.workingMemory, e);
 220    }
 221    }
 222    }
 223    }