blob: 0f682985bffb99a4da9003a53957ef010ed2f7eb [file] [log] [blame]
Joachim Bingel4b405f52013-11-15 15:29:30 +00001package de.ids_mannheim.korap.query.serialize;
2
Michael Hanl19be2922013-12-02 20:10:54 +00003import com.fasterxml.jackson.core.JsonGenerationException;
4import com.fasterxml.jackson.databind.JsonMappingException;
5import com.fasterxml.jackson.databind.ObjectMapper;
Michael Hanl9ca5edd2013-12-06 05:13:24 +00006import org.slf4j.LoggerFactory;
Joachim Bingel4b405f52013-11-15 15:29:30 +00007
Michael Hanl44c73f02013-12-06 03:12:52 +00008import java.io.File;
9import java.io.IOException;
10import java.util.List;
11import java.util.Map;
Joachim Bingelb5ada902013-11-19 14:46:04 +000012
Michael Hanl44c73f02013-12-06 03:12:52 +000013/**
14 * @author bingel, hanl
15 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000016public class QuerySerializer {
Joachim Bingel4b405f52013-11-15 15:29:30 +000017
Michael Hanl698da8c2013-12-08 21:12:36 +000018 private ObjectMapper mapper;
19 private AbstractSyntaxTree ast;
Michael Hanl9ca5edd2013-12-06 05:13:24 +000020 private org.slf4j.Logger log = LoggerFactory
Michael Hanl4fe41cc2013-12-10 17:59:51 +000021 .getLogger(QuerySerializer.class);
Joachim Bingel4b405f52013-11-15 15:29:30 +000022
Michael Hanl4fe41cc2013-12-10 17:59:51 +000023 public QuerySerializer() {
Michael Hanl44c73f02013-12-06 03:12:52 +000024 mapper = new ObjectMapper();
Michael Hanl44c73f02013-12-06 03:12:52 +000025 }
26
27 /**
28 * @param args
29 */
30 public static void main(String[] args) {
31 /*
32 * just for testing...
Joachim Bingel4b405f52013-11-15 15:29:30 +000033 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000034 QuerySerializer jg = new QuerySerializer();
Michael Hanl44c73f02013-12-06 03:12:52 +000035 int i = 0;
36 String[] queries;
37 if (args.length == 0) {
38 queries = new String[]{
Michael Hanl9ca5edd2013-12-06 05:13:24 +000039 /*
Michael Hanl560b4cd2013-12-06 14:26:44 +000040 * negation
Joachim Bingelf143ac92013-12-04 18:52:54 +000041 * elemente
42 * within
43 * regex
44 * & field_group
45 */
Michael Hanl44c73f02013-12-06 03:12:52 +000046 "[base=foo]|([base=foo][base=bar])* meta author=Goethe&year=1815",
47 "([base=foo]|[base=bar])[base=foobar]",
48 "shrink({[base=Mann]})",
49 "shrink({[base=foo]}[orth=bar])",
50 "shrink(1:[base=Der]{1:[base=Mann]})",
51
52 "[base=Katze]",
53 "[base!=Katze]",
54 "[!base=Katze]",
55 "[base=Katze&orth=Katzen]",
56 "[base=Katze][orth=und][orth=Hunde]",
57 "[!(base=Katze&orth=Katzen)]",
58 "contains(<np>,[base=Mann])",
59 "startswith(<np>,[!pos=Det])",
60 "'vers{2,3}uch'",
61 "[orth='vers.*ch']",
62 "[(base=bar|base=foo)&orth=foobar]",
63
64 };
65 } else {
66 queries = new String[]{args[0]};
67 }
68
69 for (String q : queries) {
70 i++;
71 try {
72 System.out.println(q);
73 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
74 System.out.println();
75 } catch (NullPointerException npe) {
76 npe.printStackTrace();
77 System.out.println("null\n");
78 } catch (JsonGenerationException e) {
79 e.printStackTrace();
80 } catch (JsonMappingException e) {
81 e.printStackTrace();
82 } catch (IOException e) {
83 e.printStackTrace();
84 }
85 }
86 }
87
88 /**
Michael Hanl4fe41cc2013-12-10 17:59:51 +000089 * Runs the QuerySerializer by initializing the relevant AbstractSyntaxTree implementation (depending on specified query language)
Michael Hanl44c73f02013-12-06 03:12:52 +000090 * and transforms and writes the tree's requestMap to the specified output file.
91 *
92 * @param outFile The file to which the serialization is written
93 * @param query The query string
94 * @param queryLanguage The query language. As of 13/11/20, this must be either 'poliqarp' or 'poliqarpplus'. Some extra maven stuff needs to done to support CosmasII ('cosmas') [that maven stuff would be to tell maven how to build the cosmas grammar and where to find the classes]
95 * @throws JsonGenerationException
96 * @throws JsonMappingException
97 * @throws IOException
98 */
99 public void run(String query, String queryLanguage, String outFile)
100 throws JsonGenerationException, JsonMappingException, IOException {
101 if (queryLanguage.equals("poliqarp")) {
102 ast = new PoliqarpPlusTree(query);
103// } else if (queryLanguage.equals("cosmas")) {
104// ast = new CosmasTree(query);
105 } else if (queryLanguage.equals("poliqarpplus")) {
106 ast = new PoliqarpPlusTree(query);
107 } else {
108 throw new IllegalArgumentException(queryLanguage + " is not a supported query language!");
109 }
110 Map<String, Object> requestMap = ast.getRequestMap();
111 mapper.writeValue(new File(outFile), requestMap);
112 }
113
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000114 public String run(String query, String ql, List<String> parents,
Michael Hanl8c8c2bd2013-12-07 01:37:08 +0000115 String cli, String cri, int cls, int crs, int page, int num)
116 throws IllegalArgumentException{
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000117 if (ql.toLowerCase().equals("poliqarp")) {
Michael Hanl44c73f02013-12-06 03:12:52 +0000118 ast = new PoliqarpPlusTree(query);
Michael Hanl698da8c2013-12-08 21:12:36 +0000119// } else if (ql.toLowerCase().equals("cosmas")) {
Michael Hanl44c73f02013-12-06 03:12:52 +0000120// ast = new CosmasTree(query);
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000121 } else if (ql.toLowerCase().equals("poliqarpplus")) {
Michael Hanl44c73f02013-12-06 03:12:52 +0000122 ast = new PoliqarpPlusTree(query);
123 } else {
124 throw new IllegalArgumentException(ql + " is not a supported query language!");
125 }
126 Map<String, Object> requestMap = ast.getRequestMap();
Michael Hanl04bb2b92013-12-06 23:18:14 +0000127 MetaQuery metaQuery = new MetaQuery();
128 metaQuery.addResources(parents);
129
Michael Hanl44c73f02013-12-06 03:12:52 +0000130 try {
Michael Hanl04bb2b92013-12-06 23:18:14 +0000131 requestMap.put("meta", metaQuery.raw());
Michael Hanl4fe41cc2013-12-10 17:59:51 +0000132 requestMap = QueryUtils.addParameters(requestMap, page, num,
Michael Hanl44c73f02013-12-06 03:12:52 +0000133 cli, cri, cls, crs);
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000134 String res = mapper.writeValueAsString(requestMap);
135 return res;
Michael Hanl44c73f02013-12-06 03:12:52 +0000136 } catch (IOException e) {
137 e.printStackTrace();
138 return null;
139 }
140
141 }
Joachim Bingel4b405f52013-11-15 15:29:30 +0000142}