use of com.dexels.navajo.script.api.MappableTreeNode in project navajo by Dexels.
the class ExpressionCache method parse.
public ContextExpression parse(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
Optional<ContextExpression> cachedParsedExpression = exprCache.getUnchecked(expression);
if (cachedParsedExpression.isPresent()) {
hitCount.incrementAndGet();
return cachedParsedExpression.get();
}
CompiledParser cp;
try {
StringReader sr = new StringReader(expression);
cp = new CompiledParser(sr);
cp.Expression();
ContextExpression parsed = cp.getJJTree().rootNode().interpretToLambda(problems, expression, functionClassifier, mapResolver);
parsedCount.incrementAndGet();
if (parsed.isLiteral()) {
Operand result = parsed.apply();
exprCache.put(expression, Optional.ofNullable(parsed));
if (result != null) {
expressionValueCache.put(expression, Optional.of(result));
}
return new ContextExpression() {
@Override
public boolean isLiteral() {
return true;
}
@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 result;
}
@Override
public Optional<String> returnType() {
return Optional.ofNullable(result.type);
}
@Override
public String expression() {
return expression;
}
};
} else {
exprCache.put(expression, Optional.ofNullable(parsed));
return parsed;
}
} catch (ParseException e) {
throw new TMLExpressionException("Error parsing expression: " + expression, e);
} catch (Throwable e) {
throw new TMLExpressionException("Unexpected error parsing expression: " + expression, e);
}
}
use of com.dexels.navajo.script.api.MappableTreeNode in project navajo by Dexels.
the class SimpleNode method lazyFunction.
public ContextExpression lazyFunction(List<String> problems, String expression, UnaryOperator<Operand> func, Optional<String> requiredReturnType, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
if (requiredReturnType.isPresent() && expA.returnType().isPresent()) {
String expectedType = requiredReturnType.get();
String foundType = expA.returnType().get();
if (!expectedType.equals(foundType)) {
problems.add("Error (static) type checking. Type: " + foundType + " does not match expected type: " + expectedType);
}
}
return new ContextExpression() {
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
Operand a = expA.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
return func.apply(a);
}
@Override
public boolean isLiteral() {
return expA.isLiteral();
}
@Override
public Optional<String> returnType() {
return requiredReturnType;
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.script.api.MappableTreeNode in project navajo by Dexels.
the class ASTListNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
final List<ContextExpression> exprs = new ArrayList<>();
boolean onlyImmutable = true;
for (int i = 0; i < jjtGetNumChildren(); i++) {
ContextExpression lmb = jjtGetChild(i).interpretToLambda(problems, expression, functionClassifier, mapResolver);
exprs.add(lmb);
if (!onlyImmutable && !lmb.isLiteral()) {
onlyImmutable = false;
}
}
final boolean onlyImm = onlyImmutable;
return new ContextExpression() {
@Override
public boolean isLiteral() {
return onlyImm;
}
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
List<Operand> result = new ArrayList<>();
for (ContextExpression contextExpression : exprs) {
result.add(contextExpression.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage));
}
return Operand.ofList(result.stream().map(e -> e.value).collect(Collectors.toList()));
}
@Override
public Optional<String> returnType() {
return Optional.of(Property.LIST_PROPERTY);
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.script.api.MappableTreeNode in project navajo by Dexels.
the class ASTAndNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
ContextExpression expA = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
ContextExpression expB = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
Optional<String> expressionA = expA.returnType();
checkOrAdd("In AND expression the first expression is not a boolean but a " + expressionA.orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
Optional<String> expressionB = expB.returnType();
checkOrAdd("In AND expression the second expression is not a boolean but a " + expressionB.orElse("<unknown>"), problems, expB.returnType(), Property.BOOLEAN_PROPERTY);
return new ContextExpression() {
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
Operand a = expA.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
if (a == null) {
return Operand.ofBoolean(Boolean.FALSE);
}
Boolean ba = (Boolean) a.value;
if (!(ba.booleanValue())) {
return Operand.ofBoolean(Boolean.FALSE);
}
Operand b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
if (b == null) {
return Operand.ofBoolean(Boolean.FALSE);
}
Boolean value = (Boolean) b.value;
return Operand.ofBoolean(value != null ? value : Boolean.FALSE);
}
@Override
public boolean isLiteral() {
return expA.isLiteral() && expB.isLiteral();
}
@Override
public Optional<String> returnType() {
return Optional.of(Property.BOOLEAN_PROPERTY);
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.script.api.MappableTreeNode 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;
}
}
Aggregations