use of org.mvel2.ast.ASTNode in project pinot by linkedin.
the class TransformExpressionTree method buildTree.
/**
* Given the root node for AST tree, builds the expression tree, and returns
* the root node of the expression tree.
*
* @param rootNode Root Node of AST tree
* @return Root node of the Expression tree
*/
public static TransformExpressionTree buildTree(AstNode rootNode) {
TransformExpressionTree expressionTree;
if (!rootNode.hasChildren()) {
return new TransformExpressionTree(rootNode);
} else {
expressionTree = new TransformExpressionTree(rootNode);
expressionTree._children = new ArrayList<>();
for (AstNode child : rootNode.getChildren()) {
expressionTree._children.add(buildTree(child));
}
}
return expressionTree;
}
use of org.mvel2.ast.ASTNode in project pinot by linkedin.
the class Pql2AstListener method pushNode.
private void pushNode(AstNode node) {
if (_rootNode == null) {
_rootNode = node;
}
AstNode parentNode = null;
if (!_nodeStack.isEmpty()) {
parentNode = _nodeStack.peek();
}
if (parentNode != null) {
parentNode.addChild(node);
}
node.setParent(parentNode);
_nodeStack.push(node);
}
use of org.mvel2.ast.ASTNode in project pinot by linkedin.
the class Pql2Compiler method compileToBrokerRequest.
@Override
public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException {
try {
//
CharStream charStream = new ANTLRInputStream(expression);
PQL2Lexer lexer = new PQL2Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
PQL2Parser parser = new PQL2Parser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
// Parse
ParseTree parseTree = parser.root();
ParseTreeWalker walker = new ParseTreeWalker();
Pql2AstListener listener = new Pql2AstListener(expression);
walker.walk(listener, parseTree);
AstNode rootNode = listener.getRootNode();
BrokerRequest brokerRequest = new BrokerRequest();
rootNode.updateBrokerRequest(brokerRequest);
return brokerRequest;
} catch (Pql2CompilationException e) {
throw e;
} catch (Exception e) {
throw new Pql2CompilationException(e.getMessage());
}
}
use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeNodeAccessor.
private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
AccessorNode accessorNode;
if (accessor instanceof DynamicGetAccessor) {
accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
} else if (accessor instanceof AccessorNode) {
accessorNode = (AccessorNode) accessor;
} else if (accessor instanceof CompiledExpression) {
return analyzeNode(((CompiledExpression) accessor).getFirstNode());
} else if (accessor instanceof ListCreator) {
return analyzeListCreation(((ListCreator) accessor));
} else if (accessor instanceof ArrayCreator) {
return analyzeArrayCreation(((ArrayCreator) accessor));
} else {
throw new RuntimeException("Unknown accessor type: " + accessor);
}
if (accessorNode instanceof VariableAccessor) {
if (isStaticAccessor(accessorNode)) {
while (accessorNode instanceof VariableAccessor) {
accessorNode = accessorNode.getNextNode();
}
} else {
return analyzeNodeAccessor(accessorNode, node);
}
}
while (accessorNode instanceof StaticReferenceAccessor) {
StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
Object literal = staticReferenceAccessor.getLiteral();
accessorNode = accessorNode.getNextNode();
if (accessorNode == null) {
return new FixedExpression(literal.getClass(), literal);
}
}
return analyzeExpressionNode(accessorNode, node, null);
}
use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeCondition.
private Condition analyzeCondition(ASTNode node) {
boolean isNegated = false;
if (node instanceof Negation) {
isNegated = true;
ExecutableStatement statement = ((Negation) node).getStatement();
if (statement instanceof ExecutableLiteral) {
return new FixedValueCondition(!(Boolean) ((ExecutableLiteral) statement).getLiteral());
}
node = ((ExecutableAccessor) statement).getNode();
}
node = analyzeSubstatement(node);
if (node instanceof LiteralNode && node.getEgressType() == Boolean.class) {
boolean literalValue = (Boolean) node.getLiteralValue();
return new FixedValueCondition(isNegated ? !literalValue : literalValue);
}
if (node instanceof Negation) {
isNegated = !isNegated;
node = ((ExecutableAccessor) ((Negation) node).getStatement()).getNode();
node = analyzeSubstatement(node);
}
if (node instanceof And || node instanceof Or) {
return analyzeCombinedCondition((BooleanNode) node, isNegated);
}
return analyzeSingleCondition(node, isNegated);
}
Aggregations