jadex.rules.rulesystem.rete.nodes.ReteMemory 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.commons.SReflect;
import jadex.rules.rulesystem.rete.Tuple;
import jadex.rules.state.IOAVState;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* The rete memory for all nodes.
*/
public class ReteMemory
{
//-------- attributes --------
/** The memory. */
protected Map memory;
/** The state. */
protected IOAVState state;
// /** Used for logging debug output. */
// protected List debug;
//-------- constructors --------
/**
* Create a new rete memory.
*/
public ReteMemory(IOAVState state)
{
this.state = state;
this.memory = new HashMap();
}
//-------- collection methods --------
/**
* Test if there is a memory for a node.
* @param node The node.
* @return True, if there is a memory.
*/
public boolean hasNodeMemory(INode node)
{
return memory.containsKey(node);
}
// protected Set empties = new HashSet();
// protected Set unempties = new HashSet();
/**
* Get the memory for a node.
* @param node The node.
* @return The memory.
*/
public Object getNodeMemory(INode node)
{
Object ret = memory.get(node);
if(ret == null)
{
ret = node.createNodeMemory(state);
memory.put(node, ret);
}
// // Debbuging code to check if node memory is created but never used.
// else if(!unempties.contains(ret))
// {
// boolean empty = ret instanceof Collection ? ((Collection)ret).isEmpty() : ((BetaMemory)ret).size()==0;
// if(empty)
// {
// empties.add(ret);
// System.out.println("Empties: "+empties.size());
// }
// else
// {
// empties.remove(ret);
// unempties.add(ret);
// }
// }
return ret;
}
//-------- tuple memory methods --------
/**
* Get a tuple.
* Returns an existing tuple from the cache, if present.
* Otherwise a new one is created.
*/
public Tuple getTuple(IOAVState state, Tuple left, Object right)
{
return new Tuple(state, left, right);
// Tuple ret;
// Map lefties = (Map)tuplememory.get(left);
// if(lefties==null)
// {
// lefties = new WeakHashMap();
// tuplememory.put(left, lefties);
// ret = new Tuple(left, right);
// lefties.put(right, ret);
// }
// else
// {
// ret = (Tuple)lefties.get(right);
// if(ret==null)
// {
// ret = new Tuple(left, right);
// lefties.put(right, ret);
// }
// }
// return ret;
}
//-------- methods --------
/**
* Get the total memory size
* @return The memory size.
*/
public int getTotalMemorySize()
{
int size = 0;
for(Iterator it=memory.values().iterator(); it.hasNext(); )
{
Object o = it.next();
if(o instanceof Collection)
{
size += ((Collection)o).size();
}
else if(o instanceof NotMemory)
{
size += ((NotMemory)o).size();
}
else if(o instanceof BetaMemory)
{
size += ((BetaMemory)o).size();
}
}
return size;
}
/**
* Get the string representation.
* @return The string representation.
*/
public String toString()
{
//return Srules.getInnerClassName(this.getClass())+"(values="+memory+")";
StringBuffer ret = new StringBuffer(SReflect.getInnerClassName(this.getClass()).toString());
ret.append(", size="+getTotalMemorySize());
ret.append(" : \n");
for(Iterator it=memory.keySet().iterator(); it.hasNext(); )
{
Object node = it.next();
ret.append(node).append(" : ");
ret.append(memory.get(node)).append("\n");
}
return ret.toString();
}
}