| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.serialize; |
| 2 | |
| Michael Hanl | 19be292 | 2013-12-02 20:10:54 +0000 | [diff] [blame] | 3 | import com.fasterxml.jackson.core.JsonGenerationException; |
| 4 | import com.fasterxml.jackson.databind.JsonMappingException; |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 6 | import org.slf4j.LoggerFactory; |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 7 | |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 8 | import java.io.File; |
| 9 | import java.io.IOException; |
| 10 | import java.util.List; |
| 11 | import java.util.Map; |
| Joachim Bingel | b5ada90 | 2013-11-19 14:46:04 +0000 | [diff] [blame] | 12 | |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 13 | /** |
| 14 | * @author bingel, hanl |
| 15 | */ |
| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 16 | public class QuerySerializer { |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 17 | |
| Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 18 | private ObjectMapper mapper; |
| 19 | private AbstractSyntaxTree ast; |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 20 | private org.slf4j.Logger log = LoggerFactory |
| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 21 | .getLogger(QuerySerializer.class); |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 22 | |
| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 23 | public QuerySerializer() { |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 24 | mapper = new ObjectMapper(); |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param args |
| 29 | */ |
| 30 | public static void main(String[] args) { |
| 31 | /* |
| 32 | * just for testing... |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 33 | */ |
| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 34 | QuerySerializer jg = new QuerySerializer(); |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 35 | int i = 0; |
| 36 | String[] queries; |
| 37 | if (args.length == 0) { |
| 38 | queries = new String[]{ |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 39 | /* |
| Michael Hanl | 560b4cd | 2013-12-06 14:26:44 +0000 | [diff] [blame] | 40 | * negation |
| Joachim Bingel | f143ac9 | 2013-12-04 18:52:54 +0000 | [diff] [blame] | 41 | * elemente |
| 42 | * within |
| 43 | * regex |
| 44 | * & field_group |
| 45 | */ |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 46 | "[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 Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 89 | * Runs the QuerySerializer by initializing the relevant AbstractSyntaxTree implementation (depending on specified query language) |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 90 | * 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 Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 114 | public String run(String query, String ql, List<String> parents, |
| Michael Hanl | 8c8c2bd | 2013-12-07 01:37:08 +0000 | [diff] [blame] | 115 | String cli, String cri, int cls, int crs, int page, int num) |
| 116 | throws IllegalArgumentException{ |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 117 | if (ql.toLowerCase().equals("poliqarp")) { |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 118 | ast = new PoliqarpPlusTree(query); |
| Michael Hanl | 698da8c | 2013-12-08 21:12:36 +0000 | [diff] [blame] | 119 | // } else if (ql.toLowerCase().equals("cosmas")) { |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 120 | // ast = new CosmasTree(query); |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 121 | } else if (ql.toLowerCase().equals("poliqarpplus")) { |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 122 | 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 Hanl | 04bb2b9 | 2013-12-06 23:18:14 +0000 | [diff] [blame] | 127 | MetaQuery metaQuery = new MetaQuery(); |
| 128 | metaQuery.addResources(parents); |
| 129 | |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 130 | try { |
| Michael Hanl | 04bb2b9 | 2013-12-06 23:18:14 +0000 | [diff] [blame] | 131 | requestMap.put("meta", metaQuery.raw()); |
| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 132 | requestMap = QueryUtils.addParameters(requestMap, page, num, |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 133 | cli, cri, cls, crs); |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 134 | String res = mapper.writeValueAsString(requestMap); |
| 135 | return res; |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 136 | } catch (IOException e) { |
| 137 | e.printStackTrace(); |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | } |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 142 | } |