Search in sources :

Example 16 with TupleQuery

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

the class RyaDirectExample method testAddAndFreeTextSearchWithPCJ.

private static void testAddAndFreeTextSearchWithPCJ(final SailRepositoryConnection conn) throws Exception {
    // add data to the repository using the SailRepository add methods
    final ValueFactory f = conn.getValueFactory();
    final URI person = f.createURI("http://example.org/ontology/Person");
    String uuid;
    uuid = "urn:people:alice";
    conn.add(f.createURI(uuid), RDF.TYPE, person);
    conn.add(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
    uuid = "urn:people:bobss";
    conn.add(f.createURI(uuid), RDF.TYPE, person);
    conn.add(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Bob Snob Hose", "en"));
    String queryString;
    TupleQuery tupleQuery;
    CountingResultHandler tupleHandler;
    // ///////////// search for alice
    queryString = // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "SELECT ?person ?match ?e ?c ?l ?o " + // 
    "{" + // 
    "  ?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());
    Validate.isTrue(tupleHandler.getCount() == 1);
    // ///////////// search for alice and bob
    queryString = // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "SELECT ?person ?match " + // 
    "{" + // 
    "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + // 
    "  ?person a <http://example.org/ontology/Person> . " + // 
    "  FILTER(fts:text(?match, \"(alice | bob) *SE\")) " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    Validate.isTrue(tupleHandler.getCount() == 2);
    // ///////////// search for alice and bob
    queryString = // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "SELECT ?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 | bob) *SE\")) " + // 
    "  FILTER(fts:text(?match, \"pal*\")) " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    Validate.isTrue(tupleHandler.getCount() == 1);
    // ///////////// search for bob
    queryString = // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "SELECT ?person ?match ?e ?c ?l ?o " + // 
    "{" + // 
    "  ?e a ?c . " + // 
    "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
    "  ?e <uri:talksTo> ?o . " + // 
    "  ?person a <http://example.org/ontology/Person> . " + // 
    "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + // 
    "  FILTER(fts:text(?match, \"!alice & hose\")) " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    Validate.isTrue(tupleHandler.getCount() == 1);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI)

Example 17 with TupleQuery

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

the class RyaDirectExample method testDeleteTemporalData.

// private static void testAddPointAndWithinSearchWithPCJ(
// final SailRepositoryConnection conn) throws Exception {
// 
// final String update = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "INSERT DATA { " //
// + "  <urn:feature> a geo:Feature ; " //
// + "    geo:hasGeometry [ " //
// + "      a geo:Point ; " //
// + "      geo:asWKT \"Point(-77.03524 38.889468)\"^^geo:wktLiteral "//
// + "    ] . " //
// + "}";
// 
// final Update u = conn.prepareUpdate(QueryLanguage.SPARQL, update);
// u.execute();
// 
// String queryString;
// TupleQuery tupleQuery;
// CountingResultHandler tupleHandler;
// 
// // point outside search ring
// 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 . "//
// + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " //
// + "}";//
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() == 0);
// 
// // point inside search ring
// queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
// + "SELECT ?feature ?point ?wkt ?e ?l ?o" //
// + "{" //
// + "  ?feature a ?e . "//
// + "  ?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)) " //
// + "}";//
// 
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() == 1);
// 
// // point inside search ring with Pre-Computed Join
// queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
// + "SELECT ?feature ?point ?wkt ?e ?l ?o" //
// + "{" //
// + "  ?feature a ?e . "//
// + "  ?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)) " //
// + "}";//
// 
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() >= 1); // may see points from
// // during previous runs
// 
// // point outside search ring with PCJ
// queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
// + "SELECT ?feature ?point ?wkt ?e ?l ?o " //
// + "{" //
// + "  ?feature a ?e . "//
// + "  ?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((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " //
// + "}";//
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() == 0);
// 
// // point inside search ring with different Pre-Computed Join
// queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
// + "SELECT ?feature ?point ?wkt ?e ?c ?l ?o " //
// + "{" //
// + "  ?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)) " //
// + "}";//
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() == 1);
// }
// 
// 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"//
// + "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after
// // 3
// // seconds
// + "  ?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());
// Validate.isTrue(tupleHandler.getCount() == 5);
// 
// }
// 
// 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());
// Validate.isTrue(tupleHandler.getCount() == 1);
// }
private static void testDeleteTemporalData(final SailRepositoryConnection conn) throws Exception {
    // Delete all stored dates
    final String sparqlDelete = "PREFIX time: <http://www.w3.org/2006/time#>\n" + // 
    "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n" + // 
    "DELETE {\n" + "  ?event time:inXSDDateTime ?time . \n" + "}\n" + "WHERE { \n" + // 
    "  ?event time:inXSDDateTime ?time . \n" + // 
    "}";
    final Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlDelete);
    deleteUpdate.execute();
    // Find all stored dates.
    final String queryString = // 
    "PREFIX time: <http://www.w3.org/2006/time#> \n" + // 
    "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n" + // 
    "SELECT ?event ?time \n" + "WHERE { \n" + // 
    "  ?event time:inXSDDateTime ?time . \n" + // after
    "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n" + // seconds
    "}";
    // 
    final CountingResultHandler tupleHandler = new CountingResultHandler();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    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 18 with TupleQuery

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

