use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class InferenceExamples method testIntersectionOfInference.
public static void testIntersectionOfInference(final SailRepositoryConnection conn, final Sail sail) throws MalformedQueryException, RepositoryException, UpdateExecutionException, QueryEvaluationException, TupleQueryResultHandlerException, InferenceEngineException {
log.info("Adding Data");
final String instances = "INSERT DATA\n" + "{ GRAPH <http://updated/test> {\n" + " <urn:Susan> a <urn:Mother> . \n" + " <urn:Mary> a <urn:Woman> . \n" + " <urn:Mary> a <urn:Parent> . \n" + "}}";
Update update = conn.prepareUpdate(QueryLanguage.SPARQL, instances);
update.execute();
final String inferQuery = "select distinct ?x { GRAPH <http://updated/test> { ?x a <urn:Mother> }}";
final String explicitQuery = "select distinct ?x { GRAPH <http://updated/test> {\n" + " { ?x a <urn:Mother> }\n" + " UNION {\n" + " ?x a <urn:Woman> .\n" + " ?x a <urn:Parent> .\n" + " }\n" + "}}";
log.info("Running Explicit Query");
CountingResultHandler resultHandler = new CountingResultHandler();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, explicitQuery);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 2);
log.info("Running Inference-dependant Query");
resultHandler.resetCount();
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, inferQuery);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 1);
log.info("Adding owl:intersectionOf Schema");
// ONTOLOGY - :Mother intersectionOf[:Woman, :Parent]
final String ontology = "INSERT DATA\n" + "{ GRAPH <http://updated/test> {\n" + " <urn:Mother> owl:intersectionOf _:bnode1 . \n" + " _:bnode1 rdf:first <urn:Woman> . \n" + " _:bnode1 rdf:rest _:bnode2 . \n" + " _:bnode2 rdf:first <urn:Parent> . \n" + " _:bnode2 rdf:rest rdf:nil . \n" + "}}";
update = conn.prepareUpdate(QueryLanguage.SPARQL, ontology);
update.execute();
log.info("Refreshing InferenceEngine");
((RdfCloudTripleStore) sail).getInferenceEngine().refreshGraph();
log.info("Re-running Inference-dependant Query");
resultHandler.resetCount();
resultHandler = new CountingResultHandler();
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, inferQuery);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 2);
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class RyaDirectExample method testAddAndTemporalSearchWithPCJ.
private static void testAddAndTemporalSearchWithPCJ(final SailRepositoryConnection conn) throws Exception {
// create some resources and literals to make statements out of
final String sparqlInsert = "PREFIX time: <http://www.w3.org/2006/time#>\n" + //
"INSERT DATA {\n" + "_:eventz a time:Instant ;\n" + // one
" time:inXSDDateTime '2001-01-01T01:01:01-08:00' ;\n" + // second
" time:inXSDDateTime '2001-01-01T04:01:02.000-05:00'^^<http://www.w3.org/2001/XMLSchema#dateTime> ;\n" + // seconds
" time:inXSDDateTime \"2001-01-01T01:01:03-08:00\" ;\n" + // seconds
" time:inXSDDateTime '2001-01-01T01:01:04-08:00' ;\n" + // seconds
" time:inXSDDateTime '2001-01-01T09:01:05Z' ;\n" + " time:inXSDDateTime '2006-01-01' ;\n" + " time:inXSDDateTime '2007-01-01' ;\n" + " time:inXSDDateTime '2008-01-01' ; .\n" + "}";
final Update update = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlInsert);
update.execute();
// Find all stored dates.
String queryString = //
"PREFIX time: <http://www.w3.org/2006/time#> \n" + //
"PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n" + //
"SELECT ?event ?time \n" + "WHERE { \n" + //
" ?event time:inXSDDateTime ?time . \n" + // after
" FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n" + // seconds
"}";
//
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
CountingResultHandler tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 5);
// Find all stored dates.
queryString = //
"PREFIX time: <http://www.w3.org/2006/time#> \n" + //
"PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n" + //
"SELECT ?event ?time \n" + "WHERE { \n" + //
" ?event time:inXSDDateTime ?time . \n" + //
" ?event a time:Instant . \n" + // after
" FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n" + // seconds
"}";
//
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 5);
// Find all stored dates.
queryString = //
"PREFIX time: <http://www.w3.org/2006/time#> \n" + //
"PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n" + //
"SELECT ?event ?time ?e ?c ?l ?o \n" + "WHERE { \n" + //
" ?e a ?c . \n" + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . \n" + //
" ?e <uri:talksTo> ?o . \n" + //
" ?event a time:Instant . \n" + //
" ?event time:inXSDDateTime ?time . \n" + // after
" FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n" + // seconds
"}";
//
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 5);
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class RyaDirectExample method testAddAndDelete.
public static void testAddAndDelete(final SailRepositoryConnection conn) throws MalformedQueryException, RepositoryException, UpdateExecutionException, QueryEvaluationException, TupleQueryResultHandlerException, AccumuloException, AccumuloSecurityException, TableNotFoundException {
// Add data
String query = //
"INSERT DATA\n" + //
"{ GRAPH <http://updated/test> {\n" + //
" <http://acme.com/people/Mike> " + //
" <http://acme.com/actions/likes> \"A new book\" ;\n" + " <http://acme.com/actions/likes> \"Avocados\" .\n" + "} }";
log.info("Performing Query");
Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
final CountingResultHandler resultHandler = new CountingResultHandler();
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 2);
resultHandler.resetCount();
// Delete Data
query = //
"DELETE DATA\n" + "{ GRAPH <http://updated/test> {\n" + " <http://acme.com/people/Mike> <http://acme.com/actions/likes> \"A new book\" ;\n" + " <http://acme.com/actions/likes> \"Avocados\" .\n" + "}}";
update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
update.execute();
query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleQuery.evaluate(resultHandler);
log.info("Result count : " + resultHandler.getCount());
Validate.isTrue(resultHandler.getCount() == 0);
}
use of org.openrdf.query.TupleQuery in project incubator-rya by apache.
the class RyaDirectExample method testDeleteFreeTextData.
private static void testDeleteFreeTextData(final SailRepositoryConnection conn) throws Exception {
// Delete data from the repository using the SailRepository remove
// methods
final ValueFactory f = conn.getValueFactory();
final URI person = f.createURI("http://example.org/ontology/Person");
String uuid;
uuid = "urn:people:alice";
conn.remove(f.createURI(uuid), RDF.TYPE, person);
conn.remove(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
uuid = "urn:people:bobss";
conn.remove(f.createURI(uuid), RDF.TYPE, person);
conn.remove(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Bob Snob Hose", "en"));
conn.remove(person, RDFS.LABEL, f.createLiteral("label", "en"));
String queryString;
TupleQuery tupleQuery;
CountingResultHandler tupleHandler;
// Find all
queryString = //
"PREFIX fts: <http://rdf.useekm.com/fts#> " + //
"SELECT ?person ?match " + //
"{" + //
" ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + //
" ?person a <http://example.org/ontology/Person> . " + //
"}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 0);
}
use of org.openrdf.query.TupleQuery in project backstage by zepheira.
the class Expression method computeOutputOnValue.
public ExpressionQueryResult computeOutputOnValue(Value value, Database database, SailRepositoryConnection connection) throws ExpressionException {
TupleQueryBuilder builder = new TupleQueryBuilder();
Var valueVar = builder.makeVar("value", value);
ExpressionResult expressionResult = computeOutputOnItem(database, builder, valueVar);
if (expressionResult.valueExpr instanceof Var) {
Var resultVar = (Var) expressionResult.valueExpr;
ProjectionElemList projectionElements = new ProjectionElemList();
projectionElements.addElement(new ProjectionElem(resultVar.getName()));
TupleExpr t = builder.makeFilterTupleExpr();
if (t == null) {
// TODO[dfhuynh]: This happens if the expression is just "value". I'm not sure what to do here.
return null;
}
Projection projection = new Projection(t, projectionElements);
TupleQuery query = new MyTupleQuery(new ParsedTupleQuery(projection), connection);
return new ExpressionQueryResult(query, expressionResult.valueType, resultVar);
}
return null;
}
Aggregations