Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 1 | import static org.junit.Assert.*; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import de.ids_mannheim.korap.query.serialize.AqlTree; |
| 6 | import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree; |
| 7 | import de.ids_mannheim.korap.util.QueryException; |
| 8 | |
| 9 | public class AqlTreeTest { |
| 10 | |
| 11 | AqlTree aqlt; |
| 12 | String map; |
| 13 | private String query; |
| 14 | |
| 15 | private boolean equalsQueryContent(String res, String query) throws QueryException { |
| 16 | res = res.replaceAll(" ", ""); |
| 17 | aqlt = new AqlTree(query); |
| 18 | String queryMap = aqlt.getRequestMap().get("query").toString().replaceAll(" ", ""); |
| 19 | return res.equals(queryMap); |
| 20 | } |
| 21 | |
| 22 | @Test |
| 23 | public void testContext() throws QueryException { |
| 24 | String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld"; |
| 25 | aqlt = new AqlTree("Test"); |
| 26 | assertEquals(contextString.replaceAll(" ", ""), aqlt.getRequestMap().get("@context").toString().replaceAll(" ", "")); |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | public void testSingleTokens() throws QueryException { |
| 31 | // "Mann" |
| 32 | query = "\"Mann\""; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 33 | String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 34 | assertTrue(equalsQueryContent(token1, query)); |
| 35 | |
| 36 | // [orth!=Frau] |
| 37 | query = "tok!=\"Frau\""; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 38 | String token2 = "{@type=korap:token, wrap={@type=korap:term, key=Frau, match=match:ne}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 39 | assertTrue(equalsQueryContent(token2, query)); |
| 40 | |
| 41 | // Mann |
| 42 | query = "Mann"; |
| 43 | String token4 = "{@type=korap:span, layer=Mann}"; |
| 44 | assertTrue(equalsQueryContent(token4, query)); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testSpans() throws QueryException { |
| 49 | query = "node"; |
| 50 | String span1 = |
| 51 | "{@type=korap:span}"; |
| 52 | aqlt = new AqlTree(query); |
| 53 | map = aqlt.getRequestMap().get("query").toString(); |
| 54 | assertEquals(span1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testRegex() throws QueryException { |
| 59 | query = "/Mann/"; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 60 | String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 61 | aqlt = new AqlTree(query); |
| 62 | map = aqlt.getRequestMap().get("query").toString(); |
| 63 | assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testLayers() throws QueryException { |
| 68 | query = "cnx/cat=\"NP\""; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 69 | String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 70 | aqlt = new AqlTree(query); |
| 71 | map = aqlt.getRequestMap().get("query").toString(); |
| 72 | assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 73 | |
| 74 | query = "treetagger/pos=\"NN\""; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 75 | String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 76 | aqlt = new AqlTree(query); |
| 77 | map = aqlt.getRequestMap().get("query").toString(); |
| 78 | assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testSimpleDominance() throws QueryException { |
| 83 | query = "node & node & #2 > #1"; |
| 84 | String dom1 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 85 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 86 | "{@type=korap:span}," + |
| 87 | "{@type=korap:span}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 88 | "], relation={@type=korap:treeRelation, reltype=dominance}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 89 | "}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 90 | aqlt = new AqlTree(query); |
| 91 | map = aqlt.getRequestMap().get("query").toString(); |
| 92 | assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 93 | |
| 94 | query = "\"Mann\" & node & #2 > #1"; |
| 95 | String dom2 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 96 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 97 | "{@type=korap:span}," + |
| 98 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 99 | "], relation={@type=korap:treeRelation, reltype=dominance}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 100 | "}"; |
| 101 | aqlt = new AqlTree(query); |
| 102 | map = aqlt.getRequestMap().get("query").toString(); |
| 103 | assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 104 | |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 105 | query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1"; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 106 | String dom3 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 107 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 108 | "{@type=korap:span}," + |
| 109 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 110 | "], relation={@type=korap:treeRelation, reltype=dominance, edges=[{@type=korap:edge, label=cat, key=NP, match=match:eq}]}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 111 | "}"; |
| 112 | aqlt = new AqlTree(query); |
| 113 | map = aqlt.getRequestMap().get("query").toString(); |
| 114 | assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 115 | |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 116 | query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1"; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 117 | String dom4 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 118 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 119 | "{@type=korap:span}," + |
| 120 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 121 | "], relation={@type=korap:treeRelation, reltype=dominance, index=0, edges=[{@type=korap:edge, label=cat, key=NP, match=match:eq}]}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 122 | "}"; |
| 123 | aqlt = new AqlTree(query); |
| 124 | map = aqlt.getRequestMap().get("query").toString(); |
| 125 | assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 126 | |
| 127 | query = "\"Mann\" & node & #2 >2,4 #1"; |
| 128 | String dom5 = |
| 129 | "{@type=korap:group, operation=operation:relation, operands=[" + |
| 130 | "{@type=korap:span}," + |
| 131 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
| 132 | "], relation={@type=korap:treeRelation, reltype=dominance, " + |
| 133 | "boundary={@type=korap:boundary, min=2, max=4}}" + |
| 134 | "}"; |
| 135 | aqlt = new AqlTree(query); |
| 136 | map = aqlt.getRequestMap().get("query").toString(); |
| 137 | assertEquals(dom5.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 140 | |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 141 | } |
| 142 | |