Skip to content

Commit

Permalink
scope the dataset appropriately when querying a model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Millar committed Dec 11, 2009
1 parent 84b5a9e commit dd2dfc1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .classpath
Expand Up @@ -15,7 +15,7 @@
<classpathentry kind="lib" path="lib/jena-2.6.0/arq.jar"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/icu4j_3_4.jar"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/iri.jar"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/jena.jar"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/jena.jar" sourcepath="/Jena 2.6.0/src"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/jenatest.jar"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/json.jar"/>
<classpathentry kind="lib" path="lib/jena-2.6.0/junit-4.5.jar"/>
Expand Down
14 changes: 14 additions & 0 deletions src/com/franz/agraph/jena/AGGraph.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
41 changes: 20 additions & 21 deletions src/com/franz/agraph/jena/AGGraphMaker.java
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -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

}

}
4 changes: 3 additions & 1 deletion src/com/franz/agraph/jena/AGNodeFactory.java
Expand Up @@ -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()));
Expand Down
1 change: 1 addition & 0 deletions src/com/franz/agraph/jena/AGQueryExecution.java
Expand Up @@ -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);
Expand Down
54 changes: 44 additions & 10 deletions src/tutorial/JenaTutorialExamples.java
Expand Up @@ -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";
Expand All @@ -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++) {
Expand All @@ -339,30 +339,64 @@ 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 {
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 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();
Expand All @@ -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) {
Expand All @@ -395,15 +429,15 @@ public static void example8() throws Exception {
}
output = (outputFile2 != null) ? new FileOutputStream(outputFile2)
: System.out;
// TODO: model_vcards.write(output);
model_vcards.write(output);
}

/**
* Writing the result of a statements match.
*/
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);
Expand Down

0 comments on commit dd2dfc1

Please sign in to comment.