Repository: ontodev/robot (java)

Issue:
MERGE annotates xsd datatypes
I am currently setting up a pipeline that merges all ontology modules of a project into one file. During that I notices that `robot merge --annotate-derived-from true …` also adds annotations to used XSD datatypes, as that triggered some other problems (owlcs/owlapi#1116) later on. I think, it would be better to not annotate the XSD datatypes in the same way `rdf:type` will not be annotated. 

Example:

```ttl
# test-robot-xsd.ttl

@prefix : <http://example.com/test-robot-xsd#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/test-robot-xsd> rdf:type owl:Ontology .

:someDataProperty rdf:type owl:DatatypeProperty .

:someIndividual rdf:type owl:NamedIndividual ;
                :someDataProperty "true"^^xsd:boolean ,
                                  "2000-01-01"^^xsd:date ,
                                  "2000-01-01T00:00:00"^^xsd:dateTime ,
                                  0.1 ,
                                  0.1e0 ,
                                  1 ,
                                  "a" .
```

Executed command: `robot merge --input test-robot-xsd.ttl --annotate-defined-by true --output test-robot-xsd.merge.ttl`

```ttl
# test-robot-xsd.merge.ttl

@prefix : <http://example.com/test-robot-xsd#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://example.com/test-robot-xsd> .

<http://example.com/test-robot-xsd> rdf:type owl:Ontology .

#################################################################
#    Datatypes
#################################################################

###  http://www.w3.org/2001/XMLSchema#boolean
xsd:boolean rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  http://www.w3.org/2001/XMLSchema#date
xsd:date rdf:type rdfs:Datatype ;
         rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  http://www.w3.org/2001/XMLSchema#dateTime
xsd:dateTime rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  http://www.w3.org/2001/XMLSchema#decimal
xsd:decimal rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  http://www.w3.org/2001/XMLSchema#double
xsd:double rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  http://www.w3.org/2001/XMLSchema#integer
xsd:integer rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  http://www.w3.org/2001/XMLSchema#string
xsd:string rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


#################################################################
#    Data properties
#################################################################

###  http://example.com/test-robot-xsd#someDataProperty
:someDataProperty rdf:type owl:DatatypeProperty ;
                  rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


#################################################################
#    Individuals
#################################################################

###  http://example.com/test-robot-xsd#someIndividual
:someIndividual rdf:type owl:NamedIndividual ;
                :someDataProperty "true"^^xsd:boolean ,
                                  "2000-01-01"^^xsd:date ,
                                  "2000-01-01T00:00:00"^^xsd:dateTime ,
                                  0.1 ,
                                  "0.1"^^xsd:double ,
                                  1 ,
                                  "a" ;
                rdfs:isDefinedBy <http://example.com/test-robot-xsd> .


###  Generated by the OWL API (version 4.5.26) https://github.com/owlcs/owlapi
```

Pull request:
annotate-defined-by parameter excludes datatypes
Resolves [#1163, resolves #1163]

- [ ] `docs/` have been added/updated
- [ ] tests have been added/updated
- [ ] `mvn verify` says all tests pass
- [ ] `mvn site` says all JavaDocs correct
- [ ] `CHANGELOG.md` has been updated

This pull request introduces checks to prevent annotation of entities with `rdfs:isDefinedBy` when their namespace is a OWL 2 reserved vocabulary (RDF, RDFS, XSD or OWL).

The implementation impacts `robot merge --annotate-defined-by` and `robot annotate --annotate-defined-by` parameters.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 955f1cf4..c333e4d5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Fixed
 - '--annotate-with-source true' does not work with extract --method subset [#1160]
 - Fix how Template adds entities to the QuotedEntityChecker [#1104]
+- [`merge`] and 'annotate' operations '--annotate-defined-by' excludes reserved OWL 2 vocabularies [#1171]
 - Handle IRIs that are not entities in export [#1168]
 
 ## [1.9.5] - 2023-09-20
@@ -381,6 +382,7 @@ First official release of ROBOT!
 [`template`]: http://robot.obolibrary.org/template
 [`validate`]: http://robot.obolibrary.org/validate
 
+[#1171]: https://github.com/ontodev/robot/pull/1171
 [#1168]: https://github.com/ontodev/robot/pull/1168
 [#1160]: https://github.com/ontodev/robot/pull/1160
 [#1148]: https://github.com/ontodev/robot/pull/1148
diff --git a/docs/examples/example2_defined_by.owl b/docs/examples/example2_defined_by.owl
index d15c175a..82bb7233 100644
--- a/docs/examples/example2_defined_by.owl
+++ b/docs/examples/example2_defined_by.owl
@@ -78,62 +78,12 @@
     
 
 
-    <!-- http://www.w3.org/2000/01/rdf-schema#comment -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#comment">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
-    <!-- http://www.w3.org/2000/01/rdf-schema#isDefinedBy -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#isDefinedBy">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
-    <!-- http://www.w3.org/2000/01/rdf-schema#label -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#label">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
     <!-- http://www.w3.org/ns/prov#wasDerivedFrom -->
 
     <owl:AnnotationProperty rdf:about="http://www.w3.org/ns/prov#wasDerivedFrom">
         <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
     </owl:AnnotationProperty>
-    
-
-
-    <!-- 
-    ///////////////////////////////////////////////////////////////////////////////////////
-    //
-    // Datatypes
-    //
-    ///////////////////////////////////////////////////////////////////////////////////////
-     -->
-
-    
-
-
-    <!-- http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral -->
-
-    <rdf:Description rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
-    <!-- http://www.w3.org/2001/XMLSchema#string -->
 
-    <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#string">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
     
 
 
@@ -284,18 +234,6 @@
         <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
         <rdfs:label>dummy individal 5</rdfs:label>
     </owl:NamedIndividual>
-    
-
-
-    <!-- 
-    ///////////////////////////////////////////////////////////////////////////////////////
-    //
-    // Annotations
-    //
-    ///////////////////////////////////////////////////////////////////////////////////////
-     -->
-
-    <rdf:Description rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral"/>
 </rdf:RDF>
 
 
diff --git a/docs/examples/merged_defined_by.owl b/docs/examples/merged_defined_by.owl
index d988d4fa..e71912c1 100644
--- a/docs/examples/merged_defined_by.owl
+++ b/docs/examples/merged_defined_by.owl
@@ -78,54 +78,12 @@
     
 
 
-    <!-- http://www.w3.org/2000/01/rdf-schema#comment -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#comment">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
-    <!-- http://www.w3.org/2000/01/rdf-schema#isDefinedBy -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#isDefinedBy">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
-    <!-- http://www.w3.org/2000/01/rdf-schema#label -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#label">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
-    
-
-
     <!-- http://www.w3.org/ns/prov#wasDerivedFrom -->
 
     <owl:AnnotationProperty rdf:about="http://www.w3.org/ns/prov#wasDerivedFrom">
         <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
     </owl:AnnotationProperty>
-    
-
-
-    <!-- 
-    ///////////////////////////////////////////////////////////////////////////////////////
-    //
-    // Datatypes
-    //
-    ///////////////////////////////////////////////////////////////////////////////////////
-     -->
-
-    
 
-
-    <!-- http://www.w3.org/2001/XMLSchema#string -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#string">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/examples/edit.owl"/>
-    </rdf:Description>
     
 
 
diff --git a/docs/merge.md b/docs/merge.md
index b8d1a80c..d74f96ea 100644
--- a/docs/merge.md
+++ b/docs/merge.md
@@ -46,3 +46,5 @@ It’s also possible to annotate the imported or merged ontology axioms with the
     robot merge --input example2.owl --input merge.owl \
       --annotate-defined-by true \
       --output results/merged_defined_by.owl
+
+`--annotate-defined-by` excludes entities from the reserved OWL 2 vocabularies (RDF, RDFS, XSD and OWL).
diff --git a/robot-command/src/main/java/org/obolibrary/robot/AnnotateCommand.java b/robot-command/src/main/java/org/obolibrary/robot/AnnotateCommand.java
index 8a9d3499..e35fc36b 100644
--- a/robot-command/src/main/java/org/obolibrary/robot/AnnotateCommand.java
+++ b/robot-command/src/main/java/org/obolibrary/robot/AnnotateCommand.java
@@ -318,7 +318,9 @@ public class AnnotateCommand implements Command {
         OWLAnnotationProperty rdfsIsDefinedBy =
             ontology.getOWLOntologyManager().getOWLDataFactory().getRDFSIsDefinedBy();
         for (OWLEntity owlEntity : ontology.getSignature()) {
-          OntologyHelper.addEntityAnnotation(ontology, owlEntity, rdfsIsDefinedBy, ontIRI, false);
+          if (!owlEntity.getIRI().isReservedVocabulary()) {
+            OntologyHelper.addEntityAnnotation(ontology, owlEntity, rdfsIsDefinedBy, ontIRI, false);
+          }
         }
       }
     }
diff --git a/robot-core/src/main/java/org/obolibrary/robot/MergeOperation.java b/robot-core/src/main/java/org/obolibrary/robot/MergeOperation.java
index 92d1a13a..6e5efa25 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/MergeOperation.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/MergeOperation.java
@@ -363,8 +363,10 @@ public class MergeOperation {
       OWLAnnotationProperty rdfsIsDefinedBy =
           targetOntology.getOWLOntologyManager().getOWLDataFactory().getRDFSIsDefinedBy();
       for (OWLEntity owlEntity : sourceOntology.getSignature(includeImportsClosure)) {
-        OntologyHelper.addEntityAnnotation(
-            targetOntology, owlEntity, rdfsIsDefinedBy, ontIRI, false);
+        if (!owlEntity.getIRI().isReservedVocabulary()) {
+          OntologyHelper.addEntityAnnotation(
+              targetOntology, owlEntity, rdfsIsDefinedBy, ontIRI, false);
+        }
       }
     }
   }


Test patch:
diff --git a/robot-core/src/test/java/org/obolibrary/robot/MergeOperationTest.java b/robot-core/src/test/java/org/obolibrary/robot/MergeOperationTest.java
index 6bf104b2..386a4319 100644
--- a/robot-core/src/test/java/org/obolibrary/robot/MergeOperationTest.java
+++ b/robot-core/src/test/java/org/obolibrary/robot/MergeOperationTest.java
@@ -101,9 +101,9 @@ public class MergeOperationTest extends CoreTest {
     assertEquals(5, simple.getAxiomCount());
     OWLOntology merged = MergeOperation.merge(ontologies, false, false, true, false);
 
-    assertEquals(9, merged.getAxiomCount());
+    assertEquals(7, merged.getAxiomCount());
     OWLOntology expected = loadOntology("/simple_defined_by.owl");
-    assertEquals(9, expected.getAxiomCount());
+    assertEquals(7, expected.getAxiomCount());
     assertIdentical(expected, merged);
   }
 }
diff --git a/robot-core/src/test/resources/simple_defined_by.owl b/robot-core/src/test/resources/simple_defined_by.owl
index 890c1075..06c23107 100644
--- a/robot-core/src/test/resources/simple_defined_by.owl
+++ b/robot-core/src/test/resources/simple_defined_by.owl
@@ -8,43 +8,6 @@
      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
     <owl:Ontology rdf:about="https://github.com/ontodev/robot/robot-core/src/test/resources/simple.owl"/>
     
-
-
-    <!-- 
-    ///////////////////////////////////////////////////////////////////////////////////////
-    //
-    // Annotation properties
-    //
-    ///////////////////////////////////////////////////////////////////////////////////////
-     -->
-
-    
-
-
-    <!-- http://www.w3.org/2000/01/rdf-schema#label -->
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/01/rdf-schema#label">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/robot-core/src/test/resources/simple.owl"/>
-    </rdf:Description>
-    
-
-
-    <!-- 
-    ///////////////////////////////////////////////////////////////////////////////////////
-    //
-    // Datatypes
-    //
-    ///////////////////////////////////////////////////////////////////////////////////////
-     -->
-
-    
-
-
-    <!-- http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral -->
-
-    <rdf:Description rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral">
-        <rdfs:isDefinedBy rdf:resource="https://github.com/ontodev/robot/robot-core/src/test/resources/simple.owl"/>
-    </rdf:Description>