Sparql查询以获取类的数据属性值

时间:2018-06-23 16:22:38

标签: python sparql rdf owl

我尝试了SPARQL查询来获取数据属性及其给定类的值。本体是用僧伽罗语创建的,这是对类的描述:

<owl:Class rdf:about="http://webprotege.stanford.edu/R9uiQljpDXJEZ868fnnTGfE">
    <rdfs:subClassOf rdf:resource="http://webprotege.stanford.edu/R8PYsiiP0lkvFeFlpH3aQTP"/>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://webprotege.stanford.edu/R9ZKyvH13cQVwMboXOzwAkx"/>
            <owl:someValuesFrom rdf:resource="http://webprotege.stanford.edu/R7d2MyZOxPEpWxXVctrVcgX"/>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://webprotege.stanford.edu/RCDEhkpjMY51Mr6sbD8rJAk"/>
            <owl:someValuesFrom rdf:resource="http://webprotege.stanford.edu/RC5t0y1e6l6lUULc0l37Xhw"/>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://webprotege.stanford.edu/RGKrLFedeMijO31cz81JC2"/>
            <owl:hasValue rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">1</owl:hasValue>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:label>අනූනන විභාජනය</rdfs:label>
</owl:Class>

我使用python编写查询和检索数据,这是我编写的查询:

my_world = World()
my_world.get_ontology("file://static\\ontology\\root-ontology.owl").load()  # path to the owl file is given here
self.graph = my_world.as_rdflib_graph()

query6 = ("prefix owl: <http://www.w3.org/2002/07/owl#> "
          "base <http://webprotege.stanford.edu/project/C25wqZ4qamhbITLmCGstJ0> "
          "prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> "
          "SELECT ?domain ?property ?value"
          "WHERE {"
          "rdfs:subClassOf owl:hasValue ?value."
          "owl:onProperty rdfs:label ?property."
          "?domain rdfs:label 'අනූනන විභාජනය'."
          "}")

domain_property_range = self.graph.query(query6)
    for item in domain_property_range:
        prop_label = str(item['property'].toPython())
        prop_label = re.sub(r'.*#', "", prop_label)

        value = str(item['value'].toPython())
        value = re.sub(r'.*#', "", value)
        sentences.append({'property': prop_label, 'value': value})
    print(sentences)

但是它给出了一个空列表。我对sparql查询和rdf东西很陌生。 因此,如果有人可以帮助解决此问题,将不胜感激。谢谢!

0 个答案:

没有答案