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

Turtle writer does not respect namespaces in IRIs #4921

Closed
reckart opened this issue Feb 29, 2024 · 3 comments · Fixed by #4922
Closed

Turtle writer does not respect namespaces in IRIs #4921

reckart opened this issue Feb 29, 2024 · 3 comments · Fixed by #4922
Assignees
Labels
🐞 bug issue is a bug
Milestone

Comments

@reckart
Copy link
Contributor

reckart commented Feb 29, 2024

Current Behavior

The turtle writer does not consider the namespace provided by an IRI. Instead, it turns the IRI into a string and then tries to derive a namespace from that string and then looks up that namespace in the namespace table.

Expected Behavior

If the IRI defines a namespace, that should first be used to look up the prefix in the namespace table. Interpreting the IRI as a string should only be a fallback.

Steps To Reproduce

	@Test
	public void testIriNamespace() throws Exception {
	    Model model = new DynamicModelFactory().createEmptyModel();
	    String prefix = "foo-bar";
        String ns = "foo:this.is.my.bar.";
        model.setNamespace(prefix, ns);
	    model.add(vf.createIRI(ns, "lala"), vf.createIRI(ns, "lulu"), vf.createIRI(ns, "lolo"));

        StringWriter stringWriter = new StringWriter();
	    Rio.write(model, stringWriter, RDFFormat.TURTLE);
	    
	    assertThat(stringWriter.toString()).contains("foo-bar:lala foo-bar:lulu foo-bar:lolo .");
	}

Version

4.3.9

Are you interested in contributing a solution yourself?

Yes

Anything else?

No response

@reckart reckart added the 🐞 bug issue is a bug label Feb 29, 2024
reckart added a commit to reckart/rdf4j that referenced this issue Feb 29, 2024
- SimpleValueFactory.createIRI(String, String) how actually properly respects the specified namespace and localname
- TurtleWriter tries to use the namespace encoded in the IRI unless the localname contains characters which are not valid in prefixed notation
- Added test
@reckart
Copy link
Contributor Author

reckart commented Feb 29, 2024

PR provided: #4922

@hmottestad
Copy link
Contributor

Not seen this before. Can you point to where this is defined in the turtle specification?

@reckart
Copy link
Contributor Author

reckart commented Feb 29, 2024

I have just ported code from Jena to RDF4J. In the Jena version, all IRIs I used were properly abbreviated when a namespace prefix was defined. Not so in RDF4J. I tracked this down to two issues:

  1. SimpleIRI does not retain namespace and localname separately (I could work around this using a custom subclass of AbstractIRI)
  2. TurtleWriter did not consider the explicit namespace in the IRI

Have a look at the provided test case.

As far as I can see during a quick scan of the Turtle specs, they deal mostly with parsing the turtle format. However, what I am trying to fix here is the serialization. The current serialization done by RDF4J provides valid Turtle, but it is unnecessarily verbose. My change makes more effective use of the namespace prefix declarations, leading to a serialized form that is more compact and easier to read.

reckart added a commit to reckart/rdf4j that referenced this issue Mar 6, 2024
- Added tests to check to which degree parsing a Turtle file with unconventional prefixes and serializing it back again results preserves format
reckart added a commit to reckart/rdf4j that referenced this issue Mar 6, 2024
- Commented out failing parts of new tests and refer to followup issue
reckart added a commit to reckart/rdf4j that referenced this issue Mar 6, 2024
- Fix RDFXML test by checking if a localname preference indicated by the IRI is valid in XML - if not, try to find an alternative representation
@hmottestad hmottestad added this to the 4.3.11 milestone Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug issue is a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants