jadex.rules.rulesystem.rete.nodes.ReteNode 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.IRule;
import jadex.rules.rulesystem.rete.builder.ReteBuilder;
import jadex.rules.rulesystem.rete.extractors.AttributeSet;
import jadex.rules.state.IOAVState;
import jadex.rules.state.IProfiler;
import jadex.rules.state.OAVAttributeType;
import jadex.rules.state.OAVJavaType;
import jadex.rules.state.OAVObjectType;
import jadex.rules.state.OAVTypeModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* ReteNode implementation of the IConditionSystem.
*/
public class ReteNode extends AbstractNode implements IObjectSourceNode
{
//-------- attributes --------
/** The type nodes. */
protected Map typenodes;
/** Matching nodes for each (sub)type (cached for speed). */
protected Map typenodesets;
/** Indirectly affected nodes for an attribute type (cached for speed). */
protected Map indirectnodesets;
/** The initial fact node (if any). */
protected InitialFactNode initialfact;
/** The terminal nodes (IRule -> Node). */
protected Map terminalnodes;
/** The rete builder. */
protected ReteBuilder builder;
/** The set of relevant attributes. */
protected AttributeSet relevants;
/** Do a consistency check after each state change (requires asserts). */
protected boolean check;
/** The node counter in this network. */
protected int nodecounter;
/** For debugging: node is inited and network must not be changed anymore. */
protected boolean inited;
//-------- constructors --------
/**
* Create a new rete system.
* @param state The state.
*/
public ReteNode()
{
super(0);
this.nodecounter = 1;
this.typenodes = new LinkedHashMap();
this.terminalnodes = new LinkedHashMap();
// The typenode mapping for each object type is dynamically created on first access. hack???
this.typenodesets = Collections.synchronizedMap(new LinkedHashMap());
}
//-------- methods --------
/**
* Tell the condition system about a
* new object in the state.
* @param object The new object.
*/
public void addObject(Object id, OAVObjectType type, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
// if(type.getName().equals("goal"))
// System.out.println("Value added: "+id+" "+type);
// System.out.println("Value added: "+id+" "+type);
state.getProfiler().start(IProfiler.TYPE_NODE, this);
state.getProfiler().start(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_OBJECTADDED);
Set tns = getTypeNodes(type);
if(tns!=null)
{
for(Iterator it=tns.iterator(); it.hasNext(); )
((AlphaNode)it.next()).addObject(id, state, mem, agenda);
assert !check || checkConsistency(mem);
}
// else
// System.out.println("No typenode(s) available for: "+type);
state.getProfiler().stop(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_OBJECTADDED);
state.getProfiler().stop(IProfiler.TYPE_NODE, this);
}
/**
* Tell the condition system about a
* removed object in the state.
* @param object The removed object.
*/
public void removeObject(Object id, OAVObjectType type, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
// if(type.getName().equals("goal"))
// System.out.println("Value removed: "+id+" "+type);
// if(type instanceof OAVJavaType && ((OAVJavaType)type).getClazz().getName().indexOf("Wastebin")!=-1)
// {
// System.out.println("removedRETE: "+id);
// }
state.getProfiler().start(IProfiler.TYPE_NODE, this);
state.getProfiler().start(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_OBJECTREMOVED);
Set tns = getTypeNodes(type);
if(tns!=null)
{
for(Iterator it=tns.iterator(); it.hasNext(); )
((AlphaNode)it.next()).removeObject(id, state, mem, agenda);
assert !check || checkConsistency(mem);
}
// else
// System.out.println("No typenode(s) available for: "+type);
//assert !mem.contains(id);
// System.out.println("Value removed: "+id+" "+type);
state.getProfiler().stop(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_OBJECTREMOVED);
state.getProfiler().stop(IProfiler.TYPE_NODE, this);
}
/**
* Tell the condition system about a
* modified object in the state.
* @param object The new object.
*/
public void modifyObject(Object id, OAVObjectType type, OAVAttributeType attr, Object oldvalue,
Object newvalue, IOAVState state, ReteMemory mem, AbstractAgenda agenda)
{
// if(type.getName().equals("goal"))
// System.out.println("Value set: "+id+" "+type+" "+attr+" "+newvalue);
state.getProfiler().start(IProfiler.TYPE_NODE, this);
state.getProfiler().start(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_OBJECTMODIFIED);
// if(attr.getName().indexOf("daytime")!=-1)
// System.out.println("test");
if(getRelevantAttributes().contains(attr))
{
// if(type.getName().equals("goal"))
// System.out.println("Value set: "+id+" "+type+" "+attr+" "+newvalue+" relevant!");
Set tns = getTypeNodes(type);
if(tns!=null && !tns.isEmpty())
{
for(Iterator it=tns.iterator(); it.hasNext(); )
{
TypeNode tn = (TypeNode)it.next();
tn.modifyObject(id, attr, oldvalue, newvalue, state, mem, agenda);
// old code for modified object
//tn.removeObject(id, state, mem, agenda);
//tn.addObject(id, state, mem, agenda);
}
assert !check || checkConsistency(mem);
}
//else
// System.out.println("No typenode(s) available for: "+value);
}
Set ins = getIndirectNodes(attr, state.getTypeModel());
if(ins!=null)
{
for(Iterator it=ins.iterator(); it.hasNext(); )
{
((INode)it.next()).modifyIndirectObject(id, attr, oldvalue, newvalue, state, mem, agenda);
}
}
state.getProfiler().stop(IProfiler.TYPE_NODEEVENT, IProfiler.NODEEVENT_OBJECTMODIFIED);
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)
{
// Should never be called.
throw new UnsupportedOperationException("Unsupported method.");
}
/**
* Add a rule to the network.
* @param rule The rule to add.
*/
public void addRule(IRule rule)
{
if(builder==null)
builder = new ReteBuilder();
builder.addRule(this, rule);
}
/**
* Remove a rule from the network.
* @param rule The rule to remove.
*/
public void removeRule(IRule rule)
{
if(builder==null)
builder = new ReteBuilder();
builder.removeRule(this, rule);
}
/**
* Set the terminal node for a rule.
* @param rule The rule.
* @param node The node.
*/
public void putTerminalNode(TerminalNode node)
{
terminalnodes.put(node.getRule(), node);
}
/**
* Set the terminal node for a rule.
* @param rule The rule.
* @param node The node.
*/
public TerminalNode getTerminalNode(IRule rule)
{
return (TerminalNode)terminalnodes.get(rule);
}
/**
* Get the number of nodes in the network.
* @return The number of nodes.
*/
public int getNodeCount()
{
List ret = new ArrayList();
ret.add(this);
for(int i=0; i