Search in sources :

Example 1 with NamedExpression

use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.

the class ASTReactiveScriptNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String originalExpression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    int start = hasHeader ? headers : 0;
    if (hasHeader) {
        for (int i = 0; i < headers; i++) {
            ASTKeyValueNode hdr = (ASTKeyValueNode) jjtGetChild(i);
            NamedExpression ne = (NamedExpression) hdr.interpretToLambda(problems, originalExpression, functionClassifier, mapResolver);
            String key = ne.name;
            headerMap.put(key, ne.apply());
        }
    }
    int count = jjtGetNumChildren();
    List<Node> unnamedPipes = new ArrayList<>();
    Map<String, Node> namedPipes = new HashMap<>();
    for (int i = start; i < count; i++) {
        Node child = jjtGetChild(i);
        // ASTReactivePipe pipe = null;
        if (child instanceof ASTPipeDefinition) {
            unnamedPipes.add((ASTPipeDefinition) child);
        } else if (child instanceof ASTKeyValueNode) {
            ASTKeyValueNode kvNode = (ASTKeyValueNode) child;
            String streamName = kvNode.val;
            Node namedPipe = kvNode.jjtGetChild(0);
            // assert value types perhaps? TODO
            namedPipes.put(streamName, namedPipe);
        }
    }
    List<ReactivePipeNode> pipes = unnamedPipes.stream().map(p -> (ReactivePipeNode) p.interpretToLambda(problems, originalExpression, functionClassifier, name -> {
        Optional<Node> initial = Optional.ofNullable(namedPipes.get(name));
        if (initial.isPresent()) {
            return initial;
        } else {
            return mapResolver.apply(name);
        }
    })).collect(Collectors.toList());
    return new ContextExpression() {

        @Override
        public Optional<String> returnType() {
            return Optional.of(Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
        }

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

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

        @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 Operand.ofCustom(pipes, Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
        }
    };
}
Also used : TipiLink(com.dexels.navajo.expression.api.TipiLink) Arrays(java.util.Arrays) Access(com.dexels.navajo.script.api.Access) Message(com.dexels.navajo.document.Message) Reactive(com.dexels.navajo.reactive.api.Reactive) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) List(java.util.List) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) NamedExpression(com.dexels.navajo.parser.NamedExpression) ReactivePipeNode(com.dexels.navajo.parser.compiled.api.ReactivePipeNode) Map(java.util.Map) Optional(java.util.Optional) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Collections(java.util.Collections) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Navajo(com.dexels.navajo.document.Navajo) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Optional(java.util.Optional) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) HashMap(java.util.HashMap) Selection(com.dexels.navajo.document.Selection) ReactivePipeNode(com.dexels.navajo.parser.compiled.api.ReactivePipeNode) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) ReactivePipeNode(com.dexels.navajo.parser.compiled.api.ReactivePipeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) NamedExpression(com.dexels.navajo.parser.NamedExpression)

Example 2 with NamedExpression

use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.

the class ASTFunctionNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    List<ContextExpression> unnamed = new LinkedList<>();
    // TODO make lazy?
    Map<String, ContextExpression> named = new HashMap<>();
    for (int i = 0; i < jjtGetNumChildren(); i++) {
        Node sn = jjtGetChild(i);
        ContextExpression cn = sn.interpretToLambda(problems, expression, functionClassifier, mapResolver);
        if (cn instanceof NamedExpression) {
            NamedExpression ne = (NamedExpression) cn;
            named.put(ne.name, ne.expression);
        } else {
            unnamed.add(cn);
        }
    }
    FunctionClassification mode = functionClassifier.apply(functionName);
    switch(mode) {
        case REACTIVE_HEADER:
            break;
        case REACTIVE_SOURCE:
            return new ReactiveParseItem(functionName, Reactive.ReactiveItemType.REACTIVE_SOURCE, named, unnamed, expression, this);
        case REACTIVE_TRANSFORMER:
            return new ReactiveParseItem(functionName, Reactive.ReactiveItemType.REACTIVE_TRANSFORMER, named, unnamed, expression, this);
        case REACTIVE_REDUCER:
            return new ReactiveParseItem(functionName, Reactive.ReactiveItemType.REACTIVE_MAPPER, named, unnamed, expression, this);
        case DEFAULT:
        default:
    }
    return resolveNormalFunction(unnamed, named, problems, expression);
}
Also used : HashMap(java.util.HashMap) ReactiveParseItem(com.dexels.navajo.parser.compiled.api.ReactiveParseItem) NamedExpression(com.dexels.navajo.parser.NamedExpression) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) LinkedList(java.util.LinkedList)

