use of org.openrdf.query.TupleQuery 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();
}
use of org.openrdf.query.TupleQuery 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);
}
use of org.openrdf.query.TupleQuery 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());
}
use of org.openrdf.query.TupleQuery 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();
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class RdfCloudTripleStoreConnectionTest method testClearAllGraph.
public void testClearAllGraph() 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();
tupleQuery.evaluate(tupleHandler);
assertEquals(0, tupleHandler.getCount());
conn.close();
}
Aggregations