jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rve...@apache.org
Subject [04/50] [abbrv] jena git commit: JENA-1093: return multiple literals from text query with bound subject
Date Wed, 10 Feb 2016 10:00:39 GMT
JENA-1093: return multiple literals from text query with bound subject


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/859fa47b
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/859fa47b
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/859fa47b

Branch: refs/heads/JENA-507
Commit: 859fa47b66c6ce0701d0b080c835955e96f30c73
Parents: 245d5ca
Author: Osma Suominen <osma.suominen@aalto.fi>
Authored: Tue Dec 22 21:01:25 2015 +0200
Committer: Osma Suominen <osma@apache.org>
Committed: Tue Jan 5 17:37:23 2016 +0200

----------------------------------------------------------------------
 .../org/apache/jena/query/text/TextQueryPF.java | 38 +++++++++-----------
 .../TestDatasetWithLuceneStoredLiterals.java    | 27 ++++++++++++++
 2 files changed, 43 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/859fa47b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
----------------------------------------------------------------------
diff --git a/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
index f4e1402..ea7bdf8 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
@@ -19,6 +19,7 @@
 package org.apache.jena.query.text ;
 
 import java.util.Iterator ;
+import java.util.LinkedList ;
 import java.util.List ;
 import java.util.function.Function ;
 
@@ -193,16 +194,17 @@ public class TextQueryPF extends PropertyFunctionBase {
         return qIter ;
     }
 
-    private QueryIterator variableSubject(Binding binding, Node s, Node score, Node literal,
StrMatch match, ExecutionContext execCxt) {
-        Var sVar = Var.alloc(s) ;
+    private QueryIterator resultsToQueryIterator(Binding binding, Node s, Node score, Node
literal, List<TextHit> results, ExecutionContext execCxt) {
+        Var sVar = Var.isVar(s) ? Var.alloc(s) : null ;
         Var scoreVar = (score==null) ? null : Var.alloc(score) ;
         Var literalVar = (literal==null) ? null : Var.alloc(literal) ;
-        List<TextHit> r = query(match.getProperty(), match.getQueryString(), match.getLimit(),
execCxt) ;
+
         Function<TextHit,Binding> converter = (TextHit hit) -> {
             if (score == null && literal == null)
-                return BindingFactory.binding(binding, sVar, hit.getNode());
+                return sVar != null ? BindingFactory.binding(binding, sVar, hit.getNode())
: BindingFactory.binding(binding);
             BindingMap bmap = BindingFactory.create(binding);
-            bmap.add(sVar, hit.getNode());
+            if (sVar != null)
+                bmap.add(sVar, hit.getNode());
             if (scoreVar != null)
                 bmap.add(scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()));
             if (literalVar != null)
@@ -210,44 +212,36 @@ public class TextQueryPF extends PropertyFunctionBase {
             return bmap;
         } ;
         
-        Iterator<Binding> bIter = Iter.map(r.iterator(), converter);
+        Iterator<Binding> bIter = Iter.map(results.iterator(), converter);
         QueryIterator qIter = new QueryIterPlainWrapper(bIter, execCxt);
         return qIter ;
     }
 
+    private QueryIterator variableSubject(Binding binding, Node s, Node score, Node literal,
StrMatch match, ExecutionContext execCxt) {
+        List<TextHit> r = query(match.getProperty(), match.getQueryString(), match.getLimit(),
execCxt) ;
+        return resultsToQueryIterator(binding, s, score, literal, r, execCxt);
+    }
+
     private QueryIterator concreteSubject(Binding binding, Node s, Node score, Node literal,
StrMatch match, ExecutionContext execCxt) {
         if (!s.isURI()) {
             log.warn("Subject not a URI: " + s) ;
             return IterLib.noResults(execCxt) ;
         }
 
-        Var scoreVar = (score==null) ? null : Var.alloc(score) ;
-        Var literalVar = (literal==null) ? null : Var.alloc(literal) ;
         String qs = match.getQueryString() ;
         List<TextHit> x = query(match.getProperty(), match.getQueryString(), -1, execCxt)
;
         
         if ( x == null ) // null return value - empty result
             return IterLib.noResults(execCxt) ;
         
+        List<TextHit> r = new LinkedList();
         for (TextHit hit : x ) {
             if (hit.getNode().equals(s)) {
-                // found the node among the hits
-                if (literalVar == null) {
-                    return (scoreVar == null) ?
-                        IterLib.result(binding, execCxt) :
-                        IterLib.oneResult(binding, scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()),
execCxt);
-                }
-                BindingMap bmap = BindingFactory.create(binding);
-                if (scoreVar != null) {
-                    bmap.add(scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()));
-                }
-                bmap.add(literalVar, hit.getLiteral());
-                return IterLib.result(bmap, execCxt) ;
+                r.add(hit);
             }
         }
 
-        // node was not among the hits - empty result
-        return IterLib.noResults(execCxt) ;
+        return resultsToQueryIterator(binding, s, score, literal, r, execCxt);
     }
 
     private List<TextHit> query(Node property, String queryString, int limit, ExecutionContext
execCxt) {

http://git-wip-us.apache.org/repos/asf/jena/blob/859fa47b/jena-text/src/test/java/org/apache/jena/query/text/TestDatasetWithLuceneStoredLiterals.java
----------------------------------------------------------------------
diff --git a/jena-text/src/test/java/org/apache/jena/query/text/TestDatasetWithLuceneStoredLiterals.java
b/jena-text/src/test/java/org/apache/jena/query/text/TestDatasetWithLuceneStoredLiterals.java
index 659a0dd..e60d396 100644
--- a/jena-text/src/test/java/org/apache/jena/query/text/TestDatasetWithLuceneStoredLiterals.java
+++ b/jena-text/src/test/java/org/apache/jena/query/text/TestDatasetWithLuceneStoredLiterals.java
@@ -303,4 +303,31 @@ public class TestDatasetWithLuceneStoredLiterals extends AbstractTestDatasetWith
         assertTrue(literals.contains(NodeFactory.createLiteral("another nontext comment")));
     }
 
+    @Test
+    public void testLiteralValueMultipleBoundSubject() {
+        // test capturing of multiple matching literal values in a variable, when using bound
subject
+        final String testName = "testLiteralValueMultipleBoundSubject";
+        final String turtle = StrUtils.strjoinNL(
+                TURTLE_PROLOG,
+                "<" + RESOURCE_BASE + testName + ">",
+                "  rdfs:comment 'a nontext comment', 'another nontext comment'",
+                "."
+                );
+        String queryString = StrUtils.strjoinNL(
+                QUERY_PROLOG,
+                "SELECT ?s ?literal",
+                "WHERE {",
+                "    BIND(<" + RESOURCE_BASE + testName + "> AS ?s)",
+                "    (?s ?score ?literal) text:query (rdfs:comment 'nontext') .",
+                "}"
+                );
+
+        String expectedURI = RESOURCE_BASE + testName;
+        List<Node> literals = doTestSearchWithLiteralsMultiple(turtle, queryString,
expectedURI);
+
+        assertEquals(2, literals.size());
+        assertTrue(literals.contains(NodeFactory.createLiteral("a nontext comment")));
+        assertTrue(literals.contains(NodeFactory.createLiteral("another nontext comment")));
+    }
+
 }


Mime
View raw message