Search in sources :

Example 16 with ImmutableMessage

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

the class ExpressionCache method parse.

public ContextExpression parse(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    Optional<ContextExpression> cachedParsedExpression = exprCache.getUnchecked(expression);
    if (cachedParsedExpression.isPresent()) {
        hitCount.incrementAndGet();
        return cachedParsedExpression.get();
    }
    CompiledParser cp;
    try {
        StringReader sr = new StringReader(expression);
        cp = new CompiledParser(sr);
        cp.Expression();
        ContextExpression parsed = cp.getJJTree().rootNode().interpretToLambda(problems, expression, functionClassifier, mapResolver);
        parsedCount.incrementAndGet();
        if (parsed.isLiteral()) {
            Operand result = parsed.apply();
            exprCache.put(expression, Optional.ofNullable(parsed));
            if (result != null) {
                expressionValueCache.put(expression, Optional.of(result));
            }
            return new ContextExpression() {

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

                @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 result;
                }

                @Override
                public Optional<String> returnType() {
                    return Optional.ofNullable(result.type);
                }

                @Override
                public String expression() {
                    return expression;
                }
            };
        } else {
            exprCache.put(expression, Optional.ofNullable(parsed));
            return parsed;
        }
    } catch (ParseException e) {
        throw new TMLExpressionException("Error parsing expression: " + expression, e);
    } catch (Throwable e) {
        throw new TMLExpressionException("Unexpected error parsing expression: " + expression, e);
    }
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Optional(java.util.Optional) Operand(com.dexels.navajo.document.Operand) 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) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) CompiledParser(com.dexels.navajo.parser.compiled.CompiledParser) TipiLink(com.dexels.navajo.expression.api.TipiLink) StringReader(java.io.StringReader) ParseException(com.dexels.navajo.parser.compiled.ParseException)

Example 17 with ImmutableMessage

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

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

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

the class StreamDocument method toImmutable.

public static ImmutableMessage toImmutable(Message m) {
    Map<String, ImmutableMessage> subMessages = null;
    Map<String, List<ImmutableMessage>> subMessageLists = null;
    // Map<S>
    for (Message msg : m.getAllMessages()) {
        if (msg.isArrayMessage()) {
            if (subMessageLists == null) {
                subMessageLists = new HashMap<>();
            }
            subMessageLists.put(msg.getName(), toImmutableList(msg));
        } else {
            if (subMessages == null) {
                subMessages = new HashMap<>();
            }
            subMessages.put(msg.getName(), toImmutable(m));
        }
    }
    if (subMessageLists == null) {
        subMessageLists = Collections.emptyMap();
    }
    if (subMessages == null) {
        subMessages = Collections.emptyMap();
    }
    Map<String, Object> values = new HashMap<>();
    Map<String, String> types = new HashMap<>();
    for (Property item : m.getAllProperties()) {
        String name = item.getName();
        values.put(name, item.getTypedValue());
        types.put(name, item.getType());
    }
    return ImmutableFactory.create(values, types, subMessages, subMessageLists);
// n.getAllMessages()
}
Also used : Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) HashMap(java.util.HashMap) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) List(java.util.List) Property(com.dexels.navajo.document.Property)

Example 20 with ImmutableMessage

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

the class StreamDocument method replicationToMessage.

public static Message replicationToMessage(ImmutableMessage msg, String name, boolean isArrayElement) {
    Navajo n = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(n, name, isArrayElement ? Message.MSG_TYPE_ARRAY_ELEMENT : Message.MSG_TYPE_SIMPLE);
    List<Property> pp = msg.columnNames().stream().map(e -> {
        String type = msg.columnType(e);
        Optional<Object> value = msg.value(e);
        Property p = NavajoFactory.getInstance().createProperty(n, e, type, "", 0, "", Property.DIR_OUT);
        if (value.isPresent()) {
            p.setAnyValue(value.get());
        }
        return p;
    }).collect(Collectors.toList());
    pp.stream().forEach(p -> m.addProperty(p));
    msg.subMessageListMap().forEach((msgName, submessages) -> {
        Message subArray = NavajoFactory.getInstance().createMessage(n, msgName, Message.MSG_TYPE_ARRAY_ELEMENT);
        submessages.forEach(repl -> {
            subArray.addElement(replicationToMessage(repl, msgName, true));
        });
        m.addMessage(subArray);
    });
    msg.subMessageMap().forEach((msgName, subMessage) -> m.addMessage(replicationToMessage(subMessage, msgName, false)));
    return m;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) FlowableTransformer(io.reactivex.FlowableTransformer) Map(java.util.Map) Events(com.dexels.navajo.document.stream.events.Events) Navajo(com.dexels.navajo.document.Navajo) FlowableOperator(io.reactivex.FlowableOperator) CharBuffer(java.nio.CharBuffer) Message(com.dexels.navajo.document.Message) CharsetDecoder(java.nio.charset.CharsetDecoder) Collectors(java.util.stream.Collectors) NavajoFactory(com.dexels.navajo.document.NavajoFactory) List(java.util.List) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) FlowableSubscriber(io.reactivex.FlowableSubscriber) Disposable(io.reactivex.disposables.Disposable) ObservableTransformer(io.reactivex.ObservableTransformer) SingleTransformer(io.reactivex.SingleTransformer) BaseFlowableOperator(com.dexels.navajo.document.stream.io.BaseFlowableOperator) Optional(java.util.Optional) BackpressureAdministrator(com.dexels.navajo.document.stream.base.BackpressureAdministrator) Property(com.dexels.navajo.document.Property) Binary(com.dexels.navajo.document.types.Binary) CharacterCodingException(java.nio.charset.CharacterCodingException) Method(com.dexels.navajo.document.stream.api.Method) HashMap(java.util.HashMap) Function(java.util.function.Function) Stack(java.util.Stack) CoderResult(java.nio.charset.CoderResult) Charset(java.nio.charset.Charset) Flowable(io.reactivex.Flowable) NavajoEventTypes(com.dexels.navajo.document.stream.events.NavajoStreamEvent.NavajoEventTypes) Msg(com.dexels.navajo.document.stream.api.Msg) ObservableNavajoParser(com.dexels.navajo.document.stream.xml.ObservableNavajoParser) Observable(io.reactivex.Observable) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Prop(com.dexels.navajo.document.stream.api.Prop) Subscriber(org.reactivestreams.Subscriber) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) Logger(org.slf4j.Logger) ObservableOperator(io.reactivex.ObservableOperator) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Consumer(io.reactivex.functions.Consumer) File(java.io.File) ImmutableFactory(com.dexels.immutable.factory.ImmutableFactory) AbstractMap(java.util.AbstractMap) Subscription(org.reactivestreams.Subscription) Observer(io.reactivex.Observer) NavajoHead(com.dexels.navajo.document.stream.api.NavajoHead) Bytes(com.github.davidmoten.rx2.Bytes) Collections(java.util.Collections) InputStream(java.io.InputStream) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

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