Search in sources :

Example 56 with Operand

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

the class SimpleNode method lazyFunction.

public ContextExpression lazyFunction(List<String> problems, String expression, UnaryOperator<Operand> func, Optional<String> requiredReturnType, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    if (requiredReturnType.isPresent() && expA.returnType().isPresent()) {
        String expectedType = requiredReturnType.get();
        String foundType = expA.returnType().get();
        if (!expectedType.equals(foundType)) {
            problems.add("Error (static) type checking. Type: " + foundType + " does not match expected type: " + expectedType);
        }
    }
    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);
            return func.apply(a);
        }

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

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

        @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 57 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, Message currentParam, Selection selection, Object tipiLink, Map<String, Object> params, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
    try {
        ExpressionCache ce = ExpressionCache.getInstance();
        Access access = params == null ? null : (Access) params.get(Expression.ACCESS);
        Operand val = ce.evaluate(clause, inMessage, parent, currentParam, selection, (MappableTreeNode) mappableTreeNode, (TipiLink) tipiLink, access, immutableMessage, paramMessage);
        if (val == null) {
            throw new TMLExpressionException("Clause resolved to null, shouldnt happen:  expression: " + clause);
        }
        return val;
    } 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(e.getMessage(), e);
    }
}
Also used : Operand(com.dexels.navajo.document.Operand) Access(com.dexels.navajo.script.api.Access) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 58 with Operand

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

the class ASTListNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    final List<ContextExpression> exprs = new ArrayList<>();
    boolean onlyImmutable = true;
    for (int i = 0; i < jjtGetNumChildren(); i++) {
        ContextExpression lmb = jjtGetChild(i).interpretToLambda(problems, expression, functionClassifier, mapResolver);
        exprs.add(lmb);
        if (!onlyImmutable && !lmb.isLiteral()) {
            onlyImmutable = false;
        }
    }
    final boolean onlyImm = onlyImmutable;
    return new ContextExpression() {

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

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            List<Operand> result = new ArrayList<>();
            for (ContextExpression contextExpression : exprs) {
                result.add(contextExpression.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage));
            }
            return Operand.ofList(result.stream().map(e -> e.value).collect(Collectors.toList()));
        }

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

        @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) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TipiLink(com.dexels.navajo.expression.api.TipiLink)

Example 59 with Operand

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

the class ReactiveResolvedParameters method resolveParam.

private Operand resolveParam(String key, Optional<String> expectedType, ContextExpression function) {
    Operand applied;
    try {
        // TODO move this to constructor or something
        Navajo in = this.resolvedInput != null ? this.resolvedInput : inputFlowable.isPresent() ? null : resolvedInput;
        applied = function.apply(in, currentMessage, Optional.of(paramMessage));
        resolvedNamed.put(key, applied);
        if (expectedType.isPresent()) {
            resolvedTypes.put(key, expectedType.get());
        }
        return applied;
    } catch (Exception e1) {
        throw new ReactiveParameterException("Error applying param function for named param: " + key, e1);
    }
}
Also used : Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo)

Example 60 with Operand

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

the class ASTAndNode 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);
    ContextExpression expB = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    Optional<String> expressionA = expA.returnType();
    checkOrAdd("In AND expression the first expression is not a boolean but a " + expressionA.orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
    Optional<String> expressionB = expB.returnType();
    checkOrAdd("In AND expression the second expression is not a boolean but a " + expressionB.orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
    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);
            if (a == null) {
                return Operand.ofBoolean(Boolean.FALSE);
            }
            Boolean ba = (Boolean) a.value;
            if (!(ba.booleanValue())) {
                return Operand.ofBoolean(Boolean.FALSE);
            }
            Operand b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
            if (b == null) {
                return Operand.ofBoolean(Boolean.FALSE);
            }
            Boolean value = (Boolean) b.value;
            return Operand.ofBoolean(value != null ? value : Boolean.FALSE);
        }

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

        @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) 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)

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