1

I have a RDF structure that has:

:A a :Something
:A hasProp1 "123"
:A hasProp2 :B

:B a :Something
.....

And this can be of unknown length.

I'd like to get everything related to :A (depth unknown) using recursively. Is it possible to do this with SPARQL?

DRSK
  • 63
  • 7
  • 2
    The search function is your friend...as a hint, you can use property paths to achieve this `SELECT ?o {:A (:p|!:p)+ ?o } ` – UninformedUser Jan 25 '17 at 09:57
  • Possible duplicate of [SPARQL recursive ancestor query](http://stackoverflow.com/questions/41573900/sparql-recursive-ancestor-query) – Jeen Broekstra Jan 26 '17 at 02:09
  • Possible duplicate of [SPARQL property path queries with arbitrary properties](http://stackoverflow.com/questions/26698675/sparql-property-path-queries-with-arbitrary-properties), too. – Joshua Taylor Jan 26 '17 at 14:42
  • Let me get back with little more data; at this point I'm sure that Property paths by itself would cut it. – DRSK Jan 27 '17 at 08:24

1 Answers1

1

Consider the following extraction of the pizza ontology:

:Food rdf:type owl:Class ;

  rdfs:subClassOf :DomainConcept .

:Pizza rdf:type owl:Class ;
   rdfs:label "Pizza"@en ;
   rdfs:subClassOf :Food ,
                   [ rdf:type owl:Restriction ;
                     owl:onProperty :hasBase ;
                     owl:someValuesFrom :PizzaBase
                   ] ;

   owl:disjointWith :PizzaBase ,
                    :PizzaTopping .

:NamedPizza rdf:type owl:Class ;

        rdfs:label "PizzaComUmNome"@pt ;

        rdfs:subClassOf :Pizza ;

        rdfs:comment "A pizza that can be found on a pizza menu"@en .

:American rdf:type owl:Class ;

      rdfs:label "Americana"@pt ;

      rdfs:subClassOf :NamedPizza ,
                      [ rdf:type owl:Restriction ;
                        owl:onProperty :hasTopping ;
                        owl:someValuesFrom :PeperoniSausageTopping
                      ] ,
                      [ rdf:type owl:Restriction ;
                        owl:onProperty :hasTopping ;
                        owl:someValuesFrom :MozzarellaTopping
                      ] ,
                      [ rdf:type owl:Restriction ;
                        owl:onProperty :hasCountryOfOrigin ;
                        owl:hasValue :America
                      ] ,
                      [ rdf:type owl:Restriction ;
                        owl:onProperty :hasTopping ;
                        owl:allValuesFrom [ rdf:type owl:Class ;
                                            owl:unionOf ( :MozzarellaTopping
                                                          :PeperoniSausageTopping
                                                          :TomatoTopping
                                                        )
                                          ]
                      ] ,
                      [ rdf:type owl:Restriction ;
                        owl:onProperty :hasTopping ;
                        owl:someValuesFrom :TomatoTopping
                      ] ;

      owl:disjointWith :AmericanHot ,
                       :Cajun ,
                       :Capricciosa ,
                       :Caprina ,
                       :Fiorentina ,
                       :FourSeasons ,
                       :FruttiDiMare ,
                       :Giardiniera ,
                       :LaReine ,
                       :Margherita ,
                       :Mushroom ,
                       :Napoletana ,
                       :Parmense ,
                       :PolloAdAstra ,
                       :PrinceCarlo ,
                       :QuattroFormaggi ,
                       :Rosa ,
                       :Siciliana ,
                       :SloppyGiuseppe ,
                       :Soho ,
                       :UnclosedPizza ,
                       :Veneziana .

If you're asking how you can use SPARQL to retrieve not only the direct information about the American pizza, but all properties of any of its parent classes and anonymous classes (blank nodes), I believe you could use the following query to get the direct and indirect properties:

PREFIX pza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?p ?o ?p2 ?inferred WHERE {
   {pza:American ?p ?o . }
   UNION 
   {pza:American rdfs:subClassOf+ ?parent .
   ?parent ?p2 ?inferred .} 
   FILTER (!isBlank(?o)) .
}

Notice I've opted to filter out blank node direct objects ?o. This query tells me everything that is directly stated by the pza:American class as well as properties about its ancestral classes (namedPizza, Pizza, Food). This query doesn't preserve the path between the original subject and its related properties, however.

You can see the results of the query here.

You can run the query by going to http://demo.openlinksw.com/sparql/, and entering into the default graph field 'http://protege.stanford.edu/ontologies/pizza/pizza.owl', and copying the above query into the query box.

I hope this helps.

ejmoli
  • 145
  • 1
  • 9