Search in sources :

Example 26 with ImmutableMessage

use of com.dexels.immutable.api.ImmutableMessage 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)

Example 27 with ImmutableMessage

use of com.dexels.immutable.api.ImmutableMessage in project navajo by Dexels.

the class ASTFunctionNode method resolveNormalFunction.

private ContextExpression resolveNormalFunction(List<ContextExpression> l, Map<String, ContextExpression> named, List<String> problems, String expression) {
    FunctionInterface typeCheckInstance = getFunction();
    if (typeCheckInstance == null) {
        throw new NullPointerException("Function: " + functionName + " can not be resolved!");
    }
    try {
        List<String> typeProblems = typeCheckInstance.typeCheck(l, expression);
        if (!typeProblems.isEmpty() && RuntimeConfig.STRICT_TYPECHECK.getValue() != null) {
            problems.addAll(typeProblems);
        }
    } catch (Throwable e2) {
        typechecklogger.error("Typechecker itself failed when parsing: " + expression + " function definition: " + typeCheckInstance + " Error: ", e2);
    }
    boolean isImmutable = typeCheckInstance.isPure() && l.stream().allMatch(e -> e.isLiteral());
    ContextExpression dynamic = new ContextExpression() {

        @Override
        public boolean isLiteral() {
            // TODO also check named params
            return isImmutable;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            FunctionInterface f = getFunction();
            Map<String, Operand> resolvedNamed = named.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage)));
            f.setInMessage(doc);
            f.setNamedParameter(resolvedNamed);
            f.setCurrentMessage(parentMsg);
            f.setAccess(access);
            f.reset();
            l.stream().map(e -> {
                try {
                    Operand evaluated = e.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
                    if (evaluated == null) {
                        logger.warn("Problematic expression returned null object. If you really insist, return an Operand.NULL. Evaluating expression: {}", expression);
                    }
                    return evaluated;
                } catch (TMLExpressionException e1) {
                    throw new TMLExpressionException("Error parsing parameters for function: " + functionName, e1);
                }
            }).forEach(e -> f.insertOperand(e));
            return f.evaluateWithTypeCheckingOperand();
        }

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

        @Override
        public String expression() {
            return expression;
        }
    };
    if (isImmutable && CacheSubexpression.getCacheSubExpression()) {
        Optional<String> returnType = dynamic.returnType();
        String immutablExpression = dynamic.expression();
        Operand resolved = dynamic.apply();
        return new ContextExpression() {

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

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

            @Override
            public String expression() {
                return immutablExpression;
            }

            @Override
            public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
                return resolved;
            }
        };
    } else {
        return dynamic;
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) Map(java.util.Map) OSGiFunctionFactoryFactory(com.dexels.navajo.functions.util.OSGiFunctionFactoryFactory) LinkedList(java.util.LinkedList) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) ReactiveParseItem(com.dexels.navajo.parser.compiled.api.ReactiveParseItem) Navajo(com.dexels.navajo.document.Navajo) DispatcherFactory(com.dexels.navajo.server.DispatcherFactory) TipiLink(com.dexels.navajo.expression.api.TipiLink) Logger(org.slf4j.Logger) FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) AbstractVersion(com.dexels.navajo.version.AbstractVersion) FunctionFactoryFactory(com.dexels.navajo.functions.util.FunctionFactoryFactory) Access(com.dexels.navajo.script.api.Access) RuntimeConfig(com.dexels.navajo.runtime.config.RuntimeConfig) Message(com.dexels.navajo.document.Message) Reactive(com.dexels.navajo.reactive.api.Reactive) Collectors(java.util.stream.Collectors) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) List(java.util.List) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) NamedExpression(com.dexels.navajo.parser.NamedExpression) CacheSubexpression(com.dexels.navajo.parser.compiled.api.CacheSubexpression) Optional(java.util.Optional) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) 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) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TipiLink(com.dexels.navajo.expression.api.TipiLink)

Example 28 with ImmutableMessage

use of com.dexels.immutable.api.ImmutableMessage in project navajo by Dexels.

the class ExpressionTest method testEmptySlashTMLParam.

@Test
public void testEmptySlashTMLParam() throws Exception {
    Expression.compileExpressions = true;
    ImmutableMessage outer = ImmutableFactory.empty().with("outerint", 1, "integer");
    Operand o = Expression.evaluateImmutable("[/@]", null, Optional.empty(), Optional.of(outer));
    ImmutableMessage s = o.immutableMessageValue();
    assertEquals(outer, s);
}
Also used : Operand(com.dexels.navajo.document.Operand) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Test(org.junit.Test)

Example 29 with ImmutableMessage

use of com.dexels.immutable.api.ImmutableMessage in project navajo by Dexels.

the class ExpressionTest method testEmptyTMLJustSlash.

@Test
public void testEmptyTMLJustSlash() throws Exception {
    Expression.compileExpressions = true;
    ImmutableMessage outer = ImmutableFactory.empty().with("outerint", 1, "integer");
    Operand o = Expression.evaluateImmutable("[/]", null, Optional.of(outer), Optional.empty());
    ImmutableMessage s = o.immutableMessageValue();
    assertEquals(outer, s);
}
Also used : Operand(com.dexels.navajo.document.Operand) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Test(org.junit.Test)

Example 30 with ImmutableMessage

use of com.dexels.immutable.api.ImmutableMessage in project navajo by Dexels.

the class ExpressionTest method testImmutableTMLPath.

@Test
public void testImmutableTMLPath() throws Exception {
    Expression.compileExpressions = true;
    ImmutableMessage outer = ImmutableFactory.empty().with("outerint", 1, "integer");
    ImmutableMessage inner = ImmutableFactory.empty().with("innerint", 3, "integer");
    ImmutableMessage combined = outer.withSubMessage("sub", inner);
    Operand o = Expression.evaluateImmutable("[sub/innerint]", null, Optional.of(combined), Optional.empty());
    int s = o.integerValue();
    assertEquals(3, s);
}
Also used : Operand(com.dexels.navajo.document.Operand) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Test(org.junit.Test)

Aggregations

ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)36 Message (com.dexels.navajo.document.Message)23 Navajo (com.dexels.navajo.document.Navajo)18 Operand (com.dexels.navajo.document.Operand)17 Optional (java.util.Optional)17 Selection (com.dexels.navajo.document.Selection)12 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)12 TipiLink (com.dexels.navajo.expression.api.TipiLink)12 Access (com.dexels.navajo.script.api.Access)12 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)12 Test (org.junit.Test)12 List (java.util.List)9 Property (com.dexels.navajo.document.Property)7 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)6 HashMap (java.util.HashMap)6 ImmutableFactory (com.dexels.immutable.factory.ImmutableFactory)5 ArrayList (java.util.ArrayList)5 DataItem (com.dexels.navajo.document.stream.DataItem)4 StreamScriptContext (com.dexels.navajo.document.stream.api.StreamScriptContext)4 ReactiveParameters (com.dexels.navajo.reactive.api.ReactiveParameters)4