the class RyaDirectExample method testPCJSearch.

private static void testPCJSearch(final SailRepositoryConnection conn) throws Exception {
    String queryString;
    TupleQuery tupleQuery;
    CountingResultHandler tupleHandler;
    // ///////////// search for bob
    queryString = // 
    "SELECT ?e ?c ?l ?o " + // 
    "{" + // 
    "  ?e a ?c . " + // 
    "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
    "  ?e <uri:talksTo> ?o . " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    Validate.isTrue(tupleHandler.getCount() == 1);
    // ///////////// search for bob
    queryString = // 
    "PREFIX fts: <http://rdf.useekm.com/fts#>  " + // 
    "SELECT ?e ?c ?l ?o " + // 
    "{" + // 
    "  ?c a ?e . " + // 
    "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
    "  ?e <uri:talksTo> ?o . " + // 
    "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleHandler = new CountingResultHandler();
    tupleQuery.evaluate(tupleHandler);
    log.info("Result count : " + tupleHandler.getCount());
    Validate.isTrue(tupleHandler.getCount() == 2);
}
Also used : TupleQuery(org.openrdf.query.TupleQuery)

Example 19 with TupleQuery

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

the class ConformanceTest method getTests.

/**
 * Query a connection for conformance test details.
 */
Collection<OwlTest> getTests(final RepositoryConnection conn, final Set<Value> testURIs) throws IOException, OpenRDFException {
    final Map<Value, OwlTest> tests = new HashMap<>();
    final TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, "select * where { " + "?test <" + TYPE + "> ?testType .\n" + "?test <" + TEST_PREMISE + "> ?graph .\n" + "?test <" + TEST_ID + "> ?name .\n" + "?test <" + TEST_DESC + "> ?description .\n" + "?test <" + TEST_PROFILE + "> <" + TEST_RL + "> .\n" + "?test <" + TEST_SEMANTICS + "> <" + TEST_RDFBASED + "> .\n" + "OPTIONAL {?test <" + TEST_CONCLUSION + "> ?conclusion .}\n" + "OPTIONAL {?test <" + TEST_NONCONCLUSION + "> ?nonentailed .}\n" + "}");
    final TupleQueryResult queryResult = query.evaluate();
    while (queryResult.hasNext()) {
        final BindingSet bindings = queryResult.next();
        final Value uri = bindings.getValue("test");
        if (testURIs.contains(uri)) {
            OwlTest test;
            if (tests.containsKey(uri)) {
                test = tests.get(uri);
            } else {
                test = new OwlTest();
                test.uri = uri;
                test.name = bindings.getValue("name").stringValue();
                test.description = bindings.getValue("description").stringValue();
                test.premise = bindings.getValue("graph").stringValue();
                if (bindings.hasBinding("conclusion")) {
                    test.compareTo = bindings.getValue("conclusion").stringValue();
                }
                if (bindings.hasBinding("nonentailed")) {
                    test.compareTo = bindings.getValue("nonentailed").stringValue();
                }
                tests.put(uri, test);
            }
            test.types.add(bindings.getValue("testType").stringValue());
        }
    }
    for (final OwlTest test : tests.values()) {
        if (test.compareTo != null) {
            final RDFXMLParser parser = new RDFXMLParser();
            parser.setRDFHandler(test);
            parser.parse(new StringReader(test.compareTo), "");
        }
    }
    queryResult.close();
    return tests.values();
}
Also used : BindingSet(org.openrdf.query.BindingSet) HashMap(java.util.HashMap) RDFXMLParser(org.openrdf.rio.rdfxml.RDFXMLParser) Value(org.openrdf.model.Value) StringReader(java.io.StringReader) TupleQuery(org.openrdf.query.TupleQuery) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 20 with TupleQuery

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

the class ConformanceTest method getTestURIs.

/**
 * Query a connection for conformance tests matching a particular
 * test type.
 */
Set<Value> getTestURIs(final RepositoryConnection conn, final String testType) throws IOException, OpenRDFException {
    final Set<Value> testURIs = new HashSet<>();
    final TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, "select ?test where { " + "?test <" + TYPE + "> <" + testType + "> .\n" + "?test <" + TEST_PROFILE + "> <" + TEST_RL + "> .\n" + "?test <" + TEST_SEMANTICS + "> <" + TEST_RDFBASED + "> .\n" + "}");
    final TupleQueryResult queryResult = query.evaluate();
    while (queryResult.hasNext()) {
        final BindingSet bindings = queryResult.next();
        testURIs.add(bindings.getValue("test"));
    }
    queryResult.close();
    return testURIs;
}
Also used : BindingSet(org.openrdf.query.BindingSet) Value(org.openrdf.model.Value) TupleQuery(org.openrdf.query.TupleQuery) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet)

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