jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cla...@apache.org
Subject [1/2] jena git commit: fix for JENA-1034
Date Wed, 23 Sep 2015 21:18:04 GMT
Repository: jena
Updated Branches:
  refs/heads/master df335ab28 -> 663733ba7


fix for JENA-1034


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

Branch: refs/heads/master
Commit: f7effc4e12d1f9fe73e07b7a6c0f123be80b66d3
Parents: 53ceeaf
Author: Claude Warren <claude@apache.org>
Authored: Wed Sep 23 22:15:39 2015 +0100
Committer: Claude Warren <claude@apache.org>
Committed: Wed Sep 23 22:15:39 2015 +0100

----------------------------------------------------------------------
 .../jena/arq/querybuilder/AskBuilder.java       |  7 ++++++
 .../jena/arq/querybuilder/ConstructBuilder.java |  7 ++++++
 .../jena/arq/querybuilder/SelectBuilder.java    |  7 ++++++
 .../arq/querybuilder/clauses/WhereClause.java   | 11 ++++++++-
 .../arq/querybuilder/handlers/WhereHandler.java | 24 ++++++++++++------
 .../querybuilder/clauses/WhereClauseTest.java   | 26 ++++++++++++++++++++
 .../querybuilder/handlers/WhereHandlerTest.java | 25 +++++++++++++++++++
 7 files changed, 98 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AskBuilder.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AskBuilder.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AskBuilder.java
index 42c213c..cd737f5 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AskBuilder.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/AskBuilder.java
@@ -121,6 +121,13 @@ public class AskBuilder extends AbstractQueryBuilder<AskBuilder>
implements
 		whereHandler.addOptional(t);
 		return this;
 	}
+	
+	@Override
+	public AskBuilder addOptional(SelectBuilder t)
+	{
+		whereHandler.addOptional(t.getWhereHandler());
+		return this;
+	}
 
 	@Override
 	public AskBuilder addOptional(FrontsTriple t) {

http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/ConstructBuilder.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/ConstructBuilder.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/ConstructBuilder.java
index f935e2b..0eae69a 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/ConstructBuilder.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/ConstructBuilder.java
@@ -168,6 +168,13 @@ public class ConstructBuilder extends AbstractQueryBuilder<ConstructBuilder>
 		whereHandler.addOptional(t);
 		return this;
 	}
+	
+	@Override
+	public ConstructBuilder addOptional(SelectBuilder t)
+	{
+		whereHandler.addOptional(t.getWhereHandler());
+		return this;
+	}
 
 	@Override
 	public ConstructBuilder addOptional(FrontsTriple t) {

http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/SelectBuilder.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/SelectBuilder.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/SelectBuilder.java
index 8ca8de3..3c1fb2c 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/SelectBuilder.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/SelectBuilder.java
@@ -266,6 +266,13 @@ public class SelectBuilder extends AbstractQueryBuilder<SelectBuilder>
 	}
 
 	@Override
