[jboss-svn-commits] JBL Code SVN: r20239 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri May 30 23:06:18 EDT 2008
Author: mark.proctor at jboss.com
Date: 2008-05-30 23:06:17 -0400 (Fri, 30 May 2008)
New Revision: 20239
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Scheduler.java
Log:
JBRULES-1625 Create Scheduler API with JDK implementation
-Duration rules now use the TimerService.
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2008-05-31 02:05:52 UTC (rev 20238)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2008-05-31 03:06:17 UTC (rev 20239)
@@ -265,7 +265,7 @@
* The item to schedule.
*/
public void scheduleItem(final ScheduledAgendaItem item) {
- Scheduler.getInstance().scheduleAgendaItem( item );
+ Scheduler.getInstance().scheduleAgendaItem( item, this );
if ( this.scheduledActivations == null ) {
this.scheduledActivations = new org.drools.util.LinkedList();
@@ -276,7 +276,7 @@
public void removeScheduleItem(final ScheduledAgendaItem item) {
this.scheduledActivations.remove( item );
- item.cancel();
+ Scheduler.getInstance().removeAgendaItem( item );
}
public void addAgendaGroup(final AgendaGroup agendaGroup) {
@@ -502,7 +502,7 @@
final EventSupport eventsupport = (EventSupport) this.workingMemory;
if ( this.scheduledActivations != null && !this.scheduledActivations.isEmpty() ) {
for ( ScheduledAgendaItem item = (ScheduledAgendaItem) this.scheduledActivations.removeFirst(); item != null; item = (ScheduledAgendaItem) this.scheduledActivations.removeFirst() ) {
- item.cancel();
+ Scheduler.getInstance().removeAgendaItem( item );
eventsupport.getAgendaEventSupport().fireActivationCancelled( item,
this.workingMemory );
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java 2008-05-31 02:05:52 UTC (rev 20238)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java 2008-05-31 03:06:17 UTC (rev 20239)
@@ -28,6 +28,7 @@
import org.drools.spi.AgendaGroup;
import org.drools.spi.PropagationContext;
import org.drools.spi.Tuple;
+import org.drools.time.JobHandle;
import org.drools.util.LinkedList;
import org.drools.util.LinkedListNode;
@@ -36,7 +37,7 @@
*
* @author <a href="mailto:bob at eng.werken.com">bob mcwhirter </a>
*/
-public class ScheduledAgendaItem extends TimerTask
+public class ScheduledAgendaItem extends AgendaItem
implements
Activation,
Externalizable,
@@ -45,135 +46,141 @@
// Instance members
// ------------------------------------------------------------
- /**
- *
- */
+// /**
+// *
+// */
private static final long serialVersionUID = 400L;
private LinkedListNode previous;
private LinkedListNode next;
-
- /** The tuple. */
- private Tuple tuple;
-
- /** The rule. */
- private Rule rule;
-
- /** The subrule */
- private GroupElement subrule;
-
+//
+// /** The tuple. */
+// private Tuple tuple;
+//
+// /** The rule. */
+// private Rule rule;
+//
+// /** The subrule */
+// private GroupElement subrule;
+//
private InternalAgenda agenda;
-
- private PropagationContext context;
-
- private long activationNumber;
-
- private LinkedList justified;
-
- private boolean activated;
-
- private ActivationGroupNode activationGroupNode;
-
- // ------------------------------------------------------------
- // Constructors
- // ------------------------------------------------------------
- public ScheduledAgendaItem() {
-
- }
-
- /**
- * Construct.
- *
- * @param tuple
- * The tuple.
- * @param rule
- * The rule.
- */
+
+ private JobHandle jobHandle;
+//
+// private PropagationContext context;
+//
+// private long activationNumber;
+//
+// private LinkedList justified;
+//
+// private boolean activated;
+//
+// private ActivationGroupNode activationGroupNode;
+//
+// // ------------------------------------------------------------
+// // Constructors
+// // ------------------------------------------------------------
+// public ScheduledAgendaItem() {
+//
+// }
+//
+// /**
+// * Construct.
+// *
+// * @param tuple
+// * The tuple.
+// * @param rule
+// * The rule.
+// */
public ScheduledAgendaItem(final long activationNumber,
final Tuple tuple,
final InternalAgenda agenda,
final PropagationContext context,
final Rule rule,
final GroupElement subrule) {
- this.tuple = tuple;
- this.context = context;
- this.rule = rule;
- this.subrule = subrule;
+ super(activationNumber, tuple, 0, context, rule, subrule);
+// this.tuple = tuple;
+// this.context = context;
+// this.rule = rule;
+// this.subrule = subrule;
this.agenda = agenda;
- this.activationNumber = activationNumber;
+// this.activationNumber = activationNumber;
}
-
- // ------------------------------------------------------------
- // Instance methods
- // ------------------------------------------------------------
+//
+// // ------------------------------------------------------------
+// // Instance methods
+// // ------------------------------------------------------------
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ super.readExternal( in );
+
previous = (LinkedListNode)in.readObject();
next = (LinkedListNode)in.readObject();
- tuple = (Tuple)in.readObject();
- rule = (Rule)in.readObject();
- subrule = (GroupElement)in.readObject();
+// tuple = (Tuple)in.readObject();
+// rule = (Rule)in.readObject();
+// subrule = (GroupElement)in.readObject();
agenda = (InternalAgenda)in.readObject();
- context = (PropagationContext)in.readObject();
- activationNumber = in.readLong();
- justified = (LinkedList)in.readObject();
- activated = in.readBoolean();
- activationGroupNode = (ActivationGroupNode)in.readObject();
+// context = (PropagationContext)in.readObject();
+// activationNumber = in.readLong();
+// justified = (LinkedList)in.readObject();
+// activated = in.readBoolean();
+// activationGroupNode = (ActivationGroupNode)in.readObject();
}
public void writeExternal(ObjectOutput out) throws IOException {
+ super.writeExternal( out );
out.writeObject(previous);
- out.writeObject(next);
- out.writeObject(tuple);
- out.writeObject(rule);
- out.writeObject(subrule);
+// out.writeObject(next);
+// out.writeObject(tuple);
+// out.writeObject(rule);
+// out.writeObject(subrule);
out.writeObject(agenda);
- out.writeObject(context);
- out.writeLong(activationNumber);
- out.writeObject(justified);
- out.writeBoolean(activated);
- out.writeObject(activationGroupNode);
+// out.writeObject(context);
+// out.writeLong(activationNumber);
+// out.writeObject(justified);
+// out.writeBoolean(activated);
+// out.writeObject(activationGroupNode);
}
-
- public PropagationContext getPropagationContext() {
- return this.context;
- }
-
- public int getSalience() {
- throw new UnsupportedOperationException( "salience is now application to scheduled activations" );
- }
-
- /**
- * Retrieve the rule.
- *
- * @return The rule.
- */
- public Rule getRule() {
- return this.rule;
- }
-
- /**
- * Retrieve the tuple.
- *
- * @return The tuple.
- */
- public Tuple getTuple() {
- return this.tuple;
- }
-
- /**
- * Handle the firing of an alarm.
- */
- public void run() {
- this.agenda.fireActivation( this );
- this.agenda.getScheduledActivationsLinkedList().remove( this );
- this.agenda.getWorkingMemory().fireAllRules();
- }
-
- public long getActivationNumber() {
- return this.activationNumber;
- }
-
+//
+// public PropagationContext getPropagationContext() {
+// return this.context;
+// }
+//
+// public int getSalience() {
+// throw new UnsupportedOperationException( "salience is now application to scheduled activations" );
+// }
+//
+// /**
+// * Retrieve the rule.
+// *
+// * @return The rule.
+// */
+// public Rule getRule() {
+// return this.rule;
+// }
+//
+// /**
+// * Retrieve the tuple.
+// *
+// * @return The tuple.
+// */
+// public Tuple getTuple() {
+// return this.tuple;
+// }
+//
+// /**
+// * Handle the firing of an alarm.
+// */
+// public void run() {
+// this.agenda.fireActivation( this );
+// this.agenda.getScheduledActivationsLinkedList().remove( this );
+// this.agenda.getWorkingMemory().fireAllRules();
+// }
+//
+// public long getActivationNumber() {
+// return this.activationNumber;
+// }
+//
public LinkedListNode getNext() {
return this.next;
}
@@ -189,88 +196,96 @@
public void setPrevious(final LinkedListNode previous) {
this.previous = previous;
}
-
+//
public void remove() {
this.agenda.removeScheduleItem( this );
}
-
- public String toString() {
- return "[Activation rule=" + this.rule.getName() + ", tuple=" + this.tuple + "]";
+//
+// public String toString() {
+// return "[Activation rule=" + this.rule.getName() + ", tuple=" + this.tuple + "]";
+// }
+//
+// public void addLogicalDependency(final LogicalDependency node) {
+// if ( this.justified == null ) {
+// this.justified = new LinkedList();
+// }
+//
+// this.justified.add( node );
+// }
+//
+// public LinkedList getLogicalDependencies() {
+// return this.justified;
+// }
+//
+// public boolean isActivated() {
+// return this.activated;
+// }
+//
+// public void setActivated(final boolean activated) {
+// this.activated = activated;
+// }
+//
+// public ActivationGroupNode getActivationGroupNode() {
+// return this.activationGroupNode;
+// }
+//
+// public void setActivationGroupNode(final ActivationGroupNode activationGroupNode) {
+// this.activationGroupNode = activationGroupNode;
+// }
+//
+// public RuleFlowGroupNode getRuleFlowGroupNode() {
+// return null;
+// }
+//
+// public void setRuleFlowGroupNode(final RuleFlowGroupNode ruleFlowGroupNode) {
+// throw new UnsupportedOperationException( "Scheduled activations cannot be in a Rule Flow Group" );
+// }
+//
+// public AgendaGroup getAgendaGroup() {
+// return null;
+// }
+//
+// /*
+// * (non-Javadoc)
+// *
+// * @see java.lang.Object#equals(java.lang.Object)
+// */
+// public boolean equals(final Object object) {
+// if ( object == this ) {
+// return true;
+// }
+//
+// if ( (object == null) || !(object instanceof AgendaItem) ) {
+// return false;
+// }
+//
+// final AgendaItem otherItem = (AgendaItem) object;
+//
+// return (this.rule.equals( otherItem.getRule() ) && this.tuple.equals( otherItem.getTuple() ));
+// }
+//
+// /**
+// * Return the hashode of the
+// * <code>TupleKey<code> as the hashCode of the AgendaItem
+// * @return
+// */
+// public int hashCode() {
+// return this.tuple.hashCode();
+// }
+//
+// public GroupElement getSubRule() {
+// return this.subrule;
+// }
+//
+// public void setLogicalDependencies(LinkedList justified) {
+// this.justified = justified;
+// }
+
+ public JobHandle getJobHandle() {
+ return this.jobHandle;
}
- public void addLogicalDependency(final LogicalDependency node) {
- if ( this.justified == null ) {
- this.justified = new LinkedList();
- }
-
- this.justified.add( node );
- }
-
- public LinkedList getLogicalDependencies() {
- return this.justified;
- }
-
- public boolean isActivated() {
- return this.activated;
- }
-
- public void setActivated(final boolean activated) {
- this.activated = activated;
- }
-
- public ActivationGroupNode getActivationGroupNode() {
- return this.activationGroupNode;
- }
-
- public void setActivationGroupNode(final ActivationGroupNode activationGroupNode) {
- this.activationGroupNode = activationGroupNode;
- }
-
- public RuleFlowGroupNode getRuleFlowGroupNode() {
- return null;
- }
-
- public void setRuleFlowGroupNode(final RuleFlowGroupNode ruleFlowGroupNode) {
- throw new UnsupportedOperationException( "Scheduled activations cannot be in a Rule Flow Group" );
- }
-
- public AgendaGroup getAgendaGroup() {
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(final Object object) {
- if ( object == this ) {
- return true;
- }
-
- if ( (object == null) || !(object instanceof AgendaItem) ) {
- return false;
- }
-
- final AgendaItem otherItem = (AgendaItem) object;
-
- return (this.rule.equals( otherItem.getRule() ) && this.tuple.equals( otherItem.getTuple() ));
- }
-
- /**
- * Return the hashode of the
- * <code>TupleKey<code> as the hashCode of the AgendaItem
- * @return
- */
- public int hashCode() {
- return this.tuple.hashCode();
- }
-
- public GroupElement getSubRule() {
- return this.subrule;
- }
-
- public void setLogicalDependencies(LinkedList justified) {
- this.justified = justified;
- }
+ public void setJobHandle(JobHandle jobHandle) {
+ this.jobHandle = jobHandle;
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Scheduler.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Scheduler.java 2008-05-31 02:05:52 UTC (rev 20238)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Scheduler.java 2008-05-31 03:06:17 UTC (rev 20239)
@@ -16,8 +16,13 @@
* limitations under the License.
*/
-import java.util.Date;
-import java.util.Timer;
+import org.drools.Agenda;
+import org.drools.process.instance.timer.TimerManager.TimerTrigger;
+import org.drools.time.Job;
+import org.drools.time.JobContext;
+import org.drools.time.JobHandle;
+import org.drools.time.TimerService;
+import org.drools.time.impl.JDKTimerService;
/**
* Scheduler for rules requiring truth duration.
@@ -50,7 +55,7 @@
// ------------------------------------------------------------
/** Alarm manager. */
- private final Timer scheduler;
+ private final TimerService timerService;
// ------------------------------------------------------------
// Constructors
@@ -60,7 +65,7 @@
* Construct.
*/
private Scheduler() {
- this.scheduler = new Timer( true );
+ this.timerService = new JDKTimerService();
}
/**
@@ -71,12 +76,60 @@
* @param workingMemory
* The working memory session.
*/
- void scheduleAgendaItem(final ScheduledAgendaItem item) {
- final Date now = new Date();
+ void scheduleAgendaItem(final ScheduledAgendaItem item, DefaultAgenda agenda) {
+ DuractionJob job = new DuractionJob();
+ DuractionJobContext ctx = new DuractionJobContext( item, agenda );
+ TimerTrigger trigger = new TimerTrigger( item.getRule().getDuration().getDuration( item.getTuple() ), 0);
+
+
+ JobHandle jobHandle = this.timerService.scheduleJob( job, ctx, trigger );
+ item.setJobHandle( jobHandle );
+ }
+
+ public void removeAgendaItem(final ScheduledAgendaItem item) {
+ this.timerService.removeJob( item.getJobHandle() );
+ }
+
+ public static class DuractionJob implements Job {
+ public void execute(JobContext ctx) {
+ DefaultAgenda agenda = ( DefaultAgenda ) ((DuractionJobContext)ctx).getAgenda();
+ ScheduledAgendaItem item = ((DuractionJobContext)ctx).getScheduledAgendaItem();
+
+ agenda.fireActivation( item );
+ agenda.getScheduledActivationsLinkedList().remove( item );
+ agenda.getWorkingMemory().fireAllRules();
+ }
+ }
+
+ public static class DuractionJobContext implements JobContext {
+ private JobHandle jobHandle;
+ private ScheduledAgendaItem scheduledAgendaItem;
+ private Agenda agenda;
+
+ public DuractionJobContext(ScheduledAgendaItem scheduledAgendaItem,
+ Agenda agenda) {
+ this.scheduledAgendaItem = scheduledAgendaItem;
+ this.agenda = agenda;
+ }
- final Date then = new Date( now.getTime() + item.getRule().getDuration().getDuration( item.getTuple() ) );
+ public DuractionJobContext(ScheduledAgendaItem agendaItem) {
+ this.scheduledAgendaItem = scheduledAgendaItem;
+ }
+
+ public Agenda getAgenda() {
+ return this.agenda;
+ }
+
+ public ScheduledAgendaItem getScheduledAgendaItem() {
+ return this.scheduledAgendaItem;
+ }
- this.scheduler.schedule( item,
- then );
- }
+ public JobHandle getJobHandle() {
+ return this.jobHandle;
+ }
+
+ public void setJobHandle(JobHandle jobHandle) {
+ this.jobHandle = jobHandle;
+ }
+ }
}
More information about the jboss-svn-commits
mailing list