Simple SPARQL query of foaf RDF not working - sparql

I'm new at semantic web, where I learn basics, but I'm stuck at the beginning.
After reading this link: http://www.cambridgesemantics.com/semantic-university/sparql-by-example, I try some examples by myself, but without success.
For example, using OpenLink Virtuoso SPARQL Query Editor http://demo.openlinksw.com/sparql, I put at Default Graph URI this http://njh.me/foaf.rdf and simple query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select ?name
where
{
?person foaf:name ?name .
}
to get name of a person who is owner of this RDF file. Also, if I put following query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select ?name
where
{
?OnlineAcount foaf:name ?name .
}
nothing is happening.
Where I make mistake?
The same situation is with https://ai.wu.ac.at/~polleres/foaf.rdf, http://richard.cyganiak.de/foaf.rdf for the first query.
Any help at the beginning of my semantic tour will be appreciated :)
Thanks in advance.

Below the query input pane, there's an option for Sponging. You need to select the option Retrieve remote RDF data for all missing source graphs. Then you'll get results. However, the file that you mentioned actually has lots of foaf:name triples, not just one for the creator of the file. You might try select ?person ?name to see what else is in that data.

Related

How to filter results by city?

I am using DBpedia to retrieve data on specific towns' POIs, for instance Barcelona. I've tried to find appropriate answers to my problems on Stack Overflow but almost all the answers provided did not work out for me, and given that I am new in SPARQL, I couldn't manage to figure out whether or not it was the syntax that was at fault or if there was something else. I am using the DBpedia SPARQL Endpoint available at the following address : http://dbpedia.org/sparql
I've tried to use some things such as ?citylabel or ?location, and to either use the FILTER command or directly fill a value for these classes. I've also tried a few other things, without satisfying results (and most of the times syntax errors that I could not resolve). These solutions have been, in most cases, applied and seemed to work just fine for people, so I do not understand what's going on.
BASE <http://www.dbpedia.org/resource/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia3: <http://dbpedia.org/ontology/>
SELECT (SAMPLE(?label) as ?activity_name)
(SAMPLE(?latitude) as ?activity_lat)
(SAMPLE(?longitude) as ?activity_lon)
(SAMPLE(?homepage) as ?URL)
(SAMPLE(?type) as ?activity_type)
(SAMPLE(?abstract) as ?descriptor)
WHERE
{ ?Museum a dbo:Museum ;
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:type ?type ;
geo:lat ?latitude ;
geo:long ?longitude ;
foaf:homepage ?homepage .
}
GROUP BY ?Museum ?label
The results of this query are, I think, pretty much any museum that is known by DBpedia and categorized as such. What I'd like to have is a list of museums within Barcelona. Can somebody give me an rather in-depth answer so I can understand how it is working ? Thanks, in advance.

Sparql: getting all politicians who ruled a city

I'm new to sparql and I'm trying to understand how to get the resources I need for building a query. I started trying to get all the politicians that ruled a city or a country, and at the moment I could do just the following:
I started by following the links in snorql (in the prefixes) and looking for an entity by adding "politician" at the end. I found one :
PREFIX : <http://dbpedia.org/resource/>
So I wrote http://dbpedia.org/resource/Politician and the resource does exist. I tryed to use it in this way:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?thing WHERE {
?thing a :Politician .
?thing dbo:birthPlace dbpedia:Italy.
}
LIMIT 50
Run in virtuoso.
Even if I remove the second line of the SELECT, I have no results. But if I change the first line with: ?thing a dbo:Person. or even if I remove it, I get the people born in Italy. But not just the politicians. A second problem is I don't need the politicians that were born but ruled that place. How or where can I find that kind of "relations/descriptors"? Now I am just googling and copy-pasting some existing examples, but I would like to understand how to look for more specific things.
Thanks in advance
Your first query isn't working because Politician is not part of the default (:) namespace, but instead it is present in DBpedia Ontology namespace (dbo).
So, your query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?thing WHERE {
?thing a dbo:Politician .
?thing dbo:birthPlace dbpedia:Italy.
}
LIMIT 50
To list all politician who ruled Italy you would need to know which is the predicate for "ruled". Once you have it you can construct a query.
To list all predicates present in the database you can write something like this
SELECT DISTINCT(?b) WHERE {
?a ?b ?c.
}
And it will list all predicates.
I would recommend you to browse through one or two politician and see the predicates they have to check if one works for you.

