package owltools.ontologyverification.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAxiom;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLOntology;

import owltools.graph.OWLGraphWrapper;
import owltools.io.OWLPrettyPrinter;
import owltools.ontologyverification.CheckWarning;

public class ObsoleteClassInSignature extends AbstractCheck {

	public static final String SHORT_HAND = "obsolete-in-signature";
	
	public ObsoleteClassInSignature() {
		super("OBSOLETE_CLASS_IN_SIGNATURE", "Obsolete Class in Signature Check", true, null);
	}

	@Override
	public Collection<CheckWarning> check(OWLGraphWrapper graph, Collection<OWLObject> allOwlObjects) {
		OWLPrettyPrinter pp = new OWLPrettyPrinter(graph);
		List<CheckWarning> out = new ArrayList<CheckWarning>();
		for (OWLObject owlObject : allOwlObjects) {
			if (owlObject instanceof OWLClass) {
				OWLClass owlClass = (OWLClass) owlObject;
				if (graph.isObsolete(owlClass) == false) {
					check(owlClass, graph, out, pp);
				}
			}
		}
		return out;
	}
	
	protected void check(OWLClass owlClass, OWLGraphWrapper graph, List<CheckWarning> warnings, OWLPrettyPrinter pp) {
		final Set<OWLOntology> allOntologies = graph.getAllOntologies();
		for(OWLOntology ontology : allOntologies){
			Set<OWLClassAxiom> axioms = ontology.getAxioms(owlClass);
			if (axioms != null && !axioms.isEmpty()) {
				// check axioms 
				for (OWLClassAxiom axiom : axioms) {
					Set<OWLClass> signature = axiom.getClassesInSignature();
					for (OWLClass signatureClass : signature) {
						if (graph.isObsolete(signatureClass)) {
							final IRI iri = signatureClass.getIRI();
							String message = "Obsolete class "+iri+" in axiom: "+pp.render(axiom);
							warnings.add(new CheckWarning(getID(), message , isFatal(), owlClass.getIRI()));
						}
					}
				}
			}
		}
	}

}

@@ -145,49 +145,8 @@
 t;%0A%0A
-import org.semanticweb.owlapi.model.IRI;%0A
 impo
@@ -1763,49 +1763,190 @@
 %09%09%09%09
-final IRI iri = signatureClass.getIRI(
+StringBuilder sb = new StringBuilder();%0A%09%09%09%09%09%09%09sb.append(%22Obsolete class %22);%0A%09%09%09%09%09%09%09String id = graph.getIdentifier(signatureClass);%0A%09%09%09%09%09%09%09if (id != null) %7B%0A%09%09%09%09%09%09%09%09sb.append(id
 );%0A
+%09
 %09%09%09%09
@@ -1959,54 +1959,275 @@
 ing 
-message = %22Obsolete class %22+iri+%22 in axiom: %22+
+sigLabel = graph.getLabel(signatureClass);%0A%09%09%09%09%09%09%09%09if (sigLabel != null) %7B%0A%09%09%09%09%09%09%09%09%09sb.append(%22 '%22).append(sigLabel).append('%5C'');%0A%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09else %7B%0A%09%09%09%09%09%09%09%09sb.append(signatureClass.getIRI());%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09sb.append(%22 in axiom: %22);%0A%09%09%09%09%09%09%09sb.append(
 pp.r
@@ -2238,16 +2238,17 @@
 r(axiom)
+)
 ;%0A%09%09%09%09%09%09
@@ -2291,15 +2291,21 @@
 (), 
-message
+sb.toString()
  , i

improve message for obsolete classes in signature