jadex.rules.rulesystem.rete.RetePatternMatcherFunctionality 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.IPatternMatcherFunctionality;
import jadex.rules.rulesystem.IPatternMatcherState;
import jadex.rules.rulesystem.IRule;
import jadex.rules.rulesystem.IRulebase;
import jadex.rules.rulesystem.IRulebaseListener;
import jadex.rules.rulesystem.rete.nodes.ReteMemory;
import jadex.rules.rulesystem.rete.nodes.ReteNode;
import jadex.rules.state.IOAVState;
import java.util.Iterator;
/**
* The static (reusable) part of a Rete matcher (i.e. the Rete network).
*/
public class RetePatternMatcherFunctionality
implements IPatternMatcherFunctionality, IRulebaseListener, Cloneable
{
//-------- attributes --------
/** The rulebase. */
protected IRulebase rulebase;
/** The rete node. */
protected ReteNode node;
//-------- constructors --------
/**
* Create a new Rete pattern matcher functionality.
*/
public RetePatternMatcherFunctionality(IRulebase rulebase)
{
this.rulebase = rulebase;
this.node = new ReteNode();
// Build existing rules of the rulebase.
for(Iterator it=rulebase.getRules().iterator(); it.hasNext(); )
node.addRule((IRule)it.next());
node.setInited(true);
rulebase.addRulebaseListener(this);
}
/**
* Get the rulebase.
* @return The rulebase.
*/
public IRulebase getRulebase()
{
return rulebase;
}
/**
*
* /
public void setRulebase(IRulebase rulebase)
{
// Remove listener from old rulebase
if(this.rulebase!=null)
this.rulebase.removeRulebaseListener(this);
// Create executeable rules for new rulebase
this.rulebase = rulebase;
this.node = new ReteNode();
for(Iterator it=rulebase.getRules().iterator(); it.hasNext(); )
node.addRule((IRule)it.next());
// Track rulebase changes
rulebase.addRulebaseListener(this);
}*/
//-------- IRulebaseListener interface --------
/**
* Notification when a rule has been added.
* @param rule The added rule.
*/
public void ruleAdded(IRule rule)
{
node.addRule(rule);
}
/**
* Notification when a rule has been removed.
* @param rule The removed rule.
*/
public void ruleRemoved(IRule rule)
{
node.removeRule(rule);
}
//-------- IPatternMatcherFunctionality interface --------
/**
* Create a pattern matcher instance for a given state.
*/
public IPatternMatcherState createMatcherState(IOAVState state, AbstractAgenda agenda)
{
return new RetePatternMatcherState(node, state, new ReteMemory(state), agenda);
}
//-------- methods --------
/**
* Get the Rete node.
*/
public ReteNode getReteNode()
{
return node;
}
//-------- cloneable --------
/**
* Clone this object.
* @return A clone of this object.
*/
public Object clone()
{
RetePatternMatcherFunctionality ret = null;
try
{
ret = (RetePatternMatcherFunctionality)super.clone();
ret.rulebase = (IRulebase)rulebase.clone();
ret.rulebase.addRulebaseListener(ret);
ret.node = (ReteNode)node.clone();
}
catch(CloneNotSupportedException exception)
{
throw new RuntimeException("Cloning not supported: "+this);
}
return ret;
}
}