Search in sources :

Example 31 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testNamedGraphLoadWAuth.

public void testNamedGraphLoadWAuth() throws Exception {
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("namedgraphs.trig");
    assertNotNull(stream);
    RdfCloudTripleStore tstore = new MockRdfCloudStore();
    NamespaceManager nm = new NamespaceManager(tstore.getRyaDAO(), tstore.getConf());
    tstore.setNamespaceManager(nm);
    SailRepository repo = new SailRepository(tstore);
    tstore.getRyaDAO().getConf().setCv("1|2");
    repo.initialize();
    RepositoryConnection conn = repo.getConnection();
    conn.add(stream, "", RDFFormat.TRIG);
    conn.commit();
    String query = "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" + "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" + "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" + "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "SELECT * \n" + // "FROM NAMED <http://www.example.org/exampleDocument#G1>\n" +
    "WHERE\n" + "{\n" + "  GRAPH ex:G1\n" + "  {\n" + "    ?m voc:name ?name ;\n" + "           voc:homepage ?hp .\n" + "  } .\n" + " GRAPH ex:G2\n" + "  {\n" + "    ?m voc:hasSkill ?skill .\n" + "  } .\n" + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, vf.createLiteral("2"));
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(1, tupleHandler.getCount());
    // no auth
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());
    conn.close();
    repo.shutDown();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) NamespaceManager(org.apache.rya.rdftriplestore.namespace.NamespaceManager) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) SailRepository(org.openrdf.repository.sail.SailRepository) InputStream(java.io.InputStream) TupleQuery(org.openrdf.query.TupleQuery)

Example 32 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testNamedGraphLoad.

public void testNamedGraphLoad() throws Exception {
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("namedgraphs.trig");
    assertNotNull(stream);
    RepositoryConnection conn = repository.getConnection();
    conn.add(stream, "", RDFFormat.TRIG);
    conn.commit();
    String query = "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" + "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" + "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" + "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "SELECT * \n" + // "FROM NAMED <http://www.example.org/exampleDocument#G1>\n" +
    "WHERE\n" + "{\n" + "  GRAPH ex:G1\n" + "  {\n" + "    ?m voc:name ?name ;\n" + "           voc:homepage ?hp .\n" + "  } .\n" + " GRAPH ex:G2\n" + "  {\n" + "    ?m voc:hasSkill ?skill .\n" + "  } .\n" + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    // tupleQuery.evaluate(new PrintTupleHandler());
    assertEquals(1, tupleHandler.getCount());
    query = "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" + "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" + "PREFIX  swp:  <http://www.w3.org/2004/03/trix/swp-1/>\n" + "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" + "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "SELECT * \n" + "WHERE\n" + "{\n" + "  GRAPH ex:G3\n" + "  {\n" + "    ?g swp:assertedBy ?w .\n" + "    ?w swp:authority ex:Tom .\n" + "  } .\n" + "  GRAPH ?g\n" + "  {\n" + "    ?m voc:name ?name .\n" + "  } .\n" + "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(1, tupleHandler.getCount());
    query = "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" + "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" + "PREFIX  swp:  <http://www.w3.org/2004/03/trix/swp-1/>\n" + "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" + "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "SELECT * \n" + "WHERE\n" + "{\n" + "  GRAPH ?g\n" + "  {\n" + "    ?m voc:name ?name .\n" + "  } .\n" + "}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    // tupleQuery.setBinding(BINDING_DISP_QUERYPLAN, VALUE_FACTORY.createLiteral(true));
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) InputStream(java.io.InputStream) TupleQuery(org.openrdf.query.TupleQuery)

Example 33 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testNamespaceUsage.

public void testNamespaceUsage() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    conn.setNamespace("lit", litdupsNS);
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
    final URI uri1 = vf.createURI(litdupsNS, "uri1");
    conn.add(cpu, loadPerc, uri1);
    conn.commit();
    String query = "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {lit:cpu lit:loadPerc ?o.}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleQuery.evaluate(new TupleQueryResultHandler() {

        @Override
        public void startQueryResult(List<String> strings) throws TupleQueryResultHandlerException {
        }

        @Override
        public void endQueryResult() throws TupleQueryResultHandlerException {
        }

        @Override
        public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
            assertTrue(uri1.toString().equals(bindingSet.getBinding("o").getValue().stringValue()));
        }

        @Override
        public void handleBoolean(boolean paramBoolean) throws QueryResultHandlerException {
        }

        @Override
        public void handleLinks(List<String> paramList) throws QueryResultHandlerException {
        }
    });
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) TupleQuery(org.openrdf.query.TupleQuery) TupleQueryResultHandler(org.openrdf.query.TupleQueryResultHandler) QueryResultHandlerException(org.openrdf.query.QueryResultHandlerException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) URI(org.openrdf.model.URI)

