[jboss-svn-commits] JBL Code SVN: r20216 - in labs/jbossrules/trunk/drools-core/src/main/java/org/drools: temporal and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu May 29 11:24:58 EDT 2008
Author: tirelli
Date: 2008-05-29 11:24:58 -0400 (Thu, 29 May 2008)
New Revision: 20216
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/SessionPseudoClock.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/jdk/
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ClockType.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java
Log:
JBRULES-1625: refactoring scheduler API to unify it with CEP clock API
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ClockType.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ClockType.java 2008-05-29 15:07:38 UTC (rev 20215)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/ClockType.java 2008-05-29 15:24:58 UTC (rev 20216)
@@ -18,7 +18,7 @@
package org.drools;
import org.drools.temporal.SessionClock;
-import org.drools.temporal.SessionPseudoClock;
+import org.drools.time.impl.SessionPseudoClock;
/**
* This enum represents all engine supported clocks
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java 2008-05-29 15:07:38 UTC (rev 20215)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java 2008-05-29 15:24:58 UTC (rev 20216)
@@ -45,7 +45,7 @@
import org.drools.spi.ConflictResolver;
import org.drools.spi.ConsequenceExceptionHandler;
import org.drools.temporal.SessionClock;
-import org.drools.temporal.SessionPseudoClock;
+import org.drools.time.impl.SessionPseudoClock;
import org.drools.util.ChainedProperties;
import org.drools.util.ClassUtils;
import org.drools.util.ConfFileUtils;
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java 2008-05-29 15:07:38 UTC (rev 20215)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java 2008-05-29 15:24:58 UTC (rev 20216)
@@ -1,213 +0,0 @@
-/*
- * Copyright 2007 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.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * Created on Oct 17, 2007
- */
-package org.drools.temporal;
-
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.PriorityQueue;
-
-import org.drools.TemporalSession;
-import org.drools.common.DroolsObjectInputStream;
-import org.drools.common.InternalWorkingMemory;
-import org.drools.common.WorkingMemoryAction;
-import org.drools.marshalling.MarshallerWriteContext;
-import org.drools.reteoo.ReteooTemporalSession;
-import org.drools.rule.Behavior;
-
-/**
- * A SessionPseudoClock is a clock that allows the user to explicitly
- * control current time.
- *
- * @author etirelli
- *
- */
-public class SessionPseudoClock
- implements
- SessionClock {
-
- private long timer;
- private PriorityQueue<ScheduledItem> queue;
- private transient Map<Behavior, ScheduledItem> schedules;
- private transient ReteooTemporalSession<SessionClock> session;
-
- public SessionPseudoClock() {
- this( null );
- }
-
- public SessionPseudoClock(TemporalSession<SessionClock> session) {
- this.timer = 0;
- this.queue = new PriorityQueue<ScheduledItem>();
- this.schedules = new HashMap<Behavior, ScheduledItem>();
- this.session = (ReteooTemporalSession<SessionClock>) session;
- }
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- timer = in.readLong();
- PriorityQueue<ScheduledItem> tmp = (PriorityQueue<ScheduledItem>) in.readObject();
- if( tmp != null ) {
- queue = tmp;
- for ( ScheduledItem item : queue ) {
- this.schedules.put( item.getBehavior(),
- item );
- }
- }
- session = (ReteooTemporalSession<SessionClock>) ((DroolsObjectInputStream) in).getWorkingMemory();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeLong( timer );
- // this is a work around to a bug in the object stream code, where it raises exceptions
- // when trying to deserialize an empty priority queue.
- out.writeObject( queue.isEmpty() ? null : queue );
- }
-
- /* (non-Javadoc)
- * @see org.drools.temporal.SessionClock#getCurrentTime()
- */
- public synchronized long getCurrentTime() {
- return this.timer;
- }
-
- public synchronized long advanceTime(long millisecs) {
- this.timer += millisecs;
- this.runCallBacks();
- return this.timer;
- }
-
- public synchronized void setStartupTime(int i) {
- this.timer = i;
- }
-
- public synchronized void schedule(final Behavior behavior,
- final Object behaviorContext,
- final long timestamp) {
- ScheduledItem item = schedules.remove( behavior );
- if ( item != null ) {
- queue.remove( item );
- }
- item = new ScheduledItem( timestamp,
- behavior,
- behaviorContext );
- schedules.put( behavior,
- item );
- queue.add( item );
- }
-
- public synchronized void unschedule(final Behavior behavior) {
- ScheduledItem item = schedules.remove( behavior );
- if ( item != null ) {
- queue.remove( item );
- }
- }
-
- /**
- * @return the session
- */
- public synchronized TemporalSession<SessionClock> getSession() {
- return session;
- }
-
- /**
- * @param session the session to set
- */
- public synchronized void setSession(TemporalSession<? extends SessionClock> session) {
- this.session = (ReteooTemporalSession<SessionClock>) session;
- }
-
- private void runCallBacks() {
- ScheduledItem item = queue.peek();
- while ( item != null && item.getTimestamp() <= this.timer ) {
- // remove the head
- queue.remove();
- // enqueue the callback
- session.queueWorkingMemoryAction( item );
- // get next head
- item = queue.peek();
- }
- }
-
- private static final class ScheduledItem
- implements
- Comparable<ScheduledItem>,
- WorkingMemoryAction {
- private long timestamp;
- private Behavior behavior;
- private Object behaviorContext;
-
- /**
- * @param timestamp
- * @param behavior
- * @param behaviorContext
- */
- public ScheduledItem(final long timestamp,
- final Behavior behavior,
- final Object behaviorContext) {
- super();
- this.timestamp = timestamp;
- this.behavior = behavior;
- this.behaviorContext = behaviorContext;
- }
-
- /**
- * @return the timestamp
- */
- public final long getTimestamp() {
- return timestamp;
- }
-
- /**
- * @return the behavior
- */
- public final Behavior getBehavior() {
- return behavior;
- }
-
- public int compareTo(ScheduledItem o) {
- return this.timestamp < o.getTimestamp() ? -1 : this.timestamp == o.getTimestamp() ? 0 : 1;
- }
-
- public void execute(final InternalWorkingMemory workingMemory) {
- behavior.expireTuples( behaviorContext, workingMemory );
- }
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- timestamp = in.readLong();
- behavior = (Behavior) in.readObject();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeLong( timestamp );
- out.writeObject( behavior );
- }
-
- public String toString() {
- return "ScheduledItem( timestamp="+timestamp+", behavior="+behavior+" )";
- }
-
- public void write(MarshallerWriteContext context) throws IOException {
- // TODO Auto-generated method stub
-
- }
- }
-
-}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java 2008-05-29 15:07:38 UTC (rev 20215)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java 2008-05-29 15:24:58 UTC (rev 20216)
@@ -1,6 +1,6 @@
package org.drools.time;
-import org.drools.time.impl.jdk.JDKScheduler;
+import org.drools.time.impl.JDKScheduler;
public class SchedulerFactory {
private static TimeServices scheduler = new JDKScheduler();
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java (from rev 20215, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/jdk/JDKScheduler.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java 2008-05-29 15:24:58 UTC (rev 20216)
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2008 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.drools.time.impl;
+
+import java.util.Date;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.drools.time.Job;
+import org.drools.time.JobContext;
+import org.drools.time.JobHandle;
+import org.drools.time.TimeServices;
+import org.drools.time.Trigger;
+
+/**
+ * A default Scheduler implementation that uses the
+ * JDK built-in ScheduledThreadPoolExecutor as the
+ * scheduler and the system clock as the clock.
+ *
+ */
+public class JDKScheduler
+ implements
+ TimeServices {
+ private ScheduledThreadPoolExecutor scheduler;
+
+ public JDKScheduler() {
+ this( 3 );
+ }
+
+ public JDKScheduler(int size) {
+ this.scheduler = new ScheduledThreadPoolExecutor( size );
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public long getCurrentTime() {
+ return System.currentTimeMillis();
+ }
+
+ public JobHandle scheduleJob(Job job,
+ JobContext ctx,
+ Trigger trigger) {
+ JDKJobHandle jobHandle = new JDKJobHandle();
+
+ Date date = trigger.getNextFireTime();
+
+ if ( date != null ) {
+ JDKCallableJob callableJob = new JDKCallableJob( job,
+ ctx,
+ trigger,
+ jobHandle,
+ this.scheduler );
+ ScheduledFuture future = schedule( date,
+ callableJob,
+ this.scheduler );
+ jobHandle.setFuture( future );
+
+ return jobHandle;
+ } else {
+ return null;
+ }
+ }
+
+ public boolean removeJob(JobHandle jobHandle) {
+ return this.scheduler.remove( (Runnable) ((JDKJobHandle) jobHandle).getFuture() );
+ }
+
+ private static ScheduledFuture schedule(Date date,
+ JDKCallableJob callableJob,
+ ScheduledThreadPoolExecutor scheduler) {
+ long then = date.getTime();
+ long now = System.currentTimeMillis();
+ ScheduledFuture future = null;
+ if ( then >= now ) {
+ future = scheduler.schedule( callableJob,
+ then - now,
+ TimeUnit.MILLISECONDS );
+ } else {
+ future = scheduler.schedule( callableJob,
+ 0,
+ TimeUnit.MILLISECONDS );
+ }
+ return future;
+ }
+
+ public static class JDKCallableJob
+ implements
+ Callable {
+ private Job job;
+ private Trigger trigger;
+ private JobContext ctx;
+ private ScheduledThreadPoolExecutor scheduler;
+ private JDKJobHandle handle;
+
+ public JDKCallableJob(Job job,
+ JobContext ctx,
+ Trigger trigger,
+ JDKJobHandle handle,
+ ScheduledThreadPoolExecutor scheduler) {
+ this.job = job;
+ this.ctx = ctx;
+ this.trigger = trigger;
+ this.handle = handle;
+ this.scheduler = scheduler;
+ }
+
+ public Object call() throws Exception {
+ this.job.execute( this.ctx );
+
+ // our triggers allow for flexible rescheduling
+ Date date = this.trigger.getNextFireTime();
+ if ( date != null ) {
+ ScheduledFuture future = schedule( date,
+ this,
+ this.scheduler );
+ this.handle.setFuture( future );
+ }
+
+ return null;
+ }
+ }
+
+ public static class JDKJobHandle
+ implements
+ JobHandle {
+ private ScheduledFuture future;
+
+ public JDKJobHandle() {
+
+ }
+
+ public ScheduledFuture getFuture() {
+ return future;
+ }
+
+ public void setFuture(ScheduledFuture future) {
+ this.future = future;
+ }
+
+ }
+
+}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/SessionPseudoClock.java (from rev 20208, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/SessionPseudoClock.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/SessionPseudoClock.java 2008-05-29 15:24:58 UTC (rev 20216)
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2007 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Oct 17, 2007
+ */
+package org.drools.time.impl;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.PriorityQueue;
+
+import org.drools.TemporalSession;
+import org.drools.common.DroolsObjectInputStream;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.WorkingMemoryAction;
+import org.drools.marshalling.MarshallerWriteContext;
+import org.drools.reteoo.ReteooTemporalSession;
+import org.drools.rule.Behavior;
+import org.drools.temporal.SessionClock;
+
+/**
+ * A SessionPseudoClock is a clock that allows the user to explicitly
+ * control current time.
+ *
+ * @author etirelli
+ *
+ */
+public class SessionPseudoClock
+ implements
+ SessionClock {
+
+ private long timer;
+ private PriorityQueue<ScheduledItem> queue;
+ private transient Map<Behavior, ScheduledItem> schedules;
+ private transient ReteooTemporalSession<SessionClock> session;
+
+ public SessionPseudoClock() {
+ this( null );
+ }
+
+ public SessionPseudoClock(TemporalSession<SessionClock> session) {
+ this.timer = 0;
+ this.queue = new PriorityQueue<ScheduledItem>();
+ this.schedules = new HashMap<Behavior, ScheduledItem>();
+ this.session = (ReteooTemporalSession<SessionClock>) session;
+ }
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ timer = in.readLong();
+ PriorityQueue<ScheduledItem> tmp = (PriorityQueue<ScheduledItem>) in.readObject();
+ if( tmp != null ) {
+ queue = tmp;
+ for ( ScheduledItem item : queue ) {
+ this.schedules.put( item.getBehavior(),
+ item );
+ }
+ }
+ session = (ReteooTemporalSession<SessionClock>) ((DroolsObjectInputStream) in).getWorkingMemory();
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeLong( timer );
+ // this is a work around to a bug in the object stream code, where it raises exceptions
+ // when trying to deserialize an empty priority queue.
+ out.writeObject( queue.isEmpty() ? null : queue );
+ }
+
+ /* (non-Javadoc)
+ * @see org.drools.temporal.SessionClock#getCurrentTime()
+ */
+ public synchronized long getCurrentTime() {
+ return this.timer;
+ }
+
+ public synchronized long advanceTime(long millisecs) {
+ this.timer += millisecs;
+ this.runCallBacks();
+ return this.timer;
+ }
+
+ public synchronized void setStartupTime(int i) {
+ this.timer = i;
+ }
+
+ public synchronized void schedule(final Behavior behavior,
+ final Object behaviorContext,
+ final long timestamp) {
+ ScheduledItem item = schedules.remove( behavior );
+ if ( item != null ) {
+ queue.remove( item );
+ }
+ item = new ScheduledItem( timestamp,
+ behavior,
+ behaviorContext );
+ schedules.put( behavior,
+ item );
+ queue.add( item );
+ }
+
+ public synchronized void unschedule(final Behavior behavior) {
+ ScheduledItem item = schedules.remove( behavior );
+ if ( item != null ) {
+ queue.remove( item );
+ }
+ }
+
+ /**
+ * @return the session
+ */
+ public synchronized TemporalSession<SessionClock> getSession() {
+ return session;
+ }
+
+ /**
+ * @param session the session to set
+ */
+ public synchronized void setSession(TemporalSession<? extends SessionClock> session) {
+ this.session = (ReteooTemporalSession<SessionClock>) session;
+ }
+
+ private void runCallBacks() {
+ ScheduledItem item = queue.peek();
+ while ( item != null && item.getTimestamp() <= this.timer ) {
+ // remove the head
+ queue.remove();
+ // enqueue the callback
+ session.queueWorkingMemoryAction( item );
+ // get next head
+ item = queue.peek();
+ }
+ }
+
+ private static final class ScheduledItem
+ implements
+ Comparable<ScheduledItem>,
+ WorkingMemoryAction {
+ private long timestamp;
+ private Behavior behavior;
+ private Object behaviorContext;
+
+ /**
+ * @param timestamp
+ * @param behavior
+ * @param behaviorContext
+ */
+ public ScheduledItem(final long timestamp,
+ final Behavior behavior,
+ final Object behaviorContext) {
+ super();
+ this.timestamp = timestamp;
+ this.behavior = behavior;
+ this.behaviorContext = behaviorContext;
+ }
+
+ /**
+ * @return the timestamp
+ */
+ public final long getTimestamp() {
+ return timestamp;
+ }
+
+ /**
+ * @return the behavior
+ */
+ public final Behavior getBehavior() {
+ return behavior;
+ }
+
+ public int compareTo(ScheduledItem o) {
+ return this.timestamp < o.getTimestamp() ? -1 : this.timestamp == o.getTimestamp() ? 0 : 1;
+ }
+
+ public void execute(final InternalWorkingMemory workingMemory) {
+ behavior.expireTuples( behaviorContext, workingMemory );
+ }
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ timestamp = in.readLong();
+ behavior = (Behavior) in.readObject();
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeLong( timestamp );
+ out.writeObject( behavior );
+ }
+
+ public String toString() {
+ return "ScheduledItem( timestamp="+timestamp+", behavior="+behavior+" )";
+ }
+
+ public void write(MarshallerWriteContext context) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+ }
+
+}
More information about the jboss-svn-commits
mailing list