+	public SelectBuilder addOptional(SelectBuilder t)
+	{
+		whereHandler.addOptional(t.getWhereHandler());
+		return this;
+	}
+	
+	@Override
 	public SelectBuilder addFilter(String s) throws ParseException {
 		whereHandler.addFilter(s);
 		return this;

http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/WhereClause.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/WhereClause.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/WhereClause.java
index 2e814ad..6626c46 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/WhereClause.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/clauses/WhereClause.java
@@ -68,7 +68,7 @@ public interface WhereClause<T extends AbstractQueryBuilder<T>>
{
 	public T addWhere(Object s, Object p, Object o);
 
 	/**
-	 * Adds an optional triple as to the where clause.
+	 * Adds an optional triple to the where clause.
 	 * 
 	 * @param t
 	 *            The triple to add
@@ -99,6 +99,15 @@ public interface WhereClause<T extends AbstractQueryBuilder<T>>
{
 	 * @return The Builder for chaining.
 	 */
 	public T addOptional(Object s, Object p, Object o);
+	
+	/**
+	 * Adds an optional group pattern to the where clause.
+	 * 
+	 * @param t
+	 *            The select builder to add as a group pattern
+	 * @return The Builder for chaining.
+	 */
+	public T addOptional(SelectBuilder t);
 
 	/**
 	 * Adds a filter to the where clause

http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/WhereHandler.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/WhereHandler.java
b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/WhereHandler.java
index d7e4ba4..1215c4c 100644
--- a/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/WhereHandler.java
+++ b/jena-extras/jena-querybuilder/src/main/java/org/apache/jena/arq/querybuilder/handlers/WhereHandler.java
@@ -34,7 +34,8 @@ import org.apache.jena.sparql.syntax.* ;
 import org.apache.jena.sparql.util.ExprUtils ;
 
 /**
- * The where handler
+ * The where handler.  Generally handles GroupGraphPattern.
+ * @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#rGroupGraphPattern
  *
  */
 public class WhereHandler implements Handler {
@@ -151,16 +152,19 @@ public class WhereHandler implements Handler {
 		ElementGroup eg = getClause();
 		List<Element> lst = eg.getElements();
 		if (lst.isEmpty()) {
-			ElementTriplesBlock etb = new ElementTriplesBlock();
-			etb.addTriple(t);
-			eg.addElement(etb);
+			ElementPathBlock epb = new ElementPathBlock();
+			epb.addTriple(t);
+			eg.addElement(epb);
 		} else {
 			Element e = lst.get(lst.size() - 1);
 			if (e instanceof ElementTriplesBlock) {
 				ElementTriplesBlock etb = (ElementTriplesBlock) e;
 				etb.addTriple(t);
+			} else if (e instanceof ElementPathBlock) {
+				ElementPathBlock epb = (ElementPathBlock) e;
+				epb.addTriple(t);
 			} else {
-				ElementTriplesBlock etb = new ElementTriplesBlock();
+				ElementPathBlock etb = new ElementPathBlock();
 				etb.addTriple(t);
 				eg.addElement(etb);
 			}
@@ -175,11 +179,15 @@ public class WhereHandler implements Handler {
 	 */
 	public void addOptional(Triple t) throws IllegalArgumentException {
 		testTriple(t);
-		ElementTriplesBlock etb = new ElementTriplesBlock();
-		etb.addTriple(t);
-		ElementOptional opt = new ElementOptional(etb);
+		ElementPathBlock epb = new ElementPathBlock();
+		epb.addTriple(t);
+		ElementOptional opt = new ElementOptional(epb);
 		getClause().addElement(opt);
 	}
+	
+	public void addOptional(WhereHandler whereHandler) {
+		getClause().addElement( new ElementOptional( whereHandler.getClause()));
+	}
 
 	/**
 	 * Add an expression string as a filter.

http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
index 50e6420..7f03387 100644
--- a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
+++ b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/clauses/WhereClauseTest.java
@@ -21,10 +21,13 @@ import java.util.List;
 
 import org.apache.jena.arq.querybuilder.AbstractQueryBuilder;
 import org.apache.jena.arq.querybuilder.SelectBuilder;
+import org.apache.jena.arq.querybuilder.handlers.WhereHandler;
+import org.apache.jena.graph.Node;
 import org.apache.jena.graph.NodeFactory;
 import org.apache.jena.graph.Triple;
 import org.apache.jena.graph.impl.LiteralLabelFactory;
 import org.apache.jena.query.Query;
+import org.apache.jena.query.QueryFactory;
 import org.apache.jena.sparql.core.Var;
 import org.apache.jena.sparql.expr.E_Random;
 import org.apache.jena.sparql.lang.sparql_11.ParseException;
@@ -32,6 +35,7 @@ import org.apache.jena.sparql.syntax.Element;
 import org.apache.jena.sparql.syntax.ElementGroup;
 import org.apache.jena.sparql.syntax.ElementTriplesBlock;
 import org.junit.After;
+import org.junit.Assert;
 
 import static org.junit.Assert.*;
 
@@ -107,6 +111,28 @@ public class WhereClauseTest<T extends WhereClause<?>> extends
 				+ uri("three") + OPT_SPACE + CLOSE_CURLY,
 				builder.buildString());
 	}
+	
+	@ContractTest
+	public void testAddOptionalGroupPattern() throws ParseException {
+		
+		Var s = Var.alloc("s" );
+		Node q = NodeFactory.createURI( "urn:q" );
+		Node v = NodeFactory.createURI( "urn:v" );
+		Var x = Var.alloc("x");
+		Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));	
+		
+		SelectBuilder pattern = new SelectBuilder();
+		pattern.addWhere( new Triple( s, q,  n123 ) );
+		pattern.addWhere( new Triple( s, v, x));
+		pattern.addFilter( "?x>56");
+		
+		WhereClause<?> whereClause = getProducer().newInstance();
+		AbstractQueryBuilder<?> builder = whereClause.addOptional( pattern );
+		
+		Query expected = QueryFactory.create( "SELECT * WHERE { OPTIONAL { ?s <urn:q> '123'^^<http://www.w3.org/2001/XMLSchema#int>
. ?s <urn:v> ?x . FILTER(?x>56) }}");
+		
+		Assert.assertEquals( expected.getQueryPattern(), builder.build().getQueryPattern());	
+	}
 
 	@ContractTest
 	public void testAddFilter() throws ParseException {

http://git-wip-us.apache.org/repos/asf/jena/blob/f7effc4e/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/WhereHandlerTest.java
----------------------------------------------------------------------
diff --git a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/WhereHandlerTest.java
b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/WhereHandlerTest.java
index f1bc12d..9ef1a55 100644
--- a/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/WhereHandlerTest.java
+++ b/jena-extras/jena-querybuilder/src/test/java/org/apache/jena/arq/querybuilder/handlers/WhereHandlerTest.java
@@ -27,11 +27,13 @@ import org.apache.jena.graph.NodeFactory;
 import org.apache.jena.graph.Triple;
 import org.apache.jena.graph.impl.LiteralLabelFactory;
 import org.apache.jena.query.Query;
+import org.apache.jena.query.QueryFactory;
 import org.apache.jena.rdf.model.ResourceFactory;
 import org.apache.jena.sparql.core.Var;
 import org.apache.jena.sparql.expr.E_Random;
 import org.apache.jena.sparql.lang.sparql_11.ParseException;
 import org.apache.jena.vocabulary.RDF;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -107,6 +109,29 @@ public class WhereHandlerTest extends AbstractHandlerTest {
 				query.toString());
 	}
 
+	
+	@Test
+	public void testAddOptionalWhereHandler() throws ParseException {
+		
+		WhereHandler pattern = new WhereHandler(new Query());
+		Var s = Var.alloc("s" );
+		Node q = NodeFactory.createURI( "urn:q" );
+		Node v = NodeFactory.createURI( "urn:v" );
+		Var x = Var.alloc("x");
+		Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));	
+		
+		pattern.addWhere( new Triple( s, q,  n123 ) );
+		pattern.addWhere( new Triple( s, v, x));
+		pattern.addFilter( "?x>56");
+		
+		handler.addOptional( pattern );
+		
+		Query expected = QueryFactory.create( "SELECT * WHERE { OPTIONAL { ?s <urn:q> '123'^^<http://www.w3.org/2001/XMLSchema#int>
. ?s <urn:v> ?x . FILTER(?x>56) }}");
+		
+		Assert.assertEquals( expected.getQueryPattern(), query.getQueryPattern());
+
+	}
+	
 	@Test
 	public void testAddOptionalObjects() {
 		handler.addOptional(new Triple(NodeFactory.createURI("one"),


Mime
View raw message