jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [13/50] jena git commit: Add test support in QueryTest for Construct Quad: 1) define new type of SPARQLResult for Dataset; 2) define new ResultsFormat of Trig for Dataset results;
Date Mon, 24 Aug 2015 08:13:22 GMT
Add test support in QueryTest for Construct Quad: 1) define new type of
SPARQLResult for Dataset; 2) define new ResultsFormat of Trig for
Dataset results; 

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

Branch: refs/heads/master
Commit: 6b695c59166a25a999840873bfb0af1c4198b202
Parents: 50a65c3
Author: confidencesun <confidence.sun@gmail.com>
Authored: Mon Aug 10 16:59:20 2015 +0800
Committer: confidencesun <confidence.sun@gmail.com>
Committed: Mon Aug 10 16:59:20 2015 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/jena/query/Query.java  |   3 +-
 .../jena/sparql/resultset/ResultsFormat.java    |  13 +++
 .../jena/sparql/resultset/SPARQLResult.java     |  33 +++++-
 .../org/apache/jena/sparql/syntax/Template.java |  11 +-
 .../org/apache/jena/sparql/junit/QueryTest.java | 117 ++++++++++++++-----
 .../org/apache/jena/sparql/junit/TestItem.java  |  45 ++++---
 6 files changed, 165 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/6b695c59/jena-arq/src/main/java/org/apache/jena/query/Query.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/query/Query.java b/jena-arq/src/main/java/org/apache/jena/query/Query.java
index 914e626..f0d6233 100644
--- a/jena-arq/src/main/java/org/apache/jena/query/Query.java
+++ b/jena-arq/src/main/java/org/apache/jena/query/Query.java
@@ -146,7 +146,8 @@ public class Query extends Prologue implements Cloneable, Printable
     public boolean isAskType()                  { return queryType == QueryTypeAsk ; }
 
     public boolean isUnknownType()              { return queryType == QueryTypeUnknown ;
}
-
+    
+    public boolean isConstructQuad()            { return isConstructType() && constructTemplate.containsRealQuad()
; }
     // It was a mistake to extend Prologue ... but what is done is done.
     public Prologue getPrologue()               { return this ; }
     

http://git-wip-us.apache.org/repos/asf/jena/blob/6b695c59/jena-arq/src/main/java/org/apache/jena/sparql/resultset/ResultsFormat.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/ResultsFormat.java b/jena-arq/src/main/java/org/apache/jena/sparql/resultset/ResultsFormat.java
index 2a103cf..c23cfad 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/ResultsFormat.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/resultset/ResultsFormat.java
@@ -63,7 +63,9 @@ public class ResultsFormat extends Symbol
     static public ResultsFormat FMT_RDF_TTL      = new ResultsFormat(contentTypeTurtle) ;
     static public ResultsFormat FMT_RDF_TURTLE   = new ResultsFormat(contentTypeTurtle) ;
     static public ResultsFormat FMT_RDF_NT       = new ResultsFormat(contentTypeNTriples)
;
+    static public ResultsFormat FMT_TRIG         = new ResultsFormat(contentTypeTriG) ;
     static public ResultsFormat FMT_UNKNOWN      = new ResultsFormat("unknown") ;
+    
     // ---- Compatibility
     
     // Common names to symbol (used by arq.rset)
@@ -95,6 +97,8 @@ public class ResultsFormat extends Symbol
         names.put("graph",       FMT_RDF_TTL) ;
         names.put("nt",          FMT_RDF_NT) ;
         names.put("n-triples",   FMT_RDF_NT) ;
+        
+        names.put("trig",        FMT_TRIG) ;
 
     }
 
@@ -112,6 +116,11 @@ public class ResultsFormat extends Symbol
         return false ;
     }
     
+    public static boolean isDatasetSyntax(ResultsFormat fmt){
+    	if ( FMT_TRIG.equals(fmt) ) return true ;
+    	return false;
+    }
+    
     public static ResultsFormat guessSyntax(String url, ResultsFormat defaultFormat)
     {
         // -- XML
@@ -153,6 +162,10 @@ public class ResultsFormat extends Symbol
         if ( url.endsWith(".tsv") )
             return FMT_RS_TSV ;
         
+        // Trig for Dataset
+        if ( url.endsWith(".trig") )
+            return FMT_TRIG ;
+        
         return defaultFormat ;
     }
     

http://git-wip-us.apache.org/repos/asf/jena/blob/6b695c59/jena-arq/src/main/java/org/apache/jena/sparql/resultset/SPARQLResult.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/SPARQLResult.java b/jena-arq/src/main/java/org/apache/jena/sparql/resultset/SPARQLResult.java
index 1506e44..bbc5c55 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/SPARQLResult.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/resultset/SPARQLResult.java
@@ -18,12 +18,13 @@
 
 package org.apache.jena.sparql.resultset;
 
-import org.apache.jena.atlas.logging.Log ;
-import org.apache.jena.graph.Node ;
-import org.apache.jena.query.ResultSet ;
-import org.apache.jena.rdf.model.Model ;
-import org.apache.jena.sparql.core.Var ;
-import org.apache.jena.sparql.engine.binding.BindingMap ;
+import org.apache.jena.atlas.logging.Log;
+import org.apache.jena.graph.Node;
+import org.apache.jena.query.Dataset;
+import org.apache.jena.query.ResultSet;
+import org.apache.jena.rdf.model.Model;
+import org.apache.jena.sparql.core.Var;
+import org.apache.jena.sparql.engine.binding.BindingMap;
 
 /**
  * The class "ResultSet" is reserved for the SELECT result format.
@@ -37,6 +38,7 @@ public class SPARQLResult
     private ResultSet resultSet = null ;
     private Boolean booleanResult = null ;
     private Model model = null ;
+    private Dataset dataset = null ;
     
     // Delayed choice of result type.
     protected SPARQLResult() {}
@@ -44,6 +46,7 @@ public class SPARQLResult
     public SPARQLResult(Model model)            { set(model) ; }
     public SPARQLResult(ResultSet resultSet)    { set(resultSet) ;}
     public SPARQLResult(boolean booleanResult)  { set(booleanResult) ; }
+    public SPARQLResult(Dataset dataset)        { set(dataset) ; }
     
     public boolean isResultSet()
     {
@@ -61,6 +64,13 @@ public class SPARQLResult
             throw new ResultSetException("Not set") ;
         return model != null ;
     }
+    
+    public boolean isDataset()
+    {
+        if ( ! hasBeenSet )
+            throw new ResultSetException("Not set") ;
+        return dataset != null ;
+    }
 
     public boolean isBoolean()
     {
@@ -96,6 +106,14 @@ public class SPARQLResult
         return model ;
     }
     
+    public Dataset getDataset() { 
+        if ( ! hasBeenSet )
+            throw new ResultSetException("Not set") ;
+        if ( ! isDataset() )
+            throw new ResultSetException("Not a dataset result") ;
+        return dataset ;
+    }
+    
     public boolean isHasBeenSet() { return hasBeenSet; }
     
     protected void set(ResultSet rs)
@@ -107,6 +125,9 @@ public class SPARQLResult
     protected void set(Model m)
     { model = m ; hasBeenSet = true ; }
     
+    protected void set(Dataset d)
+    { dataset = d ; hasBeenSet = true ; }
+    
     protected void set(boolean r)
     { set (new Boolean(r)) ; } 
     

http://git-wip-us.apache.org/repos/asf/jena/blob/6b695c59/jena-arq/src/main/java/org/apache/jena/sparql/syntax/Template.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/syntax/Template.java b/jena-arq/src/main/java/org/apache/jena/sparql/syntax/Template.java
index 3955866..a6e74d8 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/syntax/Template.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/syntax/Template.java
@@ -24,7 +24,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.jena.ext.com.google.common.collect.Multimap;
 import org.apache.jena.graph.Node;
 import org.apache.jena.graph.Triple;
 import org.apache.jena.sparql.core.BasicPattern;
@@ -65,6 +64,16 @@ public class Template
 //    public void addTriplePath(int index, TriplePath path)
 //    { throw new ARQException("Triples-only collector") ; }
 
+    
+    public boolean containsRealQuad(){
+    	for(Quad quad : this.getQuads()){
+    		if ( ! Quad.defaultGraphNodeGenerated.equals( quad.getGraph())){
+    			return true;
+    		}
+    	}
+    	return false;
+    }
+    
     public BasicPattern getBGP()
     { 
     	if (this.bgp != null){

http://git-wip-us.apache.org/repos/asf/jena/blob/6b695c59/jena-arq/src/test/java/org/apache/jena/sparql/junit/QueryTest.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/junit/QueryTest.java b/jena-arq/src/test/java/org/apache/jena/sparql/junit/QueryTest.java
index 89844e1..519316f 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/junit/QueryTest.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/junit/QueryTest.java
@@ -18,35 +18,55 @@
 
 package org.apache.jena.sparql.junit;
 
-import java.io.IOException ;
-import java.io.PrintStream ;
-import java.io.PrintWriter ;
-import java.util.* ;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
-import org.apache.jena.atlas.logging.Log ;
-import org.apache.jena.graph.Node ;
-import org.apache.jena.graph.NodeFactory ;
-import org.apache.jena.query.* ;
-import org.apache.jena.rdf.model.* ;
-import org.apache.jena.riot.checker.CheckerLiterals ;
-import org.apache.jena.shared.JenaException ;
-import org.apache.jena.sparql.SystemARQ ;
-import org.apache.jena.sparql.core.Var ;
-import org.apache.jena.sparql.engine.QueryIterator ;
-import org.apache.jena.sparql.engine.ResultSetStream ;
-import org.apache.jena.sparql.engine.binding.Binding ;
-import org.apache.jena.sparql.engine.binding.BindingFactory ;
-import org.apache.jena.sparql.engine.binding.BindingMap ;
-import org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper ;
-import org.apache.jena.sparql.expr.nodevalue.NodeFunctions ;
-import org.apache.jena.sparql.resultset.RDFOutput ;
-import org.apache.jena.sparql.resultset.ResultSetCompare ;
-import org.apache.jena.sparql.resultset.SPARQLResult ;
-import org.apache.jena.sparql.util.DatasetUtils ;
-import org.apache.jena.sparql.vocabulary.ResultSetGraphVocab ;
-import org.apache.jena.util.FileUtils ;
-import org.apache.jena.util.junit.TestUtils ;
-import org.apache.jena.vocabulary.RDF ;
+import org.apache.jena.atlas.logging.Log;
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.query.Dataset;
+import org.apache.jena.query.Query;
+import org.apache.jena.query.QueryException;
+import org.apache.jena.query.QueryExecution;
+import org.apache.jena.query.QueryExecutionFactory;
+import org.apache.jena.query.ResultSet;
+import org.apache.jena.query.ResultSetFactory;
+import org.apache.jena.query.ResultSetFormatter;
+import org.apache.jena.query.ResultSetRewindable;
+import org.apache.jena.rdf.model.Model;
+import org.apache.jena.rdf.model.ModelFactory;
+import org.apache.jena.rdf.model.Property;
+import org.apache.jena.rdf.model.RDFNode;
+import org.apache.jena.rdf.model.Resource;
+import org.apache.jena.rdf.model.Statement;
+import org.apache.jena.rdf.model.StmtIterator;
+import org.apache.jena.riot.Lang;
+import org.apache.jena.riot.RDFDataMgr;
+import org.apache.jena.riot.checker.CheckerLiterals;
+import org.apache.jena.shared.JenaException;
+import org.apache.jena.sparql.SystemARQ;
+import org.apache.jena.sparql.core.Var;
+import org.apache.jena.sparql.engine.QueryIterator;
+import org.apache.jena.sparql.engine.ResultSetStream;
+import org.apache.jena.sparql.engine.binding.Binding;
+import org.apache.jena.sparql.engine.binding.BindingFactory;
+import org.apache.jena.sparql.engine.binding.BindingMap;
+import org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper;
+import org.apache.jena.sparql.expr.nodevalue.NodeFunctions;
+import org.apache.jena.sparql.resultset.RDFOutput;
+import org.apache.jena.sparql.resultset.ResultSetCompare;
+import org.apache.jena.sparql.resultset.SPARQLResult;
+import org.apache.jena.sparql.util.DatasetUtils;
+import org.apache.jena.sparql.util.IsoMatcher;
+import org.apache.jena.sparql.vocabulary.ResultSetGraphVocab;
+import org.apache.jena.util.FileUtils;
+import org.apache.jena.util.junit.TestUtils;
+import org.apache.jena.vocabulary.RDF;
 
 public class QueryTest extends EarlTestCase
 {
@@ -348,8 +368,13 @@ public class QueryTest extends EarlTestCase
    void runTestConstruct(Query query, QueryExecution qe)
     {
         // Do the query!
-        Model resultsActual = qe.execConstruct() ;
-        compareGraphResults(resultsActual, query) ;
+	    if ( query.isConstructQuad() ){
+	    	Dataset resultActual = qe.execConstructDataset();
+	    	compareDatasetResults(resultActual, query) ;
+	    } else {
+	    	Model resultsActual = qe.execConstruct() ;
+	    	compareGraphResults(resultsActual, query) ;
+	    }
     }
    
    private void compareGraphResults(Model resultsActual, Query query)
@@ -373,6 +398,28 @@ public class QueryTest extends EarlTestCase
             }
         }
     }
+   
+   private void compareDatasetResults(Dataset resultsActual, Query query)
+   {
+        if ( results != null )
+        {
+            try {
+                if ( ! results.isDataset() )
+                    fail("Expected results are not a graph: "+testItem.getName()) ;
+                    
+                Dataset resultsExpected = results.getDataset() ;
+                if ( ! IsoMatcher.isomorphic( resultsExpected.asDatasetGraph(),resultsActual.asDatasetGraph()
) )
+                {
+                    printFailedDatasetTest(query, resultsExpected, resultsActual) ;
+                    fail("Results do not match: "+testItem.getName()) ;
+                }
+            } catch (Exception ex)
+            {
+                String typeName = (query.isConstructType()?"construct":"describe") ;
+                fail("Exception in result testing ("+typeName+"): "+ex) ;
+            }
+        }
+    }
     
     void runTestDescribe(Query query, QueryExecution qe)
     {
@@ -448,6 +495,16 @@ public class QueryTest extends EarlTestCase
         out.println() ;
     }
     
+    void printFailedDatasetTest(Query query, Dataset expected, Dataset results)
+    {
+    	System.out.println("=======================================") ;
+    	System.out.println("Failure: "+description()) ;
+    	RDFDataMgr.write(System.out, results, Lang.TRIG);
+    	System.out.println("---------------------------------------") ;
+    	RDFDataMgr.write(System.out, expected, Lang.TRIG);
+        System.out.println() ;
+    }
+    
     @Override
     public String toString()
     { 

http://git-wip-us.apache.org/repos/asf/jena/blob/6b695c59/jena-arq/src/test/java/org/apache/jena/sparql/junit/TestItem.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/junit/TestItem.java b/jena-arq/src/test/java/org/apache/jena/sparql/junit/TestItem.java
index b6bcf05..c6a4c40 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/junit/TestItem.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/junit/TestItem.java
@@ -18,25 +18,27 @@
 
 package org.apache.jena.sparql.junit ;
 
-import java.util.ArrayList ;
-import java.util.List ;
-
-import org.apache.jena.query.ResultSetFactory ;
-import org.apache.jena.query.Syntax ;
-import org.apache.jena.rdf.model.Model ;
-import org.apache.jena.rdf.model.Resource ;
-import org.apache.jena.rdf.model.Statement ;
-import org.apache.jena.sparql.resultset.ResultsFormat ;
-import org.apache.jena.sparql.resultset.SPARQLResult ;
-import org.apache.jena.sparql.vocabulary.TestManifest ;
-import org.apache.jena.sparql.vocabulary.TestManifestX ;
-import org.apache.jena.sparql.vocabulary.VocabTestQuery ;
-import org.apache.jena.util.FileManager ;
-import org.apache.jena.util.iterator.ClosableIterator ;
-import org.apache.jena.util.junit.TestException ;
-import org.apache.jena.util.junit.TestUtils ;
-import org.apache.jena.vocabulary.RDF ;
-import org.apache.jena.vocabulary.RDFS ;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.jena.query.Dataset;
+import org.apache.jena.query.ResultSetFactory;
+import org.apache.jena.query.Syntax;
+import org.apache.jena.rdf.model.Model;
+import org.apache.jena.rdf.model.Resource;
+import org.apache.jena.rdf.model.Statement;
+import org.apache.jena.riot.RDFDataMgr;
+import org.apache.jena.sparql.resultset.ResultsFormat;
+import org.apache.jena.sparql.resultset.SPARQLResult;
+import org.apache.jena.sparql.vocabulary.TestManifest;
+import org.apache.jena.sparql.vocabulary.TestManifestX;
+import org.apache.jena.sparql.vocabulary.VocabTestQuery;
+import org.apache.jena.util.FileManager;
+import org.apache.jena.util.iterator.ClosableIterator;
+import org.apache.jena.util.junit.TestException;
+import org.apache.jena.util.junit.TestUtils;
+import org.apache.jena.vocabulary.RDF;
+import org.apache.jena.vocabulary.RDFS;
 
 /**
  * Wrapper class for individual test items. Assumes it is a query test item,
@@ -151,6 +153,11 @@ public class TestItem
             Model m = FileManager.get().loadModel(resultFile) ;
             return new SPARQLResult(m) ;
         }
+        
+        if ( ResultsFormat.isDatasetSyntax(format) ) {
+            Dataset d = RDFDataMgr.loadDataset(resultFile) ;
+            return new SPARQLResult(d) ;
+        }
 
         // Attempt to handle as a resulset or boolean result.s
         SPARQLResult x = ResultSetFactory.result(resultFile) ;


Mime
View raw message