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

Expected behaviour of Dataset.triples? I think it always returns an empty list #2959

Closed
WhiteGobo opened this issue Oct 28, 2024 · 3 comments

Comments

@WhiteGobo
Copy link
Contributor

This is a question related to issue #2958 . During sparql evaluation graph.triples is used to find triples in context.

So with given data, no triples are returned. Dataset doesnt has any identifier, so it should return all available triples right?

from rdflib import *

GraphString = '''

PREFIX eg: <http://example.com/person/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

eg:graph-1 {
    eg:drewp a foaf:Person .
    eg:drewp eg:says "Hello World" .
}

eg:graph-2 {
    eg:nick a foaf:Person .
    eg:nick eg:says "Hi World" .
}
'''

g = Dataset()
g.parse(data=GraphString, format="trig")
print(list(g.triples((None, None, None))))
@ashleysommer
Copy link
Contributor

Hi @WhiteGobo

In your example file, there are two named graphs, and no default graph.

a) If ds.default_union is False, then ds.triples((None, None, None)) will give you the triples from the default graph, which there are none.
b) If ds.default_union is True, then ds.triples((None, None, None)) will give you all triples from all named graphs.

Two things to point out.

  1. You've mentioned the Dataset itself doesn't have any identifier, that doesn't matter because identifier attribute for a Dataset in RDFLib was deprecated in RDFLib v6.0. Identifiers are only for named graphs, and it doesn't make sense in the context of an Dataset to have an identifier, so its usage was deprecated.

  2. This Graph/ConjunctiveGraph/Dataset API surface is well past due for refresh.
    When RDFLib was first developed in 2003, the RDF v1.0 spec was still being written only Graphs existed. RDFLib v1.1 with the concept of Datasets wasn't introduced until 2014. In those 11 years, 2003-2014 RDFLib introduced ConjunctiveGraph as a new feature but never made it into any RDF spec.
    Unfortunately the way it was implemented was that ConjunctiveGraph was a hacked-on extension to Graph, and in 2014 Dataset was a hacked-on extension to ConjunctiveGraph. That is why Datasets are a confusing mess.
    We are planning a major rewrite of the implementation of Graphs and Datasets (and removal of ConjunctiveGraph) for RDFLib v8 later this year, so it will be a better experience then.

@ashleysommer
Copy link
Contributor

Here is some code to illustrate the behaviour of ds.triples()
Note, I added two triples to the unnamed default graph in the trig file too.

from rdflib import *

GraphString = '''
PREFIX eg: <http://example.com/person/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

eg:graph-1 {
    eg:drewp a foaf:Person .
    eg:drewp eg:says "Hello World" .
}

eg:graph-2 {
    eg:nick a foaf:Person .
    eg:nick eg:says "Hi World" .
}

eg:ash a foaf:Person .
eg:ash eg:says "Default" .
'''

ds = Dataset()
ds.parse(data=GraphString, format="trig")
ds.default_union = False
print(list(ds.triples((None, None, None))))
# returns only the triples from the "default" graph:
# > eg:ash a foaf:Person 
# > eg:ash eg:says "Default" 
print(list(ds.triples((None, None, None, URIRef("http://example.com/person/graph-1")))))
# Returns only from "graph-1" named graph:
# > eg:drewp a foaf:Person 
# > eg:drewp eg:says "Hello World" 
ds.default_union = True
print(list(ds.triples((None, None, None))))
# with default_union=True it returns everything
# > eg:drewp a foaf:Person 
# > eg:drewp eg:says "Hello World" 
# > eg:nick a foaf:Person 
# > eg:nick eg:says "Hi World" 
# > eg:ash a foaf:Person 
# > eg:ash eg:says "Default" 

@WhiteGobo
Copy link
Contributor Author

ok thx for the explanation. This solves the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants