NameRedundancyCheck extends AbstractCheck { @Override public Collection<CheckWarning> check(OWLGraphWrapper graph, Collection<OWLObject> allOwlObjects) { List<CheckWarning> out = new ArrayList<CheckWarning>(); Map<String, Set<OWLEntity>> labels = new HashMap<String, Set<OWLEntity>>(); Map<String, Set<OWLEntity>> synonyms = new HashMap<String, Set<OWLEntity>>(); Set<String> localSynoyms = new HashSet<String>(); for (OWLObject owlObject : allOwlObjects) { if (owlObject instanceof OWLEntity == false) { continue; } if (ignoreObsolete && graph.isObsolete(owlObject)) { continue; } OWLEntity owlEntity = (OWLEntity) owlObject; final IRI iri = owlEntity.getIRI(); final String label = graph.getLabel(owlObject); if (label == null) { out.add(new CheckWarning("HAS_NAME_CHECK", "The term with IRI: "+iri.toQuotedString()+" has no label.", isFatal(), iri)); continue; } addValue(label, owlEntity, labels); List<ISynonym> oboSynonyms = graph.getOBOSynonyms(owlObject); if (oboSynonyms != null && !oboSynonyms.isEmpty()) { localSynoyms.clear(); for (ISynonym synonym : oboSynonyms) { final String synonymLabel = synonym.getLabel(); if (OboFormatTag.TAG_EXACT.getTag().equals(synonym.getScope())) { addValue(synonymLabel, owlEntity, synonyms); } if (localSynoyms.add(synonymLabel) == false) { String message = "Duplicate synonym '"+synonymLabel+"' label for IRI: "+iri+" '"+label+"'"; out.add(new CheckWarning(getID(), message , isFatal(), iri, OboFormatTag.TAG_SYNONYM.getTag())); } } } } for(Entry<String, Set<OWLEntity>> entry : labels.entrySet()) { String label = entry.getKey(); Set<OWLEntity> entities = entry.getValue(); if (entities.size() > 1) { StringBuilder sb = new StringBuilder("Duplicate label '"); sb.append(label).append("' for IRIs: "); List<IRI> iris = renderEntities(entities, sb, graph); out.add(new CheckWarning(getID(), sb.toString(), isFatal(), iris, OboFormatTag.TAG_NAME.getTag())); } Set<OWLEntity> conflictingSynonyms = synonyms.get(label); if (conflictingSynonyms != null) { Set<OWLEntity> cleaned = new HashSet<OWLEntity>(); for (OWLEntity owlEntity : conflictingSynonyms) { if (!entities.contains(owlEntity)) { cleaned.add(owlEntity); } } if (!cleaned.isEmpty()) { IRI mainIRI = entities.iterator().next().getIRI(); List<IRI> iris = new ArrayList<IRI>(cleaned.size() + 1); iris.add(mainIRI); StringBuilder sb = new StringBuilder("Primary label '"); sb.append(label).append("' ").append(mainIRI.toQuotedString()); sb.append(" re-used as EXACT synonym for IRIs: "); iris = renderEntities(cleaned, sb, graph, iris); out.add(new CheckWarning(getID(), sb.toString(), isFatal(), iris, OboFormatTag.TAG_SYNONYM.getTag())); } } } return out; } NameRedundancyCheck(); boolean isIgnoreObsolete(); void setIgnoreObsolete(boolean ignoreObsolete); @Override Collection<CheckWarning> check(OWLGraphWrapper graph, Collection<OWLObject> allOwlObjects); static final String SHORT_HAND; }
@Test public void testNameRedundancy() throws Exception { ParserWrapper parser = new ParserWrapper(); IRI iri = IRI.create(getResource("verification/name_redundancy.obo").getAbsoluteFile()) ; OWLGraphWrapper graph = parser.parseToOWLGraph(iri.toString()); OntologyCheck check = new NameRedundancyCheck(); Collection<CheckWarning> warnings = check.check(graph, graph.getAllOWLObjects()); assertEquals(2, warnings.size()); }