Reputation: 63
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?
Upvotes: 1
Views: 1446
Reputation: 165
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.
Upvotes: 1