public void walk(Node nd) throws SemanticException { opStack.push(nd); dispatch(nd, opStack); if (nd.getChildren() != null) for (Node n : nd.getChildren()) walk(n); opStack.pop(); }
@Test public void testSimplify2() { Node node = new And(a, new And(new And(b, c), new And(d, e))); node.simplify(); assertEquals(a, node.getChildren()[0]); assertEquals(b, node.getChildren()[1]); assertEquals(c, node.getChildren()[2]); assertEquals(d, node.getChildren()[3]); assertEquals(e, node.getChildren()[4]); }
@Test public void testSimplify4() { Node x = new Or(b, c); Node y = new Or(d, e); Node node = new And(a, new And(x, y)); node.simplify(); assertEquals(a, node.getChildren()[0]); assertEquals(x, node.getChildren()[1]); assertEquals(y, node.getChildren()[2]); }
@Test public void testArrayIndexOutOfBounds1() throws TimeoutException { Node a = new And(new Literal("A"), new Equals("A", "B"), new Literal("D", false)); Node b = new And(new Literal("C"), new Equals("C", "D"), new Literal("B", false)); a = a.clone().toCNF(); b = b.clone().toCNF(); SatSolver solver = new SatSolver(a, TIMEOUT); for (Node child : b.getChildren()) { if (!(child instanceof Or)) child = new Or(child); Node[] list = Node.clone(child.getChildren()); for (Node node : list) ((Literal) node).positive ^= true; solver.isSatisfiable(list); } }
public static <T> void traverseDFS(Node<T> root, TraverseOrder order) { System.out.print("DFS(" + order + "): "); Set<Node<T>> visitedSet = new HashSet<>(); Set<Node<T>> exploredSet = new HashSet<>(); Stack<Node<T>> stack = new Stack<>(); stack.push(root); addNode(visitedSet, root, order == TraverseOrder.PRE_ORDER); while (!stack.isEmpty()) { Node<T> node = stack.peek(); boolean anyNodeAdded = false; for (Node<T> child : node.getChildren()) { if (!visitedSet.contains(child)) { addNode(visitedSet, child, order == TraverseOrder.PRE_ORDER); stack.push(child); anyNodeAdded = true; break; } } if (!anyNodeAdded) { stack.pop(); addNode(exploredSet, node, order == TraverseOrder.POST_ORDER); } } System.out.println(""); }
public void evalDepth(Node root) { if (root.getDepth() != -1) return; if (root.getChildren() == null) { root.setDepth(0); return; } ArrayList<Node> children = root.getChildren(); int depthChildren = -1; for (Node node : children) { if (node.getDepth() < 0) this.evalDepth(node); if (depthChildren > node.getDepth() || depthChildren == -1) depthChildren = node.getDepth(); } root.setDepth(depthChildren + 1); return; }
// Another BFS path-finding implementation // This one doesn't rely on getting all reachable nodes through BFS // Instead, it stops once it finds the node during BFS. // Also, the queue keeps track of paths instead of individual nodes public static List<Node> BFS2(Node root, Node find) { Node.resetTree(root); LinkedList<ArrayList<Node>> queue = new LinkedList<ArrayList<Node>>(); root.setDistance(0); ArrayList<Node> path = new ArrayList<Node>(); path.add(root); queue.add(path); while (queue.size() > 0) { ArrayList<Node> curPath = queue.remove(); Node endNode = curPath.get(curPath.size() - 1); if (endNode == find) { return curPath; } else { List<Node> endNodeChildren = endNode.getChildren(); for (Node node : endNodeChildren) { if (node.getDistance() == Double.POSITIVE_INFINITY) { node.setDistance(endNode.getDistance() + 1); node.setParent(endNode); ArrayList<Node> newPath = new ArrayList<Node>(curPath); newPath.add(node); queue.add(newPath); } } } } return Collections.emptyList(); }
/** * @param obj * @param text * @param addChildren * @return */ public ArrayList<String> getChildrenList( String obj, String text, boolean addChildren, boolean firstLine) { Node[] c = getChildren(obj, text); this.suggestions.clear(); if (c != null) { for (int idx = 0; idx < c.length; idx++) { if (addChildren) { this.addChildToList(c[idx], c[idx].getToken(), this.suggestions); } else { this.suggestions.add(c[idx].getToken()); } } } if (text.trim().length() == 0 || this.suggestions.isEmpty()) { // in the event the list is empty, we also add // the top level nodes for (Node t : rootCond.getChildren()) { if ((!firstLine || t.getToken() != null) && !this.suggestions.contains(t.getToken())) { if (addChildren) { this.addChildToList(t, t.getToken(), this.suggestions); } else { this.suggestions.add(t.getToken()); } } } } return this.suggestions; }
// Breadth first search // Returns a list of all reachable nodes in the order visited public static List<Node> BFS(Node root) { Node.resetTree(root); List<Node> reachable = new ArrayList<Node>(); LinkedList<Node> queue = new LinkedList<Node>(); root.setDistance(0); queue.add(root); reachable.add(root); while (queue.size() > 0) { Node cur = queue.remove(); List<Node> children = cur.getChildren(); for (Node node : children) { if (node.getDistance() == Double.POSITIVE_INFINITY) { node.setDistance(cur.getDistance() + 1); node.setParent(cur); queue.add(node); reachable.add(node); } } } return reachable; }
/** * method will print the node and then iterate over the children * * @param n */ protected void printNode(Node n) { printTabs(n.getDepth()); System.out.println("- \"" + n.getToken() + "\""); for (Node c : n.getChildren()) { printNode(c); } }
public void testGetLoad() { clearCounts(); Session s = openSession(); Transaction tx = s.beginTransaction(); Employer emp = new Employer(); s.persist(emp); Node node = new Node("foo"); Node parent = new Node("bar"); parent.addChild(node); s.persist(parent); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); emp = (Employer) s.get(Employer.class, emp.getId()); assertTrue(Hibernate.isInitialized(emp)); assertFalse(Hibernate.isInitialized(emp.getEmployees())); node = (Node) s.get(Node.class, node.getName()); assertTrue(Hibernate.isInitialized(node)); assertFalse(Hibernate.isInitialized(node.getChildren())); assertFalse(Hibernate.isInitialized(node.getParent())); assertNull(s.get(Node.class, "xyz")); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); emp = (Employer) s.load(Employer.class, emp.getId()); emp.getId(); assertFalse(Hibernate.isInitialized(emp)); node = (Node) s.load(Node.class, node.getName()); assertEquals(node.getName(), "foo"); assertFalse(Hibernate.isInitialized(node)); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); emp = (Employer) s.get("org.hibernate.ejb.test.ops.Employer", emp.getId()); assertTrue(Hibernate.isInitialized(emp)); node = (Node) s.get("org.hibernate.ejb.test.ops.Node", node.getName()); assertTrue(Hibernate.isInitialized(node)); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); emp = (Employer) s.load("org.hibernate.ejb.test.ops.Employer", emp.getId()); emp.getId(); assertFalse(Hibernate.isInitialized(emp)); node = (Node) s.load("org.hibernate.ejb.test.ops.Node", node.getName()); assertEquals(node.getName(), "foo"); assertFalse(Hibernate.isInitialized(node)); tx.commit(); s.close(); assertFetchCount(0); }
public Bar(Node node) { Set<Node> children = node.getChildren(); for (Node child : children) { if (child.getId().equals(":beats")) { beats = new Beats(child); } } }
@Override public void evaluatePre(int depth, OutputManager out) { // If parent is MethoNode, this Node is not the last child of parent node Node parent = this.getParent(); if (parent instanceof MethodNode && parent.getChildren().get(parent.numChildren() - 1) != this) out.writeStatement(data + ","); else out.writeStatement(data); }
/** * Performs modifications, and reassigns certain properties on the tree in place. The input is * assumed to be a properly "crunched" tree. This is mainly to aid creating the "mini-tree" for * the data selection in the RDF mapping. <br> * The only properties that are recalculated are the weights and the selectors. */ public void recrunch(Node node) { if (!node.isLeaf()) { for (Node child : node.getChildren()) { recrunch(child); } computeWeightAndSelector(node); } }
public List<Element> collectChildren(int level, Node node) { Collector<Element> collector = new Collector<Element>(); iTextContext().pushElementConsumer(collector); for (Node child : node.getChildren()) { process(level + 1, child); } iTextContext().popElementConsumer(); return collector.getCollected(); }
protected void collectChildren(Node node, List<Node> nodes) { if (node instanceof And) { for (Node childNode : node.getChildren()) { collectChildren(childNode, nodes); } } else { nodes.add(node); } }
private void order(List<Node> list) { if (list == null) { return; } SortUtils.sort(list, "order", true); for (Node node : list) { order(node.getChildren()); } }
/** * method will prepend the parent text to the child and generate the possible combinations in text * format. * * @param n * @param prefix * @param list */ public void addChildToList(Node n, String prefix, ArrayList<String> list) { if (n.getChildren().size() > 0) { for (Node child : n.getChildren()) { if (prefix != null && "-".equals(child.getToken())) { if (!list.contains(prefix)) { list.add(prefix); } return; } String text = (prefix == null ? "" : prefix + " ") + child.getToken(); // list.add(text); addChildToList(child, text, list); } } else { if (!list.contains(prefix)) { list.add(prefix); } } }
private void computeWeightAndSelector(Node node) { // set the weight after all its children are already "crunched" int weight = 0; for (Node child : node.getChildren()) { weight += child.getWeight(); } node.setWeight(weight); // set the selector node.setSelector(pickSelector(node)); }
public Instrument(Node node) { Set<Node> children = node.getChildren(); for (Node child : children) { if (child.getId().equals(":bar")) { bar = new Bar(child); } else if (!child.getId().equals(":")) { id = new InstrumentId(child); } } }
@Override protected Node[] createNodes(Object key) { Node node; if (key == keyLayout) node = new LayoutNode((RADVisualContainer) container); else { node = new RADComponentNode((RADComponent) key); node.getChildren().getNodes(); // enforce subnodes creation } return new Node[] {node}; }
private void clearLevel(int currentLevel, Node current, int targetLevel) { if (currentLevel > targetLevel) return; if (currentLevel == targetLevel) { if (current.drills != null) current.drills.clear(); } else if (current.hasChildren()) { for (Node child : current.getChildren()) { clearLevel(currentLevel + 1, child, targetLevel); } } }
/* * (non-Javadoc) * * @see * com.platzhaltr.flatlinr.core.TraverseStrategy#getNextNode(java.util.Stack * , com.platzhaltr.flatlinr.core.Node, java.lang.String) */ @Override public Node getNextNode(final Node currentNode, final String nextLine) { // bias towards the child if it starts with a constant leaf and the // current node starts with a delimited leaf if (!currentNode.getLeafs().isEmpty()) { final Leaf firstLeafOfCurrentNode = currentNode.getLeafs().get(0); for (final Node childNode : currentNode.getChildren()) { if (!childNode.getLeafs().isEmpty()) { final Leaf firstLeafOfChildNode = childNode.getLeafs().get(0); if ((firstLeafOfCurrentNode instanceof DelimitedLeaf) && (firstLeafOfChildNode instanceof ConstantLeaf)) { if (isMatchingLeaf(firstLeafOfChildNode, childNode.getLeafs().size() == 1, nextLine)) { return childNode; } } } } } // bias towards the current node if (!currentNode.getLeafs().isEmpty()) { if (isMatchingLeaf( currentNode.getLeafs().get(0), currentNode.getLeafs().size() == 1, nextLine)) { return currentNode; } } // then towards the children for (final Node childNode : currentNode.getChildren()) { if (!childNode.getLeafs().isEmpty()) { if (isMatchingLeaf( childNode.getLeafs().get(0), childNode.getLeafs().size() == 1, nextLine)) { return childNode; } } } // default to ancestor traverse strategy return ancestorTraverseStrategy.getNextNode(currentNode, nextLine); }
private static ClassHierarchyProto.Node serializeNode(Node n) { List<ClassHierarchyProto.Node> children = new ArrayList<>(); for (Node child : n.getChildren()) { children.add(serializeNode(child)); } if (n instanceof ClassNode) { ClassNode<?> cn = (ClassNode<?>) n; ConstructorDef<?>[] injectable = cn.getInjectableConstructors(); ConstructorDef<?>[] all = cn.getAllConstructors(); List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all)); others.removeAll(Arrays.asList(injectable)); List<ClassHierarchyProto.ConstructorDef> injectableConstructors = new ArrayList<>(); for (ConstructorDef<?> inj : injectable) { injectableConstructors.add(serializeConstructorDef(inj)); } List<ClassHierarchyProto.ConstructorDef> otherConstructors = new ArrayList<>(); for (ConstructorDef<?> other : others) { otherConstructors.add(serializeConstructorDef(other)); } List<String> implFullNames = new ArrayList<>(); for (ClassNode<?> impl : cn.getKnownImplementations()) { implFullNames.add(impl.getFullName()); } return newClassNode( cn.getName(), cn.getFullName(), cn.isInjectionCandidate(), cn.isExternalConstructor(), cn.isUnit(), injectableConstructors, otherConstructors, implFullNames, children); } else if (n instanceof NamedParameterNode) { NamedParameterNode<?> np = (NamedParameterNode<?>) n; return newNamedParameterNode( np.getName(), np.getFullName(), np.getSimpleArgName(), np.getFullArgName(), np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(), np.getDefaultInstanceAsStrings(), children); } else if (n instanceof PackageNode) { return newPackageNode(n.getName(), n.getFullName(), children); } else { throw new IllegalStateException("Encountered unknown type of Node: " + n); } }
public static <T> void traverseBFS(Node<T> root, NodeVisitor<T> visitor) { System.out.println("BFS"); Queue<Node<T>> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { Node<T> node = queue.poll(); for (Node<T> child : node.getChildren()) { queue.offer(child); } visitor.visit(node); } }
public void optimizeRoot() { List<Node> list = getChildren(); if (list.size() == 1) { Node node = list.get(0); List<Node> children = node.getChildren(); this.children.remove(0); for (Node child : children) { this.children.add(child); child.setParent(null); } } }
// 处理只有一个儿子的文件夹 private void move1ChildFolder(Node node) { if ("folder".equals(node.getType())) { if (node.getSize() == 1) { Node child = node.getChildren().get(0); Node parent = node.getParent(); if (parent != null) { // 将单个儿子节点往上移动 parent.getChildren().add(child); child.setParent(parent); // 并从当前节点中移除 node.getChildren().remove(0); parent.getChildren().remove(node); // 继续网上移动单个儿子节点 move1ChildFolder(child); } } } }
public <T> void parallelRecursive( final Executor exec, List<Node<T>> nodes, final Collection<T> results) { for (final Node<T> n : nodes) { exec.execute( new Runnable() { public void run() { results.add(n.compute()); } }); parallelRecursive(exec, n.getChildren(), results); } }
private void visit(List<Member> parents, Node node, Visitor visitor) throws OlapException { List<Member> drilledMembers = node.getDrills(); visitor.visit(parents, drilledMembers); if (!node.hasChildren()) return; for (Node child : node.getChildren()) { if (visitor.shouldVisitChild(parents.size(), child.getMember())) { parents.add(child.getMember()); visit(parents, child, visitor); parents.remove(parents.size() - 1); } } }
private void prune(Node n, int level) { if (level > 1) { List<Node> children = n.getChildren(); if (children != null) { for (Node child : children) { prune(child, level - 1); } } } else { n.removeChildren(); n.removeDrills(); } }