Search in sources :

Example 51 with Selection

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

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

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

the class ExpressionTest method testSelectionExpressions.

@Test
public void testSelectionExpressions() throws TMLExpressionException, SystemException {
    Navajo testDoc = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(testDoc, "MyTop");
    testDoc.addMessage(m);
    Property pt = NavajoFactory.getInstance().createProperty(testDoc, "TopProp", "1", "", Property.DIR_IN);
    Selection s = NavajoFactory.getInstance().createSelection(testDoc, "option1", "value1", true);
    pt.addSelection(s);
    m.addProperty(pt);
    Message a = NavajoFactory.getInstance().createMessage(testDoc, "MyArrayMessage", "array");
    m.addMessage(a);
    for (int i = 0; i < 5; i++) {
        Message a1 = NavajoFactory.getInstance().createMessage(testDoc, "MyArrayMessage");
        a.addMessage(a1);
        Property p = NavajoFactory.getInstance().createProperty(testDoc, "MyProp", "string", "noot" + i, 0, "", "in");
        a1.addProperty(p);
        Property p2 = NavajoFactory.getInstance().createProperty(testDoc, "MyProp2", "string", "aap" + i, 0, "", "in");
        a1.addProperty(p2);
    }
    Expression.compileExpressions = true;
    Operand o = Expression.evaluate("'hallo:' + [TopProp:name]", testDoc, null, m);
    // o = Expression.evaluate(
    // "'hallo:' + [out:/MyTop/MyArrayMessage@MyProp=noot1/MyProp2]",
    // testDoc);
    // 
    assertEquals("hallo:option1", o.value);
    o = Expression.evaluate("[TopProp:value]", testDoc, null, m);
    assertEquals("value1", o.value);
}
Also used : ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

Example 54 with Selection

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

the class BasePropertyImpl method setSelected.

@Override
public final void setSelected(Selection s) {
    List<Selection> old;
    old = getAllSelectedSelections();
    for (int i = 0; i < selectionList.size(); i++) {
        Selection current = selectionList.get(i);
        if (current.getName().equals(s.getName())) {
            current.setSelected(true);
        } else {
            if (!"+".equals(getCardinality())) {
                current.setSelected(false);
            }
        }
    }
    List<Selection> newValue = getAllSelectedSelections();
    boolean isEqual = isEqual(old, newValue);
    if (!isEqual) {
        firePropertyChanged("selection", old, newValue);
    }
}
Also used : Selection(com.dexels.navajo.document.Selection)

Example 55 with Selection

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

the class BasePropertyImpl method printElementJSONTypeless.

@Override
public void printElementJSONTypeless(final Writer sw) throws IOException {
    String value = getValue();
    if (getType().equals(Property.SELECTION_PROPERTY)) {
        Selection s = getSelected();
        if (s != null) {
            value = s.getValue();
        }
    }
    value = (value != null ? value.replace("\"", "\\\"") : "");
    writeElement(sw, "\"" + getName() + "\" : \"" + value + "\"");
}
Also used : Selection(com.dexels.navajo.document.Selection)

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