use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class JexlASTHelperTest method testNormalizeLiteral.
@Test
public void testNormalizeLiteral() throws Throwable {
LcNoDiacriticsType normalizer = new LcNoDiacriticsType();
ASTJexlScript script = JexlASTHelper.parseJexlQuery("F == 'aSTrInG'");
if (log.isDebugEnabled()) {
PrintingVisitor.printQuery(script);
}
JexlNode literal = script.jjtGetChild(0).jjtGetChild(1).jjtGetChild(0);
ASTReference ref = JexlASTHelper.normalizeLiteral(literal, normalizer);
Assert.assertEquals("astring", ref.jjtGetChild(0).image);
}
use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class DistributeAndedNodesVisitor method visit.
/**
* Checks each of the child nodes, and determines how the anded nodes should be applied.
*
* @param node
* The node that we will be distributing the anded nodes into
* @param data
* The nodes which we will be distributing into the root node
* @return An updated script with the anded nodes distributed throughout
*/
@Override
public Object visit(ASTAndNode node, Object data) {
DistributeAndedNodesVisitor.DistAndData parentData = (DistributeAndedNodesVisitor.DistAndData) data;
if (initialNode == null || initialNode instanceof ASTReference || initialNode instanceof ASTReferenceExpression)
initialNode = node;
// if this node is one of the anded nodes, or a whindex
// comprised of one of the anded nodes, halt recursion
List<JexlNode> usedAndedNodes = usesAndedNodes(node);
if (!usedAndedNodes.isEmpty()) {
parentData.usedAndedNodes.addAll(usedAndedNodes);
return node;
}
// this logic is dependent upon identifying whindex nodes by their address
if (whindexNodes.containsKey(node)) {
return node;
}
// check each child node to see how many of the desired andedNodes are present
List<JexlNode> rebuiltChildren = new ArrayList<>();
for (JexlNode child : JexlNodes.children(node)) {
DistributeAndedNodesVisitor.DistAndData foundData = new DistributeAndedNodesVisitor.DistAndData();
rebuiltChildren.add((JexlNode) child.jjtAccept(this, foundData));
parentData.usedAndedNodes.addAll(foundData.usedAndedNodes);
}
// are some anded nodes missing, and is this the initial node?
if (!parentData.usedAndedNodes.containsAll(andedNodes) && node.equals(initialNode)) {
// 'and' with the missing anded nodes, and return
List<JexlNode> nodes = andedNodes.stream().filter(andedNode -> !parentData.usedAndedNodes.contains(andedNode)).map(RebuildingVisitor::copy).collect(Collectors.toList());
nodes.add(node);
// this is probably unnecessary, but to be safe, let's set it
parentData.usedAndedNodes.addAll(andedNodes);
return WhindexVisitor.createUnwrappedAndNode(nodes);
}
return WhindexVisitor.createUnwrappedAndNode(rebuiltChildren);
}
use of org.apache.commons.jexl3.parser.ASTReference in project datawave by NationalSecurityAgency.
the class TreeFlatteningRebuilder method flattenTree.
/**
* Given a stack of nodes, representing the post order traversal of a JexlNode, iteratively flattens the AND and OR nodes of the tree.
*
* @param postOrderStack
* the post order traversal representation of the tree
* @return the flattened tree
*/
private JexlNode flattenTree(Deque<JexlNode> postOrderStack) {
Deque<JexlNode> parentStack = new LinkedList<>();
Deque<List<JexlNode>> childrenStack = new LinkedList<>();
JexlNode newNode = null;
// now that we have the post order traversal, we can operate on the nodes...
while (!postOrderStack.isEmpty()) {
JexlNode node = postOrderStack.pop();
boolean hasChildren = node.jjtGetNumChildren() > 0;
// if this is a reference node, flatten it
if (hasChildren && node instanceof ASTReference) {
newNode = flattenReference((ASTReference) JexlNodes.children(parentStack.pop(), childrenStack.pop().toArray(new JexlNode[0])));
} else // if this is an AND or OR node, flatten it
if (hasChildren && (node instanceof ASTOrNode || node instanceof ASTAndNode)) {
newNode = flattenAndOrNode(JexlNodes.children(parentStack.pop(), childrenStack.pop().toArray(new JexlNode[0])));
} else // if this is a node with children, assign the children
if (hasChildren && node == parentStack.peek()) {
newNode = JexlNodes.children(parentStack.pop(), childrenStack.pop().toArray(new JexlNode[0]));
} else // if this is a leaf node, just keep it
{
newNode = node;
}
// if we still have nodes to evaluate
if (!postOrderStack.isEmpty()) {
// otherwise, add this node to the existing parent's list of children
if (node.jjtGetParent() != parentStack.peek()) {
parentStack.push(node.jjtGetParent());
childrenStack.push(Lists.newArrayList(newNode));
} else {
childrenStack.peek().add(newNode);
}
}
}
return newNode;
}
Aggregations