jadex.rules.parser.conditions.SConditions 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;
import jadex.commons.SReflect;
import jadex.rules.rulesystem.rules.FunctionCall;
import jadex.rules.rulesystem.rules.MethodCall;
import jadex.rules.rulesystem.rules.Variable;
import jadex.rules.state.OAVAttributeType;
import jadex.rules.state.OAVJavaType;
import jadex.rules.state.OAVObjectType;
import jadex.rules.state.OAVTypeModel;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.antlr.runtime.TokenStream;
/**
* Static helper methods for conditions parser.
*/
public class SConditions
{
/**
*
* @param var
* @param type
*/
protected static void adaptConditionType(Variable var, OAVObjectType type)
{
if(type==null)
throw new RuntimeException("Type must not be null.");
OAVObjectType otype = var.getType();
// System.out.println("Having: "+otype+" "+type);
if(otype==null)
{
var.setType(type);
}
else if(!otype.equals(type))
{
// Check compatibility and use most specific type
if(otype instanceof OAVJavaType)
{
try
{
Class oclazz = ((OAVJavaType)otype).getClazz();
Class clazz = ((OAVJavaType)type).getClazz();
if(oclazz.isAssignableFrom(clazz))
{
var.setType(type);
//System.out.println("Setting: "+type);
}
else if(!clazz.isAssignableFrom(oclazz))
throw new RuntimeException("Incompatible variable types: "+var+" "+oclazz+" "+clazz);
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
if(type.isSubtype(otype))
{
var.setType(type);
//System.out.println("Setting: "+type);
}
else if(!otype.isSubtype(type))
throw new RuntimeException("Incompatible variable types: "+var+" "+otype+" "+type);
}
}
}
/**
*
* @param otype
* @param name
* @param params
* @return
*/
protected static MethodCall createMethodCall(OAVObjectType otype, String name, List params)
{
if(!(otype instanceof OAVJavaType))
{
throw new RuntimeException("Method calls only supported for java types: "+otype+"."+name+params);
}
OAVJavaType jtype = (OAVJavaType)otype;
Class clazz = jtype.getClazz();
Method[] methods = SReflect.getMethods(clazz, name);
Method method = null;
// Find one matching regardless of param types (hack???).
boolean found = false;
for(int i=0; i1)
{
System.out.println("Warning: ambiguous methods: "+otype+"."+name+params);
}
method = methods[results[0]];
}
return new MethodCall(jtype, method, params);
}
/**
*
* @param tmodel
* @param valuesource
* @return
*/
protected static OAVObjectType getValueSourceType(OAVTypeModel tmodel, Object valuesource)
{
OAVObjectType ret = null;
if(valuesource instanceof OAVAttributeType)
{
ret = ((OAVAttributeType)valuesource).getType();
}
else if(valuesource instanceof MethodCall)
{
Class rettype = ((MethodCall)valuesource).getMethod().getReturnType();
if(rettype!=null)
ret = tmodel.getJavaType(rettype);
}
else if(valuesource instanceof FunctionCall)
{
Class rettype = ((FunctionCall)valuesource).getFunction().getReturnType();
if(rettype!=null)
ret = tmodel.getJavaType(rettype);
}
return ret;
}
/**
* Lookahead for an object condition.
* Excludes (and ...) (not ...) (test ...)
* conditions but allows (test.MyObject ...) object conditions.
*/
protected static boolean lookaheadObjectCE(TokenStream input)
{
try
{
boolean ret = !"(".equals(input.LT(1).getText())
&& (!"collect".equals(input.LT(5).getText()) || ".".equals(input.LT(6).getText()))
|| "(".equals(input.LT(1).getText()) && (!"and".equals(input.LT(2).getText())
&& !"not".equals(input.LT(2).getText())
&& !"collect".equals(input.LT(2).getText())
&& !"test".equals(input.LT(2).getText()))
|| "(".equals(input.LT(1).getText()) && ".".equals(input.LT(3).getText());
// System.out.println("lookahead ObjectCE: "+ret+", "
// +input.toString(input.index(), input.size())
// +"'"+input.LT(1).getText()+"' "
// +"'"+input.LT(2).getText()+"' "
// +"'"+input.LT(3).getText()+"'");
return ret;
}
catch(Throwable e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* Lookahead for an function call.
* Excludes (contains ...) (excludes ...)
* conditions but allows (contains.MyObject ...) function calls.
*/
protected static boolean lookaheadFunctionCall(TokenStream input)
{
boolean ret = (!"contains".equals(input.LT(2).getText())
&& !"excludes".equals(input.LT(2).getText())
&& !"==".equals(input.LT(2).getText())
&& !"!=".equals(input.LT(2).getText())
&& !">".equals(input.LT(2).getText())
&& !"<".equals(input.LT(2).getText())
&& !">=".equals(input.LT(2).getText())
&& !"<=".equals(input.LT(2).getText()))
|| ".".equals(input.LT(3).getText());
// System.out.println("lookahead ObjectCE: "+ret+", "
// +input.toString(input.index(), input.size())
// +"'"+input.LT(1).getText()+"' "
// +"'"+input.LT(2).getText()+"' "
// +"'"+input.LT(3).getText()+"'");
return ret;
}
/**
* Convert slot name(s) to attribute(s) types.
* @param otype The object type.
* @param slotname The slotname string.
* @return The attribute type or an array of attribute types.
*/
protected static Object convertAttributeTypes(OAVTypeModel tmodel, OAVObjectType otype, String slotname, String[] imports)
{
Object ret;
// Value source is null for this.
if(slotname.toLowerCase().equals("this"))
{
ret = null;
}
else if(slotname.indexOf(".")!=-1)
{
StringTokenizer stok = new StringTokenizer(slotname, ".");
List aret = new ArrayList();
int num = stok.countTokens();
for(int i=0; i