Example 34 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testTransitiveProp.

public void testTransitiveProp() throws Exception {
    if (internalInferenceEngine == null) {
        // infer not supported;
        return;
    }
    RepositoryConnection conn = repository.getConnection();
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "subRegionOf"), RDF.TYPE, OWL.TRANSITIVEPROPERTY));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "Queens"), vf.createURI(litdupsNS, "subRegionOf"), vf.createURI(litdupsNS, "NYC")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "NYC"), vf.createURI(litdupsNS, "subRegionOf"), vf.createURI(litdupsNS, "NY")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "NY"), vf.createURI(litdupsNS, "subRegionOf"), vf.createURI(litdupsNS, "US")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "US"), vf.createURI(litdupsNS, "subRegionOf"), vf.createURI(litdupsNS, "NorthAmerica")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "NorthAmerica"), vf.createURI(litdupsNS, "subRegionOf"), vf.createURI(litdupsNS, "World")));
    conn.commit();
    conn.close();
    internalInferenceEngine.refreshGraph();
    conn = repository.getConnection();
    String query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s lit:subRegionOf lit:NorthAmerica.}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(4, tupleHandler.getCount());
    query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s lit:subRegionOf lit:NY.}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(2, tupleHandler.getCount());
    query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {lit:Queens lit:subRegionOf ?s.}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(5, tupleHandler.getCount());
    query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {lit:NY lit:subRegionOf ?s.}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(3, tupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) StatementImpl(org.openrdf.model.impl.StatementImpl) TupleQuery(org.openrdf.query.TupleQuery)

Example 35 with TupleQuery

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

the class RdfCloudTripleStoreConnectionTest method testUpdateWAuthOnConfig.

// Set the persistence visibilites on the config
public void testUpdateWAuthOnConfig() throws Exception {
    String sparqlUpdate = getSparqlUpdate();
    RdfCloudTripleStore tstore = new MockRdfCloudStore();
    NamespaceManager nm = new NamespaceManager(tstore.getRyaDAO(), tstore.getConf());
    tstore.setNamespaceManager(nm);
    SailRepository repo = new SailRepository(tstore);
    tstore.getRyaDAO().getConf().setCv("1|2");
    repo.initialize();
    RepositoryConnection conn = repo.getConnection();
    Update u = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlUpdate);
    u.execute();
    String query = "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" + "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" + "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" + "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "SELECT * \n" + // "FROM NAMED <http://www.example.org/exampleDocument#G1>\n" +
    "WHERE\n" + "{\n" + "  GRAPH ex:G1\n" + "  {\n" + "    ?m voc:name ?name ;\n" + "           voc:homepage ?hp .\n" + "  } .\n" + " GRAPH ex:G2\n" + "  {\n" + "    ?m voc:hasSkill ?skill .\n" + "  } .\n" + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, vf.createLiteral("2"));
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(1, tupleHandler.getCount());
    // no auth
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());
    conn.close();
    repo.shutDown();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) NamespaceManager(org.apache.rya.rdftriplestore.namespace.NamespaceManager) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) SailRepository(org.openrdf.repository.sail.SailRepository) 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