Search in sources :

Example 86 with Message

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

the class TestNavajoNonBlockingStreamReactive method testStreamParserAndSerializerWithIgnoreMessage.

@Test
public void testStreamParserAndSerializerWithIgnoreMessage() throws Exception {
    try {
        Navajo navajo = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("tiny_tml_with_ignore.xml"), 8192).lift(XML.parseFlowable(5)).concatMap(e -> e).lift(StreamDocument.parse()).concatMap(e -> e).lift(StreamDocument.filterMessageIgnore()).toObservable().compose(StreamDocument.domStreamCollector()).blockingFirst();
        Message ignored = navajo.getMessage("Message");
        System.err.println("Msg: " + ignored);
        Assert.assertNull(ignored);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) IOException(java.io.IOException) Test(org.junit.Test)

Example 87 with Message

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

the class StreamScriptContext method mergeNavajo.

// in = context.inputFlowable().get().map(e->e.event()).toObservable().compose(StreamDocument.domStreamCollector()).blockingFirst();
private Navajo mergeNavajo(Navajo inputNavajo, Optional<Navajo> merging) {
    Navajo in = inputNavajo.copy();
    if (merging.isPresent()) {
        List<Message> msgs = merging.get().getAllMessages();
        msgs.forEach(e -> {
            // TODO is this copy necessary?
            in.addMessage(e.copy());
        });
    }
    return in;
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo)

Example 88 with Message

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

the class SumExpressions method evaluate.

/* (non-Javadoc)
	 * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
	 */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            System.err.println("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (String) getOperand(0);
    String expression = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                Operand o = Expression.evaluate(expression, m.getRootDoc(), null, m);
                if (o.value == null) {
                    throw new TMLExpressionException(this, "Null value encountered");
                }
                if (o.value instanceof Integer) {
                    sum += ((Integer) o.value).doubleValue();
                } else if (o.value instanceof Double) {
                    sum += ((Double) o.value).doubleValue();
                } else {
                    throw new TMLExpressionException(this, "Incompatible type while summing: " + o.value.getClass().getName());
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException)

Example 89 with Message

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

the class SumProperties method main.

public static void main(String[] args) throws Exception {
    System.setProperty("com.dexels.navajo.DocumentImplementation", "com.dexels.navajo.document.nanoimpl.NavajoFactoryImpl");
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message top = NavajoFactory.getInstance().createMessage(doc, "Top");
    doc.addMessage(top);
    Message array = NavajoFactory.getInstance().createMessage(doc, "MyArray", Message.MSG_TYPE_ARRAY);
    top.addMessage(array);
    for (int i = 0; i < 9; i++) {
        Message elt = NavajoFactory.getInstance().createMessage(doc, "MyArray", Message.MSG_TYPE_ARRAY_ELEMENT);
        array.addMessage(elt);
        Message array2 = NavajoFactory.getInstance().createMessage(doc, "NogEenArraytje" + i, Message.MSG_TYPE_ARRAY);
        elt.addMessage(array2);
        for (int j = 0; j < 2; j++) {
            Message elt2 = NavajoFactory.getInstance().createMessage(doc, "NogEenArraytje" + i, Message.MSG_TYPE_ARRAY_ELEMENT);
            array2.addMessage(elt2);
            Property p = NavajoFactory.getInstance().createProperty(doc, "MyBoolean", Property.BOOLEAN_PROPERTY, Property.TRUE, 0, "", Property.DIR_OUT);
            elt2.addProperty(p);
        }
    }
    // doc.write(System.err);
    Operand result = Expression.evaluate("SumProperties('/Top/MyArray.*/NogEenArraytje.*', 'MyBoolean')", doc);
    System.err.println("result = " + result.value);
}
Also used : Message(com.dexels.navajo.document.Message) Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 90 with Message

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

the class SumProperties method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            logger.info("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (String) getOperand(0);
    String propertyName = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            Property p = m.getProperty(propertyName);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                if (p != null) {
                    Object o = p.getTypedValue();
                    if (o == null) {
                        continue;
                    }
                    if (!(o instanceof Integer || o instanceof Double || o instanceof Float || o instanceof Money || o instanceof Percentage || o instanceof Boolean || o instanceof String)) {
                        throw new TMLExpressionException(this, "Only numbers are supported a sum. Not: " + (o.getClass().toString()) + " value: " + o);
                    }
                    if (o instanceof String) {
                        if ("".equals(o)) {
                        // ignore
                        } else {
                            logger.error("Only numbers are supported a sum. Not strings. Value:  " + o);
                            throw new TMLExpressionException(this, "Only numbers are supported a sum. Not strings. Value:  " + o + (o.getClass().toString()));
                        }
                    }
                    if (o instanceof Integer) {
                        sumType = "int";
                        sum += ((Integer) o).doubleValue();
                    } else if (o instanceof Double) {
                        // if (!((Double)o).equals(Double.valueOf(Double.NaN))) {
                        sumType = "float";
                        sum += ((Double) o).doubleValue();
                    // }
                    } else if (o instanceof Float) {
                        // if (!((Float)o).equals(new Float(Float.NaN))) {
                        sumType = "float";
                        sum += ((Float) o).doubleValue();
                    // }
                    } else if (o instanceof Money) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "money";
                        sum += ((Money) o).doubleValue();
                    // }
                    } else if (o instanceof Percentage) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "percentage";
                        sum += ((Percentage) o).doubleValue();
                    // }
                    } else if (o instanceof Boolean) {
                        sumType = "int";
                        sum += ((Boolean) o).booleanValue() ? 1 : 0;
                    }
                } else {
                    throw new TMLExpressionException(this, "Property does not exist: " + propertyName);
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException) Property(com.dexels.navajo.document.Property)

Aggregations

Message (com.dexels.navajo.document.Message)312 Property (com.dexels.navajo.document.Property)149 Navajo (com.dexels.navajo.document.Navajo)127 Test (org.junit.Test)95 NavajoException (com.dexels.navajo.document.NavajoException)39 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)35 Access (com.dexels.navajo.script.api.Access)27 UserException (com.dexels.navajo.script.api.UserException)27 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 Selection (com.dexels.navajo.document.Selection)25 Operand (com.dexels.navajo.document.Operand)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)19 MappableException (com.dexels.navajo.script.api.MappableException)18 Ignore (org.junit.Ignore)17 SystemException (com.dexels.navajo.script.api.SystemException)16 Optional (java.util.Optional)15 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)13 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)13 HashMap (java.util.HashMap)13