use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class ClassPathConfigFunctionLoader method load.
/**
* Load custom functions from config file, default is "aviator_functions.config" in classpath.
*
* @return
*/
private Map<String, AviatorFunction> load() {
InputStream in = null;
InputStreamReader inreader = null;
BufferedReader reader = null;
Map<String, AviatorFunction> ret = new HashMap<String, AviatorFunction>();
try {
in = ClassPathConfigFunctionLoader.class.getClassLoader().getResourceAsStream(CUSTOM_FUNCTION_LIST_FILE);
if (in != null) {
inreader = new InputStreamReader(in);
reader = new BufferedReader(inreader);
String line = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#")) {
// skip comment
continue;
}
if (line.length() > 0) {
AviatorFunction func = loadClass(line);
if (func != null) {
ret.put(func.getName(), func);
}
}
}
}
} catch (Throwable e) {
error("Load aviator custom functions config from " + CUSTOM_FUNCTION_LIST_FILE + " failed.");
e.printStackTrace();
} finally {
closeQuietly(reader);
closeQuietly(inreader);
closeQuietly(in);
if (totalCustomFunctions > 0) {
info("Total " + totalCustomFunctions + " custom functions loaded.");
}
}
return ret;
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class SendIR method eval.
@Override
public void eval(final InterpretContext context) {
AviatorFunction fn = null;
if (this.name != null) {
fn = RuntimeUtils.getFunction(context.getEnv(), this.name);
}
int i = this.arity;
AviatorObject[] args = new AviatorObject[this.arity];
while (--i >= 0) {
args[i] = context.pop();
}
if (this.name == null) {
fn = (AviatorFunction) context.pop();
}
if (RuntimeUtils.isTracedEval(context.getEnv())) {
fn = TraceFunction.wrapTrace(fn);
}
if (this.unpackArgs) {
fn = RuntimeUtils.unpackArgsFunction(fn);
}
if (this.funcId >= 0) {
// put function arguments ref id to env.
context.getEnv().put(ASMCodeGenerator.FUNC_ARGS_INNER_VAR, this.funcId);
}
context.push(callFn(fn, args, this.arity, context.getEnv()));
context.dispatch();
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class SeqMakePredicateFunctionUnitTest method testMakePredicate_FixedValue.
@Test
public void testMakePredicate_FixedValue() {
SeqMakePredicateFunFunction fun = new SeqMakePredicateFunFunction("eq", OperatorType.EQ, AviatorRuntimeJavaType.valueOf("hello"));
Map<String, Object> env = new HashMap<String, Object>();
AviatorObject predicateName = fun.call(env);
assertNotNull(predicateName);
AviatorFunction predicate = (AviatorFunction) predicateName.getValue(env);
assertNotNull(predicate);
AviatorObject result = predicate.call(null, AviatorRuntimeJavaType.valueOf("hello"));
// equals self
assertTrue(result.booleanValue(null));
result = predicate.call(null, AviatorRuntimeJavaType.valueOf("he11o"));
// equals self
assertFalse(result.booleanValue(null));
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class SeqCompsitePredFunFunction method createFunction.
private static AviatorFunction createFunction(final Map<String, Object> env, final AviatorObject[] args, final LogicOp op) {
return new AbstractFunction() {
private static final long serialVersionUID = -3935448162130773442L;
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
switch(op) {
case AND:
boolean ret = true;
for (AviatorObject obj : args) {
AviatorFunction fn = FunctionUtils.getFunction(obj, env, 1);
if (fn == null)
throw new IllegalArgumentException("Expect " + obj.desc(env) + " as a function.");
ret = fn.call(env, arg1) == AviatorBoolean.TRUE;
if (!ret)
break;
}
return AviatorBoolean.valueOf(ret);
case OR:
ret = false;
for (AviatorObject obj : args) {
AviatorFunction fn = FunctionUtils.getFunction(obj, env, 1);
if (fn == null)
throw new IllegalArgumentException("Expect " + obj.desc(env) + " as a function.");
ret = fn.call(env, arg1) == AviatorBoolean.TRUE;
if (ret)
break;
}
return AviatorBoolean.valueOf(ret);
}
return AviatorBoolean.FALSE;
}
@Override
public String getName() {
return op == LogicOp.AND ? "seq.and" : "seq.or";
}
};
}
use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.
the class SeqFilterFunction method call.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object first = arg1.getValue(env);
AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
if (fun == null) {
throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
}
if (first == null) {
return AviatorNil.NIL;
}
Sequence seq = RuntimeUtils.seq(first, env);
Collector collector = seq.newCollector(0);
for (Object obj : seq) {
if (fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
collector.add(obj);
}
}
return AviatorRuntimeJavaType.valueOf(collector.getRawContainer());
}
Aggregations