package com.franz.ag.repository.examples;
import static com.franz.ag.repository.examples.AGRepositoryValueFactory.*;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.QueryLanguage;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
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;
public class AGRepositoryBooleanQuery {
/**
* Demonstrates issuing a SPARQL ASK as a BooleanQuery.
*
* @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("repositorybooleanquery", 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("ASK ");
queryBuilder.append("{ ?p foaf:name ?name }");
boolean exists = repoConn.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString()).evaluate();
System.out.println("Solution exists: " + exists);
// Close the RepositoryConnection and shutdown the Repository
// Close the store and disconnect from the server
repoConn.close();
repo.shutDown();
ts.closeTripleStore();
ags.disable();
}
}
Copyright © 2023 Franz Inc., All Rights Reserved | Privacy Statement |