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