From 0062ec47116343f053d506b905717892a33ee8e9 Mon Sep 17 00:00:00 2001 From: ignazio Date: Mon, 17 Jul 2023 00:04:49 +0100 Subject: [PATCH] Fix Shared class expression causes extra triples in output #1109 Defer check was on objects but not on lists, causing triple duplication. --- ...kNodeIdsAndAnnotationsRoundTripTestCase.java | 17 +++++++++++++++++ .../rdf/rdfxml/renderer/RDFXMLRenderer.java | 15 ++++++++++++--- .../rdf/turtle/renderer/TurtleRenderer.java | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/contract/src/test/java/org/semanticweb/owlapi/api/test/annotations/BlankNodeIdsAndAnnotationsRoundTripTestCase.java b/contract/src/test/java/org/semanticweb/owlapi/api/test/annotations/BlankNodeIdsAndAnnotationsRoundTripTestCase.java index 8468d63d07..6b94506858 100644 --- a/contract/src/test/java/org/semanticweb/owlapi/api/test/annotations/BlankNodeIdsAndAnnotationsRoundTripTestCase.java +++ b/contract/src/test/java/org/semanticweb/owlapi/api/test/annotations/BlankNodeIdsAndAnnotationsRoundTripTestCase.java @@ -39,6 +39,7 @@ import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataProperty; import org.semanticweb.owlapi.model.OWLDocumentFormat; +import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLOntology; @@ -99,6 +100,16 @@ protected OWLOntology blankNodeIdsAndAnnotationsRoundTripTestCase(OWLOntology on return ont1; } + protected OWLOntology anonClassAndAnnotationsRoundTripTestCase(OWLOntology ont1) { + Set anns = + set(df.getOWLAnnotation(df.getRDFSComment(), df.getOWLLiteral("comment"))); + OWLClassExpression restrict = df.getOWLObjectSomeValuesFrom(R, B); + OWLObjectIntersectionOf intersect = df.getOWLObjectIntersectionOf(C, restrict); + ont1.getOWLOntologyManager().addAxiom(ont1, df.getOWLEquivalentClassesAxiom(A, intersect)); + ont1.getOWLOntologyManager().addAxiom(ont1, df.getOWLSubClassOfAxiom(A, restrict, anns)); + return ont1; + } + @Override public boolean equal(OWLOntology ont1, OWLOntology ont2) { // Axioms without annotations are lost if identical axioms with @@ -124,6 +135,12 @@ void testFormat(OWLDocumentFormat format) { roundTripOntology(blankNodeIdsAndAnnotationsRoundTripTestCase(createAnon()), format); } + @ParameterizedTest + @MethodSource("formats") + void testFormat1(OWLDocumentFormat format) { + roundTripOntology(anonClassAndAnnotationsRoundTripTestCase(createAnon()), format); + } + @Test void roundTripRDFXMLAndFunctionalShouldBeSame() { OWLOntology o = blankNodeIdsAndAnnotationsRoundTripTestCase(createAnon()); diff --git a/parsers/src/main/java/org/semanticweb/owlapi/rdf/rdfxml/renderer/RDFXMLRenderer.java b/parsers/src/main/java/org/semanticweb/owlapi/rdf/rdfxml/renderer/RDFXMLRenderer.java index ca1fe1bb83..913f68c9dc 100644 --- a/parsers/src/main/java/org/semanticweb/owlapi/rdf/rdfxml/renderer/RDFXMLRenderer.java +++ b/parsers/src/main/java/org/semanticweb/owlapi/rdf/rdfxml/renderer/RDFXMLRenderer.java @@ -89,8 +89,8 @@ public RDFXMLRenderer(@Nonnull OWLOntology ontology, @Nonnull Writer w, super(checkNotNull(ontology, "ontology cannot be null"), checkNotNull(format, "format cannot be null")); this.format = checkNotNull(format, "format cannot be null"); - explicitXsdString = Boolean - .parseBoolean(format.getParameter("force xsd:string on literals", Boolean.FALSE).toString()); + explicitXsdString = Boolean.parseBoolean( + format.getParameter("force xsd:string on literals", Boolean.FALSE).toString()); qnameManager = new RDFXMLNamespaceManager(ontology, format); String defaultNamespace = qnameManager.getDefaultNamespace(); String base = base(defaultNamespace); @@ -257,7 +257,16 @@ public void render(@Nonnull RDFResource node, boolean root) throws IOException { protected void renderList(RDFNode n) throws IOException { if (n.isAnonymous()) { - render((RDFResourceBlankNode) n, false); + if (n.idRequired()) { + if (!pending.contains(n)) { + defer(n); + } + writer.writeStartElement(RDF_DESCRIPTION.getIRI()); + writer.writeNodeIDAttribute((RDFResourceBlankNode) n); + writer.writeEndElement(); + } else { + render((RDFResourceBlankNode) n, false); + } } else { if (n.isLiteral()) { write((RDFLiteral) n); diff --git a/parsers/src/main/java/org/semanticweb/owlapi/rdf/turtle/renderer/TurtleRenderer.java b/parsers/src/main/java/org/semanticweb/owlapi/rdf/turtle/renderer/TurtleRenderer.java index b86f7a73ab..df11c0a0af 100644 --- a/parsers/src/main/java/org/semanticweb/owlapi/rdf/turtle/renderer/TurtleRenderer.java +++ b/parsers/src/main/java/org/semanticweb/owlapi/rdf/turtle/renderer/TurtleRenderer.java @@ -281,7 +281,7 @@ private void write(@Nonnull RDFResource node) throws IOException { writeSpace(); pushTab(); for (Iterator it = list.iterator(); it.hasNext();) { - write(verifyNotNull(it.next())); + renderObject(verifyNotNull(it.next())); if (it.hasNext()) { writeNewLine(); }