blob: 00be4161df275c1c915f9ae93455c8eab528de51 [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
Joachim Bingel639df9c2014-05-12 15:27:00 +0000152 query = "node & node & #2 ->label[mate/coref=\"true\"] #1";
153 String dom2 =
154 "{@type=korap:group, operation=operation:relation, operands=[" +
155 "{@type=korap:span}," +
156 "{@type=korap:span}" +
157 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, foundry=mate, layer=coref, key=true, match=match:eq}]}" +
158 "}";
159 aqlt = new AqlTree(query);
160 map = aqlt.getRequestMap().get("query").toString();
161 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
162
Joachim Bingel13421ef2014-05-07 16:22:58 +0000163 }
164
165 @Test
166 public void testSequence() throws QueryException {
167 query = "node & node & #1 . #2";
168 String dom1 =
169 "{@type=korap:group, operation=operation:sequence, " +
170 "operands=[" +
171 "{@type=korap:span}," +
172 "{@type=korap:span}" +
173 "], inOrder=true" +
174 "}";
175 aqlt = new AqlTree(query);
176 map = aqlt.getRequestMap().get("query").toString();
177 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
178
179 query = "node & node & #1 .* #2";
180 String dom2 =
181 "{@type=korap:group, operation=operation:sequence, " +
182 "operands=[" +
183 "{@type=korap:span}," +
184 "{@type=korap:span}" +
185 "], distances=[" +
186 "{@type=korap:distance, key=w, min=0, max=100}" +
187 "], inOrder=true" +
188 "}";
189 aqlt = new AqlTree(query);
190 map = aqlt.getRequestMap().get("query").toString();
191 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
192
193 }
194
Joachim Bingeleee549e2014-04-29 11:15:37 +0000195
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000196}
197