Search in sources :

Example 16 with Operand

use of com.dexels.navajo.document.Operand in project navajo by Dexels.

the class CachedExpressionEvaluator method evaluate.

@Override
public Operand evaluate(String clause, Navajo inMessage, Object mappableTreeNode, Message parent, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
    ExpressionCache ce = ExpressionCache.getInstance();
    Object val;
    String type;
    try {
        val = ce.evaluate(clause, inMessage, parent, null, null, (MappableTreeNode) mappableTreeNode, null, null, immutableMessage, paramMessage);
        type = MappingUtils.determineNavajoType(val);
        return new Operand(val, type, "");
    } catch (TMLExpressionException e) {
        if (inMessage != null) {
            // Only log if we have useful context
            logger.error("TML parsing issue with expression: {} exception", clause, e);
        }
        throw new TMLExpressionException("TML parsing issue");
    }
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Operand(com.dexels.navajo.document.Operand) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 17 with Operand

use of com.dexels.navajo.document.Operand in project navajo by Dexels.

the class ExpressionCache method evaluate.

public Operand evaluate(String expression, Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
    Optional<Operand> cachedValue = expressionValueCache.getUnchecked(expression);
    if (cachedValue.isPresent()) {
        pureHitCount.incrementAndGet();
        return cachedValue.get();
    }
    List<String> problems = new ArrayList<>();
    ContextExpression parse = parse(problems, expression, functionName -> FunctionClassification.DEFAULT, name -> Optional.empty());
    if (!problems.isEmpty()) {
        problems.forEach(problem -> logger.warn("Compile-time type error when compiling expression: {} -> {}", expression, problem));
    }
    return parse.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
}
Also used : Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList)

Example 18 with Operand

use of com.dexels.navajo.document.Operand in project navajo by Dexels.

the class BasePropertyImpl method getEvaluatedValue.

public final Object getEvaluatedValue() {
    Operand o;
    // No evaluator present.
    if (NavajoFactory.getInstance().getExpressionEvaluator() == null) {
        return null;
    }
    try {
        try {
            if (!EXPRESSION_PROPERTY.equals(getType())) {
                throw NavajoFactory.getInstance().createNavajoException("Can only evaluate expression type properties!");
            }
            try {
                o = NavajoFactory.getInstance().getExpressionEvaluator().evaluate(getValue(), getRootDoc(), null, getParentMessage(), null);
                evaluatedType = o.type;
                return o.value;
            } catch (Throwable e) {
                logger.info("Exception while evaluating property: {} expression: {}", getFullPropertyName(), getValue());
                return null;
            }
        } catch (NavajoException ex) {
            logger.error("Error: ", ex);
            if (myParent != null) {
                Message pp = myParent.getArrayParentMessage();
                if (pp != null && Message.MSG_TYPE_ARRAY.equals(pp.getType())) {
                    Message def = pp.getDefinitionMessage();
                    if (def != null) {
                        Property ppp = def.getProperty(getName());
                        if (ppp != null) {
                            evaluatedType = ppp.getType();
                            return null;
                        }
                    }
                }
            }
            evaluatedType = "string";
            return null;
        }
    } catch (Throwable ex1) {
        evaluatedType = "string";
        return null;
    }
}
Also used : Message(com.dexels.navajo.document.Message) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) Property(com.dexels.navajo.document.Property)

Example 19 with Operand

use of com.dexels.navajo.document.Operand in project navajo by Dexels.

the class TestCompiledExpression method testUnicodeExpression.

// Unicode(hex-string)
@Test
public void testUnicodeExpression() throws Exception {
    Operand result = Expression.evaluate("'耀'", null, null, null);
    Assert.assertEquals("耀", result.value);
}
Also used : Operand(com.dexels.navajo.document.Operand) Test(org.junit.Test)

Example 20 with Operand

use of com.dexels.navajo.document.Operand in project navajo by Dexels.

the class TestCompiledExpression method testDoubleComparison.

@Test
public void testDoubleComparison() {
    Operand result;
    result = Expression.evaluate(" 1.0 < 1.1 ", null, null, null);
    Assert.assertTrue((boolean) result.value);
    result = Expression.evaluate(" 1.0 < 1.0 ", null, null, null);
    Assert.assertFalse((boolean) result.value);
    result = Expression.evaluate(" 1.0 <= 1.1 ", null, null, null);
    Assert.assertTrue((boolean) result.value);
    result = Expression.evaluate(" 1.0 <= 1.0 ", null, null, null);
    Assert.assertTrue((boolean) result.value);
    result = Expression.evaluate(" 1.0 > 1.1 ", null, null, null);
    Assert.assertFalse((boolean) result.value);
    result = Expression.evaluate(" 1.0 > 1.0 ", null, null, null);
    Assert.assertFalse((boolean) result.value);
    result = Expression.evaluate(" 1.0 >= 1.1 ", null, null, null);
    Assert.assertFalse((boolean) result.value);
    result = Expression.evaluate(" 1.0 >= 1.0 ", null, null, null);
    Assert.assertTrue((boolean) result.value);
}
Also used : Operand(com.dexels.navajo.document.Operand) Test(org.junit.Test)

Aggregations

Operand (com.dexels.navajo.document.Operand)95 Test (org.junit.Test)57 Message (com.dexels.navajo.document.Message)22 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)20 Navajo (com.dexels.navajo.document.Navajo)20 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)15 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)14 ArrayList (java.util.ArrayList)12 Selection (com.dexels.navajo.document.Selection)11 Property (com.dexels.navajo.document.Property)10 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)10 Access (com.dexels.navajo.script.api.Access)10 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)10 NavajoException (com.dexels.navajo.document.NavajoException)9 TipiLink (com.dexels.navajo.expression.api.TipiLink)9 Optional (java.util.Optional)9 FunctionDefinition (com.dexels.navajo.expression.api.FunctionDefinition)8 GiveLongTestFunction (com.dexels.navajo.expression.compiled.GiveLongTestFunction)6 SystemException (com.dexels.navajo.script.api.SystemException)6 List (java.util.List)6