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

RDFLibGenid not matched by Graph.query() #1808

Closed
white-gecko opened this issue Apr 13, 2022 · 4 comments · Fixed by #1821
Closed

RDFLibGenid not matched by Graph.query() #1808

white-gecko opened this issue Apr 13, 2022 · 4 comments · Fixed by #1821
Assignees
Labels
bug Something isn't working

Comments

@white-gecko
Copy link
Member

When I have a Graph object with a blank node in it and perform a skolemization on it, the graph generates RDFLibGenid instances which are not matched by IRIs in a query. To reproduce this I have created the following MWE.
As can be seen, after serializing and parsing the graph, the node is a URIRef that is matched by the same query.

from rdflib import Graph, __version__

print("rdflib version:", __version__)

g = Graph()
g.parse(data='[] <urn:prop> "val" .', format="turtle")
for s, p, o in g:
    print(type(s), s)

gs = g.skolemize()
for s, p, o in gs:
    print(type(s), s)

query_with_iri = 'select ?p ?o {{ <{}> ?p ?o }}'.format(s)
query_for_all = 'select ?s ?p ?o { ?s ?p ?o }'

print(query_with_iri)
print(query_for_all)

print("Get by IRI")
for row in gs.query(query_with_iri):
    print(row)

print("??? ^^^ Why is this empty? ^^^")

print("Get everything")
for row in gs.query(query_for_all):
    print(row)

gp = Graph()
gp.parse(data=gs.serialize(format='turtle'), format='turtle')

print("Get from parsed graph by IRI")
for row in gp.query(query_with_iri):
    print(row)

print("Get everything from parsed graph")
for row in gp.query(query_for_all):
    print(row)

The Output of the MWE:

rdflib version: 6.1.1
<class 'rdflib.term.BNode'> nbdebdd66e4a84a64b29c66ecfc019d5fb1
<class 'rdflib.term.RDFLibGenid'> http://rdlib.net/.well-known/genid/rdflib/nbdebdd66e4a84a64b29c66ecfc019d5fb1
select ?p ?o { <http://rdlib.net/.well-known/genid/rdflib/nbdebdd66e4a84a64b29c66ecfc019d5fb1> ?p ?o }
select ?s ?p ?o { ?s ?p ?o }
Get by IRI
??? ^^^ Why is this empty? ^^^
Get everything
(RDFLibGenid('http://rdlib.net/.well-known/genid/rdflib/nbdebdd66e4a84a64b29c66ecfc019d5fb1'), rdflib.term.URIRef('urn:prop'), rdflib.term.Literal('val'))
Get from parsed graph by IRI
(rdflib.term.URIRef('urn:prop'), rdflib.term.Literal('val'))
Get everything from parsed graph
(rdflib.term.URIRef('http://rdlib.net/.well-known/genid/rdflib/nbdebdd66e4a84a64b29c66ecfc019d5fb1'), rdflib.term.URIRef('urn:prop'), rdflib.term.Literal('val'))
@ghost
Copy link

ghost commented Apr 13, 2022

Mmm. The issue is related to the genid's type. Skolemization of a BNode used to return a URIRef until issue #1404 when it was first changed in commit e63450a ("Fix BNode.skolemize() returning a URIRef instead of a Genid.") and then again in 8ea101d "Use RDFLibGenid instead of Genid to maintain original blank node id when performing de-skolemization."

A quick recursive grep for skolemize in tests returns only one reference - that of test_issue1404.py -

    # Check the BNode is now an RDFLibGenid after skolemization.
    skolem_bnode = skolemized_graph.value(**query)
    assert type(skolem_bnode) == RDFLibGenid

Other than that, there aren't any tests of skolemized graphs/bnodes

Looks we need to re-visit the implemented solution to issue #1404 and add some more tests.

fwiw, just to confirm the origin of the issue, temporarily changing BNode.skolemize() to wrap the returned RDFLibGenid in a URIRef enables the query to succeed, albeit at the cost of the assertion in test_issue1404.py failing.

@white-gecko
Copy link
Member Author

I assume the issue is due to the situation, that the terms are matched with a simple object lookup in a dictionary:
https://github.com/RDFLib/rdflib/blob/master/rdflib/plugins/stores/memory.py#L87
i.e. instance of URIRef not equal to instance of RDFLibGenid

@ghost
Copy link

ghost commented Apr 13, 2022

Yes, I think you nailed where the fault lies (L#87 is SimpleMemory.triples(), need to look at L#330 in Memory.triples()):

        elif subject is not None:  # subject is given
            spo = self.__spo
            logger.debug(f"TRIPLES\nsubject=“{subject}{type(subject)}\nspo=“{spo}”")
            if subject in spo:
...
            else:  # given subject not found
                logger.debug(f"Given subject {subject} not found")
                pass

logger reports:

DEBUG    rdflib:memory.py:332 TRIPLES
subject=“http://rdlib.net/.well-known/genid/rdflib/n7938b043fd5d4c76b2bd3c38053fdabbb1” <class 'rdflib.term.URIRef'>
spo=“{RDFLibGenid('http://rdlib.net/.well-known/genid/rdflib/n7938b043fd5d4c76b2bd3c38053fdabbb1'): {rdflib.term.URIRef('urn:prop'): {rdflib.term.Literal('val'): 1}}}”
DEBUG    rdflib:memory.py:366 Given subject http://rdlib.net/.well-known/genid/rdflib/n7938b043fd5d4c76b2bd3c38053fdabbb1 not found

Inspecting the call stack reveals that the call is from evalBGP.

@edmondchuc edmondchuc self-assigned this Apr 15, 2022
edmondchuc added a commit to edmondchuc/rdflib that referenced this issue Apr 15, 2022
@edmondchuc edmondchuc mentioned this issue Apr 15, 2022
8 tasks
@aucampia
Copy link
Member

This issue may also be related:

@aucampia aucampia added the bug Something isn't working label Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants