SPARQL Path between two nodes - sparql

Given a graph:
#prefix da: <http://example.com/data/> .
#prefix on: <http://example.com/on/> .
da:Shenaz on:husband da:Javed .
da:Rita on:friend da:Noor ;
on:sister da:Tom .
da:Noor on:sister da:Shenaz .
da:Javed on:child da:Jaabir .
da:Tom on:sister da:James .
da:Jaabir on:grandFather da:Rafick .
There is a path between da:Noor and da:James which is da:Noor ^on:friend/on:sister/on:sister da:James . but the following query is returning false
PREFIX da: <http://example.com/data/>
PREFIX on: <http://example.com/on/>
ASK {
da:Noor ((<>|!<>)|^(<>|!<>))* da:James .
}
It is a possible bug in Jena, with RDFLib in Python, True is returned

For some reason the property path is not evaluated as expected. I tried it with the more simple query:
PREFIX : <http://ex.org/>
PREFIX da: <http://example.com/data/>
SELECT ?u
WHERE
{ da:Noor ^(:p1|!:p1) ?u }
The algebra looks ok, i.e. the path is reversed:
(project (?u)
(path ?u (alt <http://ex.org/p1> (notoneof <http://ex.org/p1>)) <http://example.com/data/Noor>))
Looks like a bug but I might be wrong indeed. I'll ask on the Jena mailing list and later on post the answer here.
Update:
The problem is with negations when the object itself is grounded - which is the case here due to the reverse operator ^. As per #AndyS' comment, this bug will be fixed in Apache Jena 3.3.0. See JENA-1317

Related

area not calculated in graphdb

I need to calculate Area but there is an error Query evaluation error: Unknown function 'http://www.opengis.net/def/function/geosparql/area' (HTTP status 500)
By following query in the image
But it works in stardog, see image please
Thanks for your reply in advance
geof:area does not appear to be a part of the core GeoSPARQL specification, so Stardog must have created an extension on it. GraphDB does not have the exact same extensions. Instead, it uses the uSeekM function and its prefix, http://rdf.useekm.com/ext#
So, your query would look something like this:
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX geof:<http://www.opengis.net/def/function/geosparql/>
PREFIX unit: <http://qudt.org/vocab/unit#>
PREFIX ext: <http://rdf.useekm.com/ext#>
SELECT ?a ?area
WHERE {
?a geo:hasGeometry ?g1 .
?g1 geo:asWKT ?lit
BIND(ext:area(?lit) as ?area).
}

A simple SPIN rule doesn't work in RDF4J

I've just started using the triple store RDF4J (I am using its workbench, version 2.3.1, run on Windows 10 with Tomcat 9.0)
I want to use the SPIN rules in RDF4J. Therefore, I created a new repository (In memory with RDFS+SPIN support).
I wanted to start with the SPIN example in RDF4J documentation concerning how to add SPIN rules. That is, I added the data (in Turtle, and imported to RDF4J)
#prefix ex: <http://example.org/>.
ex:John a ex:Father ;
ex:parentOf ex:Lucy .
ex:Lucy a ex:Person .
And the rule:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
#prefix sp: <http://spinrdf.org/sp#>.
#prefix spin: <http://spinrdf.org/spin#>.
#prefix ex: <http://example.org/>.
ex:Person a rdfs:Class ;
spin:rule [
a sp:Construct ;
sp:text """PREFIX ex: <http://example.org/>
CONSTRUCT { ?this ex:childOf ?parent . }
WHERE { ?parent ex:parentOf ?this . }"""
] .
And as instructed in the documentation, I exposed the query (with the checkbox 'Include inferred statements' checked),
PREFIX ex: <http://example.org/>
SELECT ?child
WHERE { ?child ex:childOf ?parent }
However, no result returned:
Could someone, please tell me am I doing something wrong, why the SPIN rule doesn't work in my RDF4J workbench, have I missed something?
(reposting my comment as an answer for future readers)
The SPIN reasoner currently assumes that all data is in the default context, I think. Make sure that your data was not added to a named graph.

How to get composer of a symphony in dbpedia?

This is my query
select *
{
?symphonies_by_composer <http://www.w3.org/2004/02/skos/core#broader> <http://dbpedia.org/resource/Category:Symphonies_by_composer> .
?symphony <http://purl.org/dc/terms/subject> ?symphonies_by_composer .
}
I run it over Dbpedia end point http://dbpedia.org/sparql/
it gives me many symphonies. i want to construct my triples, adding my own property, which is mo:composedBy like this:
PREFIX mo: <http:blablabla.com/mo#>
construct
{
?symphony mo:composedBy ?composer .
?symphony a mo:Symphony
}
{
?symphonies_by_composer <http://www.w3.org/2004/02/skos/core#broader> <http://dbpedia.org/resource/Category:Symphonies_by_composer> .
?symphony <http://purl.org/dc/terms/subject> ?symphonies_by_composer .
}
but i don't know how to get the binding for the ?composer variable.
Do you know how ?
(I'm aware that there might be no way to get it, if you think there is no way, kindly just let me know and i will pass, unfortunately, those data)
There seems to be no explicit relation in DBPedia connecting these symphonies to an actual resource that represents the composer.
A possible workaround is to extract the name of the composer from the prefLabel of the category, by snipping off the first bit ("Symphonies by"):
PREFIX mo: <http://example.com/mo#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
CONSTRUCT
{
?symphony mo:composedBy ?composer_name .
?symphony a mo:Symphony
}
WHERE
{
?symphonies_by_composer skos:broader <http://dbpedia.org/resource/Category:Symphonies_by_composer> ;
skos:prefLabel ?label .
?symphony dct:subject ?symphonies_by_composer .
BIND(SUBSTR(STR(?label), (STRLEN("Symphonies by ") + 1)) AS ?composer_name)
}
This will give you back the name of each composer as a literal value.
A second possible step is to try and reconstruct the actual IRI of the resource identifying the composer, from the name. For example, in the case of "Hans Werner Henze", the actual resource identifying the person is http://dbpedia.org/resource/Hans_Werner_Henze, so a simple further string operations or two, replacing spaces and concatenating with the dbpedia base IRI, will resolve this. However, this is brittle, as there is no guarantee that the resource exists, and even if it does, whether it actually identifies the composer (there might be more than one Hans Werner Henze, for instance).
Of course, you can expand this further by doing followup queries to verify that the resource exists and is the correct one, but it will require some additional trial and error. If the goal is simply the name of the composer, the first example query should work fine for most instances.

Stardog rule doesn't trigger

I'm having trouble writing a correct Stardog rule. As I haven't found a way to validate the syntax of the rule, I don't now if it's a syntax or a logical error. Eitherway, the rule doesn't seem to be triggered when reasoning is enabled (reasoning=SL in version 2, reasoning=true in version 3).
#prefix : <http://www.example.org/rules#> .
#prefix rule: <tag:stardog:api:rule:> .
#base <http://www.example.org/rules#> .
[] a rule:SPARQLRule ;
rule:content """
PREFIX : <http://www.example.org/rules#>
PREFIX draft: <http://our/prefix#>
IF {
?x a draft:Obs; draft:has_code ?code .
?z a <http://www.w3.org/ns/sparql#UUID> . // OR: BIND (UUID() AS ?z)
}
THEN {
?z a draft:Code .
?x draft:has_code ?z .
}
""" .
I'm trying to trigger the rule with the following SPARQL query:
PREFIX : <http://our/prefix>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?code
FROM <tag:stardog:api:context:default>
FROM <http://our/graph>
WHERE {
?s rdf:type :Obs .
?obs :has_code ?code .
}
This is likely due to the cyclic nature of the rule. You're inferring :has_code which will in turn be used to fire the rule again, and again, and so forth.
This is a bit easier to visualize when you consider how rules with more than one atom in the header a broken up.

add RDFS inference rules support in endpoint SPARQL

I have create an endpoint SPAQL on OpenLink Virtuoso.
All work well, but i have to access on the data in a Container, in particular a rdf:Seq.
I have a Seq like this:
<myrdf:has_stoptimes>
<rdf:Seq rdf:about="http://test.com/343">
<rdf:li>
<myrdf:StopTime rdf:about="http://test.com/StopTime/434">
...
</ns0:StopTime>
</rdf:li>
<rdf:li>
<myrdf:StopTime rdf:about="http://test.com/StopTime/435">
...
</ns0:StopTime>
</rdf:li>
</rdf:Seq>
Now i see that to access data in a container i can use rdfs:member or FILTER (strstarts(str(?prop), str(rdf:_)) how is explained here
But for my project i have to adopt the first solution because i'm working with Silk and i will use the code syntax like ?a/myrdf:has_stoptimes/rdfs:member without use of "complex" filter.
I have tried to follow this guide but querying the endpoint nothing work how i hoped.
So my question is: how can i query ?a/myrdf:has_stoptimes/rdfs:member on a Virtuoso endpoint SPARQL?Which inference rule i have to add in endpoint SPARQL?
Thank you in advance
UPDATE
I have created the following inference rules in Virtuoso:
ttlp (' #prefix rdfs: .
#prefix rdf: .
rdfs:Container rdf:type rdfs:Class ; rdfs:subClassOf rdfs:Resource .
rdfs:ContainerMembershipProperty a rdfs:Class ; rdfs:subClassOf rdf:Property .
rdf:Seq rdf:type rdfs:Class ; rdfs:subClassOf rdfs:Container .
rdfs:member rdf:type rdf:Property ; rdfs:domain rdfs:Resource ; rdfs:range rdfs:Resource .
', '', 'http://localhost:8890/schema/test') ;
Nothing work querying the SPARQL endpoint like:
define input:inference "http://localhost:8890/schema/property_rules1"
SELECT *
FROM
WHERE {?sep a rdf:Seq.
?seq rdfs:member ?p}
After i tried adding the follow line to the ttl file: rdf:_1 rdfs:subPropertyOf rdfs:member . In this way it work but obviously the results are only for the first element of the container. So is very unconvenient add a line for all of rdf:_n, and i think this is only a temporary solution, it is not correct.
I have tried to add an RDF dump on SILK 2.6.1, and on the section SPARQL of the data source if i run the query:
SELECT *
FROM
WHERE {?sep a rdf:Seq.
?seq rdfs:member ?p}
I obtain the correct result, without specify any inference rules. So i think that in this functionality of SILK there is something that i’m missing in my endpoint SPARQL or am i saying nonsense things?
You can't use variables in property paths, so you can't actually do
?x ?a/has_stoptimes/rdfs:member ?y
Instead, you have to use another variable or blank node in between:
?x ?a ?z . ?z has_stoptimes/rdfs:member ?y
?x ?a [ has_stoptimes/rdfs:member ?y ] .