RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.17k stars 556 forks source link

N3 serializer "randomly" omits triple #1807

Closed ghost closed 2 years ago

ghost commented 2 years ago

There seems to be an issue with the notation3 serializer randomly omitting to serialize triples.

Given the following test created in test/test_n3_formula.py ...

import pytest
import rdflib

test_n3 = """@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://test/> .
{:a :b :c;a :foo} => {:a :d :c,?y} .
_:foo a rdfs:Class .
:a :d :c ."""

@pytest.mark.xfail(reason="N3 serializer randomly omits triple")
def test():
    graph1 = rdflib.Graph()
    graph1.parse(data=test_n3, format="n3")

    """
    >>> sorted(list(graph1))
    [
        (
            rdflib.term.BNode('fde0470d85a044b6780f0c6804b119063b1'),
            rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
            rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Class')
        ),
        (
            rdflib.term.URIRef('http://test/a'),
            rdflib.term.URIRef('http://test/d'),
            rdflib.term.URIRef('http://test/c')
        ),
        (
            <Graph identifier=_:Formula2 (<class 'rdflib.graph.QuotedGraph'>)>,
            rdflib.term.URIRef('http://www.w3.org/2000/10/swap/log#implies'),
            <Graph identifier=_:Formula3 (<class 'rdflib.graph.QuotedGraph'>)>
        )
    ]
    """

    graph2 = rdflib.Graph()
    graph2.parse(data=graph1.serialize(format="n3"), format="n3")
    assert (
        rdflib.term.URIRef('http://test/a'),
        rdflib.term.URIRef('http://test/d'),
        rdflib.term.URIRef('http://test/c')
    ) in graph2

Sample run_test.py results ...

(rdflib) $ ./run_tests.py test/test_n3_formula.py 
Running pytest with: ["test/test_n3_formula.py"]
============================================ test session starts =============================================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: rdflib, configfile: tox.ini
plugins: subtests-0.6.0, cov-3.0.0
collected 1 item                                                                                             

test/test_n3_formula.py::test XFAIL (N3 serializer randomly omits triple)                              [100%]

============================================= 1 xfailed in 0.02s =============================================
(rdflib) $ ./run_tests.py test/test_n3_formula.py 
Running pytest with: ["test/test_n3_formula.py"]
============================================ test session starts =============================================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: rdflib, configfile: tox.ini
plugins: subtests-0.6.0, cov-3.0.0
collected 1 item                                                                                             

test/test_n3_formula.py::test XPASS (N3 serializer randomly omits triple)                              [100%]

============================================= 1 xpassed in 0.01s =============================================
aucampia commented 2 years ago

happy to add this to the test suite, I tried making a variant test for this but rdfpipe does not produce valid nquads form this (even if I replace ?y with :y), which is maybe another bug, but I'm actually a bit skittish on n3, and I think maybe this just cannot be encoded as nquads

ghost commented 2 years ago

happy to add this to the test suite, I tried making a variant test for this but rdfpipe does not produce valid nquads form this (even if I replace ?y with :y), which is maybe another bug, but I'm actually a bit skittish on n3, and I think maybe this just cannot be encoded as nquads

Feel free to add to the test suite and yes, you are correct formulae are specific to notation3 format.

aucampia commented 2 years ago

Fairly sure this is related to https://github.com/RDFLib/rdflib/issues/1701