clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject svn commit: r1503363 - in /clerezza/trunk/rdf.jena.sparql/src: main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
Date Mon, 15 Jul 2013 17:36:19 GMT
Author: reto
Date: Mon Jul 15 17:36:19 2013
New Revision: 1503363

URL: http://svn.apache.org/r1503363
Log:
CLEREZZA-803: added support for sparql update. Incomplete testing because of limited support
in SpaqrlPreParser 

Modified:
    clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
    clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java

Modified: clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java?rev=1503363&r1=1503362&r2=1503363&view=diff
==============================================================================
--- clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
(original)
+++ clerezza/trunk/rdf.jena.sparql/src/main/java/org/apache/clerezza/rdf/jena/sparql/JenaSparqlEngine.java
Mon Jul 15 17:36:19 2013
@@ -31,10 +31,17 @@ import org.apache.clerezza.rdf.core.spar
 import org.apache.clerezza.rdf.jena.storage.JenaGraphAdaptor;
 
 import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.query.QueryException;
 import com.hp.hpl.jena.query.QueryExecException;
 import com.hp.hpl.jena.query.QueryExecution;
 import com.hp.hpl.jena.query.QueryExecutionFactory;
 import com.hp.hpl.jena.query.QueryFactory;
+import com.hp.hpl.jena.sparql.resultset.SPARQLResult;
+import com.hp.hpl.jena.update.GraphStore;
+import com.hp.hpl.jena.update.GraphStoreFactory;
+import com.hp.hpl.jena.update.UpdateAction;
+import com.hp.hpl.jena.update.UpdateFactory;
+import com.hp.hpl.jena.update.UpdateRequest;
 
 /**
  * 
@@ -52,40 +59,7 @@ public class JenaSparqlEngine implements
 	@Override
 	public Object execute(TcManager tcManager, TripleCollection defaultGraph,
 			final Query query) {
-		final Dataset dataset = new TcDataset(tcManager, defaultGraph);
-
-		// Missing permission (java.lang.RuntimePermission getClassLoader)
-		// when calling QueryFactory.create causes ExceptionInInitializerError
-		// to be thrown.
-		// QueryExecutionFactory.create requires
-		// (java.io.FilePermission [etc/]location-mapping.* read)
-		// Thus, they are placed within doPrivileged
-		QueryExecution qexec = AccessController
-				.doPrivileged(new PrivilegedAction<QueryExecution>() {
-
-					@Override
-					public QueryExecution run() {
-						com.hp.hpl.jena.query.Query jenaQuery = QueryFactory
-								.create(query.toString());
-						return QueryExecutionFactory.create(jenaQuery, dataset);
-					}
-				});
-
-		try {
-			if (query instanceof AskQuery) {
-				return Boolean.valueOf(qexec.execAsk());
-			} else if (query instanceof DescribeQuery) {
-				return new JenaGraphAdaptor(qexec.execDescribe().getGraph())
-						.getGraph();
-			} else if (query instanceof ConstructQuery) {
-				return new JenaGraphAdaptor(qexec.execConstruct().getGraph())
-						.getGraph();
-			} else {
-				return new ResultSetWrapper(qexec.execSelect());
-			}
-		} finally {
-			qexec.close();
-		}
+		return execute(tcManager, defaultGraph, query.toString());
 	}
 
 	@Override
@@ -104,12 +78,23 @@ public class JenaSparqlEngine implements
 
 					@Override
 					public QueryExecution run() {
-						com.hp.hpl.jena.query.Query jenaQuery = QueryFactory
-								.create(query);
-						return QueryExecutionFactory.create(jenaQuery, dataset);
+                        try {
+                            com.hp.hpl.jena.query.Query jenaQuery = QueryFactory
+                                	.create(query);
+                            if (jenaQuery.isUnknownType()) {
+                                return null;
+                            }
+                            return QueryExecutionFactory.create(jenaQuery, dataset);
+                        } catch (QueryException ex) {
+                            return null;
+                        }
+                        
 					}
 				});
-
+        if (qexec == null) {
+            return executeUpdate(dataset, query);
+        }
+        //TODO check with rather than trial and error: if (qexec.getQuery().isSelectType())
{
 		try {
 			try {
 				return new ResultSetWrapper(qexec.execSelect());
@@ -130,4 +115,10 @@ public class JenaSparqlEngine implements
 			qexec.close();
 		}
 	}
+
+    private Object executeUpdate(Dataset dataset, String query) {
+        GraphStore graphStore = GraphStoreFactory.create(dataset) ;
+        UpdateAction.parseExecute(query, graphStore) ;
+        return true;
+    }
 }

Modified: clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java?rev=1503363&r1=1503362&r2=1503363&view=diff
==============================================================================
--- clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
(original)
+++ clerezza/trunk/rdf.jena.sparql/src/test/java/org/apache/clerezza/rdf/jena/sparql/SimpleTest.java
Mon Jul 15 17:36:19 2013
@@ -118,4 +118,30 @@ public class SimpleTest {
 				((Graph) TcManager.getInstance().executeSparqlQuery(
 						QueryParser.getInstance().parse(query), data)).size());
 	}
+    
+    /* Currently fails because of Preparser not yet supporting this type of queries
+    @Test
+	public void simpleInsert() throws ParseException {
+		SimpleMGraph data = new SimpleMGraph();
+		final String titleValue = "SPARQL Tutorial";
+		data.add(new TripleImpl(new UriRef("http://example.org/book/book1"),
+				DC.title, new PlainLiteralImpl(titleValue)));
+        
+		String query = "PREFIX foaf:   <http://xmlns.com/foaf/0.1/> "
+				+ "INSERT DATA { <http://foo/bar> foaf:name 'Fred'}";
+        TcManager.getInstance().executeSparqlQuery(query, data);
+		Assert.assertEquals("Not having the right number of triples after insert",2,data.size());
+	}*/
+    
+    @Test
+	public void simpleDrop() throws ParseException {
+		SimpleMGraph data = new SimpleMGraph();
+		final String titleValue = "SPARQL Tutorial";
+		data.add(new TripleImpl(new UriRef("http://example.org/book/book1"),
+				DC.title, new PlainLiteralImpl(titleValue)));
+        
+		String query = "DROP ALL";
+        TcManager.getInstance().executeSparqlQuery(query, data);
+		Assert.assertEquals("GRAPH not empty after DROP ALL",0,data.size());
+	}
 }



Mime
View raw message