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();
}
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;
}
}
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());
}
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());
}
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;
}
Aggregations