1

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']
  • Welcome to StackOverflow! Please take a moment (if you haven't done so yet) to review the help section, in particular the entries on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to create a minimal, complete, and verifiable example (MCVE)](http://stackoverflow.com/help/mcve). – Jeen Broekstra Feb 01 '16 at 19:42
  • 1
    In this instance: try and narrow it down. If this is your first SPARQL query, it's far too big and complex. Do a _very_ basic SPARQL query first (e.g. `SELECT * WHERE {?s ?p ?o} LIMIT 10`), to make sure you can get results back. If that returns results, you know the problem is in your original query. Gradually make your query more complex, step by step, until you hit the point where it doesn't give you results anymore. Then inspect the dataset to see why the condition you last added makes the query fail. – Jeen Broekstra Feb 01 '16 at 19:45
  • 1
    Also a tip: don't post code examples (or data examples) as images. Instead, add the actual text, so people can copy-paste it to test things for you. – Jeen Broekstra Feb 01 '16 at 19:50
  • The fragment of the data you showed only shows the information matching the `?pv` variable, for the `cbim:PropertyValue` instance. The variable `?asfaltPlakCP` (and everything else that connects to that variable) in your query requires additional data that you haven't shown us. – Jeen Broekstra Feb 01 '16 at 19:59
  • Thank you for your reply, @jeenbroekstra. I am now able to generate output in the form of `rdflib.term.literal(u'96.0')...` How can I print a single value, based on two selection criteria which are included in the results of the query? I have elaborated the question and included it in my original post as an update. Thank you for your help! – Ruben van der Heijden Feb 02 '16 at 08:10

0 Answers0