How to create RDF model from DBPedia data using SPARQL

I am a new one on semantic web. I would like to get all object/values for Microsoft from DBPedia using SPARQL query and save result in RDF format. I have made a query on http://dbpedia.org/sparql which works well and returns all pair/values regarding Microsoft.The code is as follows:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select * where
{{ <http://dbpedia.org/resource/Microsoft> ?property ?value }
UNION
{?property ?value <http://dbpedia.org/resource/Microsoft>}}
What I want is to create RDF format for the results. I read tutorial on https://www.w3.org/TR/rdf-sparql-query/#construct and understood it can be done by using CONSTRUCT query. I changed SELECT to CONSTRUCT, but that did not work. If possible could you tell me what is my mistake and how can I apply CONSTRUCT to my query to get RDF model from the query please? Thanks in advance!
In order to get more clear distinction of the actual triples you retrieve, I'd suggest changing the variables in the following way:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select * where
{{ <http://dbpedia.org/resource/Microsoft> ?property ?value }
UNION
{?subject ?property <http://dbpedia.org/resource/Microsoft>}}
And regarding the result format, just choose "Turtle" or "RDF/XML", instead of "HTML" from the results menu of the SPARQL interface.

Fundamental understanding of SPARQL

I try to understand SPARQL and mess around with the SPARQL Tool that is provided by dbpedia. I've read the w3 documentation and now I want to create my very own query. I would like to find the names of all books in dbpedia written by J. J. R. Tolkien.
Therefore I "designed" this query:
SELECT ?name WHERE { ?name ?author "J._R._R._Tolkien".
?name ?mediaType "Print"}
The result is empty, but I would at least expect this book popping up:
http://dbpedia.org/page/The_Lord_of_the_Rings
Can someone tell me, what my conceptual mistake is?
I good approach would be to review his DBpedia page and then choose the desired properties. In your case two good candidates are notableWork and author. Here's a query with both of them, effectively using the latter.
PREFIX : <http://dbpedia.org/resource/>
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
SELECT DISTINCT ?is_author_of #?has_notable_work
FROM <http://dbpedia.org/>
WHERE {
:J._R._R._Tolkien rdf:type o:Writer ;
#o:notableWork ?has_notable_work ;
^p:author ?is_author_of .
}
I have used rdf:type o:Writer to reduce possible ambiguity (none, in case of using URI of an individual resource in dbpedia), and ^ to get the right direction. My preference for ?is_author_of and ?has_notable_workinstead of e.g. ?book and ?popular_book was because I'm not sure what kind of work is he author of.

Query on sindice SPARQL endpoint

I tried to make this query on http://sparql.sindice.com/
PREFIX rev: <http://purl.org/stuff/rev#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
WHERE
{
?thing rdfs:label ?name .
?thing rev:hasReview ?review .
filter regex(str(?name), "harlem", "i")
} LIMIT 10
And it returns 504 Gateway Time-out
The server didn't respond in time.
What i'm doing wrong?
Thanks.
You made a query that was too hard for the endpoint to answer in a timely fashion hence why you got a timeout response. Note that there website states the following:
all queries are time and resource limited. notice that this means that
sometime you will get incomplete or even no results. If this is
happening often for you or you really want to run more complex queries
please contact us
Your query essentially selects a vast swathe of data and then makes the engine run a regular expression over ever possible value which is extremely slow.
I believe Sindice use Virtuoso as their SPARQL implementation so you can cheat and use Virtuoso specific full text query extension like so:
PREFIX rev: <http://purl.org/stuff/rev#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
WHERE
{
?thing rdfs:label ?name .
?thing rev:hasReview ?review .
?name bif:contains "harlem" .
}
LIMIT 10
However this query also seems to timeout, if you can add more conditions to constrain your query further you will have more chance of getting results in a timely fashion.