jadex.rules.rulesystem.rete.nodes.CollectMemory 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 java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* Memory for the collect node.
*/
public class CollectMemory
{
//-------- attributes --------
/** The working memory (index tuple -> result tuple). */
protected Map workingmem;
/** The result memory. */
protected Set resultmem;
//-------- methods --------
/**
* Add a tuple to the result.
* @param tuple The result node.
* @return True, if could be added.
*/
public boolean addResultTuple(Tuple tuple)
{
if(resultmem==null)
resultmem = new LinkedHashSet();
return resultmem.add(tuple);
}
/**
* Remove from the result.
* @param tuple The tuple.
* @return True, if could be removed.
*/
public boolean removeResultTuple(Tuple tuple)
{
return resultmem==null || resultmem.remove(tuple);
}
/**
* Test if tuple is contained in result memory.
* @param tuple The tuple.
* @return True, if contained.
*/
public boolean resultMemoryContains(Tuple tuple)
{
return resultmem==null? false: resultmem.contains(tuple);
}
/**
* Get the result memory.
* @return The result memory.
*/
public Collection getResultMemory()
{
return resultmem;
}
/**
* Add a tuple to the result.
* @param tuple The result node.
* @return True, if could be added.
*/
public void putWorkingTuple(Tuple key, Tuple result)
{
if(workingmem==null)
workingmem = new LinkedHashMap();
workingmem.put(key, result);
}
/**
* Add a tuple to the result.
* @param tuple The result node.
* @return True, if could be added.
*/
public Tuple getWorkingTuple(Tuple key)
{
return workingmem==null? null: (Tuple)workingmem.get(key);
}
/**
* Remove from the result.
* @param tuple The tuple.
* @return True, if could be removed.
*/
public void removeWorkingTuple(Tuple key)
{
if(workingmem!=null)
workingmem.remove(key);
}
/**
* Get the result memory.
* @return The result memory.
*/
public Map getWorkingMemory()
{
return workingmem;
}
/**
* Get the size of the beta memory (including indexed memories).
* @return The size of the memory.
*/
public int size()
{
return resultmem!=null? resultmem.size(): 0;
}
/**
* Get the string representation.
* @return The string representation.
*/
public String toString()
{
StringBuffer ret = new StringBuffer();
ret.append(SReflect.getInnerClassName(this.getClass()));
ret.append("(resultmem=");
ret.append(resultmem);
ret.append(", workingmem=");
ret.append(workingmem);
ret.append(")");
return ret.toString();
}
}