jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [09/50] jena git commit: fix hashCode for re-parsing query
Date Mon, 24 Aug 2015 08:13:18 GMT
fix hashCode for re-parsing query

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

Branch: refs/heads/master
Commit: 36d32da64b53d4af930f9395b2fd90c7d302b30f
Parents: 4bb4a7f
Author: confidencesun <confidence.sun@gmail.com>
Authored: Thu Aug 6 10:05:04 2015 +0800
Committer: confidencesun <confidence.sun@gmail.com>
Committed: Thu Aug 6 10:05:04 2015 +0800

----------------------------------------------------------------------
 .../constructquads/ExampleConstructQuads.java   | 228 ++++++++++---------
 .../jena/sparql/lang/arq/ARQParserBase.java     |  12 +-
 .../jena/sparql/serializer/FmtTemplate.java     |  59 +++--
 .../org/apache/jena/sparql/syntax/Template.java |  28 ++-
 4 files changed, 169 insertions(+), 158 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/36d32da6/jena-arq/src-examples/arq/examples/constructquads/ExampleConstructQuads.java
----------------------------------------------------------------------
diff --git a/jena-arq/src-examples/arq/examples/constructquads/ExampleConstructQuads.java
b/jena-arq/src-examples/arq/examples/constructquads/ExampleConstructQuads.java
index 0f509b0..ff54915 100644
--- a/jena-arq/src-examples/arq/examples/constructquads/ExampleConstructQuads.java
+++ b/jena-arq/src-examples/arq/examples/constructquads/ExampleConstructQuads.java
@@ -46,119 +46,119 @@ import org.apache.jena.util.PrintUtil;
 public class ExampleConstructQuads {
 	public static void main(String[] args) throws FileNotFoundException, IOException {
 
-		// create testing data :
-		// 1) default graph data
-		Model model = ModelFactory.createDefaultModel();
-		Resource s = model.createResource("http://eg.com/s");
-		Property p = model.createProperty("http://eg.com/p");
-		Resource o = model.createResource("http://eg.com/o");
-		model.add(s, p, o);
-		Dataset dataset = DatasetFactory.create(model);
-		// 2) named graph data
-		Model model1 = ModelFactory.createDefaultModel();
-		Resource s1 = model.createResource("http://eg.com/s1");
-		Property p1 = model.createProperty("http://eg.com/p1");
-		Resource o1 = model.createResource("http://eg.com/o1");
-		model1.add(s1, p1, o1);
-		dataset.addNamedModel("http://eg.com/g1", model1);
-
-		// construct named graph
-		System.out.println("construct named graph:");
-		String queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1>
?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
-		Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Quad> quads = qexec.execConstructQuads();
-			PrintUtil.printOut(quads);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// construct default graph 1
-		System.out.println("construct default graph 1:");
-		queryString = "CONSTRUCT {  {<http://eg.com/s1> <http://eg.com/p1> ?o} } WHERE{
GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Quad> quads = qexec.execConstructQuads();
-			PrintUtil.printOut(quads);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// construct default graph 2
-		System.out.println("construct default graph 2:");
-		queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o}  WHERE{
GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Quad> quads = qexec.execConstructQuads();
-			PrintUtil.printOut(quads);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// construct triples
-		System.out.println("construct default graph 2:");
-		queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o}  WHERE{
GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Triple> triples = qexec.execConstructTriples();
-			PrintUtil.printOut(triples);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// construct dataset
-		System.out.println("construct dataset:");
-		queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1>
?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Dataset d = qexec.execConstructDataset();
-			RDFDataMgr.write(System.out, d, Lang.TRIG);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// short form 1
-		System.out.println("short form 1:");
-		queryString = "CONSTRUCT WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1>
?o} }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Quad> quads = qexec.execConstructQuads();
-			PrintUtil.printOut(quads);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// short form 2
-		System.out.println("short form 2:");
-		queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Quad> quads = qexec.execConstructQuads();
-			PrintUtil.printOut(quads);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// short form 3
-		System.out.println("short form 3:");
-		queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Triple> triples = qexec.execConstructTriples();
-			PrintUtil.printOut(triples);
-		}
-		arq.qparse.main("--in", "arq", queryString);
-
-		// short form 4
-		System.out.println("short form 4:");
-		queryString = "CONSTRUCT WHERE{ {<http://eg.com/s> <http://eg.com/p> ?o} }";
-		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
-		try (QueryExecution qexec = QueryExecutionFactory
-				.create(query, dataset)) {
-			Iterator<Quad> quads = qexec.execConstructQuads();
-			PrintUtil.printOut(quads);
-		}
-		arq.qparse.main("--in", "arq", queryString);
+//		// create testing data :
+//		// 1) default graph data
+//		Model model = ModelFactory.createDefaultModel();
+//		Resource s = model.createResource("http://eg.com/s");
+//		Property p = model.createProperty("http://eg.com/p");
+//		Resource o = model.createResource("http://eg.com/o");
+//		model.add(s, p, o);
+//		Dataset dataset = DatasetFactory.create(model);
+//		// 2) named graph data
+//		Model model1 = ModelFactory.createDefaultModel();
+//		Resource s1 = model.createResource("http://eg.com/s1");
+//		Property p1 = model.createProperty("http://eg.com/p1");
+//		Resource o1 = model.createResource("http://eg.com/o1");
+//		model1.add(s1, p1, o1);
+//		dataset.addNamedModel("http://eg.com/g1", model1);
+//
+//		// construct named graph
+//		System.out.println("construct named graph:");
+//		String queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1>
?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
+//		Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Quad> quads = qexec.execConstructQuads();
+//			PrintUtil.printOut(quads);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// construct default graph 1
+//		System.out.println("construct default graph 1:");
+//		queryString = "CONSTRUCT {  {<http://eg.com/s1> <http://eg.com/p1> ?o} }
WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Quad> quads = qexec.execConstructQuads();
+//			PrintUtil.printOut(quads);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// construct default graph 2
+//		System.out.println("construct default graph 2:");
+//		queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o}  WHERE{
GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Quad> quads = qexec.execConstructQuads();
+//			PrintUtil.printOut(quads);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// construct triples
+//		System.out.println("construct default graph 2:");
+//		queryString = "CONSTRUCT {<http://eg.com/s1> <http://eg.com/p1> ?o}  WHERE{
GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Triple> triples = qexec.execConstructTriples();
+//			PrintUtil.printOut(triples);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// construct dataset
+//		System.out.println("construct dataset:");
+//		queryString = "CONSTRUCT { GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1>
?o} } WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1> ?o} }";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Dataset d = qexec.execConstructDataset();
+//			RDFDataMgr.write(System.out, d, Lang.TRIG);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// short form 1
+//		System.out.println("short form 1:");
+//		queryString = "CONSTRUCT WHERE{ GRAPH ?g {<http://eg.com/s1> <http://eg.com/p1>
?o} }";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Quad> quads = qexec.execConstructQuads();
+//			PrintUtil.printOut(quads);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// short form 2
+//		System.out.println("short form 2:");
+//		queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Quad> quads = qexec.execConstructQuads();
+//			PrintUtil.printOut(quads);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// short form 3
+//		System.out.println("short form 3:");
+//		queryString = "CONSTRUCT WHERE{ <http://eg.com/s> <http://eg.com/p> ?o }";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Triple> triples = qexec.execConstructTriples();
+//			PrintUtil.printOut(triples);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
+//
+//		// short form 4
+//		System.out.println("short form 4:");
+//		queryString = "CONSTRUCT WHERE{ {<http://eg.com/s> <http://eg.com/p> ?o}
}";
+//		query = QueryFactory.create(queryString, Syntax.syntaxARQ);
+//		try (QueryExecution qexec = QueryExecutionFactory
+//				.create(query, dataset)) {
+//			Iterator<Quad> quads = qexec.execConstructQuads();
+//			PrintUtil.printOut(quads);
+//		}
+//		arq.qparse.main("--in", "arq", queryString);
 
 		// run-construct-quad-test
 		System.out.println("run-construct-quad-test:");
