REST query function with json-ld body;
policyrewrite bug fixes;
query serializer builder class;
diff --git a/src/main/java/LayerTranslator.java b/src/main/java/LayerTranslator.java
deleted file mode 100644
index 5ab9783..0000000
--- a/src/main/java/LayerTranslator.java
+++ /dev/null
@@ -1,33 +0,0 @@
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author hanl
- * @date 24/01/2014
- */
-public class LayerTranslator {
-
- //fixme: standardize
- private String[] bases = new String[]{"morph", "lemma", "mds", "dep"};
- private Map<String, String> mapper;
-
-
- public LayerTranslator() {
- mapper = new HashMap<>();
- }
-
- public void set(String m, String l, String mds, String dep) {
- mapper.clear();
- mapper.put(bases[0], m);
- mapper.put(bases[1], l);
- mapper.put(bases[2], mds);
- mapper.put(bases[3], dep);
- }
-
- public String translate(String base) {
- String r = mapper.get(base);
- if (r != null)
- return r;
- return "";
- }
-}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java
index e34610c..6fa6465 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java
@@ -26,6 +26,8 @@
private List<Map> rq;
private Multimap<String, String> mfilter;
private Multimap<String, String> mextension;
+ private Relation simpleFilterRel = Relation.AND;
+ private Relation simpleExtendRel = Relation.AND;
public CollectionQuery() {
@@ -57,8 +59,8 @@
return this;
}
- public CollectionQuery addMetaFilter(String queries, Relation rel) {
- this.mfilter.putAll(resRel(queries, rel));
+ public CollectionQuery addMetaFilterQuery(String queries) {
+ this.mfilter.putAll(resRel(queries));
return this;
}
@@ -67,9 +69,20 @@
return this;
}
- public CollectionQuery addMetaExtend(String queries, Relation rel) {
- this.mextension.putAll(resRel(queries, rel));
+ public CollectionQuery setFilterAttributeRelation(Relation rel) {
+ simpleFilterRel = rel;
+ return this;
+ }
+
+
+ public CollectionQuery setExtendAttributeRelation(Relation rel) {
+ simpleExtendRel = rel;
+ return this;
+ }
+
+ public CollectionQuery addMetaExtendQuery(String queries) {
+ this.mextension.putAll(resRel(queries));
return this;
}
@@ -124,12 +137,12 @@
return mex;
}
- private List<Map> join(Relation filter, Relation extension) {
+ private List<Map> join() {
List<Map> cursor = new ArrayList<>(this.rq);
if (!this.mfilter.isEmpty())
- cursor.addAll(this.createFilter(filter));
+ cursor.addAll(this.createFilter(simpleFilterRel));
if (!this.mextension.isEmpty())
- cursor.addAll(this.createExtender(extension));
+ cursor.addAll(this.createExtender(simpleExtendRel));
return cursor;
}
@@ -209,18 +222,13 @@
return el;
}
- public List<Map> raw(Relation filter, Relation extension) {
- return join(filter, extension);
- }
-
public List<Map> raw() {
- return raw(Relation.AND, Relation.AND);
+ return join();
}
-
- public String toCollections(Relation filter, Relation extension) {
+ public String toCollections() {
Map meta = new LinkedHashMap();
- meta.put("collections", join(filter, extension));
+ meta.put("collections", join());
try {
return serialzer.writeValueAsString(meta);
@@ -230,23 +238,18 @@
}
}
- public String toCollections() {
- return toCollections(Relation.AND, Relation.AND);
- }
-
-
/**
* returns all references to parents and meta query as string representation
*
* @return
*/
- public JsonNode buildNode(Relation filter, Relation extension) {
- return serialzer.valueToTree(join(filter, extension));
+ public JsonNode buildNode() {
+ return serialzer.valueToTree(join());
}
- public String buildString(Relation filter, Relation extension) {
+ public String buildString() {
try {
- return serialzer.writeValueAsString(join(filter, extension));
+ return serialzer.writeValueAsString(join());
} catch (JsonProcessingException e) {
e.printStackTrace();
return "";
@@ -262,9 +265,11 @@
* @param queries
* @return
*/
- private Multimap<String, String> resRel(String queries, Relation rel) {
+ private Multimap<String, String> resRel(String queries) {
Multimap<String, String> qmap = ArrayListMultimap.create();
- String[] spl = queries.trim().split(rel.toString());
+ String rel = queries.contains("AND") ? "AND" : "OR";
+
+ String[] spl = queries.trim().split(rel);
for (String query : spl) {
String[] q = query.split("=");
if (q.length > 1) {
@@ -272,7 +277,7 @@
String val = q[1].trim();
qmap.put(attr, val);
}
- //return error when query not well formed
+ // todo: return error when query not well-formed
}
return qmap;
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
index f7f0ec6..5312274 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
@@ -22,7 +22,8 @@
this.meta = new LinkedHashMap();
}
- public MetaQuery addContext(Integer left, String leftType, Integer right, String rightType) {
+ public MetaQuery addContext(Integer left, String leftType,
+ Integer right, String rightType) {
Map map = new LinkedHashMap();
List l = new LinkedList();
List r = new LinkedList();
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index ee0cac9..a798968 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -3,7 +3,6 @@
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
import de.ids_mannheim.korap.util.QueryException;
import org.slf4j.LoggerFactory;
@@ -20,12 +19,14 @@
private ObjectMapper mapper;
private AbstractSyntaxTree ast;
+ private Object collection;
+ private Object meta;
private org.slf4j.Logger log = LoggerFactory
.getLogger(QuerySerializer.class);
public QuerySerializer() {
mapper = new ObjectMapper();
- mapper.enable(SerializationFeature.INDENT_OUTPUT);
+// mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
/**
@@ -127,9 +128,7 @@
}
// change page to startindex
- public String buildQuery(String query, String ql, String collection,
- String cli, String cri, int cls, int crs,
- int num, int page, String version)
+ public QuerySerializer setQuery(String query, String ql, String version)
throws QueryException {
try {
if (ql.toLowerCase().equals("poliqarp")) {
@@ -151,25 +150,48 @@
} catch (Exception e) {
throw new QueryException("UNKNOWN: Query could not be parsed");
}
+ return this;
+ }
- Map<String, Object> requestMap = ast.getRequestMap();
-
- //todo: use startindex instead
- MetaQuery meta = new MetaQuery();
- meta.addContext(cls, cli, crs, cri);
-// meta.addEntry("startPage", page);
- meta.addEntry("startIndex", page);
- meta.addEntry("count", num);
-
- CollectionQuery qobj = new CollectionQuery();
- qobj.addResource(collection);
-
+ public final String build() {
try {
- requestMap.put("collections", qobj.raw());
- requestMap.put("meta", meta.raw());
- return mapper.writeValueAsString(requestMap);
+ return mapper.writeValueAsString(raw());
} catch (IOException e) {
return "";
}
}
+
+
+ private Map raw() {
+ Map<String, Object> requestMap = ast.getRequestMap();
+ if (collection != null)
+ requestMap.put("collections", collection);
+ if (meta != null)
+ requestMap.put("meta", meta);
+ return requestMap;
+ }
+
+
+ public QuerySerializer setMeta(
+ String cli, String cri, int cls, int crs,
+ int num, int pageIndex) {
+ MetaQuery meta = new MetaQuery();
+ meta.addContext(cls, cli, crs, cri);
+ meta.addEntry("startIndex", pageIndex);
+ meta.addEntry("count", num);
+ this.meta = meta.raw();
+ return this;
+ }
+
+ public QuerySerializer setCollection(String collection) {
+ CollectionQuery qobj = new CollectionQuery();
+ qobj.addResource(collection);
+ this.collection = qobj.raw();
+ return this;
+ }
+
+ public QuerySerializer setCollection(CollectionQuery collections) {
+ this.collection = collections.raw();
+ return this;
+ }
}
diff --git a/src/test/java/MetaQuerySerializationTest.java b/src/test/java/MetaQuerySerializationTest.java
index 1995510..f8dc4e6 100644
--- a/src/test/java/MetaQuerySerializationTest.java
+++ b/src/test/java/MetaQuerySerializationTest.java
@@ -29,11 +29,10 @@
b.append("pubPlace:Erfurt");
b.append(" AND ");
b.append("textClass:wissenschaft");
-// CollectionQuery qu = new CollectionQuery().addMetaFilter(b.toString());
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(),
- Relation.AND);
- System.out.println(query.buildString(Relation.AND, Relation.AND));
- System.out.println(query.toCollections(Relation.AND, Relation.AND));
+// CollectionQuery qu = new CollectionQuery().addMetaFilterQuery(b.toString());
+ CollectionQuery query = new CollectionQuery().addMetaFilterQuery(b.toString());
+ System.out.println(query.buildString());
+ System.out.println(query.toCollections());
// System.out.println("value reference " + qu.stringify());
// System.out.println();
}
@@ -42,7 +41,7 @@
public void testSingle() throws IOException {
CollectionQuery query = new CollectionQuery().addMetaFilter("textClass", "wissenschaft");
// System.out.println("------ TEXT SINGLE " + query.stringify());
- System.out.println(query.buildString(Relation.AND, Relation.AND));
+ System.out.println(query.buildString());
}
@Test
@@ -53,10 +52,10 @@
b.append("pubDate:<2012-04-01");
b.append(" AND ");
b.append("author:Goethe");
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
- System.out.println("value until/since : " + query.buildString(Relation.AND, Relation.AND));
- System.out.println("meta value until/since " + query.toCollections(Relation.AND, Relation.AND));
- Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Goethe\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"between\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2012-04-01\"},{\"@type\":\"korap:date\",\"@value\":\"2013-04-01\"}]}]}}]", query.buildString(Relation.AND, Relation.AND));
+ CollectionQuery query = new CollectionQuery().addMetaFilterQuery(b.toString());
+ System.out.println("value until/since : " + query.buildString());
+ System.out.println("meta value until/since " + query.toCollections());
+ Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Goethe\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"between\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2012-04-01\"},{\"@type\":\"korap:date\",\"@value\":\"2013-04-01\"}]}]}}]", query.buildString());
// System.out.println();
}
@@ -66,9 +65,9 @@
b.append("pubDate:>2013-12-10");
b.append(" AND ");
b.append("author:Hesse");
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
+ CollectionQuery query = new CollectionQuery().addMetaFilterQuery(b.toString());
System.out.println("Running date check (until) with additional attribute author");
- Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Hesse\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"until\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.buildString(Relation.AND, Relation.AND));
+ Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Hesse\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"until\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.buildString());
// System.out.println("value until : " + query.stringify());
// System.out.println();
}
@@ -79,13 +78,12 @@
b.append("pubDate:<2013-12-10");
b.append(" AND ");
b.append("author:Kafka");
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
- System.out.println("value since : " + query.buildString(Relation.AND,
- Relation.AND));
- System.out.println("meta value since " + query.toCollections(Relation.AND, Relation.AND));
+ CollectionQuery query = new CollectionQuery().addMetaFilterQuery(b.toString());
+ System.out.println("value since : " + query.buildString());
+ System.out.println("meta value since " + query.toCollections());
// System.out.println();
System.out.println("Running date check (since) with additional attribute author");
- Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Kafka\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"since\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.buildString(Relation.AND, Relation.AND));
+ Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Kafka\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"since\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.buildString());
}
@Test
@@ -128,10 +126,10 @@
b.append("pubDate:<2013-12-10");
b.append(" AND ");
b.append("author:Kafka");
- CollectionQuery q = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
+ CollectionQuery q = new CollectionQuery().addMetaFilterQuery(b.toString());
q.addMetaExtend("author", "Hesse");
- System.out.println("--- ALL " + q.buildString(Relation.AND, Relation.AND));
+ System.out.println("--- ALL " + q.buildString());
System.out.println();
}
@@ -142,8 +140,8 @@
b.append("pubDate:<2013-12-10");
b.append(" AND ");
b.append("author:Kafka");
- CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString(), Relation.AND);
- System.out.println("array repres " + q.buildString(Relation.AND, Relation.AND));
+ CollectionQuery q = new CollectionQuery().addMetaExtendQuery(b.toString());
+ System.out.println("array repres " + q.buildString());
System.out.println();
}
@@ -152,8 +150,8 @@
CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00");
q.addMetaExtend("corpusID", "A01");
- System.out.println("results stringified " + q.buildString(Relation.AND, Relation.AND));
- System.out.println("results to meta" + q.toCollections(Relation.AND, Relation.AND));
+ System.out.println("results stringified " + q.buildString());
+ System.out.println("results to meta" + q.toCollections());
System.out.println();
}
@@ -168,13 +166,13 @@
String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
CollectionQuery q = new CollectionQuery().addResource(meta);
System.out.println("Testing Resource Meta data");
- org.junit.Assert.assertEquals("{\"collections\":" + meta + "}", q.toCollections(Relation.AND, Relation.AND));
+ org.junit.Assert.assertEquals("{\"collections\":" + meta + "}", q.toCollections());
}
@Test
public void testA00() throws IOException {
CollectionQuery q = new CollectionQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
- System.out.println("A meta: " + q.buildString(Relation.AND, Relation.AND));
+ System.out.println("A meta: " + q.buildString());
System.out.println();
}
@@ -183,8 +181,8 @@
String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
CollectionQuery q = new CollectionQuery().addResource(meta);
q.addMetaFilter("textClass", "wissenschaft");
- System.out.println("stringified meta " + q.buildString(Relation.AND, Relation.AND));
- System.out.println("meta string " + q.toCollections(Relation.AND, Relation.AND));
+ System.out.println("stringified meta " + q.buildString());
+ System.out.println("meta string " + q.toCollections());
}
}