SPARQL query universities - sparql

I am trying to fetch the names of universities using SPARQL:
The rdf data is the following (and a whole lot more, but that is irrelevant).
<Organization rdf:about="http://data.semanticweb.org/organization/the-university-of-queensland">
<rdfs:label>The University of Queensland</rdfs:label>
<homepage rdf:resource="http://www.uq.edu.au/"/>
<member rdf:resource="http://data.semanticweb.org/person/jane-hunter"/>
<member rdf:resource="http://data.semanticweb.org/person/kwok-cheung"/>
<member rdf:resource="http://data.semanticweb.org/person/robert-m-colomb"/>
<name>The University of Queensland</name>
</Organization>
I have written a java program which queries the data.
My string to query the data is the following:
queryString += "PREFIX swrc: <http://swrc.ontoware.org/ontology#> \n";
queryString += "PREFIX dc: <http://purl.org/dc/elements/1.1/> \n";
queryString += "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n";
queryString += "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \n";
queryString += "PREFIX ical: <http://www.w3.org/2002/12/cal/ical#> \n";
queryString += "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n";
queryString += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n";
queryString += "PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#> \n";
queryString += "PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#> \n";
queryString += "SELECT ?name WHERE {\n";
queryString += "?university rdfs:label ?aff . \n ?university foaf:name ?name FILTER(str(?aff)='uni') }";
Unfortunately, this is not correct, as no result is returned:
<?xml version='1.0' encoding='UTF-8'?>
<sparql xmlns='w3.org/2005/sparql-results#'>
<head>
<variable name='name'/>
</head>
<results>
</results>
</sparql>
Can anyone point me in the right direction?
P.S. If possible I'd like to only fetch 10 university names.

I understand that you're assuming all universities have the string "uni" in their names.
Notice that you're checking for equality of a value with the string "uni". There really is no such instance in your data set.
Replacing FILTER(str(?aff)='uni') with FILTER regex(?aff, "uni", "i") will allow you to match the values that contain the string "uni" instead. It's a regular expression filter that takes three arguments.
a variable
a regular expression compliant with the syntax described here
a set of optional flags, in this case, I used one flag, "i". It means the match must be case-insensitive.
In order to limit the number of results, you can just append the query with the LIMIT keyword.
The resulting query should be:
PREFIX swrc: <http://swrc.ontoware.org/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX ical: <http://www.w3.org/2002/12/cal/ical#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#>
SELECT ?name WHERE {
?university rdfs:label ?aff .
?university foaf:name ?name
FILTER regex(?aff, "uni", "i")
} LIMIT 10;

In your RDF snippet does not have the "foaf:" namespace. So, maybe you should put in your RDF. Unless of course it is the default namespace in your RDF document.

Related

SPARQL execdescribe query in jena only giving prefix as output

Below SPARQL execdescribe query giving me only two prefixes as output while running with Jena query but when I run this query on virtuoso SPARQL endpoint it's giving perfect output.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
DESCRIBE ?movie ?author ?genre
WHERE {
?movie rdf:type db:Film ;
prop:author ?author ;
prop:genre ?genre .
}
LIMIT 2
OFFSET 0
When I run with Jena I am getting only two line output like this,
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
Below is my code which I am using but with some query its working fine,
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: <http://dbpedia.org/property/>" +
"DESCRIBE ?movie ?author ?genre" +
"WHERE { " +
"?movie rdf:type db:Film ;" +
"prop:author ?author ;" +
"prop:genre ?genre ." +
"}" +
"LIMIT 2" +
"OFFSET 0";
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", queryString);
Model results = qexec.execDescribe();
results.write(System.out,"TTL");
Its perfectly giving me output on virtuoso SPARQL end point.
below is screen shot,
As suggested in comments by #AKSW, There is a space issue in the query. So after edited my query, it will perfectly work fine.

SPARQL query with FILTER returning correct values for its variables BUT only the given value for the variable being filtered

