Search in sources :

Example 21 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class TestCompiledExpression method testFunctionCallWithNamedParams.

@Test
public void testFunctionCallWithNamedParams() throws ParseException {
    FunctionInterface testFunction = new AddTestFunction();
    FunctionDefinition fd = new FunctionDefinition(testFunction.getClass().getName(), "blib", "bleb", "blab");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("addtest", fd);
    String expression = "addtest(aap='blub',3+5,4)";
    StringReader sr = new StringReader(expression);
    CompiledParser cp = new CompiledParser(sr);
    cp.Expression();
    SimpleNode atn = (SimpleNode) cp.getJJTree().rootNode();
    List<String> problems = new ArrayList<>();
    ContextExpression ne = atn.interpretToLambda(problems, expression, fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
    Operand result = ne.apply();
    Assert.assertEquals("monkey", result.value);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) AddTestFunction(com.dexels.navajo.expression.compiled.AddTestFunction) Test(org.junit.Test)

Example 22 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class TestCompiledMoneyExpression method setup.

@Before
public void setup() {
    FunctionInterface giveLongFunction = new GiveLongTestFunction();
    FunctionDefinition fd = new FunctionDefinition(giveLongFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToLong", fd);
    FunctionInterface giveMoneyFunction = new GiveMoneyTestFunction();
    fd = new FunctionDefinition(giveMoneyFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToMoney", fd);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) GiveMoneyTestFunction(com.dexels.navajo.expression.compiled.GiveMoneyTestFunction) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) GiveLongTestFunction(com.dexels.navajo.expression.compiled.GiveLongTestFunction) Before(org.junit.Before)

Example 23 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class TestCompiledStringExpression method testSubtractStringAndLong.

@Test(expected = TMLExpressionException.class)
public void testSubtractStringAndLong() {
    FunctionInterface giveLongFunction = new GiveLongTestFunction();
    FunctionDefinition fd = new FunctionDefinition(giveLongFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToLong", fd);
    Expression.evaluate("'bla' - ToLong(10)", null, null, null);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) GiveLongTestFunction(com.dexels.navajo.expression.compiled.GiveLongTestFunction) Test(org.junit.Test)

Example 24 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class TestCompiledLongExpression method testMultiplyWithLongs.

@Test
public void testMultiplyWithLongs() {
    FunctionInterface helperFunction = new GiveLongTestFunction();
    FunctionDefinition fd = new FunctionDefinition(helperFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToLong", fd);
    Operand result;
    result = Expression.evaluate("ToLong(3) * ToLong(1)", null, null, null);
    Assert.assertEquals(3L, (long) result.value);
    result = Expression.evaluate("3 * ToLong(1)", null, null, null);
    Assert.assertEquals(3L, (long) result.value);
    result = Expression.evaluate("ToLong(3) * 1", null, null, null);
    Assert.assertEquals(3L, (long) result.value);
    result = Expression.evaluate("3.0 * ToLong(1)", null, null, null);
    Assert.assertEquals(3L, (double) result.value, 0.000001);
    result = Expression.evaluate("ToLong(3) * 1.0", null, null, null);
    Assert.assertEquals(3L, (double) result.value, 0.000001);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) Operand(com.dexels.navajo.document.Operand) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) GiveLongTestFunction(com.dexels.navajo.expression.compiled.GiveLongTestFunction) Test(org.junit.Test)

Example 25 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class AbstractCoreExtension method registerAllFunctions.

private void registerAllFunctions(ExtensionDefinition extensionDef) {
    FunctionFactoryInterface fi = FunctionFactoryFactory.getInstance();
    fi.init();
    fi.clearFunctionNames();
    fi.injectExtension(extensionDef);
    final Set<String> functionNames = fi.getFunctionNames(extensionDef);
    for (String functionName : functionNames) {
        FunctionDefinition fd = fi.getDef(extensionDef, functionName);
        registerFunction(context, fi, functionName, fd, extensionDef);
    }
}
Also used : FunctionFactoryInterface(com.dexels.navajo.functions.util.FunctionFactoryInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition)

Aggregations

FunctionDefinition (com.dexels.navajo.expression.api.FunctionDefinition)26 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)20 Test (org.junit.Test)13 GiveLongTestFunction (com.dexels.navajo.expression.compiled.GiveLongTestFunction)10 Operand (com.dexels.navajo.document.Operand)8 ArrayList (java.util.ArrayList)7 StringReader (java.io.StringReader)5 AddTestFunction (com.dexels.navajo.expression.compiled.AddTestFunction)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ServiceReference (org.osgi.framework.ServiceReference)4 XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)3 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)3 Before (org.junit.Before)2 BundleContext (org.osgi.framework.BundleContext)2 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 GiveMoneyTestFunction (com.dexels.navajo.expression.compiled.GiveMoneyTestFunction)1 GivePercentageTestFunction (com.dexels.navajo.expression.compiled.GivePercentageTestFunction)1 ParameterNamesFunction (com.dexels.navajo.expression.compiled.ParameterNamesFunction)1 FunctionFactoryInterface (com.dexels.navajo.functions.util.FunctionFactoryInterface)1