diff --git a/.classpath b/.classpath index 6e895c6a..95cc5e2d 100644 --- a/.classpath +++ b/.classpath @@ -15,7 +15,7 @@ - + diff --git a/src/com/franz/agraph/jena/AGGraph.java b/src/com/franz/agraph/jena/AGGraph.java index b254c14f..040c51cd 100644 --- a/src/com/franz/agraph/jena/AGGraph.java +++ b/src/com/franz/agraph/jena/AGGraph.java @@ -3,6 +3,9 @@ import org.openrdf.http.protocol.UnauthorizedException; import org.openrdf.model.Resource; import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.query.Dataset; +import org.openrdf.query.impl.DatasetImpl; import org.openrdf.repository.RepositoryException; import org.openrdf.repository.RepositoryResult; @@ -200,4 +203,15 @@ public String toString() { return "default-graph"; return graphNode.toString(); } + + public Dataset getDataset() { + DatasetImpl dataset = new DatasetImpl(); + if (context==null) { + dataset.addDefaultGraph(null); + } else if (context instanceof URI) { + dataset.addDefaultGraph((URI)context); + dataset.addNamedGraph((URI)context); + } + return dataset; + } } diff --git a/src/com/franz/agraph/jena/AGGraphMaker.java b/src/com/franz/agraph/jena/AGGraphMaker.java index 99a39cfe..dadeaad7 100644 --- a/src/com/franz/agraph/jena/AGGraphMaker.java +++ b/src/com/franz/agraph/jena/AGGraphMaker.java @@ -2,6 +2,7 @@ import com.franz.agraph.repository.AGRepositoryConnection; import com.hp.hpl.jena.graph.GraphMaker; +import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.shared.ReificationStyle; import com.hp.hpl.jena.util.iterator.ExtendedIterator; @@ -23,29 +24,28 @@ public void close() { } @Override - public AGGraph createGraph() { - // TODO Auto-generated method stub - return null; + public AGGraph getGraph() { + if (defaultGraph==null) { + defaultGraph = new AGGraph(this, null); + } + return defaultGraph; } @Override - public AGGraph createGraph(String arg0) { + public AGGraph createGraph() { // TODO Auto-generated method stub return null; } @Override - public AGGraph createGraph(String arg0, boolean arg1) { - // TODO Auto-generated method stub - return null; + public AGGraph createGraph(String uri) { + return createGraph(uri, false); } @Override - public AGGraph getGraph() { - if (defaultGraph==null) { - defaultGraph = new AGGraph(this, null); - } - return defaultGraph; + public AGGraph createGraph(String uri, boolean strict) { + // TODO: strictness + return new AGGraph(this, Node.createURI(uri)); } @Override @@ -55,7 +55,7 @@ public ReificationStyle getReificationStyle() { } @Override - public boolean hasGraph(String arg0) { + public boolean hasGraph(String name) { // TODO Auto-generated method stub return false; } @@ -73,21 +73,20 @@ public AGGraph openGraph() { } @Override - public AGGraph openGraph(String arg0) { - // TODO Auto-generated method stub - return null; + public AGGraph openGraph(String name) { + return openGraph(name, false); } @Override - public AGGraph openGraph(String arg0, boolean arg1) { - // TODO Auto-generated method stub - return null; + public AGGraph openGraph(String uri, boolean strict) { + // TODO deal with strictness + return new AGGraph(this, Node.createURI(uri)); } @Override - public void removeGraph(String arg0) { + public void removeGraph(String name) { // TODO Auto-generated method stub - + } } diff --git a/src/com/franz/agraph/jena/AGNodeFactory.java b/src/com/franz/agraph/jena/AGNodeFactory.java index 12b46c71..e841952f 100644 --- a/src/com/franz/agraph/jena/AGNodeFactory.java +++ b/src/com/franz/agraph/jena/AGNodeFactory.java @@ -21,7 +21,9 @@ public static Triple asTriple(Statement st) { public static Node asNode(Value v) { Node node = null; - if (v instanceof URI) { + if (v==null) { + node = Node.ANY; // TODO or Node.NULL or null? + } else if (v instanceof URI) { node = Node.createURI(v.stringValue()); } else if (v instanceof BNode) { node = Node.createAnon(new AnonId(v.stringValue())); diff --git a/src/com/franz/agraph/jena/AGQueryExecution.java b/src/com/franz/agraph/jena/AGQueryExecution.java index b44cc387..00d12721 100644 --- a/src/com/franz/agraph/jena/AGQueryExecution.java +++ b/src/com/franz/agraph/jena/AGQueryExecution.java @@ -88,6 +88,7 @@ public ResultSet execSelect() { AGTupleQuery tq = model.getGraph().getConnection().prepareTupleQuery(AGQueryLanguage.SPARQL, query.getQueryString()); TupleQueryResult result; try { + tq.setDataset(model.getGraph().getDataset()); result = tq.evaluate(); } catch (QueryEvaluationException e) { throw new RuntimeException(e); diff --git a/src/tutorial/JenaTutorialExamples.java b/src/tutorial/JenaTutorialExamples.java index 74b3d35d..21d798e1 100644 --- a/src/tutorial/JenaTutorialExamples.java +++ b/src/tutorial/JenaTutorialExamples.java @@ -305,7 +305,7 @@ public static void example5() throws Exception { public static AGGraphMaker example6() throws Exception { AGGraphMaker maker = example1(false); AGModel model = new AGModel(maker.getGraph()); - AGModel model_vcards = model;//TODO:new AGModel(maker.createGraph("http://example.org#vcards")); + AGModel model_vcards = new AGModel(maker.createGraph("http://example.org#vcards")); String path1 = "src/tutorial/java-vcards.rdf"; String path2 = "src/tutorial/java-kennedy.ntriples"; String baseURI = "http://example.org/example/local"; @@ -324,7 +324,7 @@ public static AGGraphMaker example6() throws Exception { public static void example7() throws Exception { AGGraphMaker maker = example6(); AGModel model = new AGModel(maker.getGraph()); - AGModel model_vcards = model; //TODO: new AGModel(maker.openGraph("http://example.org#vcards")); + AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards")); println("\nMatch all and print subjects and graph (model)"); StmtIterator statements = model.listStatements(); for (int i = 0; i < 25 && statements.hasNext(); i++) { @@ -339,8 +339,8 @@ public static void example7() throws Exception { } statements.close(); - println("\nSame thing with SPARQL query (model)."); - String queryString = "SELECT DISTINCT ?s ?g WHERE {graph ?g {?s ?p ?o .} } LIMIT 25"; + println("\nSPARQL query over the default graph (model)."); + String queryString = "SELECT DISTINCT ?s ?p ?o WHERE {?s ?p ?o . } LIMIT 25"; AGQuery query = AGQueryFactory.create(queryString); QueryExecution qe = AGQueryExecutionFactory.create(query, model); try { @@ -348,21 +348,55 @@ public static void example7() throws Exception { while (results.hasNext()) { QuerySolution result = results.next(); RDFNode s = result.get("s"); + RDFNode p = result.get("p"); + RDFNode o = result.get("o"); + println(" " + s + " " + p + " " + o); + } + } finally { + qe.close(); + } + println("\nSPARQL query over the default graph (model_vcards)."); + qe = AGQueryExecutionFactory.create(query, model_vcards); + try { + ResultSet results = qe.execSelect(); + while (results.hasNext()) { + QuerySolution result = results.next(); + RDFNode s = result.get("s"); + RDFNode p = result.get("p"); + RDFNode o = result.get("o"); + println(" " + s + " " + p + " " + o); + } + } finally { + qe.close(); + } + println("\nSPARQL query for triples in any named graph (model)."); + queryString = "SELECT DISTINCT ?s ?p ?o ?g WHERE {graph ?g {?s ?p ?o .} } LIMIT 25"; + query = AGQueryFactory.create(queryString); + qe = AGQueryExecutionFactory.create(query, model); + try { + ResultSet results = qe.execSelect(); + while (results.hasNext()) { + QuerySolution result = results.next(); + RDFNode s = result.get("s"); + RDFNode p = result.get("p"); + RDFNode o = result.get("o"); RDFNode g = result.get("g"); - println(" " + s + " " + g); + println(" " + s + " " + p + " " + o + " " + g); } } finally { qe.close(); } - println("\nSame thing with SPARQL query (model_vcards)."); + println("\nSPARQL query for triples in any named graph (model_vcards)."); qe = AGQueryExecutionFactory.create(query, model_vcards); try { ResultSet results = qe.execSelect(); while (results.hasNext()) { QuerySolution result = results.next(); RDFNode s = result.get("s"); + RDFNode p = result.get("p"); + RDFNode o = result.get("o"); RDFNode g = result.get("g"); - println(" " + s + " " + g); + println(" " + s + " " + p + " " + o + " " + g); } } finally { qe.close(); @@ -375,7 +409,7 @@ public static void example7() throws Exception { public static void example8() throws Exception { AGGraphMaker maker = example6(); AGModel model = new AGModel(maker.getGraph()); - // TODO:AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards")); + AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards")); String outputFile = TEMPORARY_DIRECTORY + "temp.nt"; // outputFile = null; if (outputFile == null) { @@ -395,7 +429,7 @@ public static void example8() throws Exception { } output = (outputFile2 != null) ? new FileOutputStream(outputFile2) : System.out; - // TODO: model_vcards.write(output); + model_vcards.write(output); } /** @@ -403,7 +437,7 @@ public static void example8() throws Exception { */ public static void example9() throws Exception { AGGraphMaker maker = example6(); - AGModel model_vcards = new AGModel(maker.getGraph());//TODO:new AGModel(maker.openGraph("http://example.org#vcards")); + AGModel model_vcards = new AGModel(maker.openGraph("http://example.org#vcards")); StmtIterator statements = model_vcards.listStatements(null,RDF.type, (RDFNode)null); Model m = ModelFactory.createDefaultModel(); m.add(statements);