Fixed matching in SegmentSpans for relation spans.

Change-Id: Ia37357cb906877b6d9bedeb4259197cf0d7cdfc7
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/SegmentSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/SegmentSpans.java
index 8375b28..c7d4541 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/SegmentSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/SegmentSpans.java
@@ -96,11 +96,17 @@
 
             if (isRelation) {
                 RelationSpans s1 = (RelationSpans) firstSpans;
-                SimpleSpans s2 = (SimpleSpans) secondSpans;
+                if (secondSpans instanceof SimpleSpans) {
+                    SimpleSpans s2 = (SimpleSpans) secondSpans;
 
-                if (s2.hasSpanId) {
-                    if (s1.getLeftId() == s2.getSpanId()) {
-                        // setSpanId(s2.getSpanId());
+                    if (s2.hasSpanId) {
+                        if (s1.getLeftId() == s2.getSpanId()) {
+                            // setSpanId(s2.getSpanId());
+                            setMatch();
+                            return 0;
+                        }
+                    }
+                    else {
                         setMatch();
                         return 0;
                     }
@@ -109,7 +115,6 @@
                     setMatch();
                     return 0;
                 }
-
             }
             else if (firstSpans instanceof FocusSpans
                     && secondSpans instanceof SimpleSpans) {
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java
index 701d06f..2423ae6 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java
@@ -105,8 +105,20 @@
                 "focus(#[1,2]spanSegment(spanRelation(tokens:>:mate/d:HEAD), <tokens:c:s />))",
                 sq.toString());
     }
+    
+    @Test
+    public void testMatchRelationSourceToken () throws QueryException {
+        //
+        String filepath = getClass()
+                .getResource("/queries/relation/match-source-token.json").getFile();
+        SpanQueryWrapper sqwi = getJSONQuery(filepath);
+        SpanQuery sq = sqwi.toQuery();
+        assertEquals(
+                "focus(#[1,2]spanSegment(spanRelation(tokens:>:malt/d:KONJ), tokens:tt/l:um))",
+                sq.toString());
+    }
 
-
+    
     @Test
     public void testMatchRelationTarget () throws QueryException {
         //
@@ -274,5 +286,22 @@
 
     }
 
+    @Test
+    public void testTypedRelationWithWrapTokenNodes () throws QueryException {
+        // query = "corenlp/c=\"VP\" & corenlp/c=\"NP\" & #1 ->malt/d[func=\"PP\"] #2";
+        String filepath = getClass()
+                .getResource(
+                        "/queries/relation/typed-relation-with-wrap-token-nodes.json")
+                .getFile();
+        SpanQueryWrapper sqwi = getJSONQuery(filepath);
+        SpanQuery sq = sqwi.toQuery();
+        assertEquals(
+                "focus(#[1,2]spanSegment(tokens:tt/p:VVINF, "
+                + "focus(#2: spanSegment("
+                + "spanRelation(tokens:>:malt/d:KONJ), tokens:tt/p:KOUI))))",
+                sq.toString());
+
+    }
+    
     // EM: handle empty koral:span
 }
diff --git a/src/test/resources/queries/relation/match-source-token.json b/src/test/resources/queries/relation/match-source-token.json
new file mode 100644
index 0000000..6ec868f
--- /dev/null
+++ b/src/test/resources/queries/relation/match-source-token.json
@@ -0,0 +1,27 @@
+{"query": {
+    "@type": "koral:group",
+    "operands": [
+        {
+            "@type": "koral:token",
+            "wrap": {
+            	"@type": "koral:term",
+                "foundry": "tt",
+                "key": "um",
+                "layer": "l",
+                "match": "match:eq"
+            }
+        },
+        {"@type": "koral:span"}
+    ],
+    "operation": "operation:relation",
+    "relType": {
+        "@type": "koral:relation",
+        "wrap": {
+            "@type": "koral:term",
+            "foundry": "malt",
+            "key": "KONJ",
+            "layer": "d",
+            "match": "match:eq"
+        }
+    }
+}}
diff --git a/src/test/resources/queries/relation/typed-relation-with-wrap-token-nodes.json b/src/test/resources/queries/relation/typed-relation-with-wrap-token-nodes.json
new file mode 100644
index 0000000..2640229
--- /dev/null
+++ b/src/test/resources/queries/relation/typed-relation-with-wrap-token-nodes.json
@@ -0,0 +1,39 @@
+{
+    "@context": "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+    "query": {
+        "operation": "operation:relation",
+        "operands": [
+            {
+                "wrap": {
+                    "@type": "koral:term",
+                    "layer": "p",
+                    "foundry": "tt",
+                    "match": "match:eq",
+                    "key": "KOUI"
+                },
+                "@type": "koral:token"
+            },
+            {
+                "wrap": {
+                    "@type": "koral:term",
+                    "layer": "p",
+                    "foundry": "tt",
+                    "match": "match:eq",
+                    "key": "VVINF"
+                },
+                "@type": "koral:token"
+            }
+        ],
+        "@type": "koral:group",
+        "relType": {
+            "wrap": {
+                "@type": "koral:term",
+                "layer": "d",
+                "foundry": "malt",
+                "match": "match:eq",
+                "key": "KONJ"
+            },
+            "@type": "koral:relation"
+        }
+    }
+}
\ No newline at end of file