jadex.rules.parser.conditions.javagrammar.BuildContext 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.parser.conditions.javagrammar;
import jadex.rules.rulesystem.ICondition;
import jadex.rules.rulesystem.rules.AndCondition;
import jadex.rules.rulesystem.rules.ArraySelector;
import jadex.rules.rulesystem.rules.BoundConstraint;
import jadex.rules.rulesystem.rules.ConstrainableCondition;
import jadex.rules.rulesystem.rules.FunctionCall;
import jadex.rules.rulesystem.rules.IConstraint;
import jadex.rules.rulesystem.rules.IOperator;
import jadex.rules.rulesystem.rules.MethodCall;
import jadex.rules.rulesystem.rules.ObjectCondition;
import jadex.rules.rulesystem.rules.TestCondition;
import jadex.rules.rulesystem.rules.Variable;
import jadex.rules.rulesystem.rules.functions.OperatorFunction;
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.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* The build context captures knowledge about
* conditions, variables, etc. during constraint
* parsing or building.
*/
public class BuildContext
{
//-------- attributes --------
/** The list of conditions. */
protected List lcons;
/** The variables (name -> variable). */
protected Map variables;
/** The bound constraints (variable -> boundconstraint (only variable definitions, i.e. EQUAL constraints)). */
protected Map boundconstraints;
/** The object conditions (variable -> object conditions (object condition with defining bound constraint)). */
protected Map bcons;
/** The OAV type model. */
protected OAVTypeModel tmodel;
/** The dummy condition (if any). */
protected ObjectCondition dummy;
/** The parent build context (if any). */
protected BuildContext parent;
/** Stack for object conditions (for checking if constraints can be generated in current context). */
protected List oconstack;
//-------- constructors --------
/**
* Create a new build context.
* @param condition The initial condition.
*/
public BuildContext(ICondition condition, OAVTypeModel tmodel)
{
this.tmodel = tmodel;
this.lcons = new ArrayList();
this.variables = new HashMap();
this.boundconstraints = new HashMap();
this.bcons = new HashMap();
if(condition!=null)
addCondition(condition);
}
/**
* Create a new build context.
* @param parent The parent build context.
*/
public BuildContext(BuildContext parent)
{
this(null, parent.getTypeModel());
this.parent = parent;
}
//-------- methods --------
/**
* Get the conditions.
*/
public List getConditions()
{
return lcons;
}
/**
* Get the OAV type model.
*/
public OAVTypeModel getTypeModel()
{
return tmodel;
}
/**
* Get an object condition for a variable, i.e. a condition, where
* constraints related to the variable can be added to.
* @param var The variable
* @return The object condition.
* @throws RuntimeExcpetion when no condition was found.
*/
public ConstrainableCondition getConstrainableCondition(Variable var)
{
ConstrainableCondition ret = getConstrainableCondition0(var);
if(ret==null)
{
throw new RuntimeException("No object condition for: "+var);
}
return ret;
}
/**
* Get an object condition for a variable, i.e. a condition, where
* constraints related to the variable can be added to.
* @param var The variable
* @return The object condition.
* @throws RuntimeExcpetion when no condition was found.
*/
public ConstrainableCondition getConstrainableCondition0(Variable var)
{
ConstrainableCondition ret = null;
ret = parent!=null ? parent.getConstrainableCondition0(var) : null;
if(ret==null)
{
ret = (ObjectCondition)bcons.get(var);
}
return ret;
}
/**
* Get the bound constraint a variable, i.e. the value source required
* for obtaining the variable value from the variables object condition.
* @param var The variable
* @return The bound constraint.
* @throws RuntimeExcpetion when no constraint was found.
*/
public BoundConstraint getBoundConstraint(Variable var)
{
BoundConstraint ret = null;
try
{
ret = parent!=null ? parent.getBoundConstraint(var) : null;
}
catch(Exception e)
{
}
if(ret==null)
{
ret = (BoundConstraint)boundconstraints.get(var);
}
if(ret==null)
{
throw new RuntimeException("No bound constraint for: "+var);
}
return ret;
}
/**
* Create a new variable and bind it using the given object condition and value source.
* @param condition The object condition.
* @param valuesource The value source.
* @return The new variable.
*/
public Variable generateVariableBinding(ConstrainableCondition condition, Object valuesource)
{
return generateVariableBinding(condition, generateVariableName(), valuesource);
}
/**
* Generate a variable name.
* @return An unused variable name.
*/
public String generateVariableName()
{
String varname;
for(int i=1; getVariable(varname = "$tmpvar_"+i)!=null; i++);
return varname;
}
/**
* Create a new variable and bind it using the given object condition and value source.
* @param condition The object condition.
* @param name The variable name.
* @param valuesource The value source.
* @return The new variable.
*/
public Variable generateVariableBinding(ConstrainableCondition condition, String name, Object valuesource)
{
return generateVariableBinding(condition, name, getReturnType(condition, valuesource, tmodel), valuesource);
}
/**
* Create a new variable and bind it using the given object condition and value source.
* @param condition The object condition.
* @param name The variable name.
* @param valuesource The value source.
* @return The new variable.
*/
public Variable generateVariableBinding(ConstrainableCondition condition, String name, OAVObjectType type, Object valuesource)
{
Variable tmpvar = new Variable(name, type, false, true);
variables.put(name, tmpvar);
BoundConstraint bc = new BoundConstraint(valuesource, tmpvar);
boundconstraints.put(tmpvar, bc);
condition.addConstraint(bc);
bcons.put(tmpvar, condition);
return tmpvar;
}
/**
* Create a new object condition with the given constraints.
* Also adds mappings corresponding to bound constraints (if any).
* @param type The object type.
* @param constraints The constraints (if any).
*/
public ObjectCondition createObjectCondition(OAVObjectType type, IConstraint[] constraints)
{
ObjectCondition ocon = new ObjectCondition(type);
for(int i=0; constraints!=null && i1)
{
Class clazz = ((OAVJavaType)ret).getClazz();
for(int j=1; j