0

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

BigNoob
  • 17
  • 3

2 Answers2

1

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.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Thank you but this query will create a triples, not just selecting!! I just need to select these triples in result not to create them – BigNoob Dec 25 '13 at 23:46
  • @BigNoob It's not clear what you mean by "select triples" then. A SPARQL select query lets you get a set of results, each of which is a binding of a number of variables to resources. You can bind a subject, object, and predicate to variables, if you want, but you're still just binding variables, not "selecting triples". – Joshua Taylor Dec 26 '13 at 04:38
  • @BigNoob in your question you asked to "get triples". That is exactly what this query does. – Jeen Broekstra Dec 26 '13 at 21:10
  • @JeenBroekstra but i think Construct is different of select. Construct is for adding triples to triplestore and select is just to display results no? – BigNoob Dec 26 '13 at 23:45
  • 1
    No. CONSTRUCT is for retrieving triples from a triplestore. Not for adding them. SELECT also retrieves data from the store of course, but not in the form of triples (it retrieves sets of variable bindings instead). – Jeen Broekstra Dec 26 '13 at 23:46
  • @JeenBroekstra ahh ok Thank you for clarifying me. Well, i think the query that you have write is almost what i want but, in fact this query is special for the triples that o wrote (1,2,3,4) but what i need is a generic query for this kind of triples (related triples) I mean supposing that we have many other triples related to triple N°1 and N°3 what i have to do to get all triples related to the first one directly or indirectly related. – BigNoob Dec 27 '13 at 01:09
  • Then perhaps you should edit your question to indicate more clearly what it is you're after. – Jeen Broekstra Dec 27 '13 at 03:55
0

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 }
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353