Extracting Values from rdflib.term.URIRef and rdflib.term.Literal - sparql

I am newbie to SPARQL queries and I am getting error when trying to extract the values from rdflib.term.URIRef and rdflib.term.Literal. So ?o contains both URI and Literals. Please find the below query.
SELECT * WHERE {
uo:0A ?p ?o.
BIND(IF(!isLITERAL(?o), (strafter(str(?o), '#') as ?o2), ?o) AS ?result)
}
I am getting error as ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery}, found 'B' (at char 117), (line:4, col:1).
Any help would be greatly appreciated.

Re-posting comment content as an answer so this can be accepted and the question closed:
Your BIND clause is broken, please check again the syntax of IF, currently you have as twice which obviously makes no sense as there is no as in any IF but only in the BIND.
AS is only usable for BIND(or in projection part of the query) to assign a value to a variable.

Related

BUG - SPARQL Describe query returns nothing in Anzograph

The SPARQL Describe query does not do anything in Anzograph 2.2.0. I have also double checked the documentation at https://docs.cambridgesemantics.com/anzograph/v2.2/userdoc/describe.htm and the simple example fails to return triples.
To reproduce, let's insert some data.
INSERT DATA {
graph <tickit> {
<person2> <lastname> "Humphrey" ;
<like> <musicals> .
}
}
Output:
Update Successful
SPARQL Select queries work as expected.
SELECT *
FROM <tickit>
WHERE {
?s ?p ?o .
} LIMIT 10
Output:
+-----------+------------+------------+
| <person2> | <lastname> | Humphrey |
| <person2> | <like> | <musicals> |
+-----------+------------+------------+
Now if I try to use a SPARQL Describe query, nothing happens.
DESCRIBE <person2>
FROM <tickit>
Output:
No records found.
I've also enabled the sparql_spec_default_graph in settings.conf. Therefore, I'd expect to get something by running the following SPARQL Describe query on the default graph.
DESCRIBE <person2>
Output:
No records found.
Am I missing something here?
It looks like you have hit a bug in the way the AnzoGraph web console processes the DESCRIBE query results. This has now been ticketed and will be addressed in an upcoming release - probably 2.2.1 (the next one). The AnzoGraph CLI does handle DESCRIBE correctly.
Many thanks indeed for the report!

How Do I Query Against Data.gov

I am trying to teach myself this weekend how to run API queries against a data source in this case data.gov. At first I thought I'd use a simple SQL variant, but it seems in this case I have to use SPARQL.
I've read through the documentation, downloaded Twinkle, and can't seem to quite get it to run. Here is an example of a query I'm running. I'm basically trying to find all gas stations that are null around Denver, CO.
PREFIX station: https://api.data.gov/nrel/alt-fuel-stations/v1/nearest.json?api_key=???location=Denver+CO
SELECT *
WHERE
{ ?x station:network ?network like "null"
}
Any help would be very much appreciated.
SPARQL is a graph pattern language for RDF triples. A query consists of a set of "basic graph patterns" described by triple patterns of the form <subject>, <predicate>, <object>. RDF defines the subject and predicate with URI's and the object is either a URI (object property) or literal (datatype or language-tagged property). Each triple pattern in a query must therefore have three entities.
Since we don't have any examples of your data, I'll provide a way to explore the data a bit. Let's assume your prefix is correctly defined, which I doubt - it will not be the REST API URL, but the URI of the entity itself. Then you can try the following:
PREFIX station: <http://api.data.gov/nrel...>
SELECT *
WHERE
{ ?s station:network ?network .
}
...setting the PREFIX to correctly represent the namespace for network. Then look at the binding for ?network and find out how they represent null. Let's say it is a string as you show. Then the query would look like:
PREFIX station: <http://api.data.gov/nrel...>
SELECT ?s
WHERE
{ ?s station:network "null" .
}
There is no like in SPARQL, but you could use a FILTER clause using regex or other string matching features of SPARQL.
And please, please, please google "SPARQL" and "RDF". There is lots of information about SPARQL, and the W3C's SPARQL 1.1 Query Language Recommendation is a comprehensive source with many good examples.

DBpedia get all cities in the world - missing a few

