TreebankNodeUtil { public static TreebankNodePath getPath(TreebankNode source, TreebankNode target) { List<TreebankNode> sourceToRoot = getPathToRoot(source); List<TreebankNode> targetToRoot = getPathToRoot(target); TreebankNode ancestor = null; while (sourceToRoot.size() > 0 && targetToRoot.size() > 0 && sourceToRoot.get(sourceToRoot.size() - 1) == targetToRoot.get(targetToRoot.size() - 1)) { ancestor = sourceToRoot.remove(sourceToRoot.size() - 1); ancestor = targetToRoot.remove(targetToRoot.size() - 1); } return new TreebankNodePath(ancestor, sourceToRoot, targetToRoot); }  static TreebankNode selectMatchingLeaf(JCas jCas, Annotation annotation); static TreebankNode selectHighestMatchingTreebankNode(JCas jCas, Annotation annotation); static TreebankNode selectHighestCoveredTreebankNode(JCas jCas, Annotation annotation); static int getDepth(TreebankNode node); static List<TreebankNode> getPathToRoot(TreebankNode startNode); static TreebankNodePath getPath(TreebankNode source, TreebankNode target); static String toTreebankString(TreebankNode node); static TreebankNode getParent(TreebankNode node); static TreebankNode getAncestorWithType(TreebankNode node, String type); static TreebankNode newNode(JCas jCas, int begin, int end, String nodeType); static TreebankNode newNode(JCas jCas, String nodeType, TreebankNode... children); static TopTreebankNode getTopNode(TreebankNode node); static void print(PrintStream out, TreebankNode node);  }
@Test public void testGetPath() throws Exception { this.jCas.setDocumentText("The cat chased mice ."); top( "S", jCas, node("NP", jCas, leaf("DT", "The", jCas), leaf("NN", "cat", jCas)), node("VP", jCas, leaf("VBD", "chased", jCas), node("NP", jCas, leaf("NNS", "mice", jCas))), leaf(".", ".", jCas)); TopTreebankNode root = JCasUtil.selectSingle(this.jCas, TopTreebankNode.class); TreebankNode np = root.getChildren(0); TreebankNode vp = root.getChildren(1); TreebankNode period = root.getChildren(2); TreebankNode the = np.getChildren(0); TreebankNode cat = np.getChildren(1); TreebankNode chased = vp.getChildren(0); TreebankNode np2 = vp.getChildren(1); TreebankNode mice = np2.getChildren(0); TreebankNodePath path = TreebankNodeUtil.getPath(cat, mice); Assert.assertEquals(root, path.getCommonAncestor()); Assert.assertEquals(Arrays.asList(cat, np), path.getSourceToAncestorPath()); Assert.assertEquals(Arrays.asList(mice, np2, vp), path.getTargetToAncestorPath()); path = TreebankNodeUtil.getPath(mice, chased); Assert.assertEquals(vp, path.getCommonAncestor()); Assert.assertEquals(Arrays.asList(mice, np2), path.getSourceToAncestorPath()); Assert.assertEquals(Arrays.asList(chased), path.getTargetToAncestorPath()); path = TreebankNodeUtil.getPath(the, the); Assert.assertEquals(the, path.getCommonAncestor()); Assert.assertEquals(Arrays.asList(), path.getSourceToAncestorPath()); Assert.assertEquals(Arrays.asList(), path.getTargetToAncestorPath()); path = TreebankNodeUtil.getPath(root, period); Assert.assertEquals(root, path.getCommonAncestor()); Assert.assertEquals(Arrays.asList(), path.getSourceToAncestorPath()); Assert.assertEquals(Arrays.asList(period), path.getTargetToAncestorPath()); }