jadex.rules.state.javaimpl.OAVEventHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-rules Show documentation
Show all versions of jadex-rules Show documentation
Jadex Rules is a small lightweight rule engine, which currently
employs the well-known Rete algorithm for highly efficient rule
matching. Jadex rules is therefore similar to other rule engines
like JESS and Drools. Despite the similarities there are also
important differences between these systems:
* Jadex Rules is very small and
intended to be used as component
of other software. Even though rules can be specified in a Java
dialect as well as (a small variation of) the CLIPS language
its primary usage is on the API level. Jadex Rules is currently
the core component of the Jadex BDI reasoning engine.
* Jadex Rules cleanly separates between state and rule representation.
This allows the state implementation as well as the matcher to be
flexibly exchanged. Some experiments have e.g. been conducted with
a Jena representation. Regarding the matcher, it is planned to
support also the Treat algorithm, which has a lower memory footprint
than Rete.
* Jadex Rules pays close attention to rule debugging. The state as
well as the rete engine can be observed at runtime. The rule debugger
provides functionalities to execute a rule program stepwise and also
use rule breakpoints to stop the execution at those points.
package jadex.rules.state.javaimpl;
import jadex.commons.collection.IdentityHashSet;
import jadex.rules.state.IOAVState;
import jadex.rules.state.IOAVStateListener;
import jadex.rules.state.OAVAttributeType;
import jadex.rules.state.OAVObjectType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* This class handles the collection and distribution OAV
* events to registered listeners.
*/
public class OAVEventHandler
{
//-------- attributes --------
/** The state. */
protected IOAVState state;
/** The bunch state listeners. */
protected List listeners;
/** The direct state listeners. */
protected List directlisteners;
/** The collected change events. */
protected Set oavevents;
/** The collected bean events (may be added from external thread). */
protected Set beanevents;
/** The objects that have been removed in current change set. */
protected Set removed_objects;
/** The objects that have been added in current change set. */
protected Set added_objects;
/** Flag that is only true, while listeners are being notified. */
protected boolean notifying;
//-------- constructors --------
/**
* Create a new OAV event handler.
*/
public OAVEventHandler(IOAVState state)
{
this.state = state;
this.listeners = new ArrayList();
this.directlisteners = new ArrayList();
this.oavevents = new LinkedHashSet();
// this.oavevents = new CheckedCollection(new LinkedHashSet());
this.beanevents = Collections.synchronizedSet(new LinkedHashSet());
// this.beanevents = Collections.synchronizedSet(new CheckedCollection(new LinkedHashSet()));
}
//-------- state observers --------
/**
* Add a new state listener.
* @param listener The state listener.
*/
public void addStateListener(IOAVStateListener listener, boolean bunch)
{
if(bunch)
this.listeners.add(listener);
else
this.directlisteners.add(listener);
}
/**
* Remove a state listener.
* @param listener The state listener.
*/
public void removeStateListener(IOAVStateListener listener)
{
if(!this.listeners.remove(listener))
if(!this.directlisteners.remove(listener))
throw new RuntimeException("Listener not found: "+listener);
}
/**
* Throw collected events and notify the listeners.
*/
public void notifyEventListeners()
{
this.notifying = true;
if(!beanevents.isEmpty())
{
Object[] abeanevents;
synchronized(beanevents)
{
abeanevents = beanevents.toArray();
beanevents.clear();
}
for(int i=0; i