Search in sources :

Example 16 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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)

Example 17 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 18 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 19 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 20 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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)

Aggregations

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3