I use this sparql query to get as much cities as possible:
select * where {
?city rdf:type dbo:PopulatedPlace
}
However, some expected ones are missing e.g.
http://dbpedia.org/resource/Heidelberg
(neither that nor one of its wikiRedirects)
which is of a dbo:PopulatedPlace as this query returns true (in JSON):
ask {
:Heidelberg a dbo:PopulatedPlace
}
I need that list to be exhaustiv because later I will add constraints based on user input.
I use http://dbpedia.org/snorql/ to test the queries.
Any help is appreciated.
UPDATE:
One of the Devs told me the public endpoint is limited ( about 1K ).
I'll come up with a paginated solution and see if it contains the 'outlier'.
UPDATE2:
The outlier is definitly in the resultset of rdf:type dbo:Town.
Using dbo:PopulatedPlace yields too many results to check per hand, though.
The public endpoint limits results to about 1K. Pagination or use of a smaller subclass of dbo:PopulatedPlace yields the result.

SPARQL-Query results invalid?

I run a Virtuoso Server and missed a number of results when making a SPARQL-Select request. I tracked it down and find a really strange behaviour, that I cannot explain.
But to start from the beginning.
The endpoint I query can be found at http://creativeartefact.org/sparql
I) Check for a specific triple:
ASK WHERE {
<http://creativeartefact.org/mbrainzImport/f18e677a-4051-486a-aa64-d9a3bfef90af>
<http://creativeartefact.org/ontology/represents>
<http://creativeartefact.org/mbrainzImport/35ed9f2a-6ce4-44ca-9c7a-967377b0e007>. }
The query returns TRUE
II) Now getting a bit more unspecific:
ASK WHERE {
?s
<http://creativeartefact.org/ontology/represents>
<http://creativeartefact.org/mbrainzImport/35ed9f2a-6ce4-44ca-9c7a-967377b0e007>. }
If the first returns true, the second shall do as well, shouldn't it? But it doesn't. It return FALSE!
If I replace the predicate or the object with a variable, it returns true as expected. Only when setting a variable for the subject, it returns false.
That the data really exists in the triple store can be tested by running the query
SELECT * WHERE {
?s
?p
<http://creativeartefact.org/mbrainzImport/35ed9f2a-6ce4-44ca-9c7a-967377b0e007>. }
You will see, that both results comw with p = http://creativeartefact.org/ontology/represents - which is exactly the predicate I am asking for in the former query.
To make it even more strange, there ARE triples with the aforementioned format, that return the triples:
select * {
?s
<http://creativeartefact.org/ontology/represents>
<http://creativeartefact.org/mbrainzImport/e4003568-5645-4ee1-abd0-2e8156272e59>. }
Any idea, what is happening here?
Thanks in advance,
Frank
The Virtuoso being used is an original 07.00.3203 build from 2013.
I would suggest upgrading to the latest Virtuoso 07.10.3211, open source or commercial, depending on which is in use here, and see if the problem persists ...

SPARQL query: using VALUES inline data or UNION with bigdata store

I have a dataset that looks a bit like:
<item1> <isLocated> <someAddress>
<item2> <isLocated> <someAddress>
<item3> <isLocated> <someOtherAddress>
I want to be able to use SPARQL to answer the question:
"Which items will I find at someAddress or someOtherAddress?"
I could use a UNION like this:
SELECT ?item
{
{ ?item <isLocated> <someAddress> }
UNION { ?item <isLocated> <someOtherAddress }
}
But I think this will become pretty messy when I start talking about 100's or 1000's of addresses.
I think the VALUES inline data might be more suitable than a heap of UNION queries.
I've tried writing the following query but my RDF store/engine (bigdata) seems to choke on it:
SELECT ?item
{
?item <isLocated> ?loc .
}
VALUES (?loc) { (<someAddress>) (<someOtherAddress>) }
(based on http://www.w3.org/TR/sparql11-query/#inline-data)
The error I get from bigdata is:
Lexical error at line 5, column 7. Encountered: " " (32), after : "VALUES"
Am I forming this query correctly?
Is using a UNION or VALUES more appropriate?
It seems no matter how I format this query (based on the w3 examples in the link above) I get similar Lexical errors.
Any ideas?
Cheers.
At a guess, I'd say that Bigdata does not yet support the VALUES clause. This is a brand new feature introduced in the latest SPARQL working draft (published just weeks ago), so quite naturally several tools will not yet support it.
You could try using BINDINGS instead (this is roughly the same feature from previous working drafts, which was replaced).
You've mixed up the syntax a little. Either use:
VALUES ?loc { <someAddress> <someOtherAddress> }
which is a special form for a single variable, or:
VALUES (?loc) { ( <someAddress> ) ( <someOtherAddress> ) }
the general form.
You could also try IN
FILTER (?loc IN ( val1, val2, ...))