Search in sources :

Example 1 with AbstractFunction

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

the class FunctionTest method testOverloadLogicOperator.

@Test
public void testOverloadLogicOperator() {
    // instance.setOption(Options.TRACE_EVAL, true);
    this.instance.addOpFunction(OperatorType.AND, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            return arg1.add(arg2, env);
        }

        @Override
        public String getName() {
            return "&&";
        }
    });
    this.instance.addOpFunction(OperatorType.OR, new AbstractFunction() {

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

        @Override
        public String getName() {
            return "||";
        }
    });
    assertEquals(3, this.instance.execute("1 && 2"));
    assertEquals(6, this.instance.execute("1 && 2 && 3"));
    assertEquals(0, this.instance.execute("1 && 2 || 3"));
    assertEquals(-4, this.instance.execute("1 || 2 || 3"));
    this.instance.removeOpFunction(OperatorType.AND);
    this.instance.removeOpFunction(OperatorType.OR);
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Test(org.junit.Test)

Example 2 with AbstractFunction

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

the class GrammarUnitTest method testReturnNullCustomFunction.

@Test
public void testReturnNullCustomFunction() {
    this.instance.addFunction(new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
            try {
                return AviatorLong.valueOf(FunctionUtils.getJavaObject(arg1, env));
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public String getName() {
            return "SafeCastLong";
        }
    });
    assertNull(this.instance.execute("SafeCastLong('a')"));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) CompareNotSupportedException(com.googlecode.aviator.exception.CompareNotSupportedException) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) ExpressionSyntaxErrorException(com.googlecode.aviator.exception.ExpressionSyntaxErrorException) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) Test(org.junit.Test)

Example 3 with AbstractFunction

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

the class OperatorFunctionTest method testCustomUnaryOperatorFunction.

@Test
public void testCustomUnaryOperatorFunction() {
    try {
        assertEquals("3", this.instance.exec("!3"));
        fail();
    } catch (Exception e) {
    }
    this.instance.addOpFunction(OperatorType.NOT, new AbstractFunction() {

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

        @Override
        public String getName() {
            return "!";
        }
    });
    assertEquals("3", this.instance.exec("!3"));
    assertEquals("4", this.instance.exec("!a", 4));
    assertEquals("3.2", this.instance.exec("!3.2"));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) 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)

Example 4 with AbstractFunction

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

the class AviatorEvaluatorInstanceUnitTest method testIssue245.

@Test
public void testIssue245() {
    // test EQ
    this.instance.addOpFunction(OperatorType.EQ, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            if (!arg1.getValue(env).equals(arg2.getValue(env))) {
                return AviatorBoolean.TRUE;
            }
            return AviatorBoolean.FALSE;
        }

        @Override
        public String getName() {
            return OperatorType.EQ.getToken();
        }
    });
    assertFalse((boolean) this.instance.execute("1==1"));
    assertTrue((boolean) this.instance.execute("0==1"));
    assertFalse((boolean) this.instance.execute("a==b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
    assertTrue((boolean) this.instance.execute("a==b", AviatorEvaluator.newEnv("a", 1, "b", 0)));
    // test Assignment
    assertEquals(1, this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
    assertEquals(2, this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 2)));
    this.instance.addOpFunction(OperatorType.ASSIGNMENT, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            int c = arg1.compare(arg2, env);
            return AviatorBoolean.valueOf(c == 0);
        }

        @Override
        public String getName() {
            return OperatorType.ASSIGNMENT.getToken();
        }
    });
    assertTrue((boolean) this.instance.execute("1=1"));
    assertFalse((boolean) this.instance.execute("0=1"));
    assertTrue((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 1)));
    assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 0)));
    assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", 1, "b", 2)));
    assertFalse((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", "hello", "b", "world")));
    assertTrue((boolean) this.instance.execute("a=b", AviatorEvaluator.newEnv("a", "hello", "b", "hello")));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 5 with AbstractFunction

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

the class CustomDivideExample method main.

public static void main(final String[] args) {
    AviatorEvaluator.getInstance().addOpFunction(OperatorType.DIV, new AbstractFunction() {

        @Override
        public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
            if (arg1.getAviatorType() == AviatorType.Long && arg2.getAviatorType() == AviatorType.Long) {
                // If arg1 and arg2 are all long type.
                // Cast arg2 into double and divided by arg1.
                double d = FunctionUtils.getNumberValue(arg1, env).longValue() / FunctionUtils.getNumberValue(arg2, env).doubleValue();
                return AviatorDouble.valueOf(d);
            } else {
                // Otherwise, call aviatorscript's div function.
                return arg1.div(arg2, env);
            }
        }

        @Override
        public String getName() {
            return OperatorType.DIV.getToken();
        }
    });
    System.out.println(AviatorEvaluator.execute("1/2"));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

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