I'm using Jena to make a SPARQL query to search for all a document's properties by its subject. But the documents can have more than one subject, and when I do the search, it doesn't return me all the documents' properties, including all the documents' subjects, but even if it has 3 subjects (for example) it returns me all the documents properties + only the subject I set at FILTER.
I'd like to have as a return all the properties from the found document + all the subjects (that belong to the found document) and not only the one at FILTER.
Query (this.subject is a variable that has its value set in a JSF page):
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?document ?subject" +
" ?title ?description ?language WHERE { " +
"?document dc:title ?title." +
"?document dc:subject ?subject." +
"?document dc:description ?description." +
"?document dc:language ?language." +
"FILTER ( regex(?subject, replace( \"" + this.subject + "\", ' ', '|' ), 'i' )). }";
Thank you!
You likely want to use a sub-query to restrict to the documents matching the FILTER and then select the rest of the stuff you are actually interesting in e.g.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc : <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?document ?subject ?title ?description ?language
WHERE
{
{
SELECT ?document
WHERE
{
?document dc:subject ?subject .
FILTER(REGEX(?subject, REPLACE("search term", " ", "|"), "i"))
}
}
?document dc:title ?title ;
dc:description ?description ;
dc:subject ?subject ;
dc:language ?language .
}
Note that this is still going to give you a row for each document-subject combination so if you have a document with 3 subjects you'll get three rows for that document. If you want to combine documents into a single row then you can use GROUP BY and then a GROUP_CONCAT aggregate, there are other questions already on Stack Overflow that detail how to do that.
Notes
Also note that using simple string concatenation to inject constants into your query is ill advised, take a look at Jena's ParameterizedSparqlString for a more user friendly and SPARQL injection proof API for building queries.

DBpedia query returns results with Virtuoso but not with Jena

