How can I use the RDFLIB module in Python to retrieve a value from an OWL file using SparQL?

放肆的年华 提交于 2021-02-10 19:39:06

问题


I am currently trying to write a script in Python that uses the RDFLIB module in order to retrieve a specific value from an OWL file by using SPARQL.

The problem I'm facing is that the current version of my script returns no data. I tried to print each row in order for myself to analayse the output, but the output simply reads "Process finished with exit code 0".

My final goal is to retrieve a value (in the example, this value will be '96') from the OWL file. The object to which this value is linked is also linked in the OWL file to the file name:

DTB OPR 170.769 - PR.H.STRAAT VH_gml.xml

It is my goal to be able to print the value '96' while using the file name as selection criterium.

The current version of my script in Python is:

    import rdflib
    from rdflib import Namespace


    g=rdflib.Graph()
    filename = r'bim\Perceel4.owl'
    g.load(filename, format='xml')


    qres = g.query(
        """SELECT DISTINCT ?value ?otl ?file ?frag
           WHERE {
              ?pv a cbim:PropertyValue .
              ?pv cbim:propertyType <http://bim.rws.nl/OTL/COINS/otl-otl.11.owl#OB02859-PR00501.0> .
              ?pv cbim:value ?value .
              ?asfaltplakCP cbim:PropertyValue ?pv .
              ?asfaltplakCP cbim:name ?name .
              ?asfaltplak cbim:contains[cbim:cataloguePart ?asfaltplakCP].
              optional {
                ?asfaltplak cbim:shape ?rep.
                ?rep cbim:documentAliasFilePath ?file .
                ?rep cbim:documentFragment ?frag }
          Filter (?file="DTB OPR 170.769 - PR.H.STRAAT VH_gml.xml").
           }""",
         initNs=dict(
            cbim=Namespace("http://www.coinsweb.nl/cbim-1.1.owl#")))

    for row in qres.result:
        print row

An section of th OWL file in which the value '96' can be found is:

Link to OWL file cut out

I hope someone can explain the mistakes I'm making.

Updated question after receiving comments: [SOLVED]

I am now capable to generate output. An cut out from the output is:

    (rdflib.term.Literal(u'96.0', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#float')), rdflib.term.Literal(u'Asfaltplak_tussenlaag', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')), rdflib.term.Literal(u'DTB PST 167.134 - 167.274 VH_gml.xml', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')))
    (rdflib.term.Literal(u'94.0', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#float')), rdflib.term.Literal(u'Asfaltplak_onderlaag', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')), rdflib.term.Literal(u'DTB VBS 166.963 - 166.875 VH_gml.xml', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')))

This is printed by using the command line:

   for row in qres:
       print row

What command lines could I use to only print the value (for example '96.0') based on the selection criteria 'file name' and the name 'Asfaltplak_Onderlaag'?

for example criteria:

'DTB VBS 166.963 - 166.875 VH_gml.xml' and 'Asfaltplak_Onderlaag'

result in: 94.0 (by using

     print row['value']

I am not including the selection criteria in the SparQL query because I want to be able to let the python script aks the user for the two criteria in a later stage of the development of this script.

EDIT: I have been able to solve the last section of this problem. I thank you for your help.

The last piece of code for which I was looking was:

    for row in qres:
      if (rdflib.term.Literal(Layer, datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')) in row) and (rdflib.term.Literal(Road_Section, datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#string')) in row):
          obtained_value = row['value']

来源:https://stackoverflow.com/questions/35124959/how-can-i-use-the-rdflib-module-in-python-to-retrieve-a-value-from-an-owl-file-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!