01: package org.drools.common;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.Date;
20: import java.util.Timer;
21:
22: /**
23: * Scheduler for rules requiring truth duration.
24: *
25: * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
26: */
27: final class Scheduler {
28: // ------------------------------------------------------------
29: // Class members
30: // ------------------------------------------------------------
31:
32: /** Singleton instance. */
33: private static final Scheduler INSTANCE = new Scheduler();
34:
35: // ------------------------------------------------------------
36: // Class methods
37: // ------------------------------------------------------------
38:
39: /**
40: * Retrieve the singleton instance.
41: *
42: * @return The singleton instance.
43: */
44: static Scheduler getInstance() {
45: return Scheduler.INSTANCE;
46: }
47:
48: // ------------------------------------------------------------
49: // Instance members
50: // ------------------------------------------------------------
51:
52: /** Alarm manager. */
53: private final Timer scheduler;
54:
55: // ------------------------------------------------------------
56: // Constructors
57: // ------------------------------------------------------------
58:
59: /**
60: * Construct.
61: */
62: private Scheduler() {
63: this .scheduler = new Timer(true);
64: }
65:
66: /**
67: * Schedule an agenda item.
68: *
69: * @param item
70: * The item to schedule.
71: * @param workingMemory
72: * The working memory session.
73: */
74: void scheduleAgendaItem(final ScheduledAgendaItem item) {
75: final Date now = new Date();
76:
77: final Date then = new Date(now.getTime()
78: + item.getRule().getDuration().getDuration(
79: item.getTuple()));
80:
81: this.scheduler.schedule(item, then);
82: }
83: }
|