[jboss-svn-commits] JBL Code SVN: r5991 - 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
Mon Aug 28 11:42:43 EDT 2006
Author: mark.proctor at jboss.com
Date: 2006-08-28 11:42:41 -0400 (Mon, 28 Aug 2006)
New Revision: 5991
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowNode.java
Log:
JBRULES-453 RuleFlow
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowNode.java 2006-08-28 15:39:54 UTC (rev 5990)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowNode.java 2006-08-28 15:42:41 UTC (rev 5991)
@@ -0,0 +1,175 @@
+package org.drools.common;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.drools.conflict.DepthConflictResolver;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.util.BinaryHeapFifoQueue;
+import org.drools.util.Queueable;
+
+public class RuleFlowNode
+ implements
+ AgendaGroup {
+
+ private static final long serialVersionUID = 320L;
+
+ private final String name;
+
+ private final InternalAgenda agenda;
+
+ /** @todo Maybe this should just be a LinkedList and we sort on use? */
+ private final BinaryHeapFifoQueue queue;
+
+ private List childNodes = Collections.EMPTY_LIST;
+
+ private Object lock;
+
+ /**
+ * Construct an <code>RuleFlowNode</code> with the given name.
+ *
+ * @param name
+ * The <RuleFlowNode> name.
+ */
+ public RuleFlowNode(final String name,
+ final InternalAgenda agenda) {
+ this.name = name;
+ this.agenda = agenda;
+ this.queue = new BinaryHeapFifoQueue( DepthConflictResolver.getInstance() );
+ }
+
+ /* (non-Javadoc)
+ * @see org.drools.spi.RuleFlowNode#getName()
+ */
+ public String getName() {
+ return this.name;
+ }
+
+ public void addChildNode(RuleFlowNode child) {
+ if ( this.childNodes == Collections.EMPTY_LIST ) {
+ this.childNodes = new ArrayList( 1 );
+ }
+ this.childNodes.add( child );
+ }
+
+ public boolean removeChildNode(RuleFlowNode child) {
+ return this.childNodes.remove( child );
+ }
+
+ public void clear() {
+ synchronized ( this.lock ) {
+ this.queue.clear();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.drools.spi.RuleFlowNode#size()
+ */
+ public int size() {
+ synchronized ( this.lock ) {
+ return this.queue.size();
+ }
+ }
+
+ public void add(final Activation activation) {
+ synchronized ( this.lock ) {
+ this.queue.enqueue( (Queueable) activation );
+ }
+ }
+
+ public Activation getNext() {
+ synchronized ( this.lock ) {
+ return (Activation) this.queue.dequeue();
+ }
+ }
+
+ /**
+ * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
+ * otherwise it returns false;
+ *
+ * @param priorityQueue
+ * @return
+ */
+ public boolean isEmpty() {
+ synchronized ( this.lock ) {
+ return this.queue.isEmpty();
+ }
+ }
+
+ public Activation[] getActivations() {
+ synchronized ( this.lock ) {
+ return (Activation[]) this.queue.toArray( new AgendaItem[this.queue.size()] );
+ }
+ }
+
+ public Queueable[] getQueueable() {
+ return this.queue.getQueueable();
+ }
+
+ public void activate() {
+ Activation[] activations = null;
+ int i = 0;
+ // We need to make a sorted copy of the queue as an array
+ synchronized ( this.lock ) {
+ activations = new Activation[this.queue.size()];
+ while ( !this.queue.isEmpty() ) {
+ activations[i++] = (Activation) this.queue.dequeue();
+ }
+ }
+
+ // Make a runnable execution so we can fire all the activations
+ ExecuteRuleFlowNode execute = new ExecuteRuleFlowNode( this.agenda,
+ (RuleFlowNode[]) this.childNodes.toArray( new RuleFlowNode[this.childNodes.size()] ),
+ activations );
+ Thread thread = new Thread( execute );
+ thread.start();
+ }
+
+ public String toString() {
+ return "RuleFlowNode '" + this.name + "'";
+ }
+
+ public boolean equal(final Object object) {
+ if ( (object == null) || !(object instanceof RuleFlowNode) ) {
+ return false;
+ }
+
+ if ( ((RuleFlowNode) object).name.equals( this.name ) ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public int hashCode() {
+ return this.name.hashCode();
+ }
+
+ public static class ExecuteRuleFlowNode
+ implements
+ Runnable {
+ private final InternalAgenda agenda;
+ private final RuleFlowNode[] nodes;
+ private final Activation[] activations;
+
+ public ExecuteRuleFlowNode(final InternalAgenda agenda,
+ RuleFlowNode[] nodes,
+ final Activation[] activations) {
+ this.agenda = agenda;
+ this.nodes = nodes;
+ this.activations = activations;
+ }
+
+ public void run() {
+ for ( int i = 0, length = activations.length; i < length; i++ ) {
+ this.agenda.fireActivation( this.activations[i] );
+ }
+
+ for ( int i = 0, length = nodes.length; i < length; i++ ) {
+ this.nodes[i].activate();
+ }
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list