jadex.rules.rulesystem.rete.nodes.BetaNode 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.nodes;
import jadex.rules.rulesystem.AbstractAgenda;
import jadex.rules.rulesystem.rete.Tuple;
import jadex.rules.rulesystem.rete.constraints.ConstraintIndexer;
import jadex.rules.rulesystem.rete.constraints.IConstraintEvaluator;
import jadex.rules.state.IOAVState;
import jadex.rules.state.OAVAttributeType;
import java.util.Collection;
import java.util.Iterator;
/**
* A beta node has the purpose to perform a constraints check
* between (at least) two objects. The beta node has to incoming
* coming connections which both can activate the node.
*/
public class BetaNode extends AbstractBetaNode
{
//-------- constructors --------
/**
* Create a new beta node.
*/
public BetaNode(int nodeid, IConstraintEvaluator[] evaluators, ConstraintIndexer[] xevaluators)
{
super(nodeid, evaluators, xevaluators);
}
//-------- tuple consumer interface (left) --------
/**
* Modify a tuple in this node.
* @param left The tuple.
*/
public void modifyTuple(Tuple left, int tupleindex, OAVAttributeType type,
Object oldvalue, Object newvalue, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
if(!getRelevantAttributes().contains(type))
return;
super.modifyTuple(left, tupleindex, type, oldvalue, newvalue, state, mem, agenda);
if(!isAffected(type))
{
Collection omem = fetchObjectMemory(state, left, mem);
if(omem!=null)
{
for(Iterator it=omem.iterator(); it.hasNext(); )
{
Object right = it.next();
boolean contains = isMatchContained(state, left, right, mem);
// Tuple changed in memory -> propagate modification
if(contains)
propagateModification(left, right, tupleindex, type, oldvalue, newvalue, state, mem, agenda);
}
}
}
}
//-------- object consumer interface (right) --------
/**
* Propagate an object change to this node.
* @param right The new object.
*/
public void modifyObject(Object right, OAVAttributeType type, Object oldvalue, Object newvalue, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
if(!getRelevantAttributes().contains(type))
return;
super.modifyObject(right, type, oldvalue, newvalue, state, mem, agenda);
if(!isAffected(type))
{
Collection tmem = fetchTupleMemory(state, right, mem);
if(tmem!=null)
{
for(Iterator it=tmem.iterator(); it.hasNext(); )
{
Tuple left = (Tuple)it.next();
boolean contains = isMatchContained(state, left, right, mem);
// Tuple changed in memory -> propagate modification
if(contains)
propagateModification(left, right, left.size(), type, oldvalue, newvalue, state, mem, agenda);
}
}
}
}
//-------- template methods --------
/**
* Add a match to the node memory and propagate if necessary.
*/
protected void addMatch(Tuple left, Object right, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
Tuple result = mem.getTuple(state, left, right);
BetaMemory bmem = (BetaMemory)mem.getNodeMemory(this);
if(bmem.addResultTuple(result))
{
ITupleConsumerNode[] tcs = tconsumers;
for(int j=0; tcs!=null && j