Author: mar...@jb... Date: 2006-05-31 13:07:01 -0400 (Wed, 31 May 2006) New Revision: 4527 Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java Removed: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Agenda.java Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/SynchronizedWorkingMemory.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsAgenda.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TerminalNode.java labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/SchedulerTest.java Log: -Moved Agenda impl access to InternalAgenda interface Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -0,0 +1,83 @@ +package org.drools; + +import org.drools.spi.Activation; +import org.drools.spi.ActivationGroup; +import org.drools.spi.AgendaGroup; + +public interface Agenda { + + public WorkingMemory getWorkingMemory(); + + public org.drools.util.LinkedList getScheduledItems(); + + public boolean setFocus(AgendaGroup agendaGroup); + + public void setFocus(String name); + + public AgendaGroup getFocus(); + + public AgendaGroup getAgendaGroup(String name); + + public AgendaGroup[] getAgendaGroups(); + + public AgendaGroup[] getStack(); + + public ActivationGroup getActivationGroup(String name); + + /** + * Iterates all the <code>AgendGroup<code>s in the focus stack returning the total number of <code>Activation</code>s + * @return + * total number of <code>Activation</code>s on the focus stack + */ + public int focusStackSize(); + + /** + * Iterates all the modules in the focus stack returning the total number of <code>Activation</code>s + * @return + * total number of activations on the focus stack + */ + public int agendaSize(); + + public Activation[] getActivations(); + + public Activation[] getScheduledActivations(); + + /** + * Clears all Activations from the Agenda + * + */ + public void clearAgenda(); + + /** + * Clears all Activations from an Agenda Group. Any Activations that are also in an Xor Group are removed the + * the Xor Group. + * + * @param agendaGroup + */ + public void clearAgendaGroup(String name); + + /** + * Clears all Activations from an Agenda Group. Any Activations that are also in an Xor Group are removed the + * the Xor Group. + * + * @param agendaGroup + */ + public void clearAgendaGroup(AgendaGroup agendaGroup); + + /** + * Clears all Activations from an Activation-Group. Any Activations that are also in an Agenda Group are removed + * from the Agenda Group. + * + * @param activationGroup + */ + public void clearActivationGroup(String name); + + /** + * Clears all Activations from an Xor Group. Any Activations that are also in an Agenda Group are removed + * from the Agenda Group. + * + * @param activationGroup + */ + public void clearActivationGroup(ActivationGroup activationGroup); + +} \ No newline at end of file Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/SynchronizedWorkingMemory.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/SynchronizedWorkingMemory.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/SynchronizedWorkingMemory.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -19,7 +19,6 @@ import java.util.List; import java.util.Map; -import org.drools.common.Agenda; import org.drools.event.AgendaEventListener; import org.drools.event.WorkingMemoryEventListener; import org.drools.spi.AgendaFilter; Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -20,7 +20,6 @@ import java.util.List; import java.util.Map; -import org.drools.common.Agenda; import org.drools.event.AgendaEventListener; import org.drools.event.WorkingMemoryEventListener; import org.drools.spi.AgendaFilter; Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; +import org.drools.Agenda; import org.drools.FactException; import org.drools.FactHandle; import org.drools.NoSuchFactHandleException; @@ -100,7 +101,7 @@ protected final TruthMaintenanceSystem tms; /** Rule-firing agenda. */ - protected Agenda agenda; + protected DefaultAgenda agenda; protected final List factQueue = new ArrayList(); Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Agenda.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Agenda.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Agenda.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -1,426 +0,0 @@ -package org.drools.common; - -/* - * Copyright 2005 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. - */ - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import org.drools.WorkingMemory; -import org.drools.spi.Activation; -import org.drools.spi.ActivationGroup; -import org.drools.spi.AgendaFilter; -import org.drools.spi.AgendaGroup; -import org.drools.spi.ConsequenceException; -import org.drools.spi.KnowledgeHelper; -import org.drools.util.LinkedListNode; -import org.drools.util.Queueable; - -/** - * Rule-firing Agenda. - * - * <p> - * Since many rules may be matched by a single assertObject(...) all scheduled - * actions are placed into the <code>Agenda</code>. - * </p> - * - * <p> - * While processing a scheduled action, it may modify or retract objects in - * other scheduled actions, which must then be removed from the agenda. - * Non-invalidated actions are left on the agenda, and are executed in turn. - * </p> - * - * @author <a href="mailto:bo...@en...">bob mcwhirter </a> - * @author <a href="mailto:si...@re...">Simon Harris </a> - */ -public class Agenda - implements - Serializable { - // ------------------------------------------------------------ - // Instance members - // ------------------------------------------------------------ - - /** - * - */ - private static final long serialVersionUID = -9112292414291983688L; - - /** Working memory of this Agenda. */ - private final WorkingMemory workingMemory; - - private org.drools.util.LinkedList scheduledActivations; - - /** Items time-delayed. */ - - private final Map agendaGroups; - - private final Map activationGroups; - - private final LinkedList focusStack; - - private AgendaGroupImpl currentModule; - - private AgendaGroup main; - - // ------------------------------------------------------------ - // Constructors - // ------------------------------------------------------------ - - /** - * Construct. - * - * @param workingMemory - * The <code>WorkingMemory</code> of this agenda. - * @param conflictResolver - * The conflict resolver. - */ - public Agenda(final WorkingMemory workingMemory) { - this.workingMemory = workingMemory; - this.agendaGroups = new HashMap(); - this.activationGroups = new HashMap(); - this.focusStack = new LinkedList(); - - // MAIN should always be the first AgendaGroup and can never be removed - this.main = new AgendaGroupImpl( AgendaGroup.MAIN ); - - this.agendaGroups.put( AgendaGroup.MAIN, - this.main ); - - this.focusStack.add( this.main ); - - } - - public WorkingMemory getWorkingMemory() { - return this.workingMemory; - } - - /** - * Schedule an agenda item for delayed firing. - * - * @param item - * The item to schedule. - */ - public void scheduleItem(final ScheduledAgendaItem item) { - Scheduler.getInstance().scheduleAgendaItem( item ); - - if ( this.scheduledActivations == null ) { - this.scheduledActivations = new org.drools.util.LinkedList(); - } - - this.scheduledActivations.add( item ); - } - - public void removeScheduleItem(final ScheduledAgendaItem item) { - this.scheduledActivations.remove( item ); - item.cancel(); - } - - public org.drools.util.LinkedList getScheduledItems() { - return this.scheduledActivations; - } - - public void addAgendaGroup(final AgendaGroup agendaGroup) { - this.agendaGroups.put( agendaGroup.getName(), - agendaGroup ); - } - - public boolean setFocus(final AgendaGroup agendaGroup) { - // Set the focus to the agendaGroup if it doesn't already have the focus - if ( this.focusStack.getLast() != agendaGroup ) { - this.focusStack.add( agendaGroup ); - return true; - } - return false; - } - - public void setFocus(final String name) { - AgendaGroup agendaGroup = (AgendaGroup) this.agendaGroups.get( name ); - - // Agenda may not have been created yet, if not create it. - if ( agendaGroup == null ) { - agendaGroup = new AgendaGroupImpl( name ); - this.workingMemory.getAgenda().addAgendaGroup( agendaGroup ); - } - setFocus( agendaGroup ); - } - - public AgendaGroup getFocus() { - return (AgendaGroup) this.focusStack.getLast(); - } - - /** - * Returns the next populated Agenda Group. If all groups are empty then return the MAIN, dfault, Agenda Group - * - * @return - */ - public AgendaGroup getNextFocus() { - AgendaGroupImpl agendaGroup = null; - // Iterate untill we find a populate AgendaModule or we reach the MAIN, default, AgendaGroup - while ( true ) { - agendaGroup = (AgendaGroupImpl) this.focusStack.getLast(); - - final boolean empty = agendaGroup.isEmpty(); - - // No populated queus found so pop the focusStack and repeat - if ( empty && (this.focusStack.size() > 1) ) { - this.focusStack.removeLast(); - } else { - agendaGroup = (empty) ? null : agendaGroup; - break; - } - } - - return agendaGroup; - } - - public void setCurrentAgendaGroup(final AgendaGroup agendaGroup) { - this.currentModule = (AgendaGroupImpl) agendaGroup; - } - - public AgendaGroup getCurrentAgendaGroup() { - return this.currentModule; - } - - public AgendaGroup getAgendaGroup(final String name) { - return (AgendaGroup) this.agendaGroups.get( name ); - } - - public AgendaGroup[] getAgendaGroups() { - return (AgendaGroup[]) this.agendaGroups.values().toArray( new AgendaGroup[this.agendaGroups.size()] ); - } - - public AgendaGroup[] getStack() { - return (AgendaGroup[]) this.focusStack.toArray( new AgendaGroup[this.focusStack.size()] ); - } - - public ActivationGroup getActivationGroup(final String name) { - ActivationGroupImpl activationGroup = (ActivationGroupImpl) this.activationGroups.get( name ); - if ( activationGroup == null ) { - activationGroup = new ActivationGroupImpl( name ); - this.activationGroups.put( name, - activationGroup ); - } - return activationGroup; - } - - /** - * Iterates all the <code>AgendGroup<code>s in the focus stack returning the total number of <code>Activation</code>s - * @return - * total number of <code>Activation</code>s on the focus stack - */ - public int focusStackSize() { - int size = 0; - for ( final Iterator iterator = this.focusStack.iterator(); iterator.hasNext(); ) { - final AgendaGroup group = (AgendaGroupImpl) iterator.next(); - size += group.size(); - } - return size; - } - - /** - * Iterates all the modules in the focus stack returning the total number of <code>Activation</code>s - * @return - * total number of activations on the focus stack - */ - public int agendaSize() { - int size = 0; - for ( final Iterator iterator = this.agendaGroups.values().iterator(); iterator.hasNext(); ) { - final AgendaGroup group = (AgendaGroupImpl) iterator.next(); - size += group.size(); - } - return size; - } - - public Activation[] getActivations() { - final List list = new ArrayList(); - for ( final Iterator it = this.agendaGroups.values().iterator(); it.hasNext(); ) { - final AgendaGroup group = (AgendaGroup) it.next(); - list.addAll( Arrays.asList( group.getActivations() ) ); - } - return (Activation[]) list.toArray( new Activation[list.size()] ); - } - - public Activation[] getScheduledActivations() { - final List list = new ArrayList( this.scheduledActivations.size() ); - for ( LinkedListNode node = this.scheduledActivations.getFirst(); node != null; node = node.getNext() ) { - list.add( node ); - } - return (Activation[]) list.toArray( new Activation[list.size()] ); - } - - /** - * Clears all Activations from the Agenda - * - */ - public void clearAgenda() { - // Cancel all items and fire a Cancelled event for each Activation - for ( final Iterator agendaGroupIterator = this.agendaGroups.values().iterator(); agendaGroupIterator.hasNext(); ) { - final AgendaGroupImpl group = (AgendaGroupImpl) agendaGroupIterator.next(); - clearAgendaGroup( group ); - } - - 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.remove(); - eventsupport.getAgendaEventSupport().fireActivationCancelled( item ); - } - } - } - - /** - * Clears all Activations from an Agenda Group. Any Activations that are also in an Xor Group are removed the - * the Xor Group. - * - * @param agendaGroup - */ - public void clearAgendaGroup(final String name) { - final AgendaGroupImpl agendaGroup = (AgendaGroupImpl) this.agendaGroups.get( name ); - if ( agendaGroup != null ) { - clearAgendaGroup( agendaGroup ); - } - } - - /** - * Clears all Activations from an Agenda Group. Any Activations that are also in an Xor Group are removed the - * the Xor Group. - * - * @param agendaGroup - */ - public void clearAgendaGroup(final AgendaGroupImpl agendaGroup) { - final EventSupport eventsupport = (EventSupport) this.workingMemory; - - final Queueable[] queueable = agendaGroup.getQueueable(); - for ( int i = 0, length = queueable.length; i < length; i++ ) { - final AgendaItem item = (AgendaItem) queueable[i]; - if ( item == null ) { - continue; - } - - // this must be set false before removal from the XorGroup. Otherwise the XorGroup will also try to cancel the Actvation - item.setActivated( false ); - - if ( item.getActivationGroupNode() != null ) { - item.getActivationGroupNode().getActivationGroup().removeActivation( item ); - } - - eventsupport.getAgendaEventSupport().fireActivationCancelled( item ); - } - agendaGroup.clear(); - } - - /** - * Clears all Activations from an Activation-Group. Any Activations that are also in an Agenda Group are removed - * from the Agenda Group. - * - * @param activationGroup - */ - public void clearActivationGroup(final String name) { - final ActivationGroup activationGroup = (ActivationGroup) this.activationGroups.get( name ); - if ( activationGroup != null ) { - clearActivationGroup( activationGroup ); - } - } - - /** - * Clears all Activations from an Xor Group. Any Activations that are also in an Agenda Group are removed - * from the Agenda Group. - * - * @param activationGroup - */ - public void clearActivationGroup(final ActivationGroup activationGroup) { - final EventSupport eventsupport = (EventSupport) this.workingMemory; - for ( final Iterator it = activationGroup.iterator(); it.hasNext(); ) { - final Activation activation = ((ActivationGroupNode) it.next()).getActivation(); - activation.setActivationGroupNode( null ); - - if ( activation.isActivated() ) { - activation.setActivated( false ); - activation.remove(); - eventsupport.getAgendaEventSupport().fireActivationCancelled( activation ); - } - } - activationGroup.clear(); - } - - /** - * Fire the next scheduled <code>Agenda</code> item. - * - * @throws ConsequenceException - * If an error occurs while firing an agenda item. - */ - public boolean fireNextItem(final AgendaFilter filter) throws ConsequenceException { - final AgendaGroupImpl group = (AgendaGroupImpl) getNextFocus(); - - // return if there are no Activations to fire - if ( group == null ) { - return false; - } - - final AgendaItem item = (AgendaItem) group.getNext(); - if ( item == null ) { - return false; - } - - if ( filter == null || filter.accept( item ) ) { - fireActivation( item ); - } - - return true; - } - - /** - * Fire this item. - * - * @param workingMemory - * The working memory context. - * - * @throws ConsequenceException - * If an error occurs while attempting to fire the consequence. - */ - public synchronized void fireActivation(final Activation activation) throws ConsequenceException { - final EventSupport eventsupport = (EventSupport) this.workingMemory; - - eventsupport.getAgendaEventSupport().fireBeforeActivationFired( activation ); - - if ( activation.getActivationGroupNode() != null ) { - final ActivationGroup activationGroup = activation.getActivationGroupNode().getActivationGroup(); - activationGroup.removeActivation( activation ); - clearActivationGroup( activationGroup ); - } - activation.setActivated( false ); - - try { - final KnowledgeHelper knowledgeHelper = new org.drools.base.DefaultKnowledgeHelper( activation, - this.workingMemory ); - activation.getRule().getConsequence().evaluate( knowledgeHelper, - this.workingMemory ); - } catch ( final Exception e ) { - e.printStackTrace(); - throw new ConsequenceException( e, - activation.getRule() ); - } - - eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation ); - } - -} Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java (from rev 4507, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Agenda.java) =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/Agenda.java 2006-05-31 05:09:44 UTC (rev 4507) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -0,0 +1,448 @@ +package org.drools.common; + +/* + * Copyright 2005 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. + */ + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.drools.Agenda; +import org.drools.WorkingMemory; +import org.drools.spi.Activation; +import org.drools.spi.ActivationGroup; +import org.drools.spi.AgendaFilter; +import org.drools.spi.AgendaGroup; +import org.drools.spi.ConsequenceException; +import org.drools.spi.KnowledgeHelper; +import org.drools.util.LinkedListNode; +import org.drools.util.Queueable; + +/** + * Rule-firing Agenda. + * + * <p> + * Since many rules may be matched by a single assertObject(...) all scheduled + * actions are placed into the <code>Agenda</code>. + * </p> + * + * <p> + * While processing a scheduled action, it may modify or retract objects in + * other scheduled actions, which must then be removed from the agenda. + * Non-invalidated actions are left on the agenda, and are executed in turn. + * </p> + * + * @author <a href="mailto:bo...@en...">bob mcwhirter </a> + * @author <a href="mailto:si...@re...">Simon Harris </a> + */ +public class DefaultAgenda + implements + Serializable, + InternalAgenda { + // ------------------------------------------------------------ + // Instance members + // ------------------------------------------------------------ + + /** + * + */ + private static final long serialVersionUID = -9112292414291983688L; + + /** Working memory of this Agenda. */ + private final WorkingMemory workingMemory; + + private org.drools.util.LinkedList scheduledActivations; + + /** Items time-delayed. */ + + private final Map agendaGroups; + + private final Map activationGroups; + + private final LinkedList focusStack; + + private AgendaGroupImpl currentModule; + + private AgendaGroup main; + + // ------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------ + + /** + * Construct. + * + * @param workingMemory + * The <code>WorkingMemory</code> of this agenda. + * @param conflictResolver + * The conflict resolver. + */ + public DefaultAgenda(final WorkingMemory workingMemory) { + this.workingMemory = workingMemory; + this.agendaGroups = new HashMap(); + this.activationGroups = new HashMap(); + this.focusStack = new LinkedList(); + + // MAIN should always be the first AgendaGroup and can never be removed + this.main = new AgendaGroupImpl( AgendaGroup.MAIN ); + + this.agendaGroups.put( AgendaGroup.MAIN, + this.main ); + + this.focusStack.add( this.main ); + + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getWorkingMemory() + */ + public WorkingMemory getWorkingMemory() { + return this.workingMemory; + } + + /** + * Schedule an agenda item for delayed firing. + * + * @param item + * The item to schedule. + */ + public void scheduleItem(final ScheduledAgendaItem item) { + Scheduler.getInstance().scheduleAgendaItem( item ); + + if ( this.scheduledActivations == null ) { + this.scheduledActivations = new org.drools.util.LinkedList(); + } + + this.scheduledActivations.add( item ); + } + + public void removeScheduleItem(final ScheduledAgendaItem item) { + this.scheduledActivations.remove( item ); + item.cancel(); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getScheduledItems() + */ + public org.drools.util.LinkedList getScheduledItems() { + return this.scheduledActivations; + } + + public void addAgendaGroup(final AgendaGroup agendaGroup) { + this.agendaGroups.put( agendaGroup.getName(), + agendaGroup ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#setFocus(org.drools.spi.AgendaGroup) + */ + public boolean setFocus(final AgendaGroup agendaGroup) { + // Set the focus to the agendaGroup if it doesn't already have the focus + if ( this.focusStack.getLast() != agendaGroup ) { + this.focusStack.add( agendaGroup ); + return true; + } + return false; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#setFocus(java.lang.String) + */ + public void setFocus(final String name) { + AgendaGroup agendaGroup = (AgendaGroup) this.agendaGroups.get( name ); + + // Agenda may not have been created yet, if not create it. + if ( agendaGroup == null ) { + agendaGroup = new AgendaGroupImpl( name ); + ( ( DefaultAgenda ) this.workingMemory.getAgenda() ).addAgendaGroup( agendaGroup ); + } + setFocus( agendaGroup ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getFocus() + */ + public AgendaGroup getFocus() { + return (AgendaGroup) this.focusStack.getLast(); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getNextFocus() + */ + public AgendaGroup getNextFocus() { + AgendaGroupImpl agendaGroup = null; + // Iterate untill we find a populate AgendaModule or we reach the MAIN, default, AgendaGroup + while ( true ) { + agendaGroup = (AgendaGroupImpl) this.focusStack.getLast(); + + final boolean empty = agendaGroup.isEmpty(); + + // No populated queus found so pop the focusStack and repeat + if ( empty && (this.focusStack.size() > 1) ) { + this.focusStack.removeLast(); + } else { + agendaGroup = (empty) ? null : agendaGroup; + break; + } + } + + return agendaGroup; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#setCurrentAgendaGroup(org.drools.spi.AgendaGroup) + */ + public void setCurrentAgendaGroup(final AgendaGroup agendaGroup) { + this.currentModule = (AgendaGroupImpl) agendaGroup; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getCurrentAgendaGroup() + */ + public AgendaGroup getCurrentAgendaGroup() { + return this.currentModule; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getAgendaGroup(java.lang.String) + */ + public AgendaGroup getAgendaGroup(final String name) { + return (AgendaGroup) this.agendaGroups.get( name ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getAgendaGroups() + */ + public AgendaGroup[] getAgendaGroups() { + return (AgendaGroup[]) this.agendaGroups.values().toArray( new AgendaGroup[this.agendaGroups.size()] ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getStack() + */ + public AgendaGroup[] getStack() { + return (AgendaGroup[]) this.focusStack.toArray( new AgendaGroup[this.focusStack.size()] ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getActivationGroup(java.lang.String) + */ + public ActivationGroup getActivationGroup(final String name) { + ActivationGroupImpl activationGroup = (ActivationGroupImpl) this.activationGroups.get( name ); + if ( activationGroup == null ) { + activationGroup = new ActivationGroupImpl( name ); + this.activationGroups.put( name, + activationGroup ); + } + return activationGroup; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#focusStackSize() + */ + public int focusStackSize() { + int size = 0; + for ( final Iterator iterator = this.focusStack.iterator(); iterator.hasNext(); ) { + final AgendaGroup group = (AgendaGroupImpl) iterator.next(); + size += group.size(); + } + return size; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#agendaSize() + */ + public int agendaSize() { + int size = 0; + for ( final Iterator iterator = this.agendaGroups.values().iterator(); iterator.hasNext(); ) { + final AgendaGroup group = (AgendaGroupImpl) iterator.next(); + size += group.size(); + } + return size; + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getActivations() + */ + public Activation[] getActivations() { + final List list = new ArrayList(); + for ( final Iterator it = this.agendaGroups.values().iterator(); it.hasNext(); ) { + final AgendaGroup group = (AgendaGroup) it.next(); + list.addAll( Arrays.asList( group.getActivations() ) ); + } + return (Activation[]) list.toArray( new Activation[list.size()] ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#getScheduledActivations() + */ + public Activation[] getScheduledActivations() { + final List list = new ArrayList( this.scheduledActivations.size() ); + for ( LinkedListNode node = this.scheduledActivations.getFirst(); node != null; node = node.getNext() ) { + list.add( node ); + } + return (Activation[]) list.toArray( new Activation[list.size()] ); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#clearAgenda() + */ + public void clearAgenda() { + // Cancel all items and fire a Cancelled event for each Activation + for ( final Iterator agendaGroupIterator = this.agendaGroups.values().iterator(); agendaGroupIterator.hasNext(); ) { + final AgendaGroupImpl group = (AgendaGroupImpl) agendaGroupIterator.next(); + clearAgendaGroup( group ); + } + + 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.remove(); + eventsupport.getAgendaEventSupport().fireActivationCancelled( item ); + } + } + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#clearAgendaGroup(java.lang.String) + */ + public void clearAgendaGroup(final String name) { + final AgendaGroupImpl agendaGroup = (AgendaGroupImpl) this.agendaGroups.get( name ); + if ( agendaGroup != null ) { + clearAgendaGroup( agendaGroup ); + } + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#clearAgendaGroup(org.drools.common.AgendaGroupImpl) + */ + public void clearAgendaGroup(final AgendaGroup agendaGroup) { + final EventSupport eventsupport = (EventSupport) this.workingMemory; + + final Queueable[] queueable = ( ( AgendaGroupImpl) agendaGroup ).getQueueable(); + for ( int i = 0, length = queueable.length; i < length; i++ ) { + final AgendaItem item = (AgendaItem) queueable[i]; + if ( item == null ) { + continue; + } + + // this must be set false before removal from the XorGroup. Otherwise the XorGroup will also try to cancel the Actvation + item.setActivated( false ); + + if ( item.getActivationGroupNode() != null ) { + item.getActivationGroupNode().getActivationGroup().removeActivation( item ); + } + + eventsupport.getAgendaEventSupport().fireActivationCancelled( item ); + } + ( ( AgendaGroupImpl )agendaGroup ).clear(); + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#clearActivationGroup(java.lang.String) + */ + public void clearActivationGroup(final String name) { + final ActivationGroup activationGroup = (ActivationGroup) this.activationGroups.get( name ); + if ( activationGroup != null ) { + clearActivationGroup( activationGroup ); + } + } + + /* (non-Javadoc) + * @see org.drools.common.AgendaI#clearActivationGroup(org.drools.spi.ActivationGroup) + */ + public void clearActivationGroup(final ActivationGroup activationGroup) { + final EventSupport eventsupport = (EventSupport) this.workingMemory; + for ( final Iterator it = activationGroup.iterator(); it.hasNext(); ) { + final Activation activation = ((ActivationGroupNode) it.next()).getActivation(); + activation.setActivationGroupNode( null ); + + if ( activation.isActivated() ) { + activation.setActivated( false ); + activation.remove(); + eventsupport.getAgendaEventSupport().fireActivationCancelled( activation ); + } + } + activationGroup.clear(); + } + + /** + * Fire the next scheduled <code>Agenda</code> item. + * + * @throws ConsequenceException + * If an error occurs while firing an agenda item. + */ + public boolean fireNextItem(final AgendaFilter filter) throws ConsequenceException { + final AgendaGroupImpl group = (AgendaGroupImpl) getNextFocus(); + + // return if there are no Activations to fire + if ( group == null ) { + return false; + } + + final AgendaItem item = (AgendaItem) group.getNext(); + if ( item == null ) { + return false; + } + + if ( filter == null || filter.accept( item ) ) { + fireActivation( item ); + } + + return true; + } + + /** + * Fire this item. + * + * @param workingMemory + * The working memory context. + * + * @throws ConsequenceException + * If an error occurs while attempting to fire the consequence. + */ + public synchronized void fireActivation(final Activation activation) throws ConsequenceException { + final EventSupport eventsupport = (EventSupport) this.workingMemory; + + eventsupport.getAgendaEventSupport().fireBeforeActivationFired( activation ); + + if ( activation.getActivationGroupNode() != null ) { + final ActivationGroup activationGroup = activation.getActivationGroupNode().getActivationGroup(); + activationGroup.removeActivation( activation ); + clearActivationGroup( activationGroup ); + } + activation.setActivated( false ); + + try { + final KnowledgeHelper knowledgeHelper = new org.drools.base.DefaultKnowledgeHelper( activation, + this.workingMemory ); + activation.getRule().getConsequence().evaluate( knowledgeHelper, + this.workingMemory ); + } catch ( final Exception e ) { + e.printStackTrace(); + throw new ConsequenceException( e, + activation.getRule() ); + } + + eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation ); + } + +} Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -0,0 +1,23 @@ +package org.drools.common; + +import org.drools.Agenda; +import org.drools.spi.Activation; +import org.drools.spi.AgendaFilter; +import org.drools.spi.AgendaGroup; +import org.drools.spi.ConsequenceException; + +public interface InternalAgenda + extends + Agenda { + + public void fireActivation(final Activation activation) throws ConsequenceException; + + public void removeScheduleItem(final ScheduledAgendaItem item); + + public boolean fireNextItem(AgendaFilter filter) throws ConsequenceException; + + public void scheduleItem(final ScheduledAgendaItem item); + + public void addAgendaGroup(final AgendaGroup agendaGroup); + +} 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 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -19,6 +19,7 @@ import java.io.Serializable; import java.util.TimerTask; +import org.drools.Agenda; import org.drools.FactHandle; import org.drools.rule.Rule; import org.drools.spi.Activation; @@ -56,7 +57,7 @@ /** The rule. */ private final Rule rule; - private final Agenda agenda; + private final InternalAgenda agenda; private final PropagationContext context; @@ -82,7 +83,7 @@ */ public ScheduledAgendaItem(final long activationNumber, final Tuple tuple, - final Agenda agenda, + final InternalAgenda agenda, final PropagationContext context, final Rule rule) { this.tuple = tuple; Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsAgenda.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsAgenda.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsAgenda.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -16,7 +16,7 @@ * limitations under the License. */ -import org.drools.common.Agenda; +import org.drools.common.DefaultAgenda; import org.drools.rule.Query; import org.drools.spi.Activation; import org.drools.spi.ConsequenceException; @@ -26,7 +26,7 @@ * @author Alexander Bagerman * */ -public class LeapsAgenda extends Agenda { +public class LeapsAgenda extends DefaultAgenda { /** * */ Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/leaps/LeapsWorkingMemory.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -690,7 +690,7 @@ // The AgendaGroup is defined but not yet added to the // Agenda, so create the AgendaGroup and add to the Agenda. agendaGroup = new AgendaGroupImpl( rule.getAgendaGroup() ); - this.getAgenda().addAgendaGroup( agendaGroup ); + this.agenda.addAgendaGroup( agendaGroup ); } leapsRule.setAgendaGroup( agendaGroup ); Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -26,7 +26,7 @@ import org.drools.WorkingMemory; import org.drools.base.DroolsQuery; import org.drools.common.AbstractWorkingMemory; -import org.drools.common.Agenda; +import org.drools.common.DefaultAgenda; import org.drools.common.DefaultFactHandle; import org.drools.common.EqualityKey; import org.drools.common.InternalFactHandle; @@ -60,7 +60,7 @@ public ReteooWorkingMemory(final InternalRuleBase ruleBase) { super( ruleBase, ruleBase.newFactHandleFactory() ); - this.agenda = new Agenda( this ); + this.agenda = new DefaultAgenda( this ); } public void doAssertObject(final InternalFactHandle handle, Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TerminalNode.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TerminalNode.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/TerminalNode.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -18,10 +18,12 @@ import java.util.Iterator; +import org.drools.Agenda; import org.drools.RuleBaseConfiguration; -import org.drools.common.Agenda; +import org.drools.common.DefaultAgenda; import org.drools.common.AgendaGroupImpl; import org.drools.common.AgendaItem; +import org.drools.common.InternalAgenda; import org.drools.common.NodeMemory; import org.drools.common.PropagationContextImpl; import org.drools.common.ScheduledAgendaItem; @@ -122,14 +124,14 @@ if ( this.rule.getNoLoop() && this.rule.equals( context.getRuleOrigin() ) ) { return; } - final Agenda agenda = workingMemory.getAgenda(); + final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda(); final Duration dur = this.rule.getDuration(); if ( dur != null && dur.getDuration( tuple ) > 0 ) { final ScheduledAgendaItem item = new ScheduledAgendaItem( context.getPropagationNumber(), tuple, - workingMemory.getAgenda(), + agenda, context, this.rule ); @@ -168,7 +170,7 @@ // The AgendaGroup is defined but not yet added to the // Agenda, so create the AgendaGroup and add to the Agenda. agendaGroup = new AgendaGroupImpl( this.rule.getAgendaGroup() ); - workingMemory.getAgenda().addAgendaGroup( agendaGroup ); + agenda.addAgendaGroup( agendaGroup ); } memory.setAgendaGroup( agendaGroup ); Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -1,6 +1,6 @@ package org.drools.spi; -import org.drools.common.Agenda; +import org.drools.common.DefaultAgenda; /* * Copyright 2005 JBoss Inc @@ -22,7 +22,7 @@ * The <code>Agenda</code> can be partitioned into groups, called <code>AgendaGroup</code>s. <code>Rule</code>s can be assigned to * these <code>AgendaGroup</code>s. Only rules in the focus group can fire. * - * @see Agenda + * @see DefaultAgenda * * @author <a href="mailto:mar...@jb...">Mark Proctor</a> * @author <a href="mailto:bo...@we...">Bob McWhirter</a> Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -21,12 +21,14 @@ import java.util.List; import java.util.Map; +import org.drools.Agenda; import org.drools.DroolsTestCase; import org.drools.RuleBase; import org.drools.WorkingMemory; -import org.drools.common.Agenda; +import org.drools.common.DefaultAgenda; import org.drools.common.AgendaGroupImpl; import org.drools.common.DefaultFactHandle; +import org.drools.common.InternalAgenda; import org.drools.common.PropagationContextImpl; import org.drools.rule.Rule; import org.drools.spi.Activation; @@ -100,7 +102,7 @@ final RuleBase ruleBase = new ReteooRuleBase(); final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory(); - final Agenda agenda = workingMemory.getAgenda(); + final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda(); final Rule rule = new Rule( "test-rule" ); final TerminalNode node = new TerminalNode( 3, @@ -192,7 +194,7 @@ final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory(); - final Agenda agenda = workingMemory.getAgenda(); + final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda(); // // create the AgendaGroups final AgendaGroupImpl agendaGroup1 = new AgendaGroupImpl( "agendaGroup1" ); @@ -412,7 +414,7 @@ final RuleBase ruleBase = new ReteooRuleBase(); final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory(); - final Agenda agenda = workingMemory.getAgenda(); + final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda(); // create the agendaGroup final AgendaGroupImpl agendaGroup = new AgendaGroupImpl( "agendaGroup" ); @@ -491,7 +493,7 @@ final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory(); - final Agenda agenda = workingMemory.getAgenda(); + final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda(); final List list = new ArrayList(); Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -16,6 +16,7 @@ * limitations under the License. */ +import org.drools.Agenda; import org.drools.Cheese; import org.drools.DroolsTestCase; import org.drools.FactException; @@ -24,8 +25,9 @@ import org.drools.RuleBaseConfiguration; import org.drools.WorkingMemory; import org.drools.base.ClassObjectType; -import org.drools.common.Agenda; +import org.drools.common.DefaultAgenda; import org.drools.common.DefaultFactHandle; +import org.drools.common.InternalAgenda; import org.drools.common.PropagationContextImpl; import org.drools.rule.Rule; import org.drools.spi.Consequence; @@ -52,7 +54,7 @@ rule1 ); final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory(); - final Agenda agenda = workingMemory.getAgenda(); + final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda(); final Consequence consequence = new Consequence() { /** Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/SchedulerTest.java =================================================================== --- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/SchedulerTest.java 2006-05-31 17:00:18 UTC (rev 4526) +++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/SchedulerTest.java 2006-05-31 17:07:01 UTC (rev 4527) @@ -19,10 +19,10 @@ import java.util.ArrayList; import java.util.List; +import org.drools.Agenda; import org.drools.DroolsTestCase; import org.drools.RuleBase; import org.drools.WorkingMemory; -import org.drools.common.Agenda; import org.drools.common.DefaultFactHandle; import org.drools.common.PropagationContextImpl; import org.drools.rule.Rule; |