jadex.rules.rulesystem.rete.nodes.TestNode 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.IConstraintEvaluator;
import jadex.rules.rulesystem.rete.extractors.AttributeSet;
import jadex.rules.state.IOAVState;
import jadex.rules.state.IProfiler;
import jadex.rules.state.OAVAttributeType;
import java.util.Collection;
import java.util.LinkedHashSet;
/**
* A test node takes evaluates a predicate.
*/
public class TestNode extends AbstractNode implements ITupleConsumerNode, ITupleSourceNode
{
//-------- attributes --------
/** The tuple consumers. */
protected ITupleConsumerNode[] tconsumers;
/** The tuple source. */
protected ITupleSourceNode tsource;
/** The constraint evaluator. */
final protected IConstraintEvaluator evaluator;
/** The set of relevant attributes. */
protected AttributeSet relevants;
/** The set of indirect attributes. */
protected AttributeSet indirects;
//-------- constructors --------
/**
* Create a new test node.
* @param evaluator The evaluator.
*/
public TestNode(int nodeid, IConstraintEvaluator evaluator)
{
super(nodeid);
this.evaluator = evaluator;
}
//-------- ITupleConsumerNode ---------
/**
* Add a new tuple to this node.
* @param tuple The tuple.
*/
public void addTuple(Tuple tuple, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
//System.out.println("Add tuple called: "+this+" "+tuple);
state.getProfiler().start(IProfiler.TYPE_NODE, this);
state.getProfiler().start(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_TUPLEADDED);
Collection tmem = (Collection)mem.getNodeMemory(this);
if(!tmem.contains(tuple) && evaluator.evaluate(null, tuple, state))
{
tmem.add(tuple);
//System.out.println("Tuple passed constraint check: "+this+" "+object);
propagateAdditionToTupleConsumers(tuple, state, mem, agenda);
}
state.getProfiler().stop(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_TUPLEADDED);
state.getProfiler().stop(IProfiler.TYPE_NODE, this);
}
/**
* Remove a tuple from this node.
* @param tuple The tuple.
*/
public void removeTuple(Tuple tuple, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
//System.out.println("Remove tuple called: "+this+" "+tuple);
state.getProfiler().start(IProfiler.TYPE_NODE, this);
state.getProfiler().start(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_TUPLEREMOVED);
if(mem.hasNodeMemory(this))
{
if(((Collection)mem.getNodeMemory(this)).remove(tuple))
{
propagateRemovalToTupleConsumers(tuple, state, mem, agenda);
}
}
state.getProfiler().stop(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_TUPLEREMOVED);
state.getProfiler().stop(IProfiler.TYPE_NODE, this);
}
/**
* Modify a tuple in this node.
* @param tuple The tuple.
*/
public void modifyTuple(Tuple tuple, int tupleindex, OAVAttributeType type,
Object oldvalue, Object newvalue, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
//System.out.println("Modify object called: "+this+" "+object);
state.getProfiler().start(IProfiler.TYPE_NODE, this);
state.getProfiler().start(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_TUPLEMODIFIED);
if(getRelevantAttributes().contains(type))
{
// Check if modification changes node memory.
boolean affected = isAffected(type);
boolean contains = mem.hasNodeMemory(this) && ((Collection)mem.getNodeMemory(this)).contains(tuple);
if(affected)
{
boolean check = evaluator.evaluate(null, tuple, state);
// tuple no longer valid -> remove
if(contains && !check)
{
((Collection)mem.getNodeMemory(this)).remove(tuple);
propagateRemovalToTupleConsumers(tuple, state, mem, agenda);
}
// tuple newly valid -> add
else if(!contains && check)
{
((Collection)mem.getNodeMemory(this)).add(tuple);
propagateAdditionToTupleConsumers(tuple, state, mem, agenda);
}
else if(contains)
{
propagateModificationToTupleConsumers(tuple, tupleindex, type,
oldvalue, newvalue, state, mem, agenda);
}
}
else
{
// tuple changed in memory -> propagate modification
if(contains)
{
propagateModificationToTupleConsumers(tuple, tupleindex, type,
oldvalue, newvalue, state, mem, agenda);
}
}
}
state.getProfiler().stop(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_TUPLEMODIFIED);
state.getProfiler().stop(IProfiler.TYPE_NODE, this);
}
/**
* Propagate an indirect object change to this node.
* @param object The changed object.
*/
public void modifyIndirectObject(Object object, OAVAttributeType type, Object oldvalue, Object newvalue, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
throw new UnsupportedOperationException("Unsupported method.");
}
/**
* Set the tuple source of this node.
* @param node The tuple source node.
*/
public void setTupleSource(ITupleSourceNode node)
{
this.tsource = node;
}
/**
* Get the tuple source of this node.
* @return The object source node.
*/
public ITupleSourceNode getTupleSource()
{
return tsource;
}
//-------- ITupleSourceNode --------
/**
* Add an tuple consumer node.
* @param node A new consumer node.
*/
public void addTupleConsumer(ITupleConsumerNode node)
{
if(tconsumers==null)
{
tconsumers = new ITupleConsumerNode[]{node};
}
else
{
ITupleConsumerNode[] tmp = new ITupleConsumerNode[tconsumers.length+1];
System.arraycopy(tconsumers, 0, tmp, 0, tconsumers.length);
tmp[tconsumers.length] = node;
tconsumers = tmp;
}
relevants = null; // Will be recalculated on next access;
}
/**
* Remove an tuple consumer.
* @param node The consumer node.
*/
public void removeTupleConsumer(ITupleConsumerNode node)
{
if(tconsumers!=null)
{
for(int i=0; i0)
System.arraycopy(tconsumers, 0, tmp, 0, i);
if(i