Query (select and paths) named graphs via pystardog

Hi everbody,
I try to send select and paths-queries via pystardog.
Example python-code:
*with stardog.Connection(self.stardog_database, *self.conn_details) as conn:

  •        sparql_res_dict = conn.select(sparql_query,*
    
  •                                     content_type=stardog.content_types.CSV,*
    
  •                                     default_graph_uri=["urn:named_graph/me/tco/plant_0780_year_2023"])*
    

The triples are stored in the named graph "urn:named_graph/me/tco/plant_0780_year_2023".
To clarify the data structure in the db see the result from stardog studio:
query:
SELECT * WHERE
{
GRAPH ?g {
{?s ?p ?o}
}
}
Result:

urn:named_graph/me/tco/plant_0780_year_2023 http://www.semantic.bosch.com/ae-testing/2022/8/product_costing_01 rdf:type owl:Ontology
urn:named_graph/me/tco/plant_0780_year_2023 pco:Costcenter_1234567 rdf:type pco:CostCenter
...
The above python code returns no results.
If I add
FROM urn:named_graph/me/tco/plant_0780_year_2023
to the query in the python code, it does return results.
To my understanding the parameter
default_graph_uri=["urn:named_graph/me/tco/plant_0780_year_2023"]
should work like the FROM ... in the query - but it does not.

The above is also true for a paths-query issued by stardog (see above) and stardog studio.

As a workaround I can insert the FROM statement into the select query.
But for a path query this workaround is not possible - there i need the parameter
default_graph_uri=["urn:named_graph/me/tco/plant_0780_year_2023"]
Where is the error in using the default_graph_uri - parameter?
Thanks,
Thomas

Hi Thomas,

I am not sure if I follow your code example since it is a bit difficult to read due to the formatting. You should be able to retrieve the from the named graph using the default_graph_uri . For example, if you insert data in the DB as follows

INSERT DATA
{
    GRAPH <urn:myNamedGraph> {
        :a :b :c .
    }
}

you should be able to retrieve the data from the named graph with

results = conn.select('select * { ?s ?p ?o }', default_graph_uri=["urn:myNamedGraph"])

I hope this clarifies your problem. If not, it would be helpful if you could share some details on the query you are trying to execute.

Best regards

Lars

Hi Lars,
thanks for the quick reply.
Actually, what you write is exactly what i do - with one difference: I do write the triples to the db using the following code:

def write_file_to_graph(self, graph_uri: str, file_name: str):
        with stardog.Connection(self.stardog_database, **self.conn_details) as conn:
            conn.begin()
            file_path = path.join(loadEnvVar('DAYDIR'), f'{file_name}_{date.today()}.ttl')
            conn.add(content=stardog.content.File(file_path), graph_uri=graph_uri)
            conn.commit()

where:
graph_uri = 'urn:named_graph/me/tco/plant_0780_year_2023'

I will try to insert test data using your insert-Data-query and see, if that makes a difference ...
Regards,
Thomas

Hi Lars,

i tried your solution but have to say, that it does not work.
I added the data to a db where there are already some data in the default graph.
So using stardog studio, i can differentiate between querying the default graph and the named graph
"urn:myNamedGraph".
Trying the same via the pystardog api always returns the data from the default graph. It seems that the parameter "default_graph_uri" is not effective.

Can you confirm that your solution does actually work (try-out done)?
Or might there be an implementation error in the pystardog module?
Thanks,
Thomas

Hi Thomas,

I can confirm that the default_graph_uri parameter works as expected for pystardog. For example, when I execute the following example script:

import stardog
import stardog.content_types

conn_details = {
  'endpoint': 'http://localhost:5820',
  'username': 'user',
  'password': 'pw'
}


data_dg = """
[] rdfs:label "Default Graph" .
"""

data_ng = """
[] rdfs:label "Named Graph" .
"""

with stardog.Connection('testing', **conn_details) as conn:


  # Clear the data in the DB and add data to the default and named graph
  conn.begin()
  conn.clear()
  conn.add(content=stardog.content.Raw(data_dg, 'text/turtle', name='dg.ttl'))
  conn.add(content=stardog.content.Raw(data_ng, 'text/turtle', name='ng.ttl'), graph_uri="urn:ng")
  conn.commit()

  # Query the data
  results = conn.select('select ?o { ?a ?p ?o }', content_type=stardog.content_types.CSV)
  print(f"Default Graph: {results}")

  results = conn.select('select ?o { ?a ?p ?o }', default_graph_uri=["urn:ng"], content_type=stardog.content_types.CSV)
  print(f"Named Graph: {results}")

  results = conn.select('select ?o from <urn:ng> { ?a ?p ?o }', content_type=stardog.content_types.CSV)
  print(f"Named Graph (FROM): {results}")

  results = conn.select('select ?o from named <urn:ng> { graph ?g { ?a ?p ?o } }', content_type=stardog.content_types.CSV)
  print(f"Named Graph (FROM NAMED): {results}")


  results = conn.select('select ?o from stardog:context:local { ?a ?p ?o  }', content_type=stardog.content_types.CSV)
  print(f"All local graphs: {results}")

I get the expected results:

Default Graph: b'o\nDefault Graph\n'
Named Graph: b'o\nNamed Graph\n'
Named Graph (FROM): b'o\nNamed Graph\n'
Named Graph (FROM NAMED): b'o\nNamed Graph\n'
All local graphs: b'o\nDefault Graph\nNamed Graph\n'

You should be able to reproduce these results on a simple DB for testing.

Maybe one thing to check in your code is that the provided graph URIs are not encoded in <...>, that is in SPARQL you would use FROM <urn:graph> but in the default_graph_uri parameter takes urn:graph instead.

Best regards
Lars

Hello Lars,

again thanks a lot for this precise help.
I run your script with one change - i used an already existing database.
And my output corresponds to my former results (see line 2):

Default Graph: b'o\nDefault Graph\n'
Named Graph: b'o\nDefault Graph\n'
Named Graph (FROM): b'o\nNamed Graph\n'
Named Graph (FROM NAMED): b'o\nNamed Graph\n'
All local graphs: b'o\nDefault Graph\nNamed Graph\n'

So the problem seems not to be in the code but maybe in the database configuration?
Regards,
Thomas

Hi Thomas,

could you check which version of pystardog you are using? The feature was just recently added to version 0.17.0.

Best regards
Lars

This was not intended for me.

Hi Lars,

you saved my week :slight_smile:
I had the version 0.12.0. running.
After upgrading to 0.17.0 I finally get results from my named graphs!
Thanks a lot!
Best regards,
Thomas

1 Like