use of com.dexels.navajo.expression.api.TipiLink in project navajo by Dexels.
the class ASTReactiveScriptNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String originalExpression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
int start = hasHeader ? headers : 0;
if (hasHeader) {
for (int i = 0; i < headers; i++) {
ASTKeyValueNode hdr = (ASTKeyValueNode) jjtGetChild(i);
NamedExpression ne = (NamedExpression) hdr.interpretToLambda(problems, originalExpression, functionClassifier, mapResolver);
String key = ne.name;
headerMap.put(key, ne.apply());
}
}
int count = jjtGetNumChildren();
List<Node> unnamedPipes = new ArrayList<>();
Map<String, Node> namedPipes = new HashMap<>();
for (int i = start; i < count; i++) {
Node child = jjtGetChild(i);
// ASTReactivePipe pipe = null;
if (child instanceof ASTPipeDefinition) {
unnamedPipes.add((ASTPipeDefinition) child);
} else if (child instanceof ASTKeyValueNode) {
ASTKeyValueNode kvNode = (ASTKeyValueNode) child;
String streamName = kvNode.val;
Node namedPipe = kvNode.jjtGetChild(0);
// assert value types perhaps? TODO
namedPipes.put(streamName, namedPipe);
}
}
List<ReactivePipeNode> pipes = unnamedPipes.stream().map(p -> (ReactivePipeNode) p.interpretToLambda(problems, originalExpression, functionClassifier, name -> {
Optional<Node> initial = Optional.ofNullable(namedPipes.get(name));
if (initial.isPresent()) {
return initial;
} else {
return mapResolver.apply(name);
}
})).collect(Collectors.toList());
return new ContextExpression() {
@Override
public Optional<String> returnType() {
return Optional.of(Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
}
@Override
public boolean isLiteral() {
return true;
}
@Override
public String expression() {
return "";
}
@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 Operand.ofCustom(pipes, Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
}
};
}
use of com.dexels.navajo.expression.api.TipiLink in project navajo by Dexels.
the class ASTDatePatternNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
ContextExpression y = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
checkOrAdd("Year (item 0) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
ContextExpression m = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
checkOrAdd("Month (item 1) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
ContextExpression d = jjtGetChild(2).interpretToLambda(problems, expression, functionClassifier, mapResolver);
checkOrAdd("Day (item 2) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
ContextExpression h = jjtGetChild(3).interpretToLambda(problems, expression, functionClassifier, mapResolver);
checkOrAdd("Hour (item 3) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
ContextExpression min = jjtGetChild(4).interpretToLambda(problems, expression, functionClassifier, mapResolver);
checkOrAdd("Minute (item 4) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
ContextExpression s = jjtGetChild(5).interpretToLambda(problems, expression, functionClassifier, mapResolver);
checkOrAdd("Second (item 5) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
final boolean isLiteral = y.isLiteral() && m.isLiteral() && d.isLiteral() && h.isLiteral() && min.isLiteral() && s.isLiteral();
return new ContextExpression() {
@Override
public boolean isLiteral() {
return isLiteral;
}
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
int yearT = ((Integer) y.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
int monthT = ((Integer) m.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
int dayT = ((Integer) d.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
int hourT = ((Integer) h.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
int minT = ((Integer) min.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
int secT = ((Integer) s.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
return Operand.ofDatePattern(new DatePattern(yearT, monthT, dayT, hourT, minT, secT, true));
}
@Override
public Optional<String> returnType() {
return Optional.of(Property.DATE_PATTERN_PROPERTY);
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.expression.api.TipiLink in project navajo by Dexels.
the class ASTForAllNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
return new ContextExpression() {
@Override
public boolean isLiteral() {
return false;
}
@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<String> problems = new ArrayList<>();
ContextExpression a = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
ContextExpression b = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
if (!problems.isEmpty()) {
throw new TMLExpressionException(problems, expression);
}
return interpret(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage, a, b);
}
@Override
public Optional<String> returnType() {
return Optional.empty();
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.expression.api.TipiLink in project navajo by Dexels.
the class SimpleNode method lazyBiFunction.
public ContextExpression lazyBiFunction(List<String> problems, String expression, BinaryOperator<Operand> func, BiFunction<Optional<String>, Optional<String>, Boolean> acceptTypes, BiFunction<Optional<String>, Optional<String>, Optional<String>> returnTypeResolver, 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> aType = expA.returnType();
Optional<String> bType = expB.returnType();
boolean inputTypesValid = acceptTypes.apply(aType, bType);
if (!inputTypesValid) {
problems.add("Invalid input types in node: " + aType.orElse("unknown") + " and " + bType.orElse("unknown") + " in node type: " + this.getClass());
}
Optional<String> returnType = returnTypeResolver.apply(aType, bType);
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);
Operand b = expB.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
return func.apply(a, b);
}
@Override
public boolean isLiteral() {
return expA.isLiteral() && expB.isLiteral();
}
@Override
public Optional<String> returnType() {
return returnType;
}
@Override
public String expression() {
return expression;
}
};
}
use of com.dexels.navajo.expression.api.TipiLink in project navajo by Dexels.
the class ASTMappableNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
Optional<Node> resolved = mapResolver.apply(val);
if (resolved.isPresent()) {
return resolved.get().interpretToLambda(problems, expression, functionClassifier, mapResolver);
}
return new ContextExpression() {
@Override
public boolean isLiteral() {
return false;
}
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
if (mapNode == null) {
throw new TMLExpressionException("No known mapobject resolver");
}
// MappableTreeNode mapNode = mapNodeResolver.apply(val);
// if(mapNode==null) {
// throw new TMLExpressionException("No known mapobject");
//
// }
// if(mapNode==null) {
// Node resolvedItem = mapResolver.get().apply(val);
// System.err.println(">> "+resolvedItem.getClass());
// resolvedItem.interpretToLambda(problems, expression, functionClassifier, mapResolver);
//
// }
List objects = null;
// Parameter array may contain parameters that are used when calling the get method.
Object[] parameterArray = null;
if (args > 0) {
objects = new ArrayList();
}
for (int i = 0; i < args; i++) {
List<String> problems = new ArrayList<>();
Operand a = jjtGetChild(i).interpretToLambda(problems, expression, functionClassifier, mapResolver).apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage);
if (!problems.isEmpty()) {
throw new TMLExpressionException(problems, expression);
}
if (objects != null) {
objects.add(a.value);
}
}
// List<String> problems
if (objects != null) {
parameterArray = new Object[objects.size()];
parameterArray = objects.toArray(parameterArray);
}
try {
Object oValue = maybeGetMapAttribute(val, mapNode, parameterArray);
if (oValue == null)
return Operand.NULL;
else if (oValue instanceof Float) {
return Operand.ofFloat(((Float) oValue).doubleValue());
} else if (oValue instanceof Long) {
return Operand.ofLong(((Long) oValue).longValue());
} else {
return Operand.ofDynamic(oValue);
}
} catch (Exception me) {
throw new TMLExpressionException(me.getMessage(), me);
}
}
@Override
public Optional<String> returnType() {
return Optional.empty();
}
@Override
public String expression() {
return expression;
}
};
}
Aggregations