Search in sources :

Example 31 with ContextExpression

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

the class TestCompiledExpression method testParseTml.

@Test
public void testParseTml() throws ParseException, TMLExpressionException {
    String expression = "[/TestMessage/TestProperty]";
    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);
    }
    Assert.assertEquals("TestValue", ss.apply(input, Optional.empty(), Optional.empty()).value);
    Assert.assertFalse(ss.isLiteral());
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Test(org.junit.Test)

Example 32 with ContextExpression

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

the class TestCompiledExpression method testNestedFunctionType.

@Test
public void testNestedFunctionType() throws TMLExpressionException {
    List<String> problems = new ArrayList<>();
    ContextExpression o = ExpressionCache.getInstance().parse(problems, "ToUpper(ToLower('Bob'))", name -> FunctionClassification.DEFAULT);
    System.err.println("problems: " + problems);
    System.err.println("returntype: " + o.returnType().orElse("<unknown>"));
    Assert.assertTrue("Expected a return type here", o.returnType().isPresent());
    Assert.assertEquals("string", o.returnType().get());
    System.err.println("immutable: " + o.isLiteral());
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with ContextExpression

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

the class TestCompiledExpression method testFunctionEvaluation.

@Test
public void testFunctionEvaluation() {
    List<String> problems = new ArrayList<>();
    ExpressionCache ce = ExpressionCache.getInstance();
    String embeddedExpression = "StringFunction('matches','aaa', '.*[-]+7[0-9][A-z]*$' ))";
    ContextExpression cemb = ce.parse(problems, embeddedExpression, name -> FunctionClassification.DEFAULT);
    System.err.println("Problems: " + problems);
    Assert.assertEquals(0, problems.size());
    Operand result = cemb.apply(input, Optional.empty(), Optional.empty());
    System.err.println("Result: " + result.type + " value: " + result.value);
}
Also used : Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) ExpressionCache(com.dexels.navajo.parser.compiled.api.ExpressionCache) Test(org.junit.Test)

Example 34 with ContextExpression

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

the class TestCompiledExpression method testImmutablilityPropagationPerformance.

@Test
public void testImmutablilityPropagationPerformance() throws TMLExpressionException {
    CacheSubexpression.setCacheSubExpression(true);
    ;
    List<String> problems = new ArrayList<>();
    ContextExpression o = ExpressionCache.getInstance().parse(problems, "ToUpper(ToLower('Bob'))", name -> FunctionClassification.DEFAULT);
    long now = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
        o.apply();
    // .value;
    // System.err.println("tr:" +tr);
    }
    System.err.println("Now: " + (System.currentTimeMillis() - now));
    System.err.println("problems: " + problems);
    System.err.println("immutable: " + o.isLiteral());
    Assert.assertTrue(o.isLiteral());
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 35 with ContextExpression

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

the class TestCompiledExpression method testForall.

@Test
public void testForall() throws TMLExpressionException {
    List<String> problems = new ArrayList<>();
    ExpressionCache ce = ExpressionCache.getInstance();
    // String extBackup ="FORALL( '/TestArrayMessageMessage', `! ?[Delete] OR ! [Delete] OR [/__globals__/ApplicationInstance] != 'TENANT' OR ! StringFunction( 'matches', [ChargeCodeId], '.*[-]+7[0-9][A-z]*$' )` )";
    String ext = "FORALL( '/Msg', `!StringFunction( 'matches', [ChargeCodeId], '.*[-]+7[0-9][A-z]*$' )` )";
    // String expression = "FORALL( '/Charges' , `! ?[Delete] OR ! [Delete]  OR [/__globals__/ApplicationInstance] != 'SOMETENANT' OR [SomeProperty] == 'SOMESTRING' `)";
    // ce.parse(problems, expression, mode, allowLiteralResolve)
    ContextExpression cx = ce.parse(problems, ext, name -> FunctionClassification.DEFAULT);
    System.err.println("Problems: " + problems);
    Assert.assertEquals(0, problems.size());
    Operand result = cx.apply(input, Optional.empty(), Optional.empty());
    System.err.println("Result: " + result.type + " value: " + result.value);
// Operand result = Expression.evaluate(expression, testDoc,null,topMessage);
}
Also used : Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) ExpressionCache(com.dexels.navajo.parser.compiled.api.ExpressionCache) 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