sparqltriplestoretriplesblazegraphknowledge-graph

How to get all related triples to a subject in SPARQL?


I would need to get all data related to a subject. I tried to query for

SELECT * WHERE {?s ?p ?o}

But the problem is that some of the ?o objects are URIs and I also need those connections, until the connections end. For example, I have:

http://www.example.com/data/subject-test http://www.example.com#hasNetListPrice http://www.example.com/data/price-subject-test.


http://www.example.com/data/price-subject-test http://www.example.com/si-cpq/data/price-currency http://www.example.com/data/EURO.

And so on, until the triples are no longer connected to the initial subject, http://www.example.com/si-cpq/data/subject-test.

Also, there are sometimes 4 triples connected, sometimes more, so I could not use the same pattern for all. Also, would need a general select query, not one that works only for the price triples, because the data has more attributes.


Solution

  • (This is probably only a partial answer yet, but might be improved by more information from comments/edits):

    First of all it might be necessary to specify which rdf:types you accept in the result. Also, you probably will have to add "layers of optional intermediate variables":

    
    # untested due to the lack of data
    
    prefix cpq: <http://www.example.com/si-cpq/data/>
    SELECT * WHERE
    {
        # defining admissible types (here: guessed type-names)
        VALUES ?t {cpq:Currency cpq:Foobar} 
    
        ?s ?p ?o1. # first relation
        {
          # direct connection between ?s and target type
          ?o1 rdf:type ?t.
         
        }
        UNION
        {
          # level 1 indirect connection between ?s and target type
          ?o1 ?p2 ?o2.
          ?o2 rdf:type ?t.
        }
        UNION
        {
          # level 2 indirect connection between ?s and target type
          ?o1 ?p2 ?o2.
          ?o2 ?p3 ?o3.
          ?o3 rdf:type ?t.
        }
    # add more levels and restrictions as needed
    
    }
    

    You might also have a look on property paths, but they are only applicable for fixed (i.e. not variable) properties.