📄 workingmemoryimpl.java
字号:
package org.drools.reteoo;
/*
* Copyright 2001-2004 (C) The Werken Company. All Rights Reserved.
*
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain copyright statements and
* notices. Redistributions must also contain a copy of this document.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name "drools" must not be used to endorse or promote products derived
* from this Software without prior written permission of The Werken Company.
* For written permission, please contact bob@werken.com.
*
* 4. Products derived from this Software may not be called "drools" nor may
* "drools" appear in their names without prior written permission of The Werken
* Company. "drools" is a trademark of The Werken Company.
*
* 5. Due credit should be given to The Werken Company. (http://werken.com/)
*
* THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.NoSuchFactHandleException;
import org.drools.NoSuchFactObjectException;
import org.drools.RuleBase;
import org.drools.WorkingMemory;
import org.drools.event.AgendaEventListener;
import org.drools.event.AgendaEventSupport;
import org.drools.event.ReteooNodeEventListener;
import org.drools.event.ReteooNodeEventSupport;
import org.drools.event.WorkingMemoryEventListener;
import org.drools.event.WorkingMemoryEventSupport;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
import org.drools.spi.AgendaFilter;
import org.drools.spi.AsyncExceptionHandler;
import org.drools.spi.PropagationContext;
import org.drools.util.IdentityMap;
import org.drools.util.PrimitiveLongMap;
import org.drools.util.PrimitiveLongStack;
/**
* Implementation of <code>WorkingMemory</code>.
*
* @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
* @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris </a>
*/
class WorkingMemoryImpl
implements
WorkingMemory,
PropertyChangeListener
{
// ------------------------------------------------------------
// Constants
// ------------------------------------------------------------
private static final Class[] ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES = new Class[]{PropertyChangeListener.class};
// ------------------------------------------------------------
// Instance members
// ------------------------------------------------------------
/** The arguments used when adding/removing a property change listener. */
private final Object[] addRemovePropertyChangeListenerArgs = new Object[]{this};
/** The actual memory for the <code>JoinNode</code>s. */
private final PrimitiveLongMap nodeMemories = new PrimitiveLongMap( 32,
8 );
/** Application data which is associated with this memory. */
private final Map applicationData = new HashMap( );
/** Handle-to-object mapping. */
private final PrimitiveLongMap objects = new PrimitiveLongMap( 32,
8 );
/** Object-to-handle mapping. */
private final Map identityMap = new IdentityMap( );
private final Map equalsMap = new HashMap( );
private final Map justifiers = new HashMap( );
private final PrimitiveLongMap justified = new PrimitiveLongMap( 8,
32 );
private final PrimitiveLongStack factHandlePool = new PrimitiveLongStack( );
private static final String STATED = "STATED";
/** The eventSupport */
private final WorkingMemoryEventSupport workingMemoryEventSupport = new WorkingMemoryEventSupport( this );
private final AgendaEventSupport agendaEventSupport = new AgendaEventSupport( this );
private final ReteooNodeEventSupport reteooNodeEventSupport = new ReteooNodeEventSupport( this );
/** The <code>RuleBase</code> with which this memory is associated. */
private final RuleBaseImpl ruleBase;
/** Rule-firing agenda. */
private final Agenda agenda;
/** Flag to determine if a rule is currently being fired. */
private boolean firing;
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
/**
* Construct.
*
* @param ruleBase
* The backing rule-base.
*/
public WorkingMemoryImpl(RuleBaseImpl ruleBase)
{
this.ruleBase = ruleBase;
this.agenda = new Agenda( this,
ruleBase.getConflictResolver( ) );
}
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
public void addEventListener(WorkingMemoryEventListener listener)
{
this.workingMemoryEventSupport.addEventListener( listener );
}
public void removeEventListener(WorkingMemoryEventListener listener)
{
this.workingMemoryEventSupport.removeEventListener( listener );
}
public List getWorkingMemoryEventListeners()
{
return this.workingMemoryEventSupport.getEventListeners( );
}
public void addEventListener(AgendaEventListener listener)
{
this.agendaEventSupport.addEventListener( listener );
}
public void removeEventListener(AgendaEventListener listener)
{
this.agendaEventSupport.removeEventListener( listener );
}
public List getAgendaEventListeners()
{
return this.agendaEventSupport.getEventListeners( );
}
public void addEventListener(ReteooNodeEventListener listener)
{
this.reteooNodeEventSupport.addEventListener( listener );
}
public void removeEventListener(ReteooNodeEventListener listener)
{
this.reteooNodeEventSupport.removeEventListener( listener );
}
public List getReteooNodeEventListeners()
{
return this.reteooNodeEventSupport.getEventListeners( );
}
/**
* Create a new <code>FactHandle</code>.
*
* @return The new fact handle.
*/
FactHandle newFactHandle()
{
if ( !this.factHandlePool.isEmpty( ) )
{
return this.ruleBase.getFactHandleFactory( ).newFactHandle( this.factHandlePool.pop( ) );
}
return this.ruleBase.getFactHandleFactory( ).newFactHandle( );
}
/**
* @see WorkingMemory
*/
public Map getApplicationDataMap()
{
return this.applicationData;
}
/**
* @see WorkingMemory
*/
public void setApplicationData(String name,
Object value)
{
// Make sure the application data has been declared in the RuleBase
Map applicationDataDefintions = this.ruleBase.getApplicationData( );
Class type = (Class) applicationDataDefintions.get( name );
if ( (type == null) )
{
throw new RuntimeException( "Unexpected application data [" + name + "]" );
}
else if ( !type.isInstance( value ) )
{
throw new RuntimeException( "Illegal class for application data. " + "Expected [" + type.getName( ) + "], " + "found [" + value.getClass( ).getName( ) + "]." );
}
else
{
this.applicationData.put( name,
value );
}
}
/**
* @see WorkingMemory
*/
public Object getApplicationData(String name)
{
return this.applicationData.get( name );
}
/**
* Retrieve the rule-firing <code>Agenda</code> for this
* <code>WorkingMemory</code>.
*
* @return The <code>Agenda</code>.
*/
protected Agenda getAgenda()
{
return this.agenda;
}
/**
* Clear the Agenda
*/
public void clearAgenda()
{
this.agenda.clearAgenda( );
}
/**
* @see WorkingMemory
*/
public RuleBase getRuleBase()
{
return this.ruleBase;
}
public void fireAllRules(AgendaFilter agendaFilter) throws FactException
{
// If we're already firing a rule, then it'll pick up
// the firing for any other assertObject(..) that get
// nested inside, avoiding concurrent-modification
// exceptions, depending on code paths of the actions.
if ( !firing )
{
try
{
firing = true;
while ( agenda.fireNextItem( agendaFilter ) );
}
finally
{
firing = false;
}
}
}
/**
* @see WorkingMemory
*/
public void fireAllRules() throws FactException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -