Fix Koral serialization support for different distance types (2)

Change-Id: I195ef9613df3005478258e62f43f048e5c3fb193
diff --git a/src/main/java/de/ids_mannheim/korap/KrillQuery.java b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
index f2cf859..86c6922 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
@@ -911,6 +911,12 @@
                     unit = value.append("base/s:").append(unit).toString();
                 };
 
+                // Workaround for koral:distance vs cosmas:distance
+                if (constraint.get("@type").asText().equals("koral:distance")) {
+                    min++;
+                    max++;
+                };
+
                 // Set distance exclusion
                 Boolean exclusion = false;
                 if (constraint.has("exclude"))
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java
index 9decc2b..ebd3a11 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java
@@ -107,8 +107,8 @@
                 "spanReference(focus(#[1,2]spanSegment(focus(#2: "
                         + "spanSegment(spanRelation(tokens:>:stanford/d:tag), "
                         + "focus(3: spanDistance(focus(1: spanDistance({1: <tokens:vb />}, "
-                        + "{2: <tokens:prp />}, [(w[0:1], notOrdered, notExcluded)])), "
-                        + "{3: <tokens:nn />}, [(w[0:2], notOrdered, notExcluded)])))), "
+                        + "{2: <tokens:prp />}, [(w[1:2], notOrdered, notExcluded)])), "
+                        + "{3: <tokens:nn />}, [(w[1:3], notOrdered, notExcluded)])))), "
                         + "{2: <tokens:prp />})), 2)", sq.toString());
 
         SpanElementQuery seq1 = new SpanElementQuery("tokens", "vb");
@@ -122,11 +122,13 @@
         SpanClassQuery scq3 = new SpanClassQuery(seq3, (byte) 3);
 
         // vb .{0,1} prp
+        // ND: I don't know if this is correct
         SpanDistanceQuery sdq1 = new SpanDistanceQuery(scq1, scq2,
                 new DistanceConstraint(0, 1, false, false), true);
         SpanFocusQuery sfq1 = new SpanFocusQuery(sdq1, (byte) 1);
 
         // vb .{0,2} nn
+        // ND: I don't know if this is correct
         SpanDistanceQuery sdq2 = new SpanDistanceQuery(sfq1, scq3,
                 new DistanceConstraint(0, 2, false, false), true);
         SpanFocusQuery sfq2 = new SpanFocusQuery(sdq2, (byte) 3);
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index c559e81..22c6134 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -382,7 +382,7 @@
 
         // Tal []{1,} Wald
         assertEquals(sqwi.toQuery().toString(),
-                "spanDistance(tokens:s:Tal, tokens:s:Wald, [(w[2:100], ordered, notExcluded)])");
+                "spanDistance(tokens:s:Tal, tokens:s:Wald, [(w[2:101], ordered, notExcluded)])");
     };
 
 
@@ -484,7 +484,7 @@
 
         assertEquals(
                 sqwi.toQuery().toString(),
-                "spanContain(<tokens:s />, spanDistance(tokens:s:Erde, tokens:s:Sonne, [(w[0:100], ordered, notExcluded)]))");
+                "spanContain(<tokens:s />, spanDistance(tokens:s:Erde, tokens:s:Sonne, [(w[1:101], ordered, notExcluded)]))");
     };
 
 
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java
index a401916..e77cfcc 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java
@@ -65,9 +65,10 @@
                         + "{1: <tokens:c:VP />})), 1)", sq.toString());
     }
 
-
     @Test
     public void testDistanceReferences () throws QueryException {
+
+        // ND: I don't understand what this query should be about ...
         String filepath = getClass().getResource(
                 "/queries/reference/bug-multiple-distance-simple.jsonld")
                 .getFile();
@@ -92,7 +93,7 @@
                 + "2: spanDistance(" + "focus(" + "1: spanDistance("
                 + "<tokens:c:vb />, " + "{1: <tokens:c:prp />}, "
                 + "[(w[1:1], notOrdered, notExcluded)]" + ")" + "), "
-                + "{2: <tokens:c:nn />}, " + "[(w[0:2], ordered, notExcluded)]"
+                + "{2: <tokens:c:nn />}, " + "[(w[1:3], ordered, notExcluded)]"
                 + ")" + ")" + ")" + "), " + "{1: <tokens:c:prp />}" + ")"
                 + "), 1" + ")", sq.toString());
     }
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
index d88eb62..f1306b9 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
@@ -326,6 +326,14 @@
     };
 
 
+    @Test
+    public void queryJSONkoralSimpleDistanceBug () throws QueryException {
+        SpanQueryWrapper sqwi = jsonQueryFile("distance-simple.jsonld");
+
+        assertEquals("spanDistance(tokens:s:der, tokens:s:Baum, [(w[2:2], ordered, notExcluded)])",sqwi.toQuery().toString());
+    };
+
+
     // get query wrapper based on json file
     public SpanQueryWrapper jsonQueryFile (String filename) {
         return getJSONQuery(getClass().getResource(path + filename).getFile());
diff --git a/src/test/resources/queries/bsp-boundary.jsonld b/src/test/resources/queries/bsp-boundary.jsonld
index 4f72f4a..8ec5786 100644
--- a/src/test/resources/queries/bsp-boundary.jsonld
+++ b/src/test/resources/queries/bsp-boundary.jsonld
@@ -1 +1,56 @@
-{"@context":"http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld","errors":[],"warnings":[],"announcements":["Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."],"query":{"@type":"koral:group","operation":"operation:sequence","operands":[{"@type":"koral:token","wrap":{"@type":"koral:term","layer":"orth","key":"Tal","match":"match:eq"}},{"@type":"koral:token","wrap":{"@type":"koral:term","layer":"orth","key":"Wald","match":"match:eq"}}],"inOrder":true,"distances":[{"@type":"koral:distance","key":"w","boundary":{"@type":"koral:boundary","min":2},"min":2}]},"collections":[{"@type":"koral:meta-filter","@value":{"@type":"koral:term","@field":"koral:field#corpusID","@value":"WPD"}}],"meta":{"startPage":1,"itemsPerResource":1,"context":"paragraph"}}
+{
+  "@context":"http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "errors":[],
+  "warnings":[],
+  "announcements":["Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."],
+  "query":{
+    "@type":"koral:group",
+    "operation":"operation:sequence",
+    "operands":[
+      {
+	"@type":"koral:token",
+	"wrap":{
+	  "@type":"koral:term",
+	  "layer":"orth",
+	  "key":"Tal",
+	  "match":"match:eq"
+	}
+      },{
+	"@type":"koral:token",
+	"wrap":{
+	  "@type":"koral:term",
+	  "layer":"orth",
+	  "key":"Wald",
+	  "match":"match:eq"
+	}
+      }
+    ],
+    "inOrder":true,
+    "distances":[
+      {
+	"@type":"koral:distance",
+	"key":"w",
+	"boundary":{
+	  "@type":"koral:boundary",
+	  "min":1
+	},
+	"min":1
+      }
+    ]
+  },
+  "collections":[
+    {
+      "@type":"koral:meta-filter",
+      "@value":{
+	"@type":"koral:term",
+	"@field":"koral:field#corpusID",
+	"@value":"WPD"
+      }
+    }
+  ],
+  "meta":{
+    "startPage":1,
+    "itemsPerResource":1,
+    "context":"paragraph"
+  }
+}
diff --git a/src/test/resources/queries/cosmas1.json b/src/test/resources/queries/cosmas1.json
index a1456d3..700205f 100644
--- a/src/test/resources/queries/cosmas1.json
+++ b/src/test/resources/queries/cosmas1.json
@@ -5,7 +5,7 @@
     "operation" : "operation:sequence",
     "inOrder" : false,
     "distances" : [ {
-      "@type" : "koral:distance",
+      "@type" : "cosmas:distance",
       "key" : "s",
       "min" : 0,
       "max" : 0
diff --git a/src/test/resources/queries/cosmas3.json b/src/test/resources/queries/cosmas3.json
index 2dd75ac..05fcd42 100644
--- a/src/test/resources/queries/cosmas3.json
+++ b/src/test/resources/queries/cosmas3.json
@@ -5,7 +5,7 @@
     "operation" : "operation:sequence",
     "inOrder" : true,
     "distances" : [ {
-      "@type" : "koral:distance",
+      "@type" : "cosmas:distance",
       "key" : "w",
       "min" : 1,
       "max" : 3
diff --git a/src/test/resources/queries/cosmas4.json b/src/test/resources/queries/cosmas4.json
index d9bd77c..92163af 100644
--- a/src/test/resources/queries/cosmas4.json
+++ b/src/test/resources/queries/cosmas4.json
@@ -8,12 +8,12 @@
       "@type" : "koral:group",
       "operation" : "operation:and",
       "operands" : [ {
-        "@type" : "koral:distance",
+        "@type" : "cosmas:distance",
         "key" : "w",
         "min" : 1,
         "max" : 3
       }, {
-        "@type" : "koral:distance",
+        "@type" : "cosmas:distance",
         "key" : "s",
         "min" : 1,
         "max" : 1
diff --git a/src/test/resources/queries/cosmas4b.json b/src/test/resources/queries/cosmas4b.json
index fac3101..15a0999 100644
--- a/src/test/resources/queries/cosmas4b.json
+++ b/src/test/resources/queries/cosmas4b.json
@@ -5,12 +5,12 @@
     "operation" : "operation:sequence",
     "inOrder" : true,
     "distances" : [ {
-      "@type" : "koral:distance",
+      "@type" : "cosmas:distance",
       "key" : "w",
       "min" : 1,
       "max" : 3
     }, {
-      "@type" : "koral:distance",
+      "@type" : "cosmas:distance",
       "key" : "s",
       "min" : 0,
       "max" : 1
diff --git a/src/test/resources/queries/reference/bug-multiple-distance-simple.jsonld b/src/test/resources/queries/reference/bug-multiple-distance-simple.jsonld
index 72af003..8ffafaa 100644
--- a/src/test/resources/queries/reference/bug-multiple-distance-simple.jsonld
+++ b/src/test/resources/queries/reference/bug-multiple-distance-simple.jsonld
@@ -22,11 +22,11 @@
       "key": "w",
       "boundary": {
         "@type": "koral:boundary",
-        "min": 1,
-        "max": 1
+        "min": 0,
+        "max": 0
       },
-      "min": 1,
-      "max": 1
+      "min": 0,
+      "max": 0
     }]                                
   },
   "meta": {}
diff --git a/src/test/resources/queries/reference/bug-multiple-distance.jsonld b/src/test/resources/queries/reference/bug-multiple-distance.jsonld
index 056d8c6..de9bee6 100644
--- a/src/test/resources/queries/reference/bug-multiple-distance.jsonld
+++ b/src/test/resources/queries/reference/bug-multiple-distance.jsonld
@@ -43,11 +43,11 @@
                   "key": "w",
                   "boundary": {
                     "@type": "koral:boundary",
-                    "min": 1,
-                    "max": 1
+                    "min": 0,
+                    "max": 0
                   },
-                  "min": 1,
-                  "max": 1
+                  "min": 0,
+                  "max": 0
                 }]                                
               }]
             },
diff --git a/src/test/resources/queries/sequence/distance-multiple.jsonld b/src/test/resources/queries/sequence/distance-multiple.jsonld
index 0696440..6000d58 100644
--- a/src/test/resources/queries/sequence/distance-multiple.jsonld
+++ b/src/test/resources/queries/sequence/distance-multiple.jsonld
@@ -7,12 +7,12 @@
         "@type" : "koral:distance",
         "boundary" : {
           "@type" : "koral:boundary",
-          "max" : 11,
-          "min" : 1
+          "max" : 10,
+          "min" : 0
         },
         "key" : "w",
-        "max" : 11,
-        "min" : 1
+        "max" : 10,
+        "min" : 0
       }
     ],
     "inOrder" : true,
@@ -33,12 +33,12 @@
             "@type" : "koral:distance",
             "boundary" : {
               "@type" : "koral:boundary",
-              "max" : 11,
-              "min" : 2
+              "max" : 10,
+              "min" : 1
             },
             "key" : "w",
-            "max" : 11,
-            "min" : 2
+            "max" : 10,
+            "min" : 1
           }
         ],
         "inOrder" : true,
diff --git a/src/test/resources/queries/sequence/distance-simple.jsonld b/src/test/resources/queries/sequence/distance-simple.jsonld
new file mode 100644
index 0000000..9beeabb
--- /dev/null
+++ b/src/test/resources/queries/sequence/distance-simple.jsonld
@@ -0,0 +1,41 @@
+{
+  "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+  "query": {
+    "inOrder": true,
+    "operands": [
+      {
+        "@type": "koral:token",
+        "wrap": {
+          "match": "match:eq",
+          "layer": "orth",
+          "key": "der",
+          "@type": "koral:term"
+        }
+      },
+      {
+        "wrap": {
+          "match": "match:eq",
+          "layer": "orth",
+          "key": "Baum",
+          "@type": "koral:term"
+        },
+        "@type": "koral:token"
+      }
+    ],
+    "operation": "operation:sequence",
+    "distances": [
+      {
+        "key": "w",
+        "@type": "koral:distance",
+        "max": 1,
+        "boundary": {
+          "@type": "koral:boundary",
+          "max": 1,
+          "min": 1
+        },
+        "min": 1
+      }
+    ],
+    "@type": "koral:group"
+  }
+}