Example 3 with NamedExpression

use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.

the class ASTKeyValueNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String originalExpression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    int num = jjtGetNumChildren();
    if (num != 1) {
        problems.add("Incorrect # of params in named parameter");
    }
    ContextExpression exp = jjtGetChild(0).interpretToLambda(problems, originalExpression, functionClassifier, mapResolver);
    return new NamedExpression(val, exp);
}
Also used : NamedExpression(com.dexels.navajo.parser.NamedExpression) ContextExpression(com.dexels.navajo.expression.api.ContextExpression)

Example 4 with NamedExpression

use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.

the class TestCompiledExpression method testNamedExpression.

@Test
public void testNamedExpression() throws ParseException {
    String expression = "aap=1+1";
    StringReader sr = new StringReader(expression);
    CompiledParser cp = new CompiledParser(sr);
    cp.KeyValue();
    ASTKeyValueNode atn = (ASTKeyValueNode) cp.getJJTree().rootNode();
    List<String> problems = new ArrayList<>();
    NamedExpression ne = (NamedExpression) atn.interpretToLambda(problems, expression, fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
    Assert.assertEquals(0, problems.size());
    Assert.assertEquals("aap", ne.name);
    Assert.assertEquals(2, ne.apply().value);
}
Also used : FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) ParameterNamesFunction(com.dexels.navajo.expression.compiled.ParameterNamesFunction) ArrayList(java.util.ArrayList) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo) Before(org.junit.Before) FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) ExpressionCache(com.dexels.navajo.parser.compiled.api.ExpressionCache) FunctionFactoryFactory(com.dexels.navajo.functions.util.FunctionFactoryFactory) Message(com.dexels.navajo.document.Message) Test(org.junit.Test) Expression(com.dexels.navajo.parser.Expression) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) NavajoFactory(com.dexels.navajo.document.NavajoFactory) List(java.util.List) StringReader(java.io.StringReader) Ignore(org.junit.Ignore) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) NamedExpression(com.dexels.navajo.parser.NamedExpression) SystemException(com.dexels.navajo.script.api.SystemException) Optional(java.util.Optional) Property(com.dexels.navajo.document.Property) Assert(org.junit.Assert) AddTestFunction(com.dexels.navajo.expression.compiled.AddTestFunction) NamedExpression(com.dexels.navajo.parser.NamedExpression) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ContextExpression (com.dexels.navajo.expression.api.ContextExpression)4 NamedExpression (com.dexels.navajo.parser.NamedExpression)4 FunctionClassification (com.dexels.navajo.expression.api.FunctionClassification)3 Message (com.dexels.navajo.document.Message)2 Navajo (com.dexels.navajo.document.Navajo)2 Operand (com.dexels.navajo.document.Operand)2 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)1 NavajoFactory (com.dexels.navajo.document.NavajoFactory)1 Property (com.dexels.navajo.document.Property)1 Selection (com.dexels.navajo.document.Selection)1 FunctionDefinition (com.dexels.navajo.expression.api.FunctionDefinition)1 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 TipiLink (com.dexels.navajo.expression.api.TipiLink)1 AddTestFunction (com.dexels.navajo.expression.compiled.AddTestFunction)1