Fix serialization of text values in VC
Change-Id: Idcabb4d6c69020c6a9911669853bdafb8c934f21
diff --git a/dev/js/spec/vcSpec.js b/dev/js/spec/vcSpec.js
index 66183f4..c08307d 100644
--- a/dev/js/spec/vcSpec.js
+++ b/dev/js/spec/vcSpec.js
@@ -1214,8 +1214,155 @@
'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel ~ "huhu"'
);
});
+
+ it('should be deserializable from collection 1', function () {
+ var kq = {
+ "matches":["..."],
+ "collection":{
+ "@type": "koral:docGroup",
+ "operation": "operation:or",
+ "operands": [{
+ "@type": "koral:docGroup",
+ "operation": "operation:and",
+ "operands": [
+ {
+ "@type": "koral:doc",
+ "key": "title",
+ "match": "match:eq",
+ "value": "Der Birnbaum",
+ "type": "type:string"
+ },
+ {
+ "@type": "koral:doc",
+ "key": "pubPlace",
+ "match": "match:eq",
+ "value": "Mannheim",
+ "type": "type:string"
+ },
+ {
+ "@type": "koral:docGroup",
+ "operation": "operation:or",
+ "operands": [
+ {
+ "@type": "koral:doc",
+ "key": "subTitle",
+ "match": "match:eq",
+ "value": "Aufzucht und Pflege",
+ "type": "type:string"
+ },
+ {
+ "@type": "koral:doc",
+ "key": "subTitle",
+ "match": "match:eq",
+ "value": "Gedichte",
+ "type": "type:string"
+ }
+ ]
+ }
+ ]
+ },{
+ "@type": "koral:doc",
+ "key": "pubDate",
+ "match": "match:geq",
+ "value": "2015-03-05",
+ "type": "type:date",
+ "rewrites" : [{
+ "@type" : "koral:rewrite",
+ "operation" : "operation:modification",
+ "src" : "querySerializer",
+ "scope" : "tree"
+ }]
+ }]
+ }
+ };
+
+ var vc = vcClass.create().fromJson(kq["collection"]);
+ expect(vc.toQuery()).toEqual('(title = "Der Birnbaum" & pubPlace = "Mannheim" & (subTitle = "Aufzucht und Pflege" | subTitle = "Gedichte")) | pubDate since 2015-03-05');
+ });
+
+ it('should be deserializable from collection 2', function () {
+ var kq = {
+ "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+ "meta": {},
+ "query": {
+ "@type": "koral:token",
+ "wrap": {
+ "@type": "koral:term",
+ "match": "match:eq",
+ "layer": "orth",
+ "key": "Baum",
+ "foundry": "opennlp",
+ "rewrites": [
+ {
+ "@type": "koral:rewrite",
+ "src": "Kustvakt",
+ "operation": "operation:injection",
+ "scope": "foundry"
+ }
+ ]
+ },
+ "idn": "Baum_2227",
+ "rewrites": [
+ {
+ "@type": "koral:rewrite",
+ "src": "Kustvakt",
+ "operation": "operation:injection",
+ "scope": "idn"
+ }
+ ]
+ },
+ "collection": {
+ "@type": "koral:docGroup",
+ "operation": "operation:and",
+ "operands": [
+ {
+ "@type": "koral:doc",
+ "match": "match:eq",
+ "type": "type:regex",
+ "value": "CC-BY.*",
+ "key": "availability"
+ },
+ {
+ "@type": "koral:doc",
+ "match": "match:eq",
+ "type":"type:text",
+ "value": "Goethe",
+ "key": "author"
+ }
+ ],
+ "rewrites": [
+ {
+ "@type": "koral:rewrite",
+ "src": "Kustvakt",
+ "operation": "operation:insertion",
+ "scope": "availability(FREE)"
+ }
+ ]
+ },
+ "matches": []
+ };
+
+ var vc = vcClass.create().fromJson(kq["collection"]);
+ expect(vc.toQuery()).toEqual('availability = /CC-BY.*/ & author = "Goethe"');
+ });
});
+ it('shouldn\'t be deserializable from collection with unknown type', function () {
+ var kq = {
+ "@type" : "koral:doc",
+ "match": "match:eq",
+ "type":"type:failure",
+ "value": "Goethe",
+ "key": "author"
+ };
+
+ expect(function () {
+ vcClass.create().fromJson(kq)
+ }).toThrow(new Error("Unknown value type: string"));
+
+ });
+
+
describe('KorAP.Operators', function () {
it('should be initializable', function () {
var op = operatorsClass.create(true, false, false);
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index b9822fd..f7a136c 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -359,9 +359,15 @@
// Render Virtual collection
function _getCurrentVC (vcClass, vcArray) {
var vc = vcClass.create(vcArray);
- if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
- vc.fromJson(KorAP.koralQuery["collection"]);
- };
+ try {
+ if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
+ vc.fromJson(KorAP.koralQuery["collection"]);
+ };
+ }
+ catch (e) {
+ KorAP.log(0, e);
+ return;
+ }
return vc;
};
diff --git a/dev/js/src/vc.js b/dev/js/src/vc.js
index 527e116..8a5c6b8 100644
--- a/dev/js/src/vc.js
+++ b/dev/js/src/vc.js
@@ -316,18 +316,9 @@
var that = this;
var actions = panel.actions;
var statView;
-
- //delete log after solving the cq= -Problem.
- console.log("vc._root.element() = " + that._root.element().innerHTML);
- console.log("vc._root.toQuery = " + that._root.toQuery());
-
+
actions.add(loc.SHOW_STAT, [ 'statistic' ], function() {
if (statView === undefined || !statView.shown()) {
-
- //delete log after solving the cq= -Problem
- console.log("statView created mit vc.toQuery()= "
- + that.toQuery());
-
statView = corpStatVClass.create(that);
panel.add(statView);
}
diff --git a/dev/js/src/vc/doc.js b/dev/js/src/vc/doc.js
index d2a413e..15c7287 100644
--- a/dev/js/src/vc/doc.js
+++ b/dev/js/src/vc/doc.js
@@ -9,8 +9,14 @@
'util'
], function (jsonldClass, rewriteListClass, stringValClass) {
+ /*
+ * TODO:
+ * Improve error handling by using window.onerror() to
+ * capture thrown errors and log them.
+ */
const errstr802 = "Match type is not supported by value type";
+ const errstr804 = "Unknown value type";
const loc = KorAP.Locale;
loc.EMPTY = loc.EMPTY || '⋯';
@@ -319,8 +325,8 @@
}
else {
- KorAP.log(804, "Unknown value type");
- return;
+ KorAP.log(804, errstr804 + ": " + this.type());
+ throw new Error(errstr804 + ": " + this.type());
};
};
@@ -622,13 +628,11 @@
switch (this.type()) {
case "date":
return string + this.value();
- break;
case "regex":
return string + '/' + this.value().escapeRegex() + '/';
- break;
case "string":
+ case "text":
return string + '"' + this.value().quote() + '"';
- break;
};
return "";