Search in sources :

Example 1 with Selection

use of com.dexels.navajo.document.Selection 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 Selection

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

the class ASTDatePatternNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression y = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Year (item 0) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression m = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Month (item 1) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression d = jjtGetChild(2).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Day (item 2) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression h = jjtGetChild(3).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Hour (item 3) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression min = jjtGetChild(4).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Minute (item 4) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression s = jjtGetChild(5).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Second (item 5) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    final boolean isLiteral = y.isLiteral() && m.isLiteral() && d.isLiteral() && h.isLiteral() && min.isLiteral() && s.isLiteral();
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return 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) {
            int yearT = ((Integer) y.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int monthT = ((Integer) m.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int dayT = ((Integer) d.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int hourT = ((Integer) h.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int minT = ((Integer) min.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int secT = ((Integer) s.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            return Operand.ofDatePattern(new DatePattern(yearT, monthT, dayT, hourT, minT, secT, true));
        }

        @Override
        public Optional<String> returnType() {
            return Optional.of(Property.DATE_PATTERN_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) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TipiLink(com.dexels.navajo.expression.api.TipiLink) DatePattern(com.dexels.navajo.document.types.DatePattern)

Example 3 with Selection

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

the class ASTForAllNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> 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) {
            List<String> problems = new ArrayList<>();
            ContextExpression a = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
            ContextExpression b = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
            if (!problems.isEmpty()) {
                throw new TMLExpressionException(problems, expression);
            }
            return interpret(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage, a, b);
        }

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

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

Example 4 with Selection

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

the class SaxHandler method parseProperty.

private final void parseProperty(Map<String, String> h) throws NavajoException {
    // logger.info("NAME: "+(String)h.get("name"));
    String sLength = null;
    String myName = h.get(Property.PROPERTY_NAME);
    String myValue = h.get(Property.PROPERTY_VALUE);
    String subType = h.get(Property.PROPERTY_SUBTYPE);
    String description = h.get(Property.PROPERTY_DESCRIPTION);
    String direction = h.get(Property.PROPERTY_DIRECTION);
    String type = h.get(Property.PROPERTY_TYPE);
    sLength = h.get(Property.PROPERTY_LENGTH);
    String cardinality = h.get(Property.PROPERTY_CARDINALITY);
    String extendsProp = h.get(Property.PROPERTY_EXTENDS);
    String reference = h.get(Property.PROPERTY_REFERENCE);
    String key = h.get(Property.PROPERTY_KEY);
    String bind = h.get(Property.PROPERTY_BIND);
    String method = h.get(Property.PROPERTY_METHOD);
    Integer plength = null;
    Property definitionProperty = null;
    String subtype = null;
    int length = 0;
    try {
        if (sLength != null) {
            length = Integer.parseInt(sLength);
            plength = Integer.valueOf(length);
        }
    } catch (Exception e1) {
    // logger.info("ILLEGAL LENGTH IN PROPERTY " + myName + ": " +
    // sLength);
    }
    if (myName == null) {
        throw NavajoFactory.getInstance().createNavajoException("Can not parse property without a name ");
    }
    boolean isListType = (type != null && type.equals(Property.SELECTION_PROPERTY));
    if (isListType) {
        currentProperty = (BasePropertyImpl) NavajoFactory.getInstance().createProperty(currentDocument, myName, cardinality, description, direction);
    } else {
        currentProperty = (BasePropertyImpl) NavajoFactory.getInstance().createProperty(currentDocument, myName, type, myValue, length, description, direction, subtype);
    }
    if (messageStack.isEmpty()) {
        throw NavajoFactory.getInstance().createNavajoException("Can not parse property without being inside a message, probably an input error");
    }
    Message current = messageStack.peek();
    current.addProperty(currentProperty);
    BaseMessageImpl arrayParent = (BaseMessageImpl) current.getArrayParentMessage();
    if (arrayParent != null && arrayParent.isArrayMessage()) {
        definitionProperty = arrayParent.getPropertyDefinition(myName);
        if (definitionProperty != null) {
            if (description == null || "".equals(description)) {
                description = definitionProperty.getDescription();
            }
            if (direction == null || "".equals(direction)) {
                direction = definitionProperty.getDirection();
            }
            if (type == null || "".equals(type)) {
                type = definitionProperty.getType();
            }
            if (plength == null) {
                length = definitionProperty.getLength();
            }
            if (subType == null) {
                if (definitionProperty.getSubType() != null) {
                    currentProperty.setSubType(definitionProperty.getSubType());
                }
            } else {
                if (definitionProperty.getSubType() != null) {
                    /**
                     * Concatenated subtypes. The if the same key of a subtype is present
                     * in both the property and the definition property.
                     */
                    currentProperty.setSubType(definitionProperty.getSubType() + "," + subType);
                }
            }
        }
    }
    if (subType == null && NavajoFactory.getInstance().getDefaultSubtypeForType(type) != null) {
        currentProperty.setSubType(NavajoFactory.getInstance().getDefaultSubtypeForType(type));
    } else {
        currentProperty.setSubType(subType);
    }
    if (type == null && current.isArrayMessage()) {
        logger.info("Found undefined property: " + currentProperty.getName());
    }
    isListType = (type != null && type.equals(Property.SELECTION_PROPERTY));
    if (isListType && definitionProperty != null) {
        if (cardinality == null) {
            cardinality = definitionProperty.getCardinality();
        }
        type = Property.SELECTION_PROPERTY;
        List<Selection> l = definitionProperty.getAllSelections();
        for (int i = 0; i < l.size(); i++) {
            BaseSelectionImpl s1 = (BaseSelectionImpl) l.get(i);
            BaseSelectionImpl s2 = (BaseSelectionImpl) s1.copy(currentDocument);
            currentProperty.addSelection(s2);
        }
    }
    currentProperty.setType(type);
    currentProperty.setDescription(description);
    currentProperty.setDirection(direction);
    currentProperty.setCardinality(cardinality);
    currentProperty.setLength(length);
    currentProperty.setExtends(extendsProp);
    currentProperty.setKey(key);
    currentProperty.setReference(reference);
    currentProperty.setBind(bind);
    currentProperty.setMethod(method);
}
Also used : BaseMessageImpl(com.dexels.navajo.document.base.BaseMessageImpl) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) BaseSelectionImpl(com.dexels.navajo.document.base.BaseSelectionImpl) Property(com.dexels.navajo.document.Property) NavajoException(com.dexels.navajo.document.NavajoException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 5 with Selection

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

the class SaxHandler method parseSelection.

private final void parseSelection(Map<String, String> h) throws NavajoException {
    String name = h.get("name");
    String value = h.get("value");
    int selected = Integer.parseInt(h.get("selected"));
    Property definitionProperty = null;
    BaseMessageImpl arrayParent = (BaseMessageImpl) currentProperty.getParentMessage().getArrayParentMessage();
    if (arrayParent != null) {
        definitionProperty = arrayParent.getPropertyDefinition(currentProperty.getName());
    }
    if (definitionProperty == null || definitionProperty.getSelected().getName().equals(Selection.DUMMY_SELECTION) || currentProperty.getParentMessage().getType().equals(Message.MSG_TYPE_DEFINITION)) {
        Selection s = NavajoFactory.getInstance().createSelection(currentDocument, name, value, selected != 0);
        currentProperty.addSelection(s);
    } else {
        if (currentProperty.getSelectionByValue(value) != null && selected == 1) {
            currentProperty.setSelectedByValue(value);
        } else {
            Selection s = NavajoFactory.getInstance().createSelection(currentDocument, name, value, selected != 0);
            currentProperty.addSelection(s);
        }
    }
}
Also used : BaseMessageImpl(com.dexels.navajo.document.base.BaseMessageImpl) Selection(com.dexels.navajo.document.Selection) Property(com.dexels.navajo.document.Property)

Aggregations

Selection (com.dexels.navajo.document.Selection)78 Property (com.dexels.navajo.document.Property)36 Test (org.junit.Test)32 Message (com.dexels.navajo.document.Message)24 Navajo (com.dexels.navajo.document.Navajo)21 Access (com.dexels.navajo.script.api.Access)14 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)13 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)12 TipiLink (com.dexels.navajo.expression.api.TipiLink)12 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)12 ArrayList (java.util.ArrayList)12 Optional (java.util.Optional)12 Operand (com.dexels.navajo.document.Operand)11 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)11 List (java.util.List)8 NavajoException (com.dexels.navajo.document.NavajoException)6 UserException (com.dexels.navajo.script.api.UserException)4 Binary (com.dexels.navajo.document.types.Binary)3 FunctionClassification (com.dexels.navajo.expression.api.FunctionClassification)3 StringTokenizer (java.util.StringTokenizer)3