Search in sources :

Example 41 with TupleQuery

use of org.openrdf.query.TupleQuery in project incubator-rya by apache.

the class CbSailProducer method performSelect.

protected Object performSelect(final String query, final String auth, final Boolean infer) throws RepositoryException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException {
    final TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
    if (auth != null && auth.length() > 0) {
        tupleQuery.setBinding(CONF_QUERY_AUTH, valueFactory.createLiteral(auth));
    }
    if (infer != null) {
        tupleQuery.setBinding(CONF_INFER, valueFactory.createLiteral(infer));
    }
    if (CbSailEndpoint.CbSailOutput.BINARY.equals(queryOutput)) {
        final List listOutput = new ArrayList();
        final TupleQueryResultHandlerBase handler = new TupleQueryResultHandlerBase() {

            @Override
            public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
                final Map<String, String> map = new HashMap<String, String>();
                for (final String s : bindingSet.getBindingNames()) {
                    map.put(s, bindingSet.getBinding(s).getValue().stringValue());
                }
                listOutput.add(map);
            }
        };
        tupleQuery.evaluate(handler);
        return listOutput;
    } else if (CbSailEndpoint.CbSailOutput.XML.equals(queryOutput)) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(baos);
        tupleQuery.evaluate(sparqlWriter);
        return new String(baos.toByteArray(), StandardCharsets.UTF_8);
    } else {
        throw new IllegalArgumentException("Query Output[" + queryOutput + "] is not recognized");
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) TupleQueryResultHandlerBase(org.openrdf.query.TupleQueryResultHandlerBase) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TupleQuery(org.openrdf.query.TupleQuery) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 42 with TupleQuery

use of org.openrdf.query.TupleQuery in project incubator-rya by apache.

the class TriplestoreProvenanceCollectorTest method testCollect.

@Test
public void testCollect() throws ProvenanceCollectionException, RepositoryException, MalformedQueryException, QueryEvaluationException {
    Sail ms = new MemoryStore();
    SailRepository repo = new SailRepository(ms);
    repo.initialize();
    TriplestoreProvenanceCollector coll = new TriplestoreProvenanceCollector(repo, "fakeUser", "SPARQL");
    coll.recordQuery("fakeQuery");
    String queryString = "SELECT ?x ?y WHERE { ?x ?p ?y } ";
    TupleQuery tupleQuery = repo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    TupleQueryResult result = tupleQuery.evaluate();
    // TODO not asserting on the results.
    assertTrue(result.hasNext());
}
Also used : MemoryStore(org.openrdf.sail.memory.MemoryStore) SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) TupleQuery(org.openrdf.query.TupleQuery) TupleQueryResult(org.openrdf.query.TupleQueryResult) Test(org.junit.Test)

Example 43 with TupleQuery

use of org.openrdf.query.TupleQuery in project incubator-rya by apache.

the class RyaGeoDirectExample method testTemporalFreeGeoSearch.

private static void testTemporalFreeGeoSearch(final SailRepositoryConnection conn) throws MalformedQueryException, RepositoryException, UpdateExecutionException, TupleQueryResultHandlerException, QueryEvaluationException {
    String queryString;
    TupleQuery tupleQuery;
    CountingResultHandler tupleHandler;
    // ring containing point
    queryString = // 
    "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  " + // 
    "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  " + // 
    "PREFIX time: <http://www.w3.org/2006/time#> " + // 
    "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> " + // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "SELECT ?feature ?point ?wkt ?event ?time ?person ?match" + // 
    "{" + // 
    "  ?event a  time:Instant . \n" + // 
    "  ?event time:inXSDDateTime ?time . \n" + // after 3  seconds
    "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n" + // 
    "  ?feature a geo:Feature . " + // 
    "  ?feature geo:hasGeometry ?point . " + // 
    "  ?point a geo:Point . " + // 
    "  ?point geo:asWKT ?wkt . " + // 
    "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)). " + // 
    "  ?person a <http://example.org/ontology/Person> . " + // 
    "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + // 
    "  FILTER(fts:text(?match, \"pal*\")) " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    // TODO ==5  some data is missing for this query!
    Validate.isTrue(tupleHandler.getCount() == 0);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery)

Example 44 with TupleQuery

use of org.openrdf.query.TupleQuery in project incubator-rya by apache.

the class RyaGeoDirectExample method testDeleteGeoData.

private static void testDeleteGeoData(final SailRepositoryConnection conn) throws Exception {
    // Delete all stored points
    final String sparqlDelete = // 
    "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  " + // 
    "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  " + // 
    "DELETE {\n" + // 
    "  ?feature a geo:Feature . " + // 
    "  ?feature geo:hasGeometry ?point . " + // 
    "  ?point a geo:Point . " + // 
    "  ?point geo:asWKT ?wkt . " + "}\n" + "WHERE { \n" + // 
    "  ?feature a geo:Feature . " + // 
    "  ?feature geo:hasGeometry ?point . " + // 
    "  ?point a geo:Point . " + // 
    "  ?point geo:asWKT ?wkt . " + // 
    "}";
    final Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlDelete);
    deleteUpdate.execute();
    String queryString;
    TupleQuery tupleQuery;
    CountingResultHandler tupleHandler;
    // Find all stored points
    queryString = // 
    "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  " + // 
    "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  " + // 
    "SELECT ?feature ?point ?wkt " + // 
    "{" + // 
    "  ?feature a geo:Feature . " + // 
    "  ?feature geo:hasGeometry ?point . " + // 
    "  ?point a geo:Point . " + // 
    "  ?point geo:asWKT ?wkt . " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    Validate.isTrue(tupleHandler.getCount() == 0);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery) Update(org.openrdf.query.Update)

Example 45 with TupleQuery

use of org.openrdf.query.TupleQuery in project incubator-rya by apache.

the class RyaGeoDirectExample method testGeoFreetextWithPCJSearch.

private static void testGeoFreetextWithPCJSearch(final SailRepositoryConnection conn) throws MalformedQueryException, RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException {
    // ring outside point
    final String queryString = // 
    "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  " + // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  " + // 
    "SELECT ?feature ?point ?wkt ?e ?c ?l ?o ?person ?match " + // 
    "{" + // 
    "  ?person a <http://example.org/ontology/Person> . " + // 
    "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + // 
    "  FILTER(fts:text(?match, \"!alice & hose\")) " + // 
    "  ?e a ?c . " + // 
    "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
    "  ?e <uri:talksTo> ?o . " + // 
    "  ?feature a geo:Feature . " + // 
    "  ?feature geo:hasGeometry ?point . " + // 
    "  ?point a geo:Point . " + // 
    "  ?point geo:asWKT ?wkt . " + // 
    "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " + // 
    "}";
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    final CountingResultHandler tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    // TODO ==1  some data is missing for this query!
    Validate.isTrue(tupleHandler.getCount() == 0);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery)

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