jadex.rules.rulesystem.rete.RetePatternMatcherState 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.rulesystem.rete;
import jadex.rules.rulesystem.AbstractAgenda;
import jadex.rules.rulesystem.IAgenda;
import jadex.rules.rulesystem.IPatternMatcherState;
import jadex.rules.rulesystem.rete.nodes.ReteMemory;
import jadex.rules.rulesystem.rete.nodes.ReteNode;
import jadex.rules.state.IOAVState;
import jadex.rules.state.IOAVStateListener;
import jadex.rules.state.IProfiler;
import jadex.rules.state.OAVAttributeType;
import jadex.rules.state.OAVObjectType;
import java.util.Iterator;
/**
* The state specific part of a Rete pattern matcher.
*/
public class RetePatternMatcherState implements IPatternMatcherState, IOAVStateListener
{
//-------- attributes --------
/** The rete node. */
protected ReteNode node;
/** The rete memory. */
protected ReteMemory retemem;
/** The state. */
protected IOAVState state;
/** The agenda. */
protected AbstractAgenda agenda;
//-------- constructors --------
/**
* Create a state specific part of a Rete pattern matcher.
*/
public RetePatternMatcherState(ReteNode node, IOAVState state, ReteMemory retemem, AbstractAgenda agenda)
{
this.node = node;
this.retemem = retemem;
this.state = state;
this.agenda = agenda;
}
//-------- IPatternMatcherState interface --------
/**
* Initialize the pattern matcher.
* Called before the agenda is accessed
* to perform any initialization, if necessary.
*/
public void init()
{
// Initialize initial fact node, if any.
if(node.getInitialFactNode()!=null)
node.getInitialFactNode().init(state, retemem, agenda);
// Add initial objects.
for(Iterator objects=state.getDeepObjects(); objects.hasNext(); )
{
Object object = objects.next();
objectAdded(object, state.getType(object), false); // Hack!!! Should check if root?
}
state.addStateListener(this, true);
// state.addStateListener(this, false);
}
/**
* Get the agenda.
* The agenda can only be accessed, after the rule system
* has been initialized with {@link#init()}.
* @return The agenda.
*/
public IAgenda getAgenda()
{
return this.agenda;
}
//-------- IOAVStateListener interface --------
// flag for debugging threading issues
private boolean running;
/**
* Notification when an attribute value of an object has been set.
* @param id The object id.
* @param type The object type.
* @param attr The attribute type.
* @param oldvalue The oldvalue.
* @param newvalue The newvalue.
*/
public void objectModified(Object id, OAVObjectType type, OAVAttributeType attr,
Object oldvalue, Object newvalue)
{
assert !running;
running = true;
state.getProfiler().start(IProfiler.TYPE_OBJECT, type);
state.getProfiler().start(IProfiler.TYPE_OBJECTEVENT, IProfiler.OBJECTEVENT_MODIFIED);
node.modifyObject(id, type, attr, oldvalue, newvalue, state, retemem, agenda);
state.getProfiler().stop(IProfiler.TYPE_OBJECTEVENT, IProfiler.OBJECTEVENT_MODIFIED);
state.getProfiler().stop(IProfiler.TYPE_OBJECT, type);
running = false;
}
/**
* Notification when an object has been added to the state.
* @param id The object id.
* @param type The object type.
*/
public void objectAdded(Object id, OAVObjectType type, boolean root)
{
assert !running;
running = true;
state.getProfiler().start(IProfiler.TYPE_OBJECT, type);
state.getProfiler().start(IProfiler.TYPE_OBJECTEVENT, IProfiler.OBJECTEVENT_ADDED);
node.addObject(id, type, state, retemem, agenda);
state.getProfiler().stop(IProfiler.TYPE_OBJECTEVENT, IProfiler.OBJECTEVENT_ADDED);
state.getProfiler().stop(IProfiler.TYPE_OBJECT, type);
running = false;
}
/**
* Notification when an object has been removed from state.
* @param id The object id.
* @param type The object type.
*/
public void objectRemoved(Object id, OAVObjectType type)
{
assert !running;
running = true;
state.getProfiler().start(IProfiler.TYPE_OBJECT, type);
state.getProfiler().start(IProfiler.TYPE_OBJECTEVENT, IProfiler.OBJECTEVENT_REMOVED);
node.removeObject(id, type, state, retemem, agenda);
state.getProfiler().stop(IProfiler.TYPE_OBJECTEVENT, IProfiler.OBJECTEVENT_REMOVED);
state.getProfiler().stop(IProfiler.TYPE_OBJECT, type);
running = false;
}
//-------- methods --------
/**
* Get the Rete memory.
*/
public ReteMemory getReteMemory()
{
return retemem;
}
/**
* Get the node.
* @return The node.
*/
public ReteNode getReteNode()
{
return node;
}
}