SPARQL recursive query for a combination of triples

I have the following query that I run recursively in Python using ontospy:

SELECT ?c WHERE {
    ?c rdfs:subClassOf ?restriction .
    ?restriction owl:onProperty :has_part ; owl:someValuesFrom ?p .
    VALUES ?p { <some_uri> }
}

Basically, I take the values ​​returned from this and rerun the query to follow the hierarchy of the "there is a part" relationships within the ontology. I hope to avoid several SPARQL queries by adding recursion to the query itself. I know how to do this for one triple using rdfs:subClassOf*, but cannot understand the syntax for combining two triples:

?c rdfs:subClassOf ?restriction .
?restriction owl:onProperty :has_part ; owl:someValuesFrom ?p .

Is it possible?

+4
source share
1 answer

, . , (1, 2).

(, ) , -, FILTER NOT EXISTS, .

, . , SELECT CONSTRUCT. , SPARQL .


, Ontospy ,

Ontospy , .

(ontology.ttl)

@prefix : <http://www.example.org/ontology#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.example.org/ontology> .

<http://www.example.org/ontology> rdf:type owl:Ontology .

:hasPart rdf:type owl:ObjectProperty .

:Country rdf:type owl:Class ;
         rdfs:subClassOf [ rdf:type owl:Restriction ;
                           owl:onProperty :hasPart ;
                           owl:someValuesFrom :State
                         ] .

:State rdf:type owl:Class ;
       rdfs:subClassOf [ rdf:type owl:Restriction ;
                         owl:onProperty :hasPart ;
                         owl:someValuesFrom :City
                       ] .

:City rdf:type owl:Class .

Python

import rdflib

g = rdflib.Graph()
g.parse("ontology.ttl", format="n3")

qres = g.update(
    """PREFIX : <http://www.example.org/ontology#> 
       INSERT { ?c :hasSome ?p } 
       WHERE  { ?c rdfs:subClassOf [ owl:onProperty :hasPart ; 
                                     owl:someValuesFrom ?p ] }""")

qres = g.query(
    """PREFIX : <http://www.example.org/ontology#> 
       SELECT ?a ?b WHERE {?a :hasSome+ ?b }""")

for row in qres:
    print("%s :hasSome+ %s" % row)

qres = g.update(
    """PREFIX : <http://www.example.org/ontology#> 
       DELETE { ?s :hasSome ?o } WHERE { ?s :hasSome ?o }""")

:Country :hasSome+ :State
:State   :hasSome+ :City
:Country :hasSome+ :City

RDFLib, :

import rdflib

g1 = rdflib.Graph()
g1.parse("ontology.ttl", format="n3")

qres = g1.query(
    """PREFIX : <http://www.example.org/ontology#> 
       CONSTRUCT {?c :hasSome ?p } WHERE {
           ?c rdfs:subClassOf [ owl:onProperty :hasPart ;
                                owl:someValuesFrom ?p  ] }""")

g2 = rdflib.Graph();
for triple in qres: # quite a few triples
    g2.add(triple)

qres = g2.query(
    """PREFIX : <http://www.example.org/ontology#> 
       SELECT ?a ?b WHERE { ?a :hasSome+ ?b }""")

for row in qres:
    print("%s :hasSome+ %s" % row)

, transitiveClosure() transitive_objects() .

+3

Source: https://habr.com/ru/post/1694935/


All Articles