I'm trying to create a SPARQL query with Jena to query DBpedia. The query is working when I use Virtuoso but when I plug it into the following Java code, it returns an empty set.
String sr="Christopher_Nolan";
String sparqlQueryString1 = "PREFIX dbont: <http://dbpedia.org/ontology/> "+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
" SELECT distinct ?s"+
" WHERE { "+
"?s foaf:name ?label ." +
"filter(?label=\"Christpher_Nolan\"#en)." +
" }";
Query query = QueryFactory.create(sparqlQueryString1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out,results, query);
qexec.close() ;
I don't think this is a problem with Jena, but with your particular query. Your query, when run on the DBpedia SPARQL endpoint, produces no results
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?s
WHERE {
?s foaf:name ?label .
filter(?label="Christpher_Nolan"#en)
}
SPARQL results (no results)
However, if you add an o to the name Christopher, and change the underscore to a space, you get three results:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?s
WHERE {
?s foaf:name ?label .
filter(?label="Christopher Nolan"#en)
}
SPARQL results (3 results)
s
http://dbpedia.org/resource/Christopher_Nolan_(author)
http://dbpedia.org/resource/Christopher_Nolan
http://dbpedia.org/resource/Chris_Nolan_(musician)
I'd also point out that this is a rather unusual use of filter. If you want to select triples that use a certain value, just put that value into the triple pattern:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?s
WHERE {
?s foaf:name "Christopher Nolan"#en
}
SPARQL results (same 3 results)

Why is prefix replaced to ns2 when forming a SPARQL Construct query

I want to replace a property in my SPARQL result set by a CONSTRUCT, and it basically works, except that the prefix gets automatically replaced by "ns2". Does anyone know why, and how it can be avoided?
Head of query
" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
" PREFIX ma: <http://www.w3.org/ns/ma-ont#> " +
" PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
" CONSTRUCT { ?subject ma:title ?label } " +
" WHERE { "+
" ?subject rdfs:label ?label. " +
Example result:
<http://dbpedia.org/resource/Japantown,_San_Francisco> ns2:title "Japantown, San Francisco"#en
The Issue
For what it's worth, it doesn't look like Jena is doing this, but rather (assuming that the presence of a DBpedia resource means that you're querying DBpedia) DBpedia's endpoint. Given this simple data:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix ma: <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco> rdfs:label "Japantown, San Francisco"#en .
and this query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ma: <http://www.w3.org/ns/ma-ont#>
CONSTRUCT { ?subject ma:title ?label }
WHERE {
?subject rdfs:label ?label
}
Jena's ARQ seems to preserve the prefix in both RDF/XML and TURTLE:
$ arq --data data.n3 --query construct.sparql --results RDF/XML
<rdf:RDF
xmlns:ma="http://www.w3.org/ns/ma-ont#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdf:Description rdf:about="http://dbpedia.org/resource/Japantown,_San_Francisco">
<ma:title xml:lang="en">Japantown, San Francisco</ma:title>
</rdf:Description>
</rdf:RDF>
$ arq --data data.n3 --query construct.sparql
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix ma: <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
ma:title "Japantown, San Francisco"#en .
However, running a very similar query (note the VALUES that will keep our results small) run on the DBpedia public SPARQL endpoint gets the auto-generated namespace prefixes that you've mentioned.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ma: <http://www.w3.org/ns/ma-ont#>
CONSTRUCT { ?subject ma:title ?label }
WHERE {
VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }
?subject rdfs:label ?label
}
SPARQL Results
#prefix ns0: <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco> ns0:title "Japantown, San Francisco"#en .
Note that these result are the same RDF graph; the prefix is just a handy way to make some output more readable. Any RDF-processing tool will see that they are exactly the same graph. Jena's not doing anything wrong (and in fact, Jena's is nice in that it preserves the prefix), but neither is the Virtuoso instance that's running on DBpedia.
Preserving the prefix
While the prefixes only affect the human readability of a serialization of a graph, the human readability of a serialization of a graph can be important. There are some ways to preserve the prefix even though DBpedia has gone and changed it under you.
Using Federated Queries (SERVICE)
Using ARQ on the command line (as shown above) with the data available locally kept the desired prefix in the constructed model. We can write a similar query that actually queries DBpedia remotely, but since it's ARQ's job to actually construct the graph the prefixes end up the way you'd expect. For instance:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ma: <http://www.w3.org/ns/ma-ont#>
CONSTRUCT { ?subject ma:title ?label }
WHERE {
VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }
SERVICE <http://dbpedia.org/sparql> {
?subject rdfs:label ?label
}
}
ARQ still requires a --data argument, so I created an empty file, empty-data.n3. The data here that we want here is actually coming from DBpedia.
$ arq --data empty-data.n3 --query construct-remote.sparql
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix ma: <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
ma:title "Japantown, San Francisco"#en .
Changing the Prefixes in a Jena Model (PrefixMapping)
Since a Jena Model is a PrefixMapping, you can change the prefixes that are used. Here's Java code that runs a remote query (without using the a federated query) and updates the prefixes afterward.
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.vocabulary.RDFS;
public class RemoteQueryPrefixChange {
final static String MA_NS = "http://www.w3.org/ns/ma-ont#";
final static String queryString = "" +
"PREFIX rdfs: <"+RDFS.getURI()+">\n" +
"PREFIX ma: <"+MA_NS+">\n" +
"\n" +
"CONSTRUCT { ?subject ma:title ?label }\n" +
"WHERE {\n" +
" VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }\n" +
" ?subject rdfs:label ?label\n" +
"}\n" +
"";
final static String DBPEDIA_SERVICE = "http://dbpedia.org/sparql";
public static void main(String[] args) {
Model results = QueryExecutionFactory.sparqlService( DBPEDIA_SERVICE, queryString ).execConstruct();
System.out.println( "== Original Prefixes ==" );
results.write( System.out, "TTL" );
System.out.println( "== Updated Prefixes ==" );
results.removeNsPrefix( results.getNsURIPrefix( MA_NS ));
results.setNsPrefix( "ma", MA_NS);
results.write( System.out, "TTL" );
}
}
The output is (notice the ns2 in the original serialization of the model, and ma in the updated serialization):
== Original Prefixes ==
#prefix ns2: <http://www.w3.org/ns/ma-ont#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
ns2:title "Japantown, San Francisco"#en .
== Updated Prefixes ==
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix ma: <http://www.w3.org/ns/ma-ont#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
ma:title "Japantown, San Francisco"#en .

How to get data type property values using SPARQL

I have created some sample ontology in protege.According to my ontology there is a class called person and which has sub class called Student.There are some student individuals(john,paul,marry,...).
I have defined some data property called "email" and assigned their email addresses.
Following query which is resulting all the individuals in ontology.But I want to get each individual and their email address.
String queryStr =
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
"select ?ind "+
"where { "+
"?ind rdf:type <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#Student> ;"+
"}\n ";
Above query was tested on jena in eclipse IDE.
any idea..?
Thank in advance!
To get the email addresses you need to 1) add a variable for them to the SELECT line, and 2) bind that variable in the WHERE pattern. And you'll probably want to add a prefix for your own ontology, since you'll now need to refer to it twice. Something like:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#>
SELECT ?ind ?email
WHERE {
?ind rdf:type my:Student .
?ind my:Email ?email
}
A more compact way would be the following:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#>
SELECT ?ind ?email
WHERE {
?ind rdf:type my:Student ;
my:Email ?email .
}