Inserting raw Python strings into named-graph

For some reason, I cannot insert triples into a named graph that are formatted as Python strings... if the named graph contains more than 1 triple. I can insert triples into a named graph via Python strings, however, if the named graph is empty to begin with.

Not sure if I'm doing some sort of Python related thing wrong or if it's a Pystardog thing.

I have not been able to reproduce this issue. Would you please provide a minimal sample of your code that can demonstrate it?

Will do, let me collect something for you and share after the weekend.

I can insert triples using this method if I'm creating a new named graph... if the named graph already exists, though, it will not insert any additional triples. At the moment, I can insert triples into a named graph only if I create insert a TTL file directly.

LAST_UPDATED_TRIPLE = '<{iri}> <urn:ontology:lastUpdated> "{last_updated}"^^xsd:dateTime'

def insert_stardog(triple: str, graph_iri: str):
    conn = stardog.Connection(DB_NAME, **SD_CONN_DETAILS)
    conn.begin()
    conn.add(stardog.content.Raw(triples_detail, "text/turtle"), graph_uri=graph_iri)
    conn.commit()

last_updated = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S"))
iri = "urn:named_graph_1234"
triple = LAST_UPDATED_TRIPLE.format(iri=iri, last_updated=last_updated)
insert_stardog(triple, iri)



After trying to debug this more... I have some corrections.
(1) For some reason now, inserting a single triple as a Python string works through Pystardog.
(2) Furthermore, inserting a single triple a raw file now does not work (the code above) through Pystardog.

This is how our code works currently:

def insert_stardog(triple: str, graph_iri: str):
    with stardog.Connection(DB_NAME, **SD_CONN_DETAILS) as conn:
        conn.begin()
        if file_raw == 'file':
            conn.add(stardog.content.File(triple), graph_uri=graph_iri)
        else:
            conn.add(stardog.content.Raw(triple, "text/turtle"), graph_uri=graph_iri)
        conn.commit()

I've tried creating the sample triple ('<{iri}> <urn:ontology:lastUpdated> "{last_updated}"^^xsd:dateTime') and inserting it into Stardog via Pystardog. The conn.add with the Python string now works fine... but the file will not add to the named graph no matter what I do. If I try to add it through Studio to the named graph, it works.

I'm really not sure if this a Pystardog thing or if I need to blow away my Stardog DB and build it again from scratch.

What error message do you get?

I am surprised that the triple will insert without a period at the end. ie:

LAST_UPDATED_TRIPLE = '<{iri}> <urn:ontology:lastUpdated> "{last_updated}"^^xsd:dateTime .'

content.File is for inserting a file using the file name. If you try to load a file it should look something like this: conn.add(stardog.content.File('filename.ttl', 'text/turtle'). The content-type is optional. It will use 'text/turtle' if the file extension is ttl. Is it possible that you have passed a string with the file contents instead? If you do that, content.Raw should be used instead of content.File.
Link to documentation for File and Raw

What error message do you get?

That's the thing... I don't get any error message back nor cause a StardogException.

I am surprised that the triple will insert without a period at the end.

Sorry, that's a copy/paste error on my part -- I did try to insert this with a period at the end.

content.File is for inserting a file using the file name. If you try to load a file it should look something like this: conn.add(stardog.content.File('filename.ttl', 'text/turtle') . The content-type is optional. It will use 'text/turtle' if the file extension is ttl . Is it possible that you have passed a string with the file contents instead? If you do that, content.Raw should be used instead of content.File .

Right, that's exactly I tried -- in the case that I was trying to add a Turtle file, my "triple" parameter would be a filename.

I tried to reproduce the problem. Here is what I did.

  1. Created an empty database called test.
  2. Made a test.ttl file with this as the contents: <http://foo/abc> :testprop "xyz" .
  3. Ran this script:
import stardog

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

conn = stardog.Connection('test', **conn_details)

graph_uri = 'http://foo'
triple = '<http://foo/bar> :testprop "testobject" .'

conn.begin()

conn.add(
    stardog.content.Raw(triple, stardog.content_types.TURTLE),
    graph_uri=graph_uri
)

conn.add(
    stardog.content.File('test.ttl', stardog.content_types.TURTLE),
    graph_uri=graph_uri
)

conn.commit()
  1. Ran this query and my two triples are there in the named graph:
select * {
    graph <http://foo> {
        ?s ?p ?o .
    }
}

Does this simple test case work on your system? I hope this will help debug the problem.