jadex.rules.rulesystem.rete.nodes.VirtualFact 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.state.OAVAttributeType;
import java.util.List;
/**
* A virtual fact represents a fact with some specified multislot
* variable bindings.
* The values of the virtual fact store the bindings for the sub variables,
* i.e. [var0] -> {val1, val2, ..}, [var1] -> {val3}, ...
*/
public class VirtualFact
{
//-------- attributes --------
/** The real fact (object). */
protected Object object;
/** The attribute. */
protected OAVAttributeType attr;
/** The values of the sub attributes. */
protected List values;
//-------- constructors --------
/**
* Create a new virtual fact.
*/
public VirtualFact(Object object, OAVAttributeType attr, List values)
{
this.object = object;
this.attr = attr;
this.values = values;
}
//-------- methods --------
/**
* Get the real fact.
* @return The real fact.
*/
public Object getObject()
{
Object ret = object;
while(ret instanceof VirtualFact)
ret = ((VirtualFact)ret).getObject();
return ret;
}
/**
* Get the attribute.
* @return The attribute.
*/
public OAVAttributeType getAttribute()
{
return attr;
}
/**
* Get the containing fact.
* @return The containing fact.
*/
public VirtualFact getSuperFact()
{
return object instanceof VirtualFact? (VirtualFact)object: null;
}
/**
* Get the value or values of a sub attribute.
* The subindex specifies the variable which should be accessed.
* Each virtual fact has bindings for every subvariable, i.e.
* the values contain for each variable its current values.
* @param attr The attribute.
* @param subindex The subindex.
*/
public Object getSubAttributeValue(OAVAttributeType attr, int subindex)
{
assert attr!=null;
Object ret;
if(attr.equals(getAttribute()))
{
ret = values.get(subindex);
}
else
{
ret = getSuperFact().getSubAttributeValue(attr, subindex);
}
return ret;
}
/**
* Get the string representation.
* @return The string representation.
*/
public String toString()
{
return SReflect.getInnerClassName(this.getClass())+
"object="+object+" attribute="+attr+", values="+values+")";
}
}