简体   繁体   中英

sparql: how to avoid visited subject

and I am trying to write SPARQL query to find distinct object.

here is the dataset:

<https://permid.org/1-36436064275> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://permid.org/1-34414203048> .

<https://permid.org/1-34414203048> <http://permid.org/ontology/person/hasTenureInOrganization> <https://permid.org/1-36436064275> .

my sparql query is like this:

select distinct ?n where {

      <https://permid.org/1-36436064275> ?a ?b .
      ?b ?c ?d . 
      ?d ?e ?n .                     
}

From the dataset, "?d" is https://permid.org/1-36436064275 , which is the visited subject. I want to skip visited subject, so that "?d" is empty, thus "?n" is also empty.

There's a couple of things to address here:

From the dataset, "?d" is https://permid.org/1-36436064275 , which is the visited subject.

This happens because your RDF graph is a cycle. The first bit of your SPARQL query is:

<https://permid.org/1-36436064275> ?a ?b .

This binds ?a to rdf:type , and ?b to 1-34414203048 . The second part of your query pattern is:

?b ?c ?d .

Since ?b is already bound to 1-34414203048 by the previous pattern, there's only one option for ?c ( hasTenureInOrganization ) and ?d ( 1-36436064275 ). Then the third bit of your query is:

?d ?e ?n . 

?d is already bound by the previous pattern, so there's one option for ?e ( rdf:type again) and ?n ( 1-36436064275 again).

I want to skip visited subject, so that "?d" is empty, thus "?n" is also empty.

That's not how SPARQL works. SPARQL only returns a query result if the entire pattern has a result. If ?n has no value, the query will return an empty result.

Having said that, if you want to ensure that ?d is never equal to the subject that you started your query with, you could simply add a FILTER condition:

FILTER (?d != <https://permid.org/1-36436064275>)

But, like I said in the comments as well, I think you may need to rethink your data model a little bit, and consider the purpose of your query as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM