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