📄 workingmemoryimpl.java
字号:
}
public List getObjects(Class objectClass)
{
List matching = new LinkedList( );
Iterator objIter = this.objects.values( ).iterator( );
Object obj;
while ( objIter.hasNext( ) )
{
obj = objIter.next( );
if ( objectClass.isInstance( obj ) )
{
matching.add( obj );
}
}
return matching;
}
/**
* @see WorkingMemory
*/
public boolean containsObject(FactHandle handle)
{
return this.objects.containsKey( ((FactHandleImpl) handle).getId( ) );
}
/**
* @see WorkingMemory
*/
public FactHandle assertObject(Object object) throws FactException
{
return assertObject( object, /* Not-Dynamic */
false );
}
public FactHandle assertObject(Object object,
boolean dynamic) throws FactException
{
FactHandle handle = (FactHandle) handles.get( object );
if ( handle != null )
{
return handle;
}
handle = newFactHandle( );
putObject( handle,
object );
if ( dynamic )
{
addPropertyChangeListener( object );
}
this.agenda.setMode( Agenda.ASSERT );
ruleBase.assertObject( handle,
object,
this );
eventSupport.fireObjectAsserted( handle,
object );
this.agenda.setMode( Agenda.NONE );
return handle;
}
private void addPropertyChangeListener(Object object)
{
try
{
Method method = object.getClass( ).getMethod( "addPropertyChangeListener",
ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES );
method.invoke( object,
addRemovePropertyChangeListenerArgs );
}
catch ( NoSuchMethodException e )
{
System.err.println( "Warning: Method addPropertyChangeListener not found"
+ " on the class " + object.getClass( )
+ " so Drools will be unable to process JavaBean"
+ " PropertyChangeEvents on the asserted Object" );
}
catch ( IllegalArgumentException e )
{
System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass( ) + " does not take" + " a simple PropertyChangeListener argument" + " so Drools will be unable to process JavaBean"
+ " PropertyChangeEvents on the asserted Object" );
}
catch ( IllegalAccessException e )
{
System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass( ) + " is not public" + " so Drools will be unable to process JavaBean" + " PropertyChangeEvents on the asserted Object" );
}
catch ( InvocationTargetException e )
{
System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass( ) + " threw an InvocationTargetException" + " so Drools will be unable to process JavaBean"
+ " PropertyChangeEvents on the asserted Object: " + e.getMessage( ) );
}
catch ( SecurityException e )
{
System.err.println( "Warning: The SecurityManager controlling the class " + object.getClass( ) + " did not allow the lookup of a" + " addPropertyChangeListener method" + " so Drools will be unable to process JavaBean"
+ " PropertyChangeEvents on the asserted Object: " + e.getMessage( ) );
}
}
private void removePropertyChangeListener(FactHandle handle) throws NoSuchFactObjectException
{
Object object = null;
try
{
object = getObject( handle );
Method mehod = handle.getClass( ).getMethod( "removePropertyChangeListener",
ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES );
mehod.invoke( handle,
addRemovePropertyChangeListenerArgs );
}
catch ( NoSuchMethodException e )
{
// The removePropertyChangeListener method on the class
// was not found so Drools will be unable to
// stop processing JavaBean PropertyChangeEvents
// on the retracted Object
}
catch ( IllegalArgumentException e )
{
System.err.println( "Warning: The removePropertyChangeListener method" + " on the class " + object.getClass( ) + " does not take" + " a simple PropertyChangeListener argument" + " so Drools will be unable to stop processing JavaBean"
+ " PropertyChangeEvents on the retracted Object" );
}
catch ( IllegalAccessException e )
{
System.err.println( "Warning: The removePropertyChangeListener method" + " on the class " + object.getClass( ) + " is not public" + " so Drools will be unable to stop processing JavaBean" + " PropertyChangeEvents on the retracted Object" );
}
catch ( InvocationTargetException e )
{
System.err.println( "Warning: The removePropertyChangeL istener method" + " on the class " + object.getClass( ) + " threw an InvocationTargetException" + " so Drools will be unable to stop processing JavaBean"
+ " PropertyChangeEvents on the retracted Object: " + e.getMessage( ) );
}
catch ( SecurityException e )
{
System.err.println( "Warning: The SecurityManager controlling the class " + object.getClass( ) + " did not allow the lookup of a" + " removePropertyChangeListener method" + " so Drools will be unable to stop processing JavaBean"
+ " PropertyChangeEvents on the retracted Object: " + e.getMessage( ) );
}
}
/**
* Associate an object with its handle.
*
* @param handle
* The handle.
* @param object
* The object.
*/
Object putObject(FactHandle handle,
Object object)
{
Object oldValue = this.objects.put( ((FactHandleImpl) handle).getId( ),
object );
this.handles.put( object,
handle );
return oldValue;
}
Object removeObject(FactHandle handle)
{
Object object = this.objects.remove( ((FactHandleImpl) handle).getId( ) );
this.handles.remove( object );
return object;
}
/**
* @see WorkingMemory
*/
public void retractObject(FactHandle handle) throws FactException
{
removePropertyChangeListener( handle );
this.agenda.setMode( Agenda.RETRACT );
ruleBase.retractObject( handle,
this );
Object oldObject = removeObject( handle );
factHandlePool.push( ((FactHandleImpl) handle).getId( ) );
eventSupport.fireObjectRetracted( handle,
oldObject );
this.agenda.setMode( Agenda.NONE );
((FactHandleImpl) handle).invalidate( );
}
/**
* @see WorkingMemory
*/
public void modifyObject(FactHandle handle,
Object object) throws FactException
{
Object originalObject = removeObject( handle );
if ( originalObject == null )
{
throw new NoSuchFactObjectException( handle );
}
putObject( handle,
object );
this.agenda.setMode( Agenda.MODIFY );
this.ruleBase.retractObject( handle,
this );
this.ruleBase.assertObject( handle,
object,
this );
this.agenda.removeMarkedItemsFromAgenda( );
this.agenda.setMode( Agenda.NONE );
/*
* this.ruleBase.modifyObject( handle, object, this );
*/
this.eventSupport.fireObjectModified( handle,
originalObject,
object );
}
/**
* Retrieve the <code>JoinMemory</code> for a particular
* <code>JoinNode</code>.
*
* @param node
* The <code>JoinNode</code> key.
*
* @return The node's memory.
*/
public JoinMemory getJoinMemory(JoinNode node)
{
JoinMemory memory = (JoinMemory) this.joinMemories.get( node );
if ( memory == null )
{
memory = new JoinMemory( node.getTupleDeclarations( ),
node.getCommonDeclarations( ) );
this.joinMemories.put( node,
memory );
}
return memory;
}
public WorkingMemoryEventSupport getEventSupport()
{
return eventSupport;
}
/**
* Sets the AsyncExceptionHandler to handle exceptions thrown by the Agenda
* Scheduler used for duration rules.
*
* @param handler
*/
public void setAsyncExceptionHandler(AsyncExceptionHandler handler)
{
this.agenda.setAsyncExceptionHandler( handler );
}
public void dumpMemory()
{
Iterator it = this.joinMemories.keySet( ).iterator( );
while ( it.hasNext( ) )
{
((JoinMemory) this.joinMemories.get( it.next( ) )).dump( );
}
}
public void propertyChange(PropertyChangeEvent event)
{
Object object = event.getSource( );
try
{
modifyObject( getFactHandle( object ),
object );
}
catch ( NoSuchFactHandleException e )
{
// Not a fact so unable to process the chnage event
}
catch ( FactException e )
{
throw new RuntimeException( e.getMessage( ) );
}
}
public FactHandleFactory getFactHandleFactory()
{
return factHandleFactory;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -