use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.
the class TreeFlatteningRebuilder method getAndOrLeaves.
private List<JexlNode> getAndOrLeaves(JexlNode node) {
LinkedList<JexlNode> children = new LinkedList<>();
LinkedList<JexlNode> stack = new LinkedList<>();
stack.push(node);
while (!stack.isEmpty()) {
JexlNode currNode = stack.pop();
// @formatter:off
if (currNode == node || (node.getClass().isInstance(currNode) && (currNode instanceof ASTOrNode || (currNode instanceof ASTAndNode && !isBoundedRange((ASTAndNode) currNode))))) {
// @formatter:on
for (JexlNode child : children(currNode)) {
stack.push(child);
}
} else {
children.push(currNode);
}
}
return children;
}
use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.
the class TreeFlatteningRebuilder method flattenTree.
/**
* Given a JexlNode, creates a copy of that node which has had it's AND and OR nodes flattened.
*
* @param rootNode
* the node to flatten
* @param <T>
* @return the flattened copy
*/
public <T extends JexlNode> T flattenTree(T rootNode) {
Deque<JexlNode> postOrderStack = new LinkedList<>();
// iteratively copy the root node, and create the post order traversal stack
copyTree(rootNode, postOrderStack);
// use the copied post order traversal stack to iteratively flatten the tree
JexlNode newRoot = flattenTree(postOrderStack);
return (T) newRoot;
}
use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.
the class TreeFlatteningRebuilder method rebuildNode.
/**
* Returns a copy of the passed in node.
*
* If the original node has children, those exact children (not copies) will be added to the copied node. However, the parentage of those child nodes will
* be left as-is.
*
* If the original node has no children, we will simply use the RebuildingVisitor to copy the node.
*
* @param node
* the node to copy
* @return the copied node
*/
private JexlNode rebuildNode(JexlNode node) {
JexlNode newNode;
if (node.jjtGetNumChildren() == 0) {
newNode = RebuildingVisitor.copy(node);
} else {
newNode = JexlNodes.newInstanceOfType(node);
newNode.image = node.image;
JexlNodes.ensureCapacity(newNode, node.jjtGetNumChildren());
int nodeIdx = 0;
for (JexlNode child : children(node)) newNode.jjtAddChild(child, nodeIdx++);
}
return newNode;
}
use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.
the class TreeWrappingRebuildingVisitor method visit.
@Override
public Object visit(ASTOrNode node, Object data) {
ASTOrNode newNode = new ASTOrNode(ParserTreeConstants.JJTORNODE);
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
JexlNode child = (JexlNode) node.jjtGetChild(i).jjtAccept(this, data);
newNode.jjtAddChild(child, newNode.jjtGetNumChildren());
child.jjtSetParent(newNode);
}
return JexlNodeFactory.wrap(newNode);
}
use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.
the class UniqueExpressionTermsVisitor method getUniqueChildren.
private List<JexlNode> getUniqueChildren(List<JexlNode> nodes) {
Set<String> childKeys = new HashSet<>();
List<JexlNode> unique = new ArrayList<>();
for (JexlNode node : nodes) {
String childKey = JexlStringBuildingVisitor.buildQueryWithoutParse(TreeFlatteningRebuildingVisitor.flatten(node), true);
if (childKeys.add(childKey)) {
unique.add(node);
} else {
this.duplicates++;
}
}
return unique;
}
Aggregations