Search in sources :

Example 46 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testInsertDeleteData.

public void testInsertDeleteData() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    String insert = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "INSERT DATA\n" + "{ <http://example/book3> dc:title    \"A new book\" ;\n" + "                         dc:creator  \"A.N.Other\" .\n" + "}";
    Update 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(2, tupleHandler.getCount());
    String delete = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "\n" + "DELETE DATA\n" + "{ <http://example/book3> dc:title    \"A new book\" ;\n" + "                         dc:creator  \"A.N.Other\" .\n" + "}";
    update = conn.prepareUpdate(QueryLanguage.SPARQL, delete);
    update.execute();
    query = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "select * where { <http://example/book3> ?p ?o. }";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) TupleQuery(org.openrdf.query.TupleQuery) Update(org.openrdf.query.Update)

Example 47 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testRegexFilter.

public void testRegexFilter() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
    URI testClass = vf.createURI(litdupsNS, "test");
    Literal six = vf.createLiteral("6");
    Literal sev = vf.createLiteral("7");
    Literal ten = vf.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu, loadPerc, sev);
    conn.add(cpu, loadPerc, ten);
    conn.add(cpu, RDF.TYPE, testClass);
    conn.commit();
    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" + "select * where {" + String.format("<%s> ?p ?o.\n", cpu.stringValue()) + "FILTER(regex(?o, '^1'))." + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(cth.getCount(), 1);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Literal(org.openrdf.model.Literal) TupleQuery(org.openrdf.query.TupleQuery) URI(org.openrdf.model.URI)

Example 48 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testPOObjRange.

public void testPOObjRange() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
    Literal six = vf.createLiteral("6");
    Literal sev = vf.createLiteral("7");
    Literal ten = vf.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu, loadPerc, sev);
    conn.add(cpu, loadPerc, ten);
    conn.commit();
    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" + "select * where {" + "?x <" + loadPerc.stringValue() + "> ?o.\n" + "FILTER(org.apache:range(?o, '6', '8'))." + "}";
    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 49 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testSubClassOf.

public void testSubClassOf() throws Exception {
    if (internalInferenceEngine == null) {
        // infer not supported;
        return;
    }
    RepositoryConnection conn = repository.getConnection();
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "UndergraduateStudent"), RDFS.SUBCLASSOF, vf.createURI(litdupsNS, "Student")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "Student"), RDFS.SUBCLASSOF, vf.createURI(litdupsNS, "Person")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "UgradA"), RDF.TYPE, vf.createURI(litdupsNS, "UndergraduateStudent")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "StudentB"), RDF.TYPE, vf.createURI(litdupsNS, "Student")));
    conn.add(new StatementImpl(vf.createURI(litdupsNS, "PersonC"), RDF.TYPE, vf.createURI(litdupsNS, "Person")));
    conn.commit();
    conn.close();
    internalInferenceEngine.refreshGraph();
    conn = repository.getConnection();
    // simple api first
    RepositoryResult<Statement> person = conn.getStatements(null, RDF.TYPE, vf.createURI(litdupsNS, "Person"), true);
    int count = 0;
    while (person.hasNext()) {
        count++;
        person.next();
    }
    person.close();
    assertEquals(3, count);
    String query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s rdf:type lit:Person.}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(3, tupleHandler.getCount());
    query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s rdf:type lit:Student.}";
    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 {?s rdf:type lit:UndergraduateStudent.}";
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(1, tupleHandler.getCount());
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Statement(org.openrdf.model.Statement) StatementImpl(org.openrdf.model.impl.StatementImpl) TupleQuery(org.openrdf.query.TupleQuery)

Example 50 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testAddStatement.

public void testAddStatement() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
    URI uri1 = vf.createURI(litdupsNS, "uri1");
    conn.add(cpu, loadPerc, uri1);
    conn.commit();
    RepositoryResult<Statement> result = conn.getStatements(cpu, loadPerc, null, true);
    int count = 0;
    while (result.hasNext()) {
        count++;
        result.next();
    }
    result.close();
    assertEquals(1, count);
    // clean up
    conn.remove(cpu, loadPerc, uri1);
    // //test removal
    result = conn.getStatements(cpu, loadPerc, null, true, new Resource[0]);
    count = 0;
    while (result.hasNext()) {
        count++;
        result.next();
    }
    result.close();
    assertEquals(0, count);
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI)

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