| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.serialize; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | import java.util.regex.Matcher; |
| 6 | import java.util.regex.Pattern; |
| 7 | |
| 8 | import org.antlr.runtime.Parser; |
| 9 | import org.antlr.runtime.tree.Tree; |
| 10 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 11 | /** |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 12 | * This class is provides methods for navigation and search in |
| 13 | * Abstract Syntax |
| 14 | * Trees returned by ANTLR v3 parsers, using ANTLR v3 libraries. Any |
| 15 | * class that |
| 16 | * extends this abstract class will thus be equipped with such |
| 17 | * methods, |
| 18 | * which makes it easier to, e.g., retrieve children of a specific |
| 19 | * category |
| 20 | * for some node. |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 21 | * |
| 22 | * @author Joachim Bingel (bingel@ids-mannheim.de) |
| Joachim Bingel | 7cb346e | 2015-03-09 10:56:20 +0100 | [diff] [blame] | 23 | * @version 0.3.0 |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 24 | * @since 0.1.0 |
| 25 | */ |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 26 | public abstract class Antlr3AbstractQueryProcessor extends |
| 27 | AbstractQueryProcessor { |
| 28 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 29 | /** |
| 30 | * The ANTLR parser. Subclasses need to instantiate this field. |
| 31 | */ |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 32 | protected Parser parser; |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 33 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 34 | |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 35 | /** |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 36 | * Returns the category (or 'label') of the root of a (sub-) |
| 37 | * ParseTree (ANTLR 3). |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 38 | * |
| 39 | * @param node |
| 40 | * The tree node. |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 41 | * @return The category of the node. |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 42 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 43 | protected static String getNodeCat (Tree node) { |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 44 | String nodeCat = node.toStringTree(); |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 45 | // from opening parenthesis to 1st whitespace |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 46 | Pattern p = Pattern.compile("\\((.*?)\\s"); |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 47 | Matcher m = p.matcher(node.toStringTree()); |
| 48 | if (m.find()) { |
| 49 | nodeCat = m.group(1); |
| 50 | } |
| 51 | return nodeCat; |
| 52 | } |
| 53 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 54 | |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 55 | /** |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 56 | * Tests whether a certain node has a child of a certain category. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 57 | * |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 58 | * @param node |
| 59 | * The parent node. |
| 60 | * @param childCat |
| 61 | * The category of the potential child. |
| 62 | * @return true iff one or more children belong to the specified |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 63 | * category. |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 64 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 65 | protected static boolean hasChild (Tree node, String childCat) { |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 66 | for (int i = 0; i < node.getChildCount(); i++) { |
| 67 | if (getNodeCat(node.getChild(i)).equals(childCat)) { |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | return false; |
| 72 | } |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 73 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 74 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 75 | /** |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 76 | * Tests whether a certain node has a descendant (direct or |
| 77 | * indirect child) |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 78 | * of a certain category. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 79 | * |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 80 | * @param node |
| 81 | * The parent node. |
| 82 | * @param childCat |
| 83 | * The category of the potential descendant. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 84 | * @return true iff one or more descendants belong to the |
| 85 | * specified |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 86 | * category. |
| 87 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 88 | protected static boolean hasDescendantWithCat (Tree node, String childCat) { |
| Joachim Bingel | 0e54d22 | 2015-01-12 13:22:16 +0000 | [diff] [blame] | 89 | for (int i = 0; i < node.getChildCount(); i++) { |
| 90 | Tree child = node.getChild(i); |
| 91 | if (getNodeCat(child).equals(childCat)) { |
| 92 | return true; |
| 93 | } |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 94 | if (hasDescendantWithCat(child, childCat)) { |
| Joachim Bingel | 0e54d22 | 2015-01-12 13:22:16 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | return false; |
| 99 | } |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 100 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 101 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 102 | /** |
| 103 | * Returns all children of a node. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 104 | * |
| 105 | * @param node |
| 106 | * The node. |
| 107 | * @return A list containing all children. |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 108 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 109 | protected static List<Tree> getChildren (Tree node) { |
| Joachim Bingel | 896067a | 2014-11-07 19:02:27 +0000 | [diff] [blame] | 110 | ArrayList<Tree> children = new ArrayList<Tree>(); |
| 111 | for (int i = 0; i < node.getChildCount(); i++) { |
| 112 | children.add(node.getChild(i)); |
| 113 | } |
| 114 | return children; |
| 115 | } |
| 116 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 117 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 118 | /** |
| 119 | * Returns all children of a node which are of a given category. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 120 | * |
| 121 | * @param node |
| 122 | * The node. |
| 123 | * @param nodeCat |
| 124 | * The node category constraining the returned |
| 125 | * children. |
| 126 | * @return A (possibly empty) list containing all children of the |
| 127 | * given |
| 128 | * category. |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 129 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 130 | protected static List<Tree> getChildrenWithCat (Tree node, String nodeCat) { |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 131 | ArrayList<Tree> children = new ArrayList<Tree>(); |
| 132 | for (int i = 0; i < node.getChildCount(); i++) { |
| 133 | if (getNodeCat(node.getChild(i)).equals(nodeCat)) { |
| 134 | children.add(node.getChild(i)); |
| 135 | } |
| 136 | } |
| 137 | return children; |
| 138 | } |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 139 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 140 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 141 | /** |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 142 | * Returns all descendants (direct or indirect children) of a node |
| 143 | * which |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 144 | * are of a given category. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 145 | * |
| 146 | * @param node |
| 147 | * The node. |
| 148 | * @param nodeCat |
| 149 | * The node category constraining the returned |
| 150 | * descendants. |
| 151 | * @return A (possibly empty) list containing all descendants of |
| 152 | * the given |
| 153 | * category. |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 154 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 155 | protected List<Tree> getDescendantsWithCat (Tree node, String nodeCat) { |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 156 | ArrayList<Tree> descendants = new ArrayList<Tree>(); |
| 157 | for (Tree child : getChildren(node)) { |
| 158 | if (getNodeCat(child).equals(nodeCat)) { |
| 159 | descendants.add(child); |
| 160 | } |
| 161 | descendants.addAll(getDescendantsWithCat(child, nodeCat)); |
| 162 | } |
| 163 | return descendants; |
| 164 | } |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 165 | |
| 166 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 167 | /** |
| 168 | * Returns the first child of a node which is of a given category. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 169 | * |
| 170 | * @param node |
| 171 | * The node. |
| 172 | * @param nodeCat |
| 173 | * The node category constraining the returned child. |
| 174 | * @return The first child with the given category, <tt>null</tt> |
| 175 | * if no |
| 176 | * such child exists. |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 177 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 178 | protected static Tree getFirstChildWithCat (Tree node, String nodeCat) { |
| Joachim Bingel | 0e54d22 | 2015-01-12 13:22:16 +0000 | [diff] [blame] | 179 | return getNthChildWithCat(node, nodeCat, 1); |
| 180 | } |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 181 | |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 182 | |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 183 | /** |
| 184 | * Returns the nth child of a node which is of a given category. |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 185 | * |
| 186 | * @param node |
| 187 | * The node. |
| 188 | * @param nodeCat |
| 189 | * The node category constraining the returned child. |
| 190 | * @param n |
| 191 | * The index of the child to return, among all children |
| 192 | * with the |
| 193 | * given category. |
| 194 | * @return The nth child with the given category, <tt>null</tt> if |
| 195 | * no |
| 196 | * such child exists (i.e., if n is larger than the number |
| 197 | * of children |
| 198 | * with the given category). |
| Joachim Bingel | a145c98 | 2015-02-18 18:31:57 +0100 | [diff] [blame] | 199 | */ |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 200 | protected static Tree getNthChildWithCat (Tree node, String nodeCat, int n) { |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 201 | int counter = 0; |
| 202 | for (int i = 0; i < node.getChildCount(); i++) { |
| 203 | if (getNodeCat(node.getChild(i)).equals(nodeCat)) { |
| 204 | counter++; |
| 205 | if (counter == n) { |
| 206 | return node.getChild(i); |
| 207 | } |
| 208 | } |
| 209 | } |
| Joachim Bingel | c8a28e4 | 2014-04-24 15:06:42 +0000 | [diff] [blame] | 210 | return null; |
| 211 | } |
| 212 | } |