# AGRepositoryTupleQuery.java

`package com.franz.ag.repository.examples;
import static com.franz.ag.repository.examples.AGRepositoryValueFactory.*;
import com.franz.ag.repository.AGRepository;
import com.franz.agbase.AllegroGraph;
import com.franz.agbase.AllegroGraphConnection;
import com.franz.agbase.AllegroGraphException;
import com.franz.agbase.examples.AGPaths;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
public class AGRepositoryTupleQuery {
/**
* Demonstrates issuing a SPARQL SELECT as a TupleQuery and showing results.
*
* @param args unused
* @throws AllegroGraphException
*/
public static void main(String[] args) throws Exception {
// Connect to the server, which must already be running.
AllegroGraphConnection ags = new AllegroGraphConnection();
try {
ags.enable();
} catch (Exception e) {
throw new AllegroGraphException("Server connection problem", e);
}
// Get a connection to a fresh repository
AllegroGraph ts = ags.renew("repositorytuplequery", AGPaths.TRIPLE_STORES);
Repository repo = new AGRepository(ts);
repo.initialize();
RepositoryConnection repoConn = repo.getConnection();
// Get the ValueFactory for the Repository and create some example values.
ValueFactory vf = repo.getValueFactory();
createExampleValues(vf);
repoConn.add(alice, name, nameAlice, context2);
repoConn.add(alice, mbox, mboxAlice, context2);
repoConn.add(context2, publisher, nameAlice);
repoConn.add(bob, name, nameBob, context1);
repoConn.add(bob, mbox, mboxBob, context1);
repoConn.add(context1, publisher, nameBob);
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append(" PREFIX foaf: <" + FOAF_NS + ">");
queryBuilder.append(" SELECT ?name ?mbox");
queryBuilder.append(" WHERE { ?x foaf:name ?name .");
queryBuilder.append(" ?x foaf:mbox ?mbox .}");
TupleQueryResult result = repoConn.prepareTupleQuery(
QueryLanguage.SPARQL, queryBuilder.toString()).evaluate();
try {
while (result.hasNext()) {
BindingSet solution = result.next();
Value nameResult = solution.getValue("name");
Value mboxResult = solution.getValue("mbox");
System.out.print("name = " + nameResult.stringValue());
System.out.println(", mbox = " + mboxResult.stringValue());
}
} finally {
result.close();
}
// Close the RepositoryConnection and shutdown the Repository
// Close the store and disconnect from the server
repoConn.close();
repo.shutDown();
ts.closeTripleStore();
ags.disable();
}
}
`

Up | Next