use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class MongoSpinIT method executeQuery.
Set<BindingSet> executeQuery(URL queryFile) throws Exception {
SailRepositoryConnection conn = repository.getConnection();
try {
InputStream queryIS = queryFile.openStream();
BufferedReader br = new BufferedReader(new java.io.InputStreamReader(queryIS, "UTF-8"));
String query = br.lines().collect(Collectors.joining("\n"));
br.close();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
TupleQueryResult result = tupleQuery.evaluate();
Set<BindingSet> solutions = new HashSet<>();
while (result.hasNext()) {
solutions.add(result.next());
}
return solutions;
} finally {
closeQuietly(conn);
}
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class PCJOptionalTestIT method testEvaluateSingeIndex.
@Test
public void testEvaluateSingeIndex() throws TupleQueryResultHandlerException, QueryEvaluationException, MalformedQueryException, RepositoryException, AccumuloException, AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException, PcjException, InferenceEngineException, NumberFormatException, UnknownHostException {
final String indexSparqlString = //
"" + //
"SELECT ?e ?l ?o" + //
"{" + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + //
" OPTIONAL{?e <uri:talksTo> ?o } . " + //
"}";
PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix + "INDEX_1", indexSparqlString, new String[] { "e", "l", "o" }, Optional.<PcjVarOrderFactory>absent());
final String queryString = //
"" + //
"SELECT ?e ?c ?l ?o " + //
"{" + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + //
" OPTIONAL {?e <uri:talksTo> ?o } . " + //
" ?e a ?c . " + //
"}";
final CountingResultHandler crh = new CountingResultHandler();
PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
repo = PcjIntegrationTestingUtil.getAccumuloPcjRepo(tablePrefix, "instance");
conn = repo.getConnection();
conn.add(sub, RDF.TYPE, subclass);
conn.add(sub2, RDF.TYPE, subclass2);
conn.add(sub3, RDF.TYPE, subclass3);
final TupleQuery tupQuery = pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupQuery.evaluate(crh);
Assert.assertEquals(3, crh.getCount());
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class EntityDirectExample method testAddAndDelete.
public static void testAddAndDelete(final SailRepositoryConnection conn) throws MalformedQueryException, RepositoryException, UpdateExecutionException, QueryEvaluationException, TupleQueryResultHandlerException, AccumuloException, AccumuloSecurityException, TableNotFoundException {
// Add data
String query = //
"INSERT DATA\n" + //
"{ GRAPH <http://updated/test> {\n" + //
" <http://acme.com/people/Mike> " + //
" <http://acme.com/actions/likes> \"A new book\" ;\n" + " <http://acme.com/actions/likes> \"Avocados\" .\n" + "} }";
log.info("Performing Query");
Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = //
"select ?x {GRAPH <http://updated/test> {?x <http://acme.com/actions/likes> \"A new book\" . " + " ?x <http://acme.com/actions/likes> \"Avocados\" }}";
final CountingResultHandler resultHandler = new CountingResultHandler();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 1);
resultHandler.resetCount();
// Delete Data
query = //
"DELETE DATA\n" + "{ GRAPH <http://updated/test> {\n" + " <http://acme.com/people/Mike> <http://acme.com/actions/likes> \"A new book\" ;\n" + " <http://acme.com/actions/likes> \"Avocados\" .\n" + "}}";
update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = //
"select ?x {GRAPH <http://updated/test> {?x <http://acme.com/actions/likes> \"A new book\" . " + " ?x <http://acme.com/actions/likes> \"Avocados\" }}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 0);
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class MongoRyaDirectExample method testAddAndDelete.
public static void testAddAndDelete(final SailRepositoryConnection conn) throws MalformedQueryException, RepositoryException, UpdateExecutionException, QueryEvaluationException, TupleQueryResultHandlerException {
// Add data
String query = //
"INSERT DATA\n" + //
"{ GRAPH <http://updated/test> {\n" + //
" <http://acme.com/people/Mike> " + //
" <http://acme.com/actions/likes> \"A new book\" ;\n" + " <http://acme.com/actions/likes> \"Avocados\" .\n" + "} }";
log.info("Performing Query");
Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
final CountingResultHandler resultHandler = new CountingResultHandler();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 2);
resultHandler.resetCount();
// Delete Data
query = //
"DELETE DATA\n" + "{ GRAPH <http://updated/test> {\n" + " <http://acme.com/people/Mike> <http://acme.com/actions/likes> \"A new book\" ;\n" + " <http://acme.com/actions/likes> \"Avocados\" .\n" + "}}";
update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 0);
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class MongoRyaDirectExample method testPropertyChainInference.
public static void testPropertyChainInference(final SailRepositoryConnection conn, final Sail sail) throws MalformedQueryException, RepositoryException, UpdateExecutionException, QueryEvaluationException, TupleQueryResultHandlerException, InferenceEngineException {
// Add data
String query = //
"INSERT DATA\n" + //
"{ GRAPH <http://updated/test> {\n" + " <urn:paulGreatGrandfather> <urn:father> <urn:paulGrandfather> . " + " <urn:paulGrandfather> <urn:father> <urn:paulFather> . " + " <urn:paulFather> <urn:father> <urn:paul> . " + " <urn:paul> <urn:father> <urn:paulSon> . }}";
log.info("Performing Query");
Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = "select ?p { GRAPH <http://updated/test> {<urn:paulGreatGrandfather> <urn:father>/<urn:father> ?p}}";
CountingResultHandler resultHandler = new CountingResultHandler();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
// try adding a property chain and querying for it
query = //
"INSERT DATA\n" + //
"{ GRAPH <http://updated/test> {\n" + " <urn:greatGrandfather> owl:propertyChainAxiom <urn:1234> . " + " <urn:1234> <http://www.w3.org/2000/10/swap/list#length> 3 . " + " <urn:1234> <http://www.w3.org/2000/10/swap/list#index> (0 <urn:father>) . " + " <urn:1234> <http://www.w3.org/2000/10/swap/list#index> (1 <urn:father>) . " + " <urn:1234> <http://www.w3.org/2000/10/swap/list#index> (2 <urn:father>) . }}";
update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = //
"INSERT DATA\n" + //
"{ GRAPH <http://updated/test> {\n" + " <urn:grandfather> owl:propertyChainAxiom <urn:12344> . " + " <urn:12344> <http://www.w3.org/2000/10/swap/list#length> 2 . " + " <urn:12344> <http://www.w3.org/2000/10/swap/list#index> (0 <urn:father>) . " + " <urn:12344> <http://www.w3.org/2000/10/swap/list#index> (1 <urn:father>) . }}";
update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
((RdfCloudTripleStore) sail).getInferenceEngine().refreshGraph();
resultHandler.resetCount();
query = "select ?p { GRAPH <http://updated/test> {<urn:paulGreatGrandfather> <urn:greatGrandfather> ?p}}";
resultHandler = new CountingResultHandler();
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
resultHandler.resetCount();
query = "select ?s ?p { GRAPH <http://updated/test> {?s <urn:grandfather> ?p}}";
resultHandler = new CountingResultHandler();
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
}
Aggregations