Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataset: NQuads serialization only makes sense for context-aware stores! #2335

Closed
sdasda7777 opened this issue Apr 9, 2023 · 13 comments
Closed
Labels
bug Something isn't working core Relates to core functionality of RDFLib, i.e. `rdflib.{graph,store,term}` format: N-Quads Related to N-Quads format. serialization Related to serialization.

Comments

@sdasda7777
Copy link

Hi, I'd like to serialize JSON-LD to N-Quads, however I keep getting NQuads serialization only makes sense for context-aware stores! exception. From the Dataset definition and this issue it seems that it however should work, correct? Here is my code (formatting might be bit off):

from rdflib import Dataset
        
    try:
        data = """{
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Jane Doe",
  "jobTitle": "Professor",
  "telephone": "(425) 123-4567",
  "url": "http://www.janedoe.com"
}"""
         ds = Dataset()
         ds = ds.parse(data=data, format="json-ld")
         print(ds.serialize(format="nquads"))
    except Exception as e:
         print(e)
@aucampia
Copy link
Member

@sdasda7777 this is a known bug (IMO at least) recorded here #1892.

I will close this as a duplicate, however, I think that NQuads for a non-context-aware store should be identical to NTriples [ref].

@aucampia aucampia added bug Something isn't working duplicate This issue or pull request already exists marked for closing The issue or PR will be closed soon if no further feedback is provided. serialization Related to serialization. format: N-Quads Related to N-Quads format. labels Apr 11, 2023
@sdasda7777
Copy link
Author

@sdasda7777 this is a known bug (IMO at least) recorded here #1892.

I see, I was hoping for some kind of solution, but since it's open for that long I guess I can forget about that.

I think that NQuads for a non-context-aware store should be identical to NTriples.

For this example, yes. However JSON-LD may actually have context information, and I want it to work in that case as well.

@sdasda7777
Copy link
Author

Section number 8 of the standard has an example that should translate into multiple graphs. You can see it really does when you try converting it to N-Quads on the JSON-LD playground, which is exactly what I want to be able to do.

@aucampia
Copy link
Member

For this example, yes. However JSON-LD may actually have context information, and I want it to work in that case as well.

But then you should use a context-aware store like rdflib.graph.Dataset, otherwise it will be lost anyway.

I see, I was hoping for some kind of solution, but since it's open for that long I guess I can forget about that.

The reason this bug is not fixed, even though it is simple to fix, is that it can easily be worked around by users, even if we fix it, it will be no different from just using NTriples, and this is because non-context aware stores cannot contain named graphs, and if there are no graph names NQuads is equivalent to NTriples.

We will be grateful for PRs that fix #1892.

@sdasda7777
Copy link
Author

But then you should use a context-aware store like rdflib.graph.Dataset, otherwise it will be lost anyway.

But I am using a Dataset, am I not?

The reason this bug is not fixed, even though it is simple to fix, is that it can easily be worked around by users, even if we fix it, it will be no different from just using NTriples, and this is because non-context aware stores cannot contain named graphs, and if there are no graph names NQuads is equivalent to NTriples.

Okay, so I should be able to do this conversion just fine if I use context-aware store? Which stores are those?

@aucampia
Copy link
Member

aucampia commented Apr 11, 2023

But I am using a Dataset, am I not?

I overlooked that, you are indeed using Dataset. In that case, this is a somewhat separate bug from #1892.

Okay, so I should be able to do this conversion just fine if I use context-aware store? Which stores are those?

Dataset should be context aware, but I guess for some reason the serializers disagree. rdflib.graph.ConjunctiveGraph is also context aware, and I suspect that should work, could you try using that instead of rdflib.graph.Dataset ?

@aucampia aucampia added core Relates to core functionality of RDFLib, i.e. `rdflib.{graph,store,term}` and removed duplicate This issue or pull request already exists marked for closing The issue or PR will be closed soon if no further feedback is provided. labels Apr 11, 2023
@sdasda7777
Copy link
Author

Dataset should be context aware, but I guess for some reason the serializers disagree. rdflib.graph.ConjunctiveGraph is also context aware, and I suspect that should work, could you try using that instead of rdflib.graph.Dataset ?

Yeah, it is the same output, NQuads serialization only makes sense for context-aware stores! as well.

I am using Python 3.10.4 and rdflib 6.3.2, if that helps in any way.

@aucampia
Copy link
Member

aucampia commented Apr 11, 2023

Okay, then I don't really have an immediate answer. I will look at it when I have time. It does seem like a bug, and a pull requests fixing this will be welcome.

@aucampia
Copy link
Member

aucampia commented Apr 11, 2023

Oh wait, I think the problem is this:

ds = ds.parse(data=data, format="json-ld")

Don't reassign ds, I think what is being returned there is not a Dataset. This in itself is probably a bit messed up, but I think if you just don't reassign ds, and use the created ds, it should work.

parse mutates the object, so it is also a bit redundant in this case.

@sdasda7777
Copy link
Author

Oh, you're right! Thank you so much!!!

@namedgraph
Copy link

@aucampia how about making nquads the default serialization for Dataset? Currently ds.serialize() returns nothing because I'm assuming it defaults to ntriples? Should I create a separate issue for this?

@aucampia
Copy link
Member

aucampia commented Jun 8, 2023

@namedgraph there is actually a PR that will fix this - though it will use trig instead of nquads:

But this needs some other things fixed before we can merge it.

@aucampia
Copy link
Member

aucampia commented Jun 8, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core Relates to core functionality of RDFLib, i.e. `rdflib.{graph,store,term}` format: N-Quads Related to N-Quads format. serialization Related to serialization.
Projects
None yet
Development

No branches or pull requests

3 participants