Difference in days between two dates in GraphDB - sparql

I need some help to calculate the difference between two dates with SPARQL in Ontotext GraphDB. I know that SPARQL protocol does not support arithmetic operation on dates, however some SPARQL engines support it.
Just as an example in Fuseki I could do .
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?duration
WHERE {
BIND (("2011-02-04T14:45:13.815-05:00"^^xsd:dateTime - "2011-02-
02T14:45:13.815-05:00"^^xsd:dateTime) AS ?duration)
}
the result is duration: "P2DT0H0M0.000S"^^xsd:duration. Then I can get 2 days diff, or Virtuoso provides a built-in function bif:datediff.
My question is, if is there something similar on GraphDB to solve this easy problem without a big workaround.
Thanks in advance.

That was solved in 8.7.0 (released 28 September):
GDB-2887 As a GraphDB user I need support of “+” / “-” operations between xsd:dateTime and xsd:duration with SPARQL
Now your query should return "P2DT0H0M0.000S"^^xsd:dateTimeDuration.
Alternatively, one could use federated queries to public endpoints based on Virtuoso, Blazegraph, Fuseki etc.:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bif: <bif:>
SELECT ?duration1 ?duration2 ?duration3 ?duration4 {
VALUES (?start ?end)
{("2011-02-02T14:45:14"^^xsd:dateTime "2011-02-04T14:45:13"^^xsd:dateTime)}
SERVICE <http://dbpedia.org/sparql> #-- Virtuoso
{ BIND ((?end - ?start)/86400.0 AS ?duration1) }
SERVICE <http://dbpedia.org/sparql> #-- Virtuoso
{ BIND (bif:datediff("day", ?start, ?end) AS ?duration2) }
SERVICE <https://query.wikidata.org/sparql> #-- Blazegraph
{ BIND ((?end - ?start) AS ?duration3) }
SERVICE <http://zbw.eu/beta/sparql/stw/query> #-- Fuseki
{ BIND (day(?end - ?start) AS ?duration4) }
}
Try on FactForge!

Related

A problem with querying a graph with SPARQL on Bioportal

I am querying an ontology on the Bioportal endpoint. The ontology (NIF) is stored as a graph, so I put it in the FROM clause as the endpoint instructed.
SELECT DISTINCT ?p
FROM <http://bioportal.bioontology.org/ontologies/NIF>
WHERE{
?p a rdf:Property
}
limit 100
However, as can be seen below, the results came back showing few properties related to NIF and others to a different ontology called SKOS (Simple Knowledge Organization System).
In the Bioportal documentation it is said it maps some properties to SKOS properties, so I thought maybe the results are fine.
However, I had to test if I am querying the correct graph. So I used the below code to count the number of nodes since I know the NIF has around 3.6 million triples!
SELECT (count (*) as ?nodes)
FROM <http://bioportal.bioontology.org/ontologies/NIF>
WHERE{
?s ?p ?o
}
This resulted in 7984 nodes with and without the FROM clause! So I guessed I should be using the "count" incorrectly!
So I wonder how I should make sure that I am just querying the NIF ontology. Also, how to count its nodes?
Thanks :)
Try using SERVICE keyword.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT (count (*) as ?nodes)
WHERE
{
SERVICE <http://bioportal.bioontology.org/ontologies/NIF>
{
?s ?p ?o
}
}
If this fails, possibly the service you are connecting is not correct or up.
Try below example which connects to DBpedia:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT (count (*) as ?nodes)
WHERE
{
SERVICE <http://DBpedia.org/sparql>
{
?s ?p ?o
}
}
By the way I can;t access URL http://bioportal.bioontology.org/ontologies/NIF. Seems to be unavailable or down.

How to reference different repositories in a SPARQL query (federated)?

I would like to know what are the triples that are in a repository but that are not included in other repository.
But for doing this, I would need to reference the two repositories in a federated query.
I'm using Allegrograph.
Example on FactForge (i. e., using SPARQL 1.1 federated queries, not Allegrograph's federated stores):
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
CONSTRUCT { ?s ?p ?o }
{
VALUES (?s) {(dbr:Yekaterinburg)}
?s ?p ?o.
MINUS
{
SERVICE <http://dbpedia.org/sparql>
{
VALUES (?s) {(dbr:Yekaterinburg)}
?s ?p ?o
}
}
}
Obviously, one can't subtract DBpedia from DBpedia Live in this way, at least one resultset should be relatively small. It seems that Allegrograph's federated stores couldn't help in such substraction task. Probably you should divide your triples into named graphs, not into separate stores.
With AllegroGraph there are 2 ways this can be achieved:
(1) AllegroGraph allows you to federate multiple local and/or remote triple stores into a single virtual store which can then be queried as a single triple store.
(2) You can use SPARQL 1.1 federated queries which use the SERVICE keyword. Here is an example from the Learning SPARQL book:
PREFIX gp: <http://wifo5-04.informatik.uni-mannheim.de/gutendata/resource/people/>
SELECT ?dbpProperty ?dbpValue ?gutenProperty ?gutenValue
WHERE
{
SERVICE <http://DBpedia.org/sparql>
{
<http://dbpedia.org/resource/Joseph_Hocking> ?dbpProperty ?dbpValue .
}
SERVICE <http://wifo5-04.informatik.uni-mannheim.de/gutendata/sparql>
{
gp:Hocking_Joseph ?gutenProperty ?gutenValue .
}
}

SPARQL Apache Jena - Execution Limit on queries in DBpedia?

I'm dealing with a problem that I can't execute the same query (see Example below) multiple times against DBpedia, because the execution freezes after like 4-5 queries.
The thing is, with the same Apache Jena Code, I can execute hundreds of same queries against Wikidata (see Example below) without a problem. The Apache Jena Code is literally standard code (see below). The result is just returned as a normal string value. I also tried variations here, but that's not the issue.
Apache Jena Code Snippet from the DBpedia query (Wikidata is basically the same except the createServiceRequest-Method uses https://query.wikidata.org/sparql)
Query query = queryFactory.create(**!!see Query Example below!!**);
QueryEngineHTTP queryEngine = QueryExecutionFactory.createServiceRequest("http://de.dbpedia.org/sparql",
query);
ResultSet results = queryEngine.execSelect();
for (; results.hasNext();) {
QuerySolution solution = results.nextSolution();
String stringElement = solution.getLiteral("item").toString();
stringArray.add(stringElement);
// stringArray is then returned simply with System.out.println
}
DBpedia Query:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dbpprop-de: <http://de.dbpedia.org/property/>
prefix dbpedia-de: <http://de.dbpedia.org/resource/>
SELECT DISTINCT ?item WHERE {
dbpedia-de:Deutschland dbpprop-de:hauptstadt ?y .
?y rdfs:label ?item
}
Wikidata Query:
prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
prefix wikibase: <http://wikiba.se/ontology#>
prefix bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?itemLabel WHERE {
wd:Q183 wdt:P36 ?item .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "de" .
}
}
Is there some kind of Execution Limit on queries for users in DBpedia? I know there are limits on ResultSets, but that's not the issue here as i only get like one result per one query back. It would be really helpful as i couldn't find a similar problem. Thanks in advance.

SPARQL handling of DBPedia city redirects

This SPARQL statement works:
PREFIX s: <http://dbpedia.org/resource/Del_Mar,_California>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT * WHERE {
OPTIONAL { s: dbp:officialName ?officialName . }
OPTIONAL { s: dbp:name ?foaf_name . }
#-- ... [ about 20 more lines like the above ] ...
} LIMIT 2;
But some cities have redirect. For instance, Pacific_Beach,_California redirects to Pacific_Beach,_San_Diego. How can I handle this?
I've read Joshua Taylor's answer to Retrieving dbpedia-owl:type value of resource with dbpedia-owl:wikiPageRedirect value?, and I wonder if my s: prefix is messing me up here? I can't seem to implement his solution of:
select ?type where {
dbpedia:Cupertino dbpedia-owl:wikiPageRedirects*/dbpedia-owl:type ?type
}
Some other resources suggest using a union:
sparql using wikiPageRedirects
Using SPARQL to find the right DBpedia URI
Reading the Wikibooks chapter, XQuery/DBpedia with SPARQL - Football teams leaves me even more confused.
I am trying to learn this stuff, but I do have an immediate need to "just make it work". A solution and some links to read would be appreciated!
Pacific Beach, San Diego doesn't redirect to anything, but it is the redirect target of a number of resources:
is dbo:wikiPageRedirects of
dbr:Pacific_Beach,_CA
dbr:Pacific_Beach,_California
dbr:Pacific_Beach,_San_Diego,_CA
dbr:Pacific_Beach,_San_Diego,_California
dbr:Pacific_Beach,_San_Diego_California
That means that a modified version of your query should work just fine:
PREFIX s: <http://dbpedia.org/resource/Pacific_Beach,_San_Diego>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT * WHERE {
OPTIONAL { s: dbp:officialName ?officialName . }
OPTIONAL { s: dbp:name ?foaf_name . }
#-- ... [ about 20 more lines like the above ] ...
} LIMIT 2
SPARQL results
Note that using prefix to abbreviate a single IRI is sort of unusual. It would be more common to use a values block to bind a specific variable, that you can use in the rest of your query. That is, I would write that query as:
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT * WHERE {
values ?s { <http://dbpedia.org/resource/Pacific_Beach,_San_Diego> }
OPTIONAL { ?s dbp:officialName ?officialName . }
OPTIONAL { ?s dbp:name ?foaf_name . }
#-- ... [ about 20 more lines like the above ] ...
} LIMIT 2;
That has the advantage that if you want to search values for additional cities, you can just add them to the list of values. Now, if you want to add a value that redirects to something else, that's not a problem. You'd just want to add a non-optional pattern that helps you follow to the redirect targets:
SELECT * WHERE {
values ?x { <http://dbpedia.org/resource/Pacific_Beach,_California> }
?x dbo:wikiPageRedirects* ?s .
OPTIONAL { ?s dbp:officialName ?officialName . }
OPTIONAL { ?s dbp:name ?foaf_name . }
#-- ... [ about 20 more lines like the above ] ...
} LIMIT 2
SPARQL results
As a final note, do be careful that if you're copying and pasting URIs from the browser that you make sure to use the right URI in the query. When you visit a DBpedia resource, e.g., http://dbpedia.org/resource/foo in the browser, you're redirected to the human readable form http://dbpedia.org/page/foo in the browser. That's just a quirk of DBpedia. (Really, they should use content-type negotiation and just return an HTML document for the former when a web browser is making the request.)
I'd suggest taking another look at "this answer from Joshua Taylor". One of the suggestions is to use SPARQL to inspect the data. So try this query:
SELECT * WHERE {
<http://dbpedia.org/resource/Pacific_Beach,_California> ?p ?o
}
This give you all of the data available for the URI <http://dbpedia.org/resource/Pacific_Beach,_San_Diego>. Note that in this case it does not include a dbpedia-owl:wikiPageRedirects property. It appears that the redirect is internal to the DBPedia browser and not included in the DBPedia data.
Using the aforementioned query will help you find what is actually in the data (nothing very interesting in this case).

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.