Skip to content

Commit

Permalink
Jena Prolog query support with examples, and a Jena transaction example.
Browse files Browse the repository at this point in the history
Added support for Prolog select queries via Jena, including two examples
(17 and 18) in AGMoreJenaExamples.java for possible inclusion in the
Jena tutorial.  Also added a Jena transaction example22 for possible
inclusion in the Jena tutorial.

Tests added for:  no
make test-suite run? no
make prepush run? yes, in agraph-java-client

<release-note>
rfe9359: Jena Prolog support.

Added support for Prolog select queries over a Jena model.  Queries
must be written to select for variables that are bound to RDF parts,
rather than to arbitrary Lisp objects.
</release-note>

Signed-off-by: Ahmon Dancy <dancy@franz.com>
  • Loading branch information
Bill Millar authored and dancyatfranz committed Feb 25, 2010
1 parent 0d345d6 commit 8b4ccf9
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/com/franz/agraph/http/AGHttpRepoClient.java
Expand Up @@ -642,7 +642,7 @@ protected List<NameValuePair> getQueryMethodParameters(QueryLanguage ql,
planner));
}

if (dataset != null) {
if (ql==QueryLanguage.SPARQL && dataset != null) {
for (URI defaultGraphURI : dataset.getDefaultGraphs()) {
String param = Protocol.NULL_PARAM_VALUE;
if (defaultGraphURI == null) {
Expand All @@ -661,6 +661,7 @@ protected List<NameValuePair> getQueryMethodParameters(QueryLanguage ql,
}
} // TODO: no else clause here assumes AG's default dataset matches
// Sesame's, confirm this.
// TODO: deal with prolog queries scoped to a graph for Jena

for (int i = 0; i < bindings.length; i++) {
String paramName = Protocol.BINDING_PREFIX + bindings[i].getName();
Expand Down
1 change: 0 additions & 1 deletion src/com/franz/agraph/jena/AGBulkUpdateHandler.java
Expand Up @@ -17,7 +17,6 @@

import com.franz.agraph.repository.AGValueFactory;
import com.hp.hpl.jena.graph.BulkUpdateHandler;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.graph.impl.SimpleBulkUpdateHandler;

Expand Down
17 changes: 16 additions & 1 deletion src/com/franz/agraph/jena/AGQuery.java
Expand Up @@ -8,16 +8,31 @@

package com.franz.agraph.jena;

import org.openrdf.query.QueryLanguage;

import com.franz.agraph.repository.AGQueryLanguage;

public class AGQuery {

private final QueryLanguage language;
private final String queryString;

AGQuery(String queryString) {
this.language = AGQueryLanguage.SPARQL;
this.queryString = queryString;
}

public AGQuery(QueryLanguage language, String queryString) {
this.language = language;
this.queryString = queryString;
}

public QueryLanguage getLanguage() {
return language;
}

public String getQueryString() {
return queryString;
}

}
14 changes: 10 additions & 4 deletions src/com/franz/agraph/jena/AGQueryExecution.java
Expand Up @@ -10,11 +10,11 @@

import org.openrdf.query.GraphQueryResult;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQueryResult;

import com.franz.agraph.repository.AGBooleanQuery;
import com.franz.agraph.repository.AGGraphQuery;
import com.franz.agraph.repository.AGQueryLanguage;
import com.franz.agraph.repository.AGTupleQuery;
import com.franz.util.Closeable;
import com.hp.hpl.jena.query.Dataset;
Expand Down Expand Up @@ -48,7 +48,10 @@ public void close() {

@Override
public boolean execAsk() {
AGBooleanQuery bq = model.getGraph().getConnection().prepareBooleanQuery(AGQueryLanguage.SPARQL, query.getQueryString());
if (query.getLanguage()!=QueryLanguage.SPARQL) {
throw new UnsupportedOperationException(query.getLanguage().getName() + " language does not support ASK queries.");
}
AGBooleanQuery bq = model.getGraph().getConnection().prepareBooleanQuery(query.getLanguage(), query.getQueryString());
boolean result;
try {
bq.setDataset(model.getGraph().getDataset());
Expand All @@ -66,7 +69,10 @@ public Model execConstruct() {

@Override
public Model execConstruct(Model m) {
AGGraphQuery gq = model.getGraph().getConnection().prepareGraphQuery(AGQueryLanguage.SPARQL, query.getQueryString());
if (query.getLanguage()!=QueryLanguage.SPARQL) {
throw new UnsupportedOperationException(query.getLanguage().getName() + " language does not support CONSTRUCT queries.");
}
AGGraphQuery gq = model.getGraph().getConnection().prepareGraphQuery(query.getLanguage(), query.getQueryString());
GraphQueryResult result;
try {
gq.setDataset(model.getGraph().getDataset());
Expand Down Expand Up @@ -100,7 +106,7 @@ public Model execDescribe(Model m) {

@Override
public ResultSet execSelect() {
AGTupleQuery tq = model.getGraph().getConnection().prepareTupleQuery(AGQueryLanguage.SPARQL, query.getQueryString());
AGTupleQuery tq = model.getGraph().getConnection().prepareTupleQuery(query.getLanguage(), query.getQueryString());
TupleQueryResult result;
try {
tq.setDataset(model.getGraph().getDataset());
Expand Down
12 changes: 10 additions & 2 deletions src/com/franz/agraph/jena/AGQueryFactory.java
Expand Up @@ -8,11 +8,19 @@

package com.franz.agraph.jena;

import org.openrdf.query.QueryLanguage;

import com.franz.agraph.repository.AGQueryLanguage;

public class AGQueryFactory {

public static AGQuery create(String queryString) {
AGQuery query = new AGQuery(queryString);
return query ;
return create(AGQueryLanguage.SPARQL, queryString);
}

public static AGQuery create(QueryLanguage language, String queryString) {
AGQuery query = new AGQuery(language, queryString);
return query ;
}

}
190 changes: 190 additions & 0 deletions src/test/AGMoreJenaExamples.java
@@ -0,0 +1,190 @@
package test;

import java.io.FileInputStream;

import tutorial.JenaTutorialExamples;

import com.franz.agraph.jena.AGGraph;
import com.franz.agraph.jena.AGGraphMaker;
import com.franz.agraph.jena.AGModel;
import com.franz.agraph.jena.AGQuery;
import com.franz.agraph.jena.AGQueryExecutionFactory;
import com.franz.agraph.jena.AGQueryFactory;
import com.franz.agraph.repository.AGQueryLanguage;
import com.franz.agraph.repository.AGRepositoryConnection;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;

public class AGMoreJenaExamples extends JenaTutorialExamples {

public static void exampleBulkUpdate() throws Exception {
AGGraphMaker maker = example6();
AGModel model = new AGModel(maker.getGraph());
AGModel model2 = new AGModel(maker.createGraph("http://example.org/foo"));
StmtIterator statements = model.listStatements(null,RDF.type, (RDFNode)null);
model2.add(statements);
System.out.println("Size: "+model2.size());
model2.write(System.out);
statements = model.listStatements(null,RDF.type, (RDFNode)null);
model2.remove(statements);
System.out.println("Size: "+model2.size());
model2.write(System.out);
}

/**
* Prolog queries
*/
public static void example17() throws Exception {
AGGraphMaker maker = example6();
AGGraph graph = maker.getGraph();
AGModel model = new AGModel(graph);
try {
model.setNsPrefix("kdy", "http://www.franz.com/simple#");
String rules1 =
"(<-- (woman ?person) ;; IF\n" +
" (q ?person !kdy:sex !kdy:female)\n" +
" (q ?person !rdf:type !kdy:person))\n" +
"(<-- (man ?person) ;; IF\n" +
" (q ?person !kdy:sex !kdy:male)\n" +
" (q ?person !rdf:type !kdy:person))";
maker.getRepositoryConnection().addRules(rules1);
println("\nFirst and Last names of all \"men.\"");
String queryString =
"(select (?first ?last)\n" +
" (man ?person)\n" +
" (q ?person !kdy:first-name ?first)\n" +
" (q ?person !kdy:last-name ?last))";
AGQuery prolog = AGQueryFactory.create(AGQueryLanguage.PROLOG,queryString);
QueryExecution qe = AGQueryExecutionFactory.create(prolog, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode f = result.get("first");
RDFNode l = result.get("last");
System.out.println(f + " " + l);
}
} finally {
qe.close();
}
} finally {
model.close();
}
}

/**
* Loading Prolog rules
*/
public static void example18() throws Exception {
AGGraphMaker maker = example6();
AGGraph graph = maker.createGraph("http://example18.org");
AGModel model = new AGModel(graph);
try {
model.setNsPrefix("kdy", "http://www.franz.com/simple#");
model.setNsPrefix("rltv", "http://www.franz.com/simple#");
String path = "src/tutorial/java-rules.txt";
maker.getRepositoryConnection().addRules(new FileInputStream(path));
String queryString =
"(select (?person ?uncle) " +
"(uncle ?y ?x)" +
"(name ?x ?person)" +
"(name ?y ?uncle))";
AGQuery prolog = AGQueryFactory.create(AGQueryLanguage.PROLOG,queryString);
QueryExecution qe = AGQueryExecutionFactory.create(prolog, model);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
RDFNode p = result.get("person");
RDFNode u = result.get("uncle");
println(u + " is the uncle of " + p);
}
} finally {
qe.close();
}
} finally {
model.close();
}
}

/**
* Transactions
*/
public static void example22() throws Exception {
AGGraphMaker maker1 = example1(false);
// Get another graph maker for the repository on a another connection
AGRepositoryConnection conn2 = maker1.getRepositoryConnection().getRepository().getConnection();
closeBeforeExit(conn2);
AGGraphMaker maker2 = new AGGraphMaker(conn2);
AGModel model1 = new AGModel(maker1.getGraph());
AGModel model2 = new AGModel(maker2.getGraph());
model2.begin();
String baseURI = "http://example.org/example/local";
model1.read(new FileInputStream("src/tutorial/java-lesmis.rdf"), baseURI);
println("Loaded " + model1.size() + " java-lesmis.rdf triples via conn1.");
model2.read(new FileInputStream("src/tutorial/java-kennedy.ntriples"), baseURI, "N-TRIPLE");
println("Loaded " + model2.size() + " java-kennedy.ntriples via conn2.");

println("\nSince model1 is not in a transaction, java-lesmis.rdf triples are committed " +
"and retrievable via model2. Since model2 is in a transaction, and " +
"no commit() has yet been issued on model2, kennedy.rdf triples are not " +
" retrievable via model1.");
// Check transaction isolation semantics:
Literal valjean = model1.createLiteral("Valjean");
Literal kennedy = model1.createLiteral("Kennedy");
printRows("\nUsing listStatements() on model1; should find Valjean:",
1, model1.listStatements(null, null, valjean));
printRows("\nUsing listStatements() on model1; should not find Kennedy:",
1, model1.listStatements(null, null, kennedy));
printRows("\nUsing listStatements() on model2; should not find Valjean (until a rollback or commit occurs on model2):",
1, model2.listStatements(null, null, valjean));
printRows("\nUsing listStatements() on model2; should find Kennedy:",
1, model2.listStatements(null, null, kennedy));

// Rollback
println("\nRolling back contents of model2.");
model2.abort();
println("There are now " + model2.size() + " triples visible via model2.");
printRows("\nUsing listStatements() on model1; should find Valjean:",
1, model1.listStatements(null, null, valjean));
printRows("\nUsing listStatements() on model1; should not find Kennedys:",
1, model1.listStatements(null, null, kennedy));
printRows("\nUsing listStatements() on model2; should not find Kennedys:",
1, model2.listStatements(null, null, kennedy));
printRows("\nUsing listStatements() on model2; should find Valjean:",
1, model2.listStatements(null, null, valjean));
// Reload and Commit
println("\nReload java-kennedy.ntriples into model2.");
model2.begin();
model2.read(new FileInputStream("src/tutorial/java-kennedy.ntriples"), baseURI, "N-TRIPLE");
println("There are now " + model1.size() + " triples visible on model1.");
println("There are now " + model2.size() + " triples visible on model2.");
println("\nCommitting contents of model2.");
model2.commit();
println("There are now " + model1.size() + " triples visible on model1.");
println("There are now " + model2.size() + " triples visible on model2.");
printRows("\nUsing listStatements() on model1; should find Valjean:",
1, model1.listStatements(null, null, valjean));
printRows("\nUsing listStatements() on model1; should find Kennedys:",
1, model1.listStatements(null, null, kennedy));
printRows("\nUsing listStatements() on model2; should find Kennedys:",
1, model2.listStatements(null, null, kennedy));
printRows("\nUsing listStatements() on model2; should find Valjean:",
1, model2.listStatements(null, null, valjean));
maker1.close();
maker2.close();
}

public static void main(String[] args) throws Exception {
example17();
example18();
example22();
exampleBulkUpdate();
closeAll();
}
}
3 changes: 2 additions & 1 deletion src/tutorial/JenaTutorialExamples.java
Expand Up @@ -67,6 +67,7 @@ public static AGGraphMaker example1(boolean close)
println("Available repositories in catalog "
+ (catalog.getCatalogName()) + ": "
+ catalog.listRepositories());
closeAll();
catalog.deleteRepository(REPOSITORY_ID);
AGRepository myRepository = catalog.createRepository(REPOSITORY_ID);
println("Got a repository.");
Expand Down Expand Up @@ -607,7 +608,7 @@ public static void example19() throws Exception {
// We'll create two new rdf:type classes. Note that classes are
// capitalized.
Resource parent = model.createResource("http://example.org/ontology/Parent");
Resource child = model.createResource("http://exmaple.org/ontology/Child");
Resource child = model.createResource("http://example.org/ontology/Child");
// The following triples say that a fatherOf link points from a parent
// to a child.
model.add(fatherOf, RDFS.domain, parent);
Expand Down

0 comments on commit 8b4ccf9

Please sign in to comment.