Repository: ontodev/robot
Pull request #298 (2018-05-22 12:38:19)

Search full import closure for entity labels
See https://github.com/obi-ontology/obi/pull/932#event-1636013540

jamesaoverton: @rctauber Please add a unit test for this.

Patch:
diff --git a/robot-core/src/main/java/org/obolibrary/robot/QuotedEntityChecker.java b/robot-core/src/main/java/org/obolibrary/robot/QuotedEntityChecker.java
index c59733dc..91b1d588 100644
--- a/robot-core/src/main/java/org/obolibrary/robot/QuotedEntityChecker.java
+++ b/robot-core/src/main/java/org/obolibrary/robot/QuotedEntityChecker.java
@@ -1,5 +1,6 @@
 package org.obolibrary.robot;
 
+import com.google.common.collect.Sets;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -8,16 +9,18 @@ import java.util.Map;
 import java.util.Set;
 import org.semanticweb.owlapi.expression.OWLEntityChecker;
 import org.semanticweb.owlapi.model.IRI;
-import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
+import org.semanticweb.owlapi.model.OWLAnnotation;
 import org.semanticweb.owlapi.model.OWLAnnotationProperty;
 import org.semanticweb.owlapi.model.OWLClass;
 import org.semanticweb.owlapi.model.OWLDataFactory;
 import org.semanticweb.owlapi.model.OWLDataProperty;
 import org.semanticweb.owlapi.model.OWLDatatype;
 import org.semanticweb.owlapi.model.OWLEntity;
+import org.semanticweb.owlapi.model.OWLLiteral;
 import org.semanticweb.owlapi.model.OWLNamedIndividual;
 import org.semanticweb.owlapi.model.OWLObjectProperty;
 import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.search.EntitySearcher;
 import org.semanticweb.owlapi.util.ReferencedEntitySetProvider;
 import org.semanticweb.owlapi.util.ShortFormProvider;
 import org.slf4j.Logger;
@@ -37,7 +40,7 @@ public class QuotedEntityChecker implements OWLEntityChecker {
   private static OWLDataFactory dataFactory = new OWLDataFactoryImpl();
 
   /** Shared IOHelper. */
-  private static IOHelper ioHelper = null;
+  private IOHelper ioHelper = null;
 
   /** Optional short form providers for additional names. */
   private List<ShortFormProvider> providers = new ArrayList<ShortFormProvider>();
@@ -142,8 +145,9 @@ public class QuotedEntityChecker implements OWLEntityChecker {
    * @param ontology the ontology to add mappings for
    */
   public void addAll(OWLOntology ontology) {
-    Set<OWLOntology> ontologies = new HashSet<OWLOntology>();
+    Set<OWLOntology> ontologies = new HashSet<>();
     ontologies.add(ontology);
+    ontologies.addAll(ontology.getImports());
     ReferencedEntitySetProvider resp = new ReferencedEntitySetProvider(ontologies);
     for (OWLEntity entity : resp.getEntities()) {
       add(ontology, entity);
@@ -207,14 +211,16 @@ public class QuotedEntityChecker implements OWLEntityChecker {
     }
 
     if (parentOntology != null && properties != null) {
+      // Get a set of the parent ontology and its imports
+      Set<OWLOntology> ontologies = Sets.newHashSet(parentOntology);
+      ontologies.addAll(parentOntology.getImports());
       for (OWLAnnotationProperty property : properties) {
-        for (OWLAnnotationAssertionAxiom ax :
-            parentOntology.getAnnotationAssertionAxioms(entity.getIRI())) {
-          if (ax.getProperty().equals(property)) {
-            String value = OntologyHelper.getValue(ax.getValue());
-            if (value != null) {
-              map.put(value, entity.getIRI());
-            }
+        // Get the labels for all entities
+        for (OWLAnnotation ann : EntitySearcher.getAnnotations(entity, ontologies, property)) {
+          OWLLiteral value = ann.getValue().asLiteral().orNull();
+          // If it has a label, add it to the map (will replace short form)
+          if (value != null) {
+            map.put(value.getLiteral(), entity.getIRI());
           }
         }
       }


Test patch:
diff --git a/robot-core/src/test/java/org/obolibrary/robot/QuotedEntityCheckerTest.java b/robot-core/src/test/java/org/obolibrary/robot/QuotedEntityCheckerTest.java
new file mode 100644
index 00000000..d05cbaa3
--- /dev/null
+++ b/robot-core/src/test/java/org/obolibrary/robot/QuotedEntityCheckerTest.java
@@ -0,0 +1,35 @@
+package org.obolibrary.robot;
+
+import static junit.framework.TestCase.assertTrue;
+
+import org.junit.Test;
+import org.semanticweb.owlapi.model.*;
+import org.semanticweb.owlapi.util.SimpleShortFormProvider;
+
+/**
+ * Test QuotedEntityChecker methods.
+ *
+ * @author <a href="mailto:rctauber@gmail.com">Bekcy Tauber</a>
+ */
+public class QuotedEntityCheckerTest extends CoreTest {
+
+  /**
+   * Test resolving a label from an import.
+   *
+   * @throws Exception on any issue.
+   */
+  @Test
+  public void testImportLabel() throws Exception {
+    OWLOntology ontology = loadOntologyWithCatalog("/import_test.owl");
+    QuotedEntityChecker checker = new QuotedEntityChecker();
+    checker.setIOHelper(new IOHelper());
+    checker.addProvider(new SimpleShortFormProvider());
+    checker.addProperty(dataFactory.getRDFSLabel());
+    checker.addAll(ontology);
+
+    OWLEntity cls = checker.getOWLClass("test one");
+    assertTrue(
+        "https://github.com/ontodev/robot/robot-core/src/test/resources/simple.owl#test1"
+            .equals(cls.getIRI().toString()));
+  }
+}
diff --git a/robot-core/src/test/resources/catalog-v001.xml b/robot-core/src/test/resources/catalog-v001.xml
index 2484240f..e618bd1b 100644
--- a/robot-core/src/test/resources/catalog-v001.xml
+++ b/robot-core/src/test/resources/catalog-v001.xml
@@ -18,4 +18,5 @@
     <uri id="User Entered Import Resolution" name="https://github.com/ontodev/robot/robot-core/src/test/resources/top.owl" uri="./top.owl"/>
     <uri id="User Entered Import Resolution" name="https://github.com/ontodev/robot/robot-core/src/test/resources/without_redundant_subclasses.owl" uri="./without_redundant_subclasses.owl"/>
     <uri id="User Entered Import Resolution" name="https://github.com/ontodev/robot/robot-core/src/test/resources/non-reasoned.owl" uri="./non-reasoned.owl"/>
+    <uri id="User Entered Import Resolution" name="https://github.com/ontodev/robot/robot-core/src/test/resources/import_test.owl" uri="./import_test.owl"/>
 </catalog>