Search in sources :

Example 26 with ContextExpression

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

the class TestTypeCheckCompiledExpressionEvaluator method testBadType.

@Test
public void testBadType() {
    // test unknowable type
    ExpressionCache ce = ExpressionCache.getInstance();
    List<String> problems = new ArrayList<>();
    // the [] expression has been added to prevent the expression to be pre-evaluated (as all other params are literals
    ContextExpression cx = ce.parse(problems, "1 AND 'monkey' AND [whoop]", fn -> FunctionClassification.DEFAULT);
    System.err.println("Problems: " + problems);
    Assert.assertEquals(2, problems.size());
    Assert.assertEquals(Property.BOOLEAN_PROPERTY, cx.returnType().get());
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) ExpressionCache(com.dexels.navajo.parser.compiled.api.ExpressionCache) Test(org.junit.Test)

Example 27 with ContextExpression

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

the class TestTypeCheckCompiledExpressionEvaluator method testBasicTypes.

@Test
public void testBasicTypes() {
    ExpressionCache ce = ExpressionCache.getInstance();
    List<String> problems = new ArrayList<>();
    ContextExpression cx = ce.parse(problems, "1", fn -> FunctionClassification.DEFAULT);
    Assert.assertEquals(Property.INTEGER_PROPERTY, cx.returnType().get());
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) ExpressionCache(com.dexels.navajo.parser.compiled.api.ExpressionCache) Test(org.junit.Test)

Example 28 with ContextExpression

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

the class TestCompiledExpression method testParseTmlComplex.

@Test
public void testParseTmlComplex() throws ParseException, TMLExpressionException {
    String expression = "[TestArrayMessageMessage@Property=Prop2/Property]";
    StringReader sr = new StringReader(expression);
    CompiledParser cp = new CompiledParser(sr);
    cp.Expression();
    List<String> problems = new ArrayList<>();
    ContextExpression ss = cp.getJJTree().rootNode().interpretToLambda(problems, sr.toString(), fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
    if (!problems.isEmpty()) {
        throw new TMLExpressionException(problems, expression);
    }
    Navajo copy = NavajoFactory.getInstance().createNavajo();
    Message rootMessage = NavajoFactory.getInstance().createMessage(copy, "TopMessage");
    copy.addMessage(rootMessage);
    rootMessage.addMessage(input.getMessage("TestArrayMessageMessage"));
    Object value = ss.apply(copy, rootMessage, null, null, null, null, null, Optional.empty(), Optional.empty()).value;
    Assert.assertEquals("Prop2", value);
    Assert.assertFalse(ss.isLiteral());
}
Also used : Message(com.dexels.navajo.document.Message) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Test(org.junit.Test)

Example 29 with ContextExpression

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

the class TestCompiledExpression method testEmptyFunctionCall.

@Test
public void testEmptyFunctionCall() throws ParseException {
    FunctionInterface testFunction = new AddTestFunction();
    FunctionDefinition fd = new FunctionDefinition(testFunction.getClass().getName(), "blib", "bleb", "blab");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("addtest", fd);
    String expression = "addtest()";
    StringReader sr = new StringReader(expression);
    CompiledParser cp = new CompiledParser(sr);
    cp.Expression();
    List<String> problems = new ArrayList<>();
    ContextExpression ss = cp.getJJTree().rootNode().interpretToLambda(problems, sr.toString(), fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
    Operand o = ss.apply();
    Assert.assertEquals("monkey", o.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 30 with ContextExpression

use of com.dexels.navajo.expression.api.ContextExpression 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)

Aggregations

ContextExpression (com.dexels.navajo.expression.api.ContextExpression)36 ArrayList (java.util.ArrayList)25 Test (org.junit.Test)18 Operand (com.dexels.navajo.document.Operand)14 Message (com.dexels.navajo.document.Message)13 Navajo (com.dexels.navajo.document.Navajo)13 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)13 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)12 Selection (com.dexels.navajo.document.Selection)12 TipiLink (com.dexels.navajo.expression.api.TipiLink)12 Access (com.dexels.navajo.script.api.Access)12 Optional (java.util.Optional)12 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)9 ExpressionCache (com.dexels.navajo.parser.compiled.api.ExpressionCache)7 StringReader (java.io.StringReader)7 FunctionClassification (com.dexels.navajo.expression.api.FunctionClassification)4 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)4 NamedExpression (com.dexels.navajo.parser.NamedExpression)4 List (java.util.List)4 Function (java.util.function.Function)4