startpage, contexts and page length serialization. convenience methods
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java b/src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java
index e8a8c33..61f388b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java
@@ -1,62 +1,40 @@
package de.ids_mannheim.korap.query.serialize;
-import java.io.File;
-import java.io.IOException;
-import java.util.Map;
-
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.ids_mannheim.korap.query.serialize.AbstractSyntaxTree;
-import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+/**
+ * @author bingel, hanl
+ */
public class JsonGenerator {
- ObjectMapper mapper;
- AbstractSyntaxTree ast;
-
- public JsonGenerator() {
- mapper = new ObjectMapper();
- }
+ ObjectMapper mapper;
+ AbstractSyntaxTree ast;
+ private Serializer serializer;
- /**
- * Runs the JsonGenerator by initializing the relevant AbstractSyntaxTree implementation (depending on specified query language)
- * and transforms and writes the tree's requestMap to the specified output file.
- * @param outFile The file to which the serialization is written
- * @param query The query string
- * @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]
- * @throws JsonGenerationException
- * @throws JsonMappingException
- * @throws IOException
- */
- public void run(String query, String queryLanguage, String outFile)
- throws JsonGenerationException, JsonMappingException, IOException {
- if (queryLanguage.equals("poliqarp")) {
- ast = new PoliqarpPlusTree(query);
-// } else if (queryLanguage.equals("cosmas")) {
-// ast = new CosmasTree(query);
- } else if (queryLanguage.equals("poliqarpplus")) {
- ast = new PoliqarpPlusTree(query);
- } else {
- throw new IllegalArgumentException(queryLanguage+ " is not a supported query language!");
- }
- Map<String, Object> requestMap = ast.getRequestMap();
- mapper.writeValue(new File(outFile), requestMap);
- }
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- /*
- * just for testing...
+ public JsonGenerator() {
+ mapper = new ObjectMapper();
+ serializer = new Serializer();
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ /*
+ * just for testing...
*/
- JsonGenerator jg = new JsonGenerator();
- int i=0;
- String[] queries;
- if (args.length==0) {
- queries = new String[] {
+ JsonGenerator jg = new JsonGenerator();
+ int i = 0;
+ String[] queries;
+ if (args.length == 0) {
+ queries = new String[]{
/*
* negation
* elemente
@@ -64,45 +42,96 @@
* regex
* & field_group
*/
- "[base=foo]|([base=foo][base=bar])* meta author=Goethe&year=1815",
- "([base=foo]|[base=bar])[base=foobar]",
- "shrink({[base=Mann]})",
- "shrink({[base=foo]}[orth=bar])",
- "shrink(1:[base=Der]{1:[base=Mann]})",
-
- "[base=Katze]",
- "[base!=Katze]",
- "[!base=Katze]",
- "[base=Katze&orth=Katzen]",
- "[base=Katze][orth=und][orth=Hunde]",
- "[!(base=Katze&orth=Katzen)]",
- "contains(<np>,[base=Mann])",
- "startswith(<np>,[!pos=Det])",
- "'vers{2,3}uch'",
- "[orth='vers.*ch']",
- "[(base=bar|base=foo)&orth=foobar]",
-
- };
- } else {
- queries = new String[] {args[0]};
- }
-
- for (String q : queries) {
- i++;
- try {
- System.out.println(q);
- jg.run(q, "poliqarp", System.getProperty("user.home")+"/bsp"+i+".json");
- System.out.println();
- } catch (NullPointerException npe) {
- npe.printStackTrace();
- System.out.println("null\n");
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
+ "[base=foo]|([base=foo][base=bar])* meta author=Goethe&year=1815",
+ "([base=foo]|[base=bar])[base=foobar]",
+ "shrink({[base=Mann]})",
+ "shrink({[base=foo]}[orth=bar])",
+ "shrink(1:[base=Der]{1:[base=Mann]})",
+
+ "[base=Katze]",
+ "[base!=Katze]",
+ "[!base=Katze]",
+ "[base=Katze&orth=Katzen]",
+ "[base=Katze][orth=und][orth=Hunde]",
+ "[!(base=Katze&orth=Katzen)]",
+ "contains(<np>,[base=Mann])",
+ "startswith(<np>,[!pos=Det])",
+ "'vers{2,3}uch'",
+ "[orth='vers.*ch']",
+ "[(base=bar|base=foo)&orth=foobar]",
+
+ };
+ } else {
+ queries = new String[]{args[0]};
+ }
+
+ for (String q : queries) {
+ i++;
+ try {
+ System.out.println(q);
+ jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
+ System.out.println();
+ } catch (NullPointerException npe) {
+ npe.printStackTrace();
+ System.out.println("null\n");
+ } catch (JsonGenerationException e) {
+ e.printStackTrace();
+ } catch (JsonMappingException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Runs the JsonGenerator by initializing the relevant AbstractSyntaxTree implementation (depending on specified query language)
+ * and transforms and writes the tree's requestMap to the specified output file.
+ *
+ * @param outFile The file to which the serialization is written
+ * @param query The query string
+ * @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]
+ * @throws JsonGenerationException
+ * @throws JsonMappingException
+ * @throws IOException
+ */
+ public void run(String query, String queryLanguage, String outFile)
+ throws JsonGenerationException, JsonMappingException, IOException {
+ if (queryLanguage.equals("poliqarp")) {
+ ast = new PoliqarpPlusTree(query);
+// } else if (queryLanguage.equals("cosmas")) {
+// ast = new CosmasTree(query);
+ } else if (queryLanguage.equals("poliqarpplus")) {
+ ast = new PoliqarpPlusTree(query);
+ } else {
+ throw new IllegalArgumentException(queryLanguage + " is not a supported query language!");
+ }
+ Map<String, Object> requestMap = ast.getRequestMap();
+ mapper.writeValue(new File(outFile), requestMap);
+ }
+
+ public String run(String query, String ql, List<String> meta,
+ String cli, String cri, int cls, int crs, int page, int num) {
+ if (ql.equals("poliqarp")) {
+ ast = new PoliqarpPlusTree(query);
+// } else if (queryLanguage.equals("cosmas")) {
+// ast = new CosmasTree(query);
+ } else if (ql.equals("poliqarpplus")) {
+ ast = new PoliqarpPlusTree(query);
+ } else {
+ throw new IllegalArgumentException(ql + " is not a supported query language!");
+ }
+ Map<String, Object> requestMap = ast.getRequestMap();
+ try {
+ List<Map> meta_re = serializer.serializeResources(meta);
+ requestMap.put("meta", meta_re);
+ requestMap = serializer.addParameters(requestMap, page, num,
+ cli, cri, cls, crs);
+ return mapper.writeValueAsString(requestMap);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ }
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java
index 17b6392..c7d31f1 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java
@@ -64,7 +64,8 @@
//todo: how to handle regex types?
// only handles AND relation between query attributes and values!
// value pair : pubdate=<date>, pubPlace=<place>, etc.
- public List serializeQueries(Map<String, String> queries, TYPE type) {
+ public List<Map> serializeQueries(Map<String, String> queries, TYPE type) {
+ //single is redundant!
boolean extend, single = true; //single = true;
boolean multypes = queries.keySet().size() > 1;
List<Map> metavalue;
@@ -80,8 +81,8 @@
break;
}
- List value = new LinkedList();
- List<String> dates = new LinkedList<>();
+ List value = new ArrayList<>();
+ List<String> dates = new ArrayList<>();
for (String key : queries.keySet()) {
if (!multypes)
def_key = key;
@@ -115,10 +116,7 @@
// }
Map term;
- if (multypes)
- term = types.createTerm(key, null, queries.get(key).trim(), null);
- else
- term = types.createTerm(def_key, null, queries.get(key).trim(), null);
+ term = types.createTerm(key, null, queries.get(key).trim(), null);
value.add(term);
}
@@ -166,7 +164,7 @@
else
metavalue = Arrays.asList(types.createMetaFilter(group));
}
- return metavalue;
+ return new ArrayList<>(metavalue);
}
//todo: resource id must be added!
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaSerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaSerializer.java
deleted file mode 100644
index 1ed0737..0000000
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaSerializer.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package de.ids_mannheim.korap.query.serialize;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author hanl
- * @date 05/12/2013
- */
-public class MetaSerializer {
-
-
- private MetaQuerySerializer qs;
- private MetaCollectionSerializer cs;
- private ObjectMapper mapper;
-
-
- public MetaSerializer() {
- this.qs = new MetaQuerySerializer();
- this.cs = new MetaCollectionSerializer();
- this.mapper = new ObjectMapper();
- }
-
- public String serializeMeta(List m_queries) throws JsonProcessingException {
- Map metas = new HashMap();
- metas.put("meta", m_queries);
- return mapper.writeValueAsString(metas);
- }
-
- public List<Map> serialzeResources(List<String> r_queries) throws IOException {
- return cs.serialize(r_queries);
- }
-
- public List<Map> serializeQueries(Map<String, String> queries, MetaQuerySerializer.TYPE type) {
- return qs.serializeQueries(queries, type);
- }
-}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java
index 125ec1c..fc43a37 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java
@@ -88,11 +88,13 @@
return meta;
}
+ //fixme, two digits!
public String formatDate(long date, String format) {
DateTime time = new DateTime(date);
+ String year, month, day;
switch (format) {
case YM:
- String s = time.getYear() + "-" + time.getMonthOfYear();
+ String s = time.getYear() + "-" + time.getDayOfMonth();
return s;
case YMD:
String s1 = time.getYear() + "-" + time.getMonthOfYear() + "-" + time.getDayOfMonth();
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/Serializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/Serializer.java
new file mode 100644
index 0000000..4cb32b9
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/Serializer.java
@@ -0,0 +1,61 @@
+package de.ids_mannheim.korap.query.serialize;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * @author hanl
+ * @date 05/12/2013
+ */
+public class Serializer {
+
+
+ private MetaQuerySerializer qs;
+ private MetaCollectionSerializer cs;
+ private ObjectMapper mapper;
+
+
+ public Serializer() {
+ this.qs = new MetaQuerySerializer();
+ this.cs = new MetaCollectionSerializer();
+ this.mapper = new ObjectMapper();
+ }
+
+ public String serializeMeta(List m_queries) throws JsonProcessingException {
+ Map metas = new HashMap();
+ metas.put("meta", m_queries);
+ return mapper.writeValueAsString(metas);
+ }
+
+ public List<Map> serializeResources(List<String> r_queries) throws IOException {
+ return cs.serialize(r_queries);
+ }
+
+ public List<Map> serializeQueries(Map<String, String> queries, MetaQuerySerializer.TYPE type) {
+ return qs.serializeQueries(queries, type);
+ }
+
+ public Map addParameters(Map request, int page, int num, String cli, String cri,
+ int cls, int crs) {
+ Map ctx = new LinkedHashMap();
+ List left = new ArrayList();
+ left.add(cli);
+ left.add(cls);
+ List right = new ArrayList();
+ right.add(cri);
+ right.add(crs);
+ ctx.put("left", left);
+ ctx.put("right", right);
+
+ request.put("startPage", page);
+ request.put("count", num);
+ request.put("context", ctx);
+
+ return request;
+
+ }
+
+}