Search in sources :

Example 16 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class OperationRuntime method eval.

/**
 * Eval with unary operator
 *
 * @param arg
 * @param env
 * @param opType
 * @return
 */
public static AviatorObject eval(final AviatorObject arg, final Map<String, Object> env, final OperatorType opType) {
    AviatorFunction func = RuntimeUtils.getInstance(env).getOpFunction(opType);
    AviatorObject ret = eval0(arg, env, opType, func);
    if (RuntimeUtils.isTracedEval(env)) {
        trace(env, opType, ret, arg);
    }
    return ret;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 17 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method setOption.

/**
 * Adds a evaluator option
 *
 * @since 2.3.4
 * @see Options
 * @param opt
 * @param val
 */
public void setOption(final Options opt, final Object val) {
    if (opt == null || val == null) {
        throw new IllegalArgumentException("Option and value should not be null.");
    }
    if (!opt.isValidValue(val)) {
        throw new IllegalArgumentException("Invalid value for option:" + opt.name());
    }
    Map<Options, Value> newOpts = new IdentityHashMap<>(this.options);
    newOpts.put(opt, opt.intoValue(val));
    if (opt == Options.FEATURE_SET) {
        Set<Feature> oldSet = new HashSet<>(getFeatures());
        @SuppressWarnings("unchecked") Set<Feature> newSet = (Set<Feature>) val;
        if (oldSet.removeAll(newSet)) {
            // removed functions that feature is disabled.
            for (Feature feat : oldSet) {
                for (AviatorFunction fn : feat.getFunctions()) {
                    this.removeFunction(fn);
                }
            }
        }
    }
    this.options = newOpts;
    if (opt == Options.FEATURE_SET) {
        loadFeatureFunctions();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IdentityHashMap(java.util.IdentityHashMap) Value(com.googlecode.aviator.Options.Value) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) HashSet(java.util.HashSet)

Example 18 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method defineFunction.

/**
 * Define a function by name and expression with the execution env.
 *
 * @param name the function name
 * @param expression the expression to be executed and it's result must be a function.
 * @param env the expression execution env
 * @since 4.0.0
 */
public void defineFunction(final String name, final String expression, final Map<String, Object> env) {
    AviatorFunction function = (AviatorFunction) this.execute(expression, env);
    this.addFunction(name, function);
}
Also used : AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

Example 19 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method loadInternalFunctions.

private Map<String, AviatorFunction> loadInternalFunctions() {
    Map<String, AviatorFunction> funcs = new HashMap<>();
    for (String lib : libs) {
        try (final InputStream in = this.getClass().getResourceAsStream("/" + lib);
            final BufferedInputStream bis = new BufferedInputStream(in);
            final Reader reader = new InputStreamReader(bis)) {
            Expression exp = this.compile(lib, Utils.readFully(reader), false);
            Map<String, Object> exports = executeModule(exp, lib);
            for (Map.Entry<String, Object> entry : exports.entrySet()) {
                if (entry.getValue() instanceof AviatorFunction) {
                    final AviatorFunction fn = (AviatorFunction) entry.getValue();
                    addFunction(entry.getKey(), fn);
                    funcs.put(entry.getKey(), fn);
                }
            }
        } catch (IOException e) {
            throw new IllegalStateException("Fail to load internal lib: " + lib, e);
        }
    }
    return funcs;
}
Also used : InputStreamReader(java.io.InputStreamReader) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LRUMap(com.googlecode.aviator.utils.LRUMap)

Example 20 with AviatorFunction

use of com.googlecode.aviator.runtime.type.AviatorFunction in project aviatorscript by killme2008.

the class ClassPathConfigFunctionLoader method loadClass.

private AviatorFunction loadClass(String className) {
    info("Loading custom aviator function class: '" + className + "'.");
    try {
        @SuppressWarnings("unchecked") Class<AviatorFunction> clazz = (Class<AviatorFunction>) Class.forName(className);
        AviatorFunction func = clazz.newInstance();
        if (func != null) {
            totalCustomFunctions++;
        }
        return func;
    } catch (Throwable e) {
        error("Load custom aviator function class: " + className + "' failed with error:" + e.getMessage() + ".");
    }
    return null;
}
Also used : AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction)

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