sparql query to get a triplet related to other triples

720 views Asked by At

Supposing we have many RDF triples related as:

1: <S,P1,O>        

2: <O,P2,O3>       
3: <O,P3,O4>  

4: <O3,P4,O5> 

I would like to get triple N° 2, 3 and 4 by just knowing triple number 1 because triple N° 2 and 3 share "O" with triple N°1 and triple N° 4 share "O3" with triple N°2 which share "O" with triple N°1 Is it possible to formulate that with sparql query without knowung P2,P3,P4?

Thank you in advance

2

There are 2 answers

7
Jeen Broekstra On

Sure. This query will do that just that:

  CONSTRUCT {
     ?s ?p ?o . 
     ?o ?q ?z. 
     ?w ?r ?o .
  }
  WHERE {
        ?s ?p ?o . 
        FILTER (?s = :O || ?o = :O) 
        OPTIONAL { ?o ?q ?z . }
        OPTIONAL { ?w ?r ?o . }
   }

It's unlikely to be very efficient though. However without knowing more about your actual data or why you want this particular result, it's difficult to come up with a better solution.

2
Joshua Taylor On

It sounds like you know :O and you're asking for triples that have it as the subject, and triples that have the object of those triples as subjects. Are you asking for something like this, then?

select * where {
  :O ?p2 ?o2 .
  optional { ?o2 ?p3 ?o3 }
}