Search in sources :

Example 21 with AviatorFunction

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 22 with AviatorFunction

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();
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 23 with AviatorFunction

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));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Example 24 with AviatorFunction

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";
        }
    };
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) Map(java.util.Map)

Example 25 with AviatorFunction

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());
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Collector(com.googlecode.aviator.runtime.type.Collector) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Sequence(com.googlecode.aviator.runtime.type.Sequence)

Aggregations

AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)16 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 Map (java.util.Map)5 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 IdentityHashMap (java.util.IdentityHashMap)2 FunctionMap (org.casbin.jcasbin.model.FunctionMap)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 Expression (com.googlecode.aviator.Expression)1 Value (com.googlecode.aviator.Options.Value)1 OperatorIR (com.googlecode.aviator.code.interpreter.ir.OperatorIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 OperatorType (com.googlecode.aviator.lexer.token.OperatorType)1