Sparql Querying
Once you have RDF2GoRepository, you can use RDF2Go without any restrictions. If you are familiar with RDF2Go, you will have no problems programming with the Nepomuk RDF Repository.
RDF2GoRepository repo = ...; // see above
ModelSet ms = repo.getMainRepository();
QueryResultTable result = ms.sparqlSelect("SELECT ?x WHERE {?x ?y ?z}");
for (QueryRow row : result)
{
System.out.println("x= "+row.getValue("x"));
}
You can also use SERQL
// use a proprietary query language like SERQL
QueryResultTable result = ms.querySelect("SELECT x WHERE {x} y {z}", "SERQL");
for (QueryRow row : result)
{
System.out.println("x= "+row.getValue("x"));
}
Include namespace (PREFIX)
Rember to include prefixes in your Sparql queries, you should only include needed. Example:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX nrl: <http://www.semanticdesktop.org/ontologies/2007/08/15/nrl#> PREFIX nao: <http://www.semanticdesktop.org/ontologies/2007/08/15/nao#> PREFIX pimo: <http://www.semanticdesktop.org/ontologies/2007/11/01/pimo#> PREFIX nie: <http://www.semanticdesktop.org/ontologies/2007/01/19/nie#> PREFIX nmo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> PREFIX ncal: <http://www.semanticdesktop.org/ontologies/2007/04/02/ncal#> PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>
In Java code you can use constants, e.g.:
ClientSession.SPARQL_PREAMBLE or PIMO.NS_PIMO
Using different dataypes in query
This example shows how to write query, when for examle object has not literal type.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?x ?z WHERE {
?x rdf:type ncal:Event .
?x ncal:summary ?z .
?x ncal:dtstart ?y .
?y ncal:date "+ dateLiteral.toSPARQL() + " .
}
But dateLiteral is not simple literral but the DatatypeLiteralImpl
To create such literal you can write such a code:
DatatypeLiteralImpl dateLiteral = new DatatypeLiteralImpl("2008-09-04T09:58:42Z", new URIImpl("http://www.w3.org/2001/XMLSchema#date"));
