Search in sources :

Example 71 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class Expression method replacePropertyValues.

public static final String replacePropertyValues(String clause, Navajo inMessage) {
    // Find all property references in clause.
    StringBuilder result = new StringBuilder();
    int begin = clause.indexOf('[');
    if (// Clause does not contain properties.
    begin == -1)
        return clause;
    result.append(clause.substring(0, begin));
    while (begin >= 0) {
        int end = clause.indexOf(']');
        String propertyRef = clause.substring(begin + 1, end);
        Property prop = inMessage.getProperty(propertyRef);
        String value = "null";
        if (prop != null) {
            String type = prop.getType();
            if (type.equals(Property.SELECTION_PROPERTY)) {
                if (!prop.getCardinality().equals("+")) {
                    // Uni-selection property.
                    try {
                        List<Selection> list = prop.getAllSelectedSelections();
                        if (!list.isEmpty()) {
                            Selection sel = list.get(0);
                            value = sel.getValue();
                        }
                    } catch (com.dexels.navajo.document.NavajoException te) {
                        throw new TMLExpressionException(te.getMessage());
                    }
                } else {
                    // Multi-selection property.
                    try {
                        List<Selection> list = prop.getAllSelectedSelections();
                        List<String> selectedValues = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            Selection sel = list.get(i);
                            String o = sel.getValue();
                            selectedValues.add(o);
                        }
                        value = String.join(";", selectedValues);
                    } catch (NavajoException te) {
                        throw new TMLExpressionException(te.getMessage(), te);
                    }
                }
            } else if (type.equals(Property.STRING_PROPERTY)) {
                value = "\"" + prop.getValue() + "\"";
            } else {
                value = prop.getValue();
            }
        }
        result.append("{" + value + "}");
        clause = clause.substring(end + 1, clause.length());
        begin = clause.indexOf('[');
        if (begin >= 0)
            result.append(clause.substring(0, begin));
        else
            result.append(clause.substring(0, clause.length()));
    }
    return result.toString();
}
Also used : Selection(com.dexels.navajo.document.Selection) ArrayList(java.util.ArrayList) NavajoException(com.dexels.navajo.document.NavajoException) NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 72 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException 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 73 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class TestCompiledExpression method testParseTmlComplex.

@Test
public void testParseTmlComplex() throws ParseException, TMLExpressionException {
    String expression = "[TestArrayMessageMessage@Property=Prop2/Property]";
    StringReader sr = new StringReader(expression);
    CompiledParser cp = new CompiledParser(sr);
    cp.Expression();
    List<String> problems = new ArrayList<>();
    ContextExpression ss = cp.getJJTree().rootNode().interpretToLambda(problems, sr.toString(), fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
    if (!problems.isEmpty()) {
        throw new TMLExpressionException(problems, expression);
    }
    Navajo copy = NavajoFactory.getInstance().createNavajo();
    Message rootMessage = NavajoFactory.getInstance().createMessage(copy, "TopMessage");
    copy.addMessage(rootMessage);
    rootMessage.addMessage(input.getMessage("TestArrayMessageMessage"));
    Object value = ss.apply(copy, rootMessage, null, null, null, null, null, Optional.empty(), Optional.empty()).value;
    Assert.assertEquals("Prop2", value);
    Assert.assertFalse(ss.isLiteral());
}
Also used : Message(com.dexels.navajo.document.Message) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Test(org.junit.Test)

Example 74 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class TestCompiledExpression method testParseTml.

@Test
public void testParseTml() throws ParseException, TMLExpressionException {
    String expression = "[/TestMessage/TestProperty]";
    StringReader sr = new StringReader(expression);
    CompiledParser cp = new CompiledParser(sr);
    cp.Expression();
    List<String> problems = new ArrayList<>();
    ContextExpression ss = cp.getJJTree().rootNode().interpretToLambda(problems, sr.toString(), fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
    if (!problems.isEmpty()) {
        throw new TMLExpressionException(problems, expression);
    }
    Assert.assertEquals("TestValue", ss.apply(input, Optional.empty(), Optional.empty()).value);
    Assert.assertFalse(ss.isLiteral());
}
Also used : ContextExpression(com.dexels.navajo.expression.api.ContextExpression) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Test(org.junit.Test)

Example 75 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class ArraySelection method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 3) {
        throw new TMLExpressionException(this, usage());
    }
    String messageName = getStringOperand(0);
    String propertyName = getStringOperand(1);
    String valuePropertyName = getStringOperand(2);
    // Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    Message array = doc.getMessage(messageName);
    if (array == null) {
        throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
    }
    List<Message> arrayMsg = array.getAllMessages();
    List<Selection> result = new ArrayList<Selection>();
    if (arrayMsg == null) {
        throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
    }
    for (int i = 0; i < arrayMsg.size(); i++) {
        Message m = arrayMsg.get(i);
        Property p = m.getProperty(propertyName);
        Property val = m.getProperty(valuePropertyName);
        Selection s = NavajoFactory.getInstance().createSelection(doc, "" + p.getTypedValue(), "" + val.getTypedValue(), false);
        result.add(s);
    }
    return result;
}
Also used : Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Aggregations

TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)129 Message (com.dexels.navajo.document.Message)26 Navajo (com.dexels.navajo.document.Navajo)26 Binary (com.dexels.navajo.document.types.Binary)23 Property (com.dexels.navajo.document.Property)17 ArrayList (java.util.ArrayList)16 Operand (com.dexels.navajo.document.Operand)15 List (java.util.List)13 NavajoException (com.dexels.navajo.document.NavajoException)12 IOException (java.io.IOException)12 Selection (com.dexels.navajo.document.Selection)11 Access (com.dexels.navajo.script.api.Access)10 Calendar (java.util.Calendar)10 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)9 Test (org.junit.Test)9 Money (com.dexels.navajo.document.types.Money)8 SystemException (com.dexels.navajo.script.api.SystemException)8 StringTokenizer (java.util.StringTokenizer)8 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)7 ClockTime (com.dexels.navajo.document.types.ClockTime)7