Search in sources :

Example 26 with TupleQuery

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

the class ArbitraryLengthQueryTest method testWithoutSubquery.

/**
 * This test works. The expected result is 6 rows ranging from "Model1Class 1" through "Model1Class 6".
 *
 * @throws RepositoryException
 * @throws QueryEvaluationException
 * @throws TupleQueryResultHandlerException
 *
 * @throws MalformedQueryException
 */
public void testWithoutSubquery() throws RepositoryException, QueryEvaluationException, TupleQueryResultHandlerException, MalformedQueryException {
    final String query = "SELECT ?i ?i_label ?i_class ?i_v1" + "WHERE {" + "?i <http://www.w3.org/2000/01/rdf-schema#label> ?i_label ." + "?i a ?i_class ." + "?i_class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <http://dragon-research.com/cham/model/model1#Model1Class> ." + "OPTIONAL { ?i <http://dragon-research.com/cham/model/model1#name> ?i_v1 } ." + "}" + "ORDER BY ?i_label";
    final RepositoryConnection conn = repository.getConnection();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    RdfCloudTripleStoreConnectionTest.CountTupleHandler countTupleHandler = new RdfCloudTripleStoreConnectionTest.CountTupleHandler();
    tupleQuery.evaluate(countTupleHandler);
    assertEquals(6, countTupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) TupleQuery(org.openrdf.query.TupleQuery)

Example 27 with TupleQuery

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

the class ArbitraryLengthQueryTest method testWithSubquery.

/**
 * This test fails. The expected result is 6 rows ranging from "Model1Class 1 Event" to "Model1Class 6 Event". The
 * current result is a RejectedExecutionException.
 *
 * @throws RepositoryException
 * @throws QueryEvaluationException
 * @throws TupleQueryResultHandlerException
 *
 * @throws MalformedQueryException
 */
public void testWithSubquery() throws RepositoryException, QueryEvaluationException, TupleQueryResultHandlerException, MalformedQueryException {
    final String query = "SELECT ?i ?i_label ?i_class ?i_v1 ?i_v2 ?i_v2_label ?i_v2_class ?i_v2_v1" + "WHERE {" + "?i <http://www.w3.org/2000/01/rdf-schema#label> ?i_label ." + "?i a ?i_class ." + "?i_class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <http://dragon-research.com/cham/model/model1#Event> ." + "OPTIONAL { ?i <http://dragon-research.com/cham/model/model1#name> ?i_v1 } ." + "?i <http://dragon-research.com/cham/model/model1#hasTemporalEntity> ?i_v2 ." + "{" + "SELECT ?i_v2 ?i_v2_label ?i_v2_class ?i_v2_v1" + "WHERE {" + "?i_v2 <http://www.w3.org/2000/01/rdf-schema#label> ?i_v2_label ." + "?i_v2 a ?i_v2_class ." + "?i_v2_class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <http://dragon-research.com/cham/model/model1#TemporalInstant> ." + "OPTIONAL { ?i_v2 <http://dragon-research.com/cham/model/model1#dateTime> ?i_v2_v1 } ." + "}" + "ORDER BY ?i_v2_label" + "}" + "}" + "ORDER BY ?i_label";
    final RepositoryConnection conn = repository.getConnection();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    RdfCloudTripleStoreConnectionTest.CountTupleHandler countTupleHandler = new RdfCloudTripleStoreConnectionTest.CountTupleHandler();
    tupleQuery.evaluate(countTupleHandler);
    assertEquals(6, countTupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) TupleQuery(org.openrdf.query.TupleQuery)

Example 28 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testClearGraph.

public void testClearGraph() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "PREFIX ex: <http://example/addresses#>\n" + "INSERT DATA\n" + "{ GRAPH ex:G1 {\n" + "<http://example/book3> dc:title    \"A new book\" ;\n" + "                         dc:creator  \"A.N.Other\" .\n" + "}\n" + "}";
    Update update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();
    insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "PREFIX ex: <http://example/addresses#>\n" + "INSERT DATA\n" + "{ GRAPH ex:G2 {\n" + "<http://example/book3> dc:title    \"A new book\" ;\n" + "                         dc:creator  \"A.N.Other\" .\n" + "}\n" + "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, insert);
    update.execute();
    String query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "select * where { <http://example/book3> ?p ?o. }";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());
    tupleHandler = new CountTupleHandler();
    conn.clear(new URIImpl("http://example/addresses#G2"));
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());
    tupleHandler = new CountTupleHandler();
    conn.clear(new URIImpl("http://example/addresses#G1"));
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) TupleQuery(org.openrdf.query.TupleQuery) URIImpl(org.openrdf.model.impl.URIImpl) Update(org.openrdf.query.Update)

Example 29 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testSPOPredRange.

public void testSPOPredRange() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc1");
    URI loadPerc2 = vf.createURI(litdupsNS, "loadPerc2");
    URI loadPerc3 = vf.createURI(litdupsNS, "loadPerc3");
    URI loadPerc4 = vf.createURI(litdupsNS, "loadPerc4");
    Literal six = vf.createLiteral("6");
    Literal sev = vf.createLiteral("7");
    Literal ten = vf.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu, loadPerc2, sev);
    conn.add(cpu, loadPerc4, ten);
    conn.commit();
    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" + "select * where {" + "<" + cpu.stringValue() + "> ?p ?o.\n" + "FILTER(org.apache:range(?p, <" + loadPerc.stringValue() + ">, <" + loadPerc3.stringValue() + ">))." + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(2, cth.getCount());
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Literal(org.openrdf.model.Literal) TupleQuery(org.openrdf.query.TupleQuery) URI(org.openrdf.model.URI)

Example 30 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testPOPredRange.

public void testPOPredRange() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc1");
    URI loadPerc2 = vf.createURI(litdupsNS, "loadPerc2");
    URI loadPerc3 = vf.createURI(litdupsNS, "loadPerc3");
    URI loadPerc4 = vf.createURI(litdupsNS, "loadPerc4");
    Literal six = vf.createLiteral("6");
    Literal sev = vf.createLiteral("7");
    Literal ten = vf.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu, loadPerc2, sev);
    conn.add(cpu, loadPerc4, ten);
    conn.commit();
    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" + "select * where {" + "?x ?p ?o.\n" + "FILTER(org.apache:range(?p, <" + loadPerc.stringValue() + ">, <" + loadPerc3.stringValue() + ">))." + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(cth.getCount(), 2);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Literal(org.openrdf.model.Literal) TupleQuery(org.openrdf.query.TupleQuery) URI(org.openrdf.model.URI)

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