blob: 15a5c0b322e6a25f8f4373222555f765d4b56ee4 [file] [log] [blame]
Joachim Bingel019ba5c2014-04-28 14:59:04 +00001import static org.junit.Assert.*;
2
3import org.junit.Test;
4
5import de.ids_mannheim.korap.query.serialize.AqlTree;
Joachim Bingel019ba5c2014-04-28 14:59:04 +00006import de.ids_mannheim.korap.util.QueryException;
7
8public class AqlTreeTest {
9
10 AqlTree aqlt;
11 String map;
12 private String query;
13
14 private boolean equalsQueryContent(String res, String query) throws QueryException {
15 res = res.replaceAll(" ", "");
16 aqlt = new AqlTree(query);
17 String queryMap = aqlt.getRequestMap().get("query").toString().replaceAll(" ", "");
18 return res.equals(queryMap);
19 }
20
21 @Test
22 public void testContext() throws QueryException {
23 String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
24 aqlt = new AqlTree("Test");
25 assertEquals(contextString.replaceAll(" ", ""), aqlt.getRequestMap().get("@context").toString().replaceAll(" ", ""));
26 }
27
28 @Test
29 public void testSingleTokens() throws QueryException {
30 // "Mann"
31 query = "\"Mann\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000032 String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000033 assertTrue(equalsQueryContent(token1, query));
34
35 // [orth!=Frau]
36 query = "tok!=\"Frau\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000037 String token2 = "{@type=korap:token, wrap={@type=korap:term, key=Frau, match=match:ne}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000038 assertTrue(equalsQueryContent(token2, query));
39
40 // Mann
41 query = "Mann";
42 String token4 = "{@type=korap:span, layer=Mann}";
43 assertTrue(equalsQueryContent(token4, query));
44 }
45
46 @Test
47 public void testSpans() throws QueryException {
48 query = "node";
49 String span1 =
50 "{@type=korap:span}";
51 aqlt = new AqlTree(query);
52 map = aqlt.getRequestMap().get("query").toString();
53 assertEquals(span1.replaceAll(" ", ""), map.replaceAll(" ", ""));
54 }
55
56 @Test
57 public void testRegex() throws QueryException {
58 query = "/Mann/";
Joachim Bingeleee549e2014-04-29 11:15:37 +000059 String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000060 aqlt = new AqlTree(query);
61 map = aqlt.getRequestMap().get("query").toString();
62 assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
63 }
64
65 @Test
66 public void testLayers() throws QueryException {
67 query = "cnx/cat=\"NP\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000068 String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000069 aqlt = new AqlTree(query);
70 map = aqlt.getRequestMap().get("query").toString();
71 assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", ""));
72
73 query = "treetagger/pos=\"NN\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000074 String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000075 aqlt = new AqlTree(query);
76 map = aqlt.getRequestMap().get("query").toString();
77 assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", ""));
78 }
79
80 @Test
81 public void testSimpleDominance() throws QueryException {
82 query = "node & node & #2 > #1";
83 String dom1 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000084 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel019ba5c2014-04-28 14:59:04 +000085 "{@type=korap:span}," +
86 "{@type=korap:span}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000087 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +000088 "}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000089 aqlt = new AqlTree(query);
90 map = aqlt.getRequestMap().get("query").toString();
91 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeleee549e2014-04-29 11:15:37 +000092
93 query = "\"Mann\" & node & #2 > #1";
94 String dom2 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000095 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +000096 "{@type=korap:span}," +
97 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000098 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +000099 "}";
100 aqlt = new AqlTree(query);
101 map = aqlt.getRequestMap().get("query").toString();
102 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
103
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000104 query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000105 String dom3 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000106 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000107 "{@type=korap:span}," +
108 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000109 "], relation={@type=korap:treeRelation, reltype=dominance, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000110 "}";
111 aqlt = new AqlTree(query);
112 map = aqlt.getRequestMap().get("query").toString();
113 assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", ""));
114
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000115 query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000116 String dom4 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000117 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000118 "{@type=korap:span}," +
119 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000120 "], relation={@type=korap:treeRelation, reltype=dominance, index=0, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000121 "}";
122 aqlt = new AqlTree(query);
123 map = aqlt.getRequestMap().get("query").toString();
124 assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000125
126 query = "\"Mann\" & node & #2 >2,4 #1";
127 String dom5 =
128 "{@type=korap:group, operation=operation:relation, operands=[" +
129 "{@type=korap:span}," +
130 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
131 "], relation={@type=korap:treeRelation, reltype=dominance, " +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000132 "distance={@type=korap:distance, key=r, min=2, max=4}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000133 "}";
134 aqlt = new AqlTree(query);
135 map = aqlt.getRequestMap().get("query").toString();
136 assertEquals(dom5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000137 }
138
Joachim Bingel13421ef2014-05-07 16:22:58 +0000139 @Test
140 public void testPointingRelations() throws QueryException {
141 query = "node & node & #2 ->label[coref=\"true\"] #1";
142 String dom1 =
143 "{@type=korap:group, operation=operation:relation, operands=[" +
144 "{@type=korap:span}," +
145 "{@type=korap:span}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000146 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, layer=coref, key=true, match=match:eq}]}" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000147 "}";
148 aqlt = new AqlTree(query);
149 map = aqlt.getRequestMap().get("query").toString();
150 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
151
152 }
153
154 @Test
155 public void testSequence() throws QueryException {
156 query = "node & node & #1 . #2";
157 String dom1 =
158 "{@type=korap:group, operation=operation:sequence, " +
159 "operands=[" +
160 "{@type=korap:span}," +
161 "{@type=korap:span}" +
162 "], inOrder=true" +
163 "}";
164 aqlt = new AqlTree(query);
165 map = aqlt.getRequestMap().get("query").toString();
166 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
167
168 query = "node & node & #1 .* #2";
169 String dom2 =
170 "{@type=korap:group, operation=operation:sequence, " +
171 "operands=[" +
172 "{@type=korap:span}," +
173 "{@type=korap:span}" +
174 "], distances=[" +
175 "{@type=korap:distance, key=w, min=0, max=100}" +
176 "], inOrder=true" +
177 "}";
178 aqlt = new AqlTree(query);
179 map = aqlt.getRequestMap().get("query").toString();
180 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
181
182 }
183
Joachim Bingeleee549e2014-04-29 11:15:37 +0000184
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000185}
186