Search in sources :

Example 6 with AbstractFunction

use of com.googlecode.aviator.runtime.function.AbstractFunction 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 7 with AbstractFunction

use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.

the class OperatorFunctionTest method testCustomArrayIndexOperator.

@Test
public void testCustomArrayIndexOperator() {
    Map<String, Object> env = new HashMap<>();
    env.put("a", Arrays.asList(4, 5, 6));
    assertEquals(4, this.instance.execute("a[0]", env));
    try {
        assertEquals(6, this.instance.execute("a[3]", env));
        fail();
    } catch (Exception e) {
    }
    this.instance.addOpFunction(OperatorType.INDEX, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            return arg1.getElement(env, arg2.sub(AviatorLong.valueOf(1), env));
        }

        @Override
        public String getName() {
            return "[]";
        }
    });
    assertEquals(6, this.instance.execute("a[3]", env));
    assertEquals(5, this.instance.execute("a[2]", env));
    assertEquals(4, this.instance.execute("a[1]", env));
    try {
        assertEquals(4, this.instance.execute("a[0]", env));
        fail();
    } catch (Exception e) {
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) HashMap(java.util.HashMap) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 8 with AbstractFunction

use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.

the class OperatorFunctionTest method testCustomBinOperatorFunction.

@Test
public void testCustomBinOperatorFunction() {
    try {
        assertEquals("hello world", this.instance.execute("'hello' & ' world'"));
        fail();
    } catch (Exception e) {
    }
    this.instance.addOpFunction(OperatorType.BIT_AND, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            return new AviatorString(arg1.getValue(env).toString() + arg2.getValue(env).toString());
        }

        @Override
        public String getName() {
            return "&";
        }
    });
    assertEquals("43", this.instance.exec("a&3", 4));
    assertEquals("43", this.instance.exec("4&3", 4));
    assertEquals("hello world", this.instance.execute("'hello' & ' world'"));
    assertEquals("hello world", this.instance.exec("a&' world'", "hello"));
    assertEquals("hello3 world", this.instance.exec("a & 3 & ' world'", "hello"));
    Map<String, Object> env = new HashMap<>();
    env.put("list", Arrays.asList(1, 2, 3));
    assertEquals("123", this.instance.execute("reduce(list, &, '')", env));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Aggregations

AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)8 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)8 Test (org.junit.Test)6 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)5 HashMap (java.util.HashMap)2 CompareNotSupportedException (com.googlecode.aviator.exception.CompareNotSupportedException)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)1 Map (java.util.Map)1