Search in sources :

Example 11 with ContextExpression

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

the class SimpleNode method lazyBiFunction.

public ContextExpression lazyBiFunction(List<String> problems, String expression, BinaryOperator<Operand> func, BiFunction<Optional<String>, Optional<String>, Boolean> acceptTypes, BiFunction<Optional<String>, Optional<String>, Optional<String>> returnTypeResolver, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    ContextExpression expB = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    Optional<String> aType = expA.returnType();
    Optional<String> bType = expB.returnType();
    boolean inputTypesValid = acceptTypes.apply(aType, bType);
    if (!inputTypesValid) {
        problems.add("Invalid input types in node: " + aType.orElse("unknown") + " and " + bType.orElse("unknown") + " in node type: " + this.getClass());
    }
    Optional<String> returnType = returnTypeResolver.apply(aType, bType);
    return new ContextExpression() {

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            Operand a = expA.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
            Operand b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
            return func.apply(a, b);
        }

        @Override
        public boolean isLiteral() {
            return expA.isLiteral() && expB.isLiteral();
        }

        @Override
        public Optional<String> returnType() {
            return returnType;
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) Operand(com.dexels.navajo.document.Operand) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo)

Example 12 with ContextExpression

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

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

the class ReactiveResolvedParameters method paramValue.

private Optional<Operand> paramValue(String key) {
    if (resolvedNamed.containsKey(key)) {
        return Optional.ofNullable(resolvedNamed.get(key));
    }
    Optional<String> expectedType = expectedTypes.isPresent() ? Optional.ofNullable(expectedTypes.get().get(key)) : Optional.empty();
    ContextExpression function = named.get(key);
    if (function == null) {
        return Optional.empty();
    }
    return Optional.of(resolveParam(key, expectedType, function));
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression)

Example 14 with ContextExpression

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

the class ASTMappableNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    Optional<Node> resolved = mapResolver.apply(val);
    if (resolved.isPresent()) {
        return resolved.get().interpretToLambda(problems, expression, functionClassifier, mapResolver);
    }
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return false;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            if (mapNode == null) {
                throw new TMLExpressionException("No known mapobject resolver");
            }
            // MappableTreeNode mapNode = mapNodeResolver.apply(val);
            // if(mapNode==null) {
            // throw new TMLExpressionException("No known mapobject");
            // 
            // }
            // if(mapNode==null) {
            // Node resolvedItem = mapResolver.get().apply(val);
            // System.err.println(">> "+resolvedItem.getClass());
            // resolvedItem.interpretToLambda(problems, expression, functionClassifier, mapResolver);
            // 
            // }
            List objects = null;
            // Parameter array may contain parameters that are used when calling the get method.
            Object[] parameterArray = null;
            if (args > 0) {
                objects = new ArrayList();
            }
            for (int i = 0; i < args; i++) {
                List<String> problems = new ArrayList<>();
                Operand a = jjtGetChild(i).interpretToLambda(problems, expression, functionClassifier, mapResolver).apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
                if (!problems.isEmpty()) {
                    throw new TMLExpressionException(problems, expression);
                }
                if (objects != null) {
                    objects.add(a.value);
                }
            }
            // List<String> problems
            if (objects != null) {
                parameterArray = new Object[objects.size()];
                parameterArray = objects.toArray(parameterArray);
            }
            try {
                Object oValue = maybeGetMapAttribute(val, mapNode, parameterArray);
                if (oValue == null)
                    return Operand.NULL;
                else if (oValue instanceof Float) {
                    return Operand.ofFloat(((Float) oValue).doubleValue());
                } else if (oValue instanceof Long) {
                    return Operand.ofLong(((Long) oValue).longValue());
                } else {
                    return Operand.ofDynamic(oValue);
                }
            } catch (Exception me) {
                throw new TMLExpressionException(me.getMessage(), me);
            }
        }

        @Override
        public Optional<String> returnType() {
            return Optional.empty();
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) Operand(com.dexels.navajo.document.Operand) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TipiLink(com.dexels.navajo.expression.api.TipiLink) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with ContextExpression

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

the class ASTOrNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Or expression failed, first expression is not a boolean but a " + expA.returnType().orElse("<unknown>"), problems, expA.returnType(), Property.BOOLEAN_PROPERTY);
    ContextExpression expB = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Or expression failed, second expression is not a boolean but a " + expB.returnType().orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return expA.isLiteral() && expB.isLiteral();
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            Object a = expA.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value;
            Boolean ba = (Boolean) a;
            if (a == null) {
                ba = Boolean.FALSE;
            }
            if (ba.booleanValue())
                return Operand.ofBoolean(true);
            Object b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value;
            Boolean bb = (Boolean) b;
            if (b == null) {
                bb = Boolean.FALSE;
            }
            return Operand.ofBoolean(ba.booleanValue() || bb.booleanValue());
        }

        @Override
        public Optional<String> returnType() {
            return Optional.of(Property.BOOLEAN_PROPERTY);
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo)

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