- bump up to most recent REST version (0.7).
- policy serialization
- bug fixes
- term relation integration for corpus views (refactored to collection views)
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 d6ec238..63ae367 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,10 @@
private Multimap<String, String> mextension;
+ public enum RELATION {
+ AND, OR
+ }
+
public CollectionQuery() {
this.rq = new ArrayList<>();
this.mfilter = ArrayListMultimap.create();
@@ -42,16 +46,6 @@
throw new IllegalArgumentException("Conversion went wrong!");
}
-
-// try {
-// JsonParser jp = factory.createParser(query);
-// JsonNode m = jp.readValueAsTree();
-// for (JsonNode n : m)
-// this.rq.add(serialzer.treeToValue(n, Map.class));
-// } catch (IOException e) {
-// e.printStackTrace();
-// throw new IllegalArgumentException("Conversion went wrong!");
-// }
return this;
}
@@ -61,14 +55,13 @@
return this;
}
-
public CollectionQuery addMetaFilter(String key, String value) {
this.mfilter.put(key, value);
return this;
}
- public CollectionQuery addMetaFilter(String queries) {
- this.mfilter.putAll(resEq(queries));
+ public CollectionQuery addMetaFilter(String queries, RELATION rel) {
+ this.mfilter.putAll(resRel(queries, rel));
return this;
}
@@ -77,13 +70,15 @@
return this;
}
- public CollectionQuery addMetaExtend(String queries) {
- this.mextension.putAll(resEq(queries));
+ public CollectionQuery addMetaExtend(String queries, RELATION rel) {
+ this.mextension.putAll(resRel(queries, rel));
+
return this;
}
- private List<Map> createFilter() {
+ private List<Map> createFilter(RELATION rel) {
+ String relation = rel == RELATION.AND ? "and" : "or";
List<Map> mfil = new ArrayList();
boolean multypes = this.mfilter.keySet().size() > 1;
String def_key = null;
@@ -100,15 +95,16 @@
else {
Map group;
if (!multypes)
- group = types.createGroup("and", def_key, value);
+ group = types.createGroup(relation, def_key, value);
else
- group = types.createGroup("and", null, value);
+ group = types.createGroup(relation, null, value);
Collections.addAll(mfil, types.createMetaFilter(group));
}
return mfil;
}
- private List<Map> createExtender() {
+ private List<Map> createExtender(RELATION rel) {
+ String relation = rel == RELATION.AND ? "and" : "or";
List<Map> mex = new ArrayList();
boolean multypes = this.mextension.keys().size() > 1;
String def_key = null;
@@ -123,20 +119,20 @@
else {
Map group;
if (!multypes)
- group = types.createGroup("and", def_key, value);
+ group = types.createGroup(relation, def_key, value);
else
- group = types.createGroup("and", null, value);
+ group = types.createGroup(relation, null, value);
Collections.addAll(mex, types.createMetaExtend(group));
}
return mex;
}
- private List<Map> join() {
+ private List<Map> join(RELATION filter, RELATION extension) {
List<Map> cursor = new ArrayList<>(this.rq);
if (!this.mfilter.isEmpty())
- cursor.addAll(this.createFilter());
+ cursor.addAll(this.createFilter(filter));
if (!this.mextension.isEmpty())
- cursor.addAll(this.createExtender());
+ cursor.addAll(this.createExtender(extension));
return cursor;
}
@@ -216,14 +212,14 @@
return el;
}
- public List<Map> raw() {
- return join();
+ public List<Map> raw(RELATION filter, RELATION extension) {
+ return join(filter, extension);
}
- public String toCollections() {
+ public String toCollections(RELATION filter, RELATION extension) {
Map meta = new LinkedHashMap();
- meta.put("collections", join());
+ meta.put("collections", join(filter, extension));
try {
return serialzer.writeValueAsString(meta);
@@ -233,19 +229,23 @@
}
}
+ public String toCollections() {
+ return toCollections(RELATION.AND, RELATION.AND);
+ }
+
/**
* returns all references to parents and meta query as string representation
*
* @return
*/
- public JsonNode buildNode() {
- return serialzer.valueToTree(join());
+ public JsonNode buildNode(RELATION filter, RELATION extension) {
+ return serialzer.valueToTree(join(filter, extension));
}
- public String buildString() {
+ public String buildString(RELATION filter, RELATION extension) {
try {
- return serialzer.writeValueAsString(join());
+ return serialzer.writeValueAsString(join(filter, extension));
} catch (JsonProcessingException e) {
e.printStackTrace();
return "";
@@ -260,9 +260,9 @@
* @param queries
* @return
*/
- private Multimap<String, String> resEq(String queries) {
+ private Multimap<String, String> resRel(String queries, RELATION rel) {
Multimap<String, String> qmap = ArrayListMultimap.create();
- String[] spl = queries.split(" AND ");
+ String[] spl = queries.split(rel.toString());
for (String query : spl) {
String[] q = query.split(":");
String attr = q[0];
diff --git a/src/test/java/MetaQuerySerializationTest.java b/src/test/java/MetaQuerySerializationTest.java
index 7e430fa..2f7d8c3 100644
--- a/src/test/java/MetaQuerySerializationTest.java
+++ b/src/test/java/MetaQuerySerializationTest.java
@@ -4,7 +4,6 @@
import de.ids_mannheim.korap.query.serialize.CollectionQuery;
import de.ids_mannheim.korap.query.serialize.QuerySerializer;
import de.ids_mannheim.korap.util.QueryException;
-
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -30,9 +29,10 @@
b.append(" AND ");
b.append("textClass:wissenschaft");
// CollectionQuery qu = new CollectionQuery().addMetaFilter(b.toString());
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
- System.out.println(query.buildString());
- System.out.println(query.toCollections());
+ CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(),
+ CollectionQuery.RELATION.AND);
+ System.out.println(query.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
+ System.out.println(query.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
// System.out.println("value reference " + qu.stringify());
// System.out.println();
}
@@ -41,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());
+ System.out.println(query.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
}
@Test
@@ -52,10 +52,10 @@
b.append("pubDate:<2012-04-01");
b.append(" AND ");
b.append("author:Goethe");
- CollectionQuery query = new CollectionQuery().addMetaFilter(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());
+ CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
+ System.out.println("value until/since : " + query.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
+ System.out.println("meta value until/since " + query.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.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(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
// System.out.println();
}
@@ -65,9 +65,9 @@
b.append("pubDate:>2013-12-10");
b.append(" AND ");
b.append("author:Hesse");
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
+ CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
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());
+ 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(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
// System.out.println("value until : " + query.stringify());
// System.out.println();
}
@@ -78,12 +78,13 @@
b.append("pubDate:<2013-12-10");
b.append(" AND ");
b.append("author:Kafka");
- CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
- System.out.println("value since : " + query.buildString());
- System.out.println("meta value since " + query.toCollections());
+ CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
+ System.out.println("value since : " + query.buildString(CollectionQuery.RELATION.AND,
+ CollectionQuery.RELATION.AND));
+ System.out.println("meta value since " + query.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
// 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());
+ 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(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
}
@Test
@@ -126,10 +127,10 @@
b.append("pubDate:<2013-12-10");
b.append(" AND ");
b.append("author:Kafka");
- CollectionQuery q = new CollectionQuery().addMetaFilter(b.toString());
+ CollectionQuery q = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
q.addMetaExtend("author", "Hesse");
- System.out.println("--- ALL " + q.buildString());
+ System.out.println("--- ALL " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
System.out.println();
}
@@ -140,8 +141,8 @@
b.append("pubDate:<2013-12-10");
b.append(" AND ");
b.append("author:Kafka");
- CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString());
- System.out.println("array repres " + q.buildString());
+ CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString(), CollectionQuery.RELATION.AND);
+ System.out.println("array repres " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
System.out.println();
}
@@ -150,8 +151,8 @@
CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00");
q.addMetaExtend("corpusID", "A01");
- System.out.println("results stringified " + q.buildString());
- System.out.println("results to meta" + q.toCollections());
+ System.out.println("results stringified " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
+ System.out.println("results to meta" + q.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
System.out.println();
}
@@ -166,13 +167,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());
+ org.junit.Assert.assertEquals("{\"collections\":" + meta + "}", q.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
}
@Test
public void testA00() throws IOException {
CollectionQuery q = new CollectionQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
- System.out.println("A meta: " + q.buildString());
+ System.out.println("A meta: " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
System.out.println();
}
@@ -181,8 +182,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());
- System.out.println("meta string " + q.toCollections());
+ System.out.println("stringified meta " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
+ System.out.println("meta string " + q.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
}
}