Search in sources :

Example 1 with Operand

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

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

the class ASTForAllNode method interpret.

/**
 * FORALL(<EXPRESSION>, `[$x] <EXPRESSION>`) E.G.
 * FORALL([/ClubMembership/ClubMemberships/ClubIdentifier],
 * `CheckRelatieCode([$x])`)
 *
 * @return
 */
private final Operand interpret(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage, ContextExpression a, ContextExpression b) {
    boolean matchAll = true;
    if (functionName.equals("FORALL"))
        matchAll = true;
    else
        matchAll = false;
    String msgList = (String) a.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value;
    try {
        List<Message> list = null;
        if (parentMsg == null) {
            list = doc.getMessages(msgList);
        } else {
            list = parentMsg.getMessages(msgList);
        }
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            parentMsg = (Message) o;
            // ignore definition messages in the evaluation
            if (parentMsg.getType().equals(Message.MSG_TYPE_DEFINITION))
                continue;
            Operand apply = b.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
            boolean result = (Boolean) apply.value;
            if ((!(result)) && matchAll)
                return Operand.ofBoolean(false);
            if ((result) && !matchAll)
                return Operand.ofBoolean(true);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException("Invalid expression in FORALL construct: \n" + ne.getMessage());
    }
    return Operand.ofBoolean(matchAll);
}
Also used : Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 3 with Operand

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

the class Expression method match.

public static final Message match(String matchString, Navajo inMessage, MappableTreeNode o, Message parent) throws SystemException {
    try {
        StringTokenizer tokens = new StringTokenizer(matchString, ";");
        String matchSet = tokens.nextToken();
        if (matchSet == null)
            throw new TMLExpressionException("Invalid usage of match: match=\"[match set];[match value]\"");
        String matchValue = tokens.nextToken();
        if (matchValue == null)
            throw new TMLExpressionException("Invalid usage of match: match=\"[match set];[match value]\"");
        Operand value = evaluate(matchValue, inMessage, o, parent, null, null, null, null);
        List<Property> properties;
        if (parent == null)
            properties = inMessage.getProperties(matchSet);
        else
            properties = parent.getProperties(matchSet);
        for (int i = 0; i < properties.size(); i++) {
            Property prop = properties.get(i);
            Message parentMsg = prop.getParentMessage();
            if (prop.getValue().equals(value.value))
                return parentMsg;
        }
    } catch (NavajoException e) {
        throw new SystemException(-1, e.getMessage(), e);
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) SystemException(com.dexels.navajo.script.api.SystemException) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 4 with Operand

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

the class Switch method testDirectUnicodeExpressionFunction.

@Test
public void testDirectUnicodeExpressionFunction() throws Exception {
    Operand result = Expression.evaluate("'€2,29'", null, null, null);
    System.err.println("Result:" + result.value);
}
Also used : Operand(com.dexels.navajo.document.Operand) Test(org.junit.Test)

Example 5 with Operand

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

the class TestCompiledExpression method parseFunction.

@Test
public void parseFunction() throws TMLExpressionException {
    Operand o = ExpressionCache.getInstance().evaluate("ToUpper('ble')", input, (Message) null, (Message) null, (Selection) null, null, null, null, Optional.<ImmutableMessage>empty(), Optional.<ImmutableMessage>empty());
    System.err.println(": " + o);
    Assert.assertEquals("BLE", o.value);
}
Also used : Operand(com.dexels.navajo.document.Operand) Test(org.junit.Test)

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