blob: ccb9eafbd6b6065dbe8b4f9cffda7f3eae6d431a [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;
6import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree;
7import de.ids_mannheim.korap.util.QueryException;
8
9public 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 Bingeleee549e2014-04-29 11:15:37 +000033 String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000034 assertTrue(equalsQueryContent(token1, query));
35
36 // [orth!=Frau]
37 query = "tok!=\"Frau\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000038 String token2 = "{@type=korap:token, wrap={@type=korap:term, key=Frau, match=match:ne}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000039 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 Bingeleee549e2014-04-29 11:15:37 +000060 String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000061 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 Bingeleee549e2014-04-29 11:15:37 +000069 String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000070 aqlt = new AqlTree(query);
71 map = aqlt.getRequestMap().get("query").toString();
72 assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", ""));
73
74 query = "treetagger/pos=\"NN\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000075 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 +000076 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 Bingeld4ae5fd2014-04-29 15:00:16 +000085 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel019ba5c2014-04-28 14:59:04 +000086 "{@type=korap:span}," +
87 "{@type=korap:span}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000088 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +000089 "}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000090 aqlt = new AqlTree(query);
91 map = aqlt.getRequestMap().get("query").toString();
92 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeleee549e2014-04-29 11:15:37 +000093
94 query = "\"Mann\" & node & #2 > #1";
95 String dom2 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000096 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +000097 "{@type=korap:span}," +
98 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +000099 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000100 "}";
101 aqlt = new AqlTree(query);
102 map = aqlt.getRequestMap().get("query").toString();
103 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
104
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000105 query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000106 String dom3 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000107 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000108 "{@type=korap:span}," +
109 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000110 "], relation={@type=korap:treeRelation, reltype=dominance, edges=[{@type=korap:edge, label=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000111 "}";
112 aqlt = new AqlTree(query);
113 map = aqlt.getRequestMap().get("query").toString();
114 assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", ""));
115
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000116 query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000117 String dom4 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000118 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000119 "{@type=korap:span}," +
120 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000121 "], relation={@type=korap:treeRelation, reltype=dominance, index=0, edges=[{@type=korap:edge, label=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000122 "}";
123 aqlt = new AqlTree(query);
124 map = aqlt.getRequestMap().get("query").toString();
125 assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000126
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 Bingel019ba5c2014-04-28 14:59:04 +0000138 }
139
Joachim Bingeleee549e2014-04-29 11:15:37 +0000140
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000141}
142