Search in sources :

Example 46 with TupleQuery

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);
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) InputStream(java.io.InputStream) Rio(org.openrdf.rio.Rio) BufferedReader(java.io.BufferedReader) TupleQuery(org.openrdf.query.TupleQuery) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet)

Example 47 with TupleQuery

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());
}
Also used : CountingResultHandler(org.apache.rya.indexing.external.PrecompJoinOptimizerIT.CountingResultHandler) TupleQuery(org.openrdf.query.TupleQuery) Test(org.junit.Test)

Example 48 with TupleQuery

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);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery) Update(org.openrdf.query.Update)

Example 49 with TupleQuery

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);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery) Update(org.openrdf.query.Update)

Example 50 with TupleQuery

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());
}
Also used : TupleQuery(org.openrdf.query.TupleQuery) Update(org.openrdf.query.Update)

Aggregations

TupleQuery (org.openrdf.query.TupleQuery)86 Update (org.openrdf.query.Update)33 RepositoryConnection (org.openrdf.repository.RepositoryConnection)27 URI (org.openrdf.model.URI)13 BindingSet (org.openrdf.query.BindingSet)12 MalformedQueryException (org.openrdf.query.MalformedQueryException)11 TupleQueryResult (org.openrdf.query.TupleQueryResult)11 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)10 RepositoryException (org.openrdf.repository.RepositoryException)10 SailRepository (org.openrdf.repository.sail.SailRepository)10 HashSet (java.util.HashSet)8 Literal (org.openrdf.model.Literal)8 TupleQueryResultHandlerException (org.openrdf.query.TupleQueryResultHandlerException)8 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)8 Sail (org.openrdf.sail.Sail)7 StatementImpl (org.openrdf.model.impl.StatementImpl)6 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)5 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)5 Test (org.junit.Test)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3