@@ -171,7 +171,9 @@ public class ExampleConstructQuads {
 						return false;
 					}
 				});
+		int i=0;
 		for (File test : tests) {
+//			if (++i != 4) continue;
 			System.out.println("======== File: "+test.getName());
 			try (BufferedReader br = new BufferedReader(new FileReader(test))) {
 				String line = null;

http://git-wip-us.apache.org/repos/asf/jena/blob/36d32da6/jena-arq/src/main/java/org/apache/jena/sparql/lang/arq/ARQParserBase.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/lang/arq/ARQParserBase.java b/jena-arq/src/main/java/org/apache/jena/sparql/lang/arq/ARQParserBase.java
index bb2a01b..abfb512 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/lang/arq/ARQParserBase.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/lang/arq/ARQParserBase.java
@@ -19,6 +19,7 @@
 package org.apache.jena.sparql.lang.arq;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.jena.atlas.json.io.JSONHandler ;
 import org.apache.jena.atlas.json.io.JSONHandlerBase ;
@@ -81,16 +82,7 @@ class ARQParserBase
     protected void jsonValueVar(String image, long currLine, long currCol)       { throw
new NotImplemented("yet") ; }
     protected ElementGroup createQueryPattern(Template t){
         ElementGroup elg = new ElementGroup();
-        List<Quad> quads = t.getQuads();
-        HashMap<Node, BasicPattern> graphs = new HashMap<>();
-        for (Quad q: quads){
-          BasicPattern bgp = graphs.get(q.getGraph());
-          if (bgp == null){
-            bgp = new BasicPattern();
-            graphs.put(q.getGraph(), bgp);
-          }
-          bgp.add( q.asTriple() );
-        }
+        Map<Node, BasicPattern> graphs = t.getGraphPattern();
         for(Node n: graphs.keySet()){
           Element el = new ElementPathBlock(graphs.get(n));
           if(! Quad.defaultGraphNodeGenerated.equals(n) ){

http://git-wip-us.apache.org/repos/asf/jena/blob/36d32da6/jena-arq/src/main/java/org/apache/jena/sparql/serializer/FmtTemplate.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/serializer/FmtTemplate.java b/jena-arq/src/main/java/org/apache/jena/sparql/serializer/FmtTemplate.java
index d422d1c..facb963 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/serializer/FmtTemplate.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/serializer/FmtTemplate.java
@@ -18,8 +18,12 @@
 
 package org.apache.jena.sparql.serializer;
 
+import java.util.List;
+
 import org.apache.jena.atlas.io.IndentedLineBuffer;
 import org.apache.jena.atlas.io.IndentedWriter;
+import org.apache.jena.sparql.core.BasicPattern;
+import org.apache.jena.sparql.core.Quad;
 import org.apache.jena.sparql.syntax.Template;
 import org.apache.jena.sparql.util.FmtUtils;
 
@@ -52,35 +56,42 @@ public class FmtTemplate extends FormatterBase
     @Override
     public void format(Template template)
     {
-    	
-    	if (template.isConstructQuadTemplate()){
-            out.print("{") ;
-            out.incIndent(INDENT) ;
-            out.pad() ;
-            
-    		out.print("GRAPH");
-    		out.print(" ");
-    		out.print(FmtUtils.stringForNode(template.getGraphNode()));
-    		out.print(" ");
-    	}
-    	
-    
         out.print("{") ;
         out.incIndent(INDENT) ;
         out.pad() ;
-    
-        formatTriples(template.getBGP()) ;
         
-        out.decIndent(INDENT) ;
-        out.print("}") ;
-        out.newline() ;
-        
-    	if (template.isConstructQuadTemplate()){
-            out.decIndent(INDENT) ;
-            out.print("}") ;
-            out.newline() ;
-    	}
+        List<Quad> quads = template.getQuads();
+        for(Quad quad: quads){
+          BasicPattern bgp = new BasicPattern();
+          bgp.add(quad.asTriple());
+          out.newline() ;
+          if(! Quad.defaultGraphNodeGenerated.equals(quad.getGraph()) ){
 
+        	out.print("GRAPH");
+      		out.print(" ");
+      		out.print(FmtUtils.stringForNode(quad.getGraph()));
+      		out.print(" ");
+      		
+      		out.newline() ;
+            out.incIndent(INDENT) ;
+            out.pad() ;
+            out.print("{") ;
+            out.incIndent(INDENT) ;
+            out.pad() ;
+          }
+          
+          formatTriples(bgp) ;
+          
+          if(! Quad.defaultGraphNodeGenerated.equals(quad.getGraph()) ){
+              out.decIndent(INDENT) ;
+              out.print("}") ;
+              out.decIndent(INDENT) ;
+          }
+       }
+       out.newline() ;
+       out.decIndent(INDENT) ;
+       out.print("}") ;
+       out.newline() ;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/36d32da6/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 eb970c1..3955866 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
@@ -20,9 +20,11 @@ package org.apache.jena.sparql.syntax;
 
 import java.util.ArrayList;
 import java.util.Collection;
+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;
@@ -54,17 +56,6 @@ public class Template
     	this.qp = null;
     }
     
-    public boolean isConstructQuadTemplate(){
-    	return this.qp != null && ! this.getQuads().isEmpty();
-    }
-    
-    public Node getGraphNode() {
-    	if (!this.isConstructQuadTemplate()){
-    		return null;
-    	}
-    	return this.getQuads().get(0).getGraph();
-    }
-
 //    public void addTriple(Triple t) { quads.addTriple(t) ; }
 //    public int mark() { return quads.mark() ; }
 //    public void addTriple(int index, Triple t) { quads.addTriple(index, t) ; }
@@ -106,6 +97,21 @@ public class Template
     	}   	
     	return qp.getQuads() ; 
     }
+    
+    public Map<Node, BasicPattern> getGraphPattern(){
+        List<Quad> quads = getQuads();
+        HashMap<Node, BasicPattern> graphs = new HashMap<>();
+        for (Quad q: quads){
+          BasicPattern bgp = graphs.get(q.getGraph());
+          if (bgp == null){
+            bgp = new BasicPattern();
+            graphs.put(q.getGraph(), bgp);
+          }
+          bgp.add( q.asTriple() );
+        }
+        return graphs;
+    }
+
     // -------------------------
 
     public void subst(Collection<Triple> acc, Map<Node, Node> bNodeMap, Binding
b)


Mime
View raw message