package owltools.test;

import static junit.framework.Assert.*;

import java.io.File;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.semanticweb.owlapi.io.OWLXMLOntologyFormat;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAnnotationValue;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedObject;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

import owltools.InferenceBuilder;
import owltools.graph.OWLGraphWrapper;
import owltools.io.ParserWrapper;


public class RestrictToELTest extends OWLToolsTestBasics {

	/**
	 * Simple test for {@link InferenceBuilder#enforceEL(OWLGraphWrapper)}.
	 * 
	 * @throws Exception
	 */
	@Test
	public void testEnforceEL() throws Exception {
		// Simple test for the method: no logic checks here
		ParserWrapper pw = new ParserWrapper();
		OWLGraphWrapper g = pw.parseToOWLGraph(getResourceIRIString("pizza-2007-02-12.owl"));
		OWLGraphWrapper gEL = InferenceBuilder.enforceEL(g);
		assertTrue(g.getSourceOntology().getAxiomCount() > gEL.getSourceOntology().getAxiomCount());
		writeOWLOntolog(gEL, "pizza-2007-02-12-el.owl");
	}
	
	/**
	 * Test whether the ontology has the same ontology id after the conversion.
	 * 
	 * @throws Exception
	 */
	@Test
	public void testRetainOntologyId() throws Exception {
		ParserWrapper pw = new ParserWrapper();
		OWLGraphWrapper g = pw.parseToOWLGraph(getResourceIRIString("simple-deprecated.owl"));
		String ontologyId = "fribble";
		assertEquals(ontologyId, g.getOntologyId());
		OWLGraphWrapper gEL = InferenceBuilder.enforceEL(g);
		assertEquals(ontologyId, gEL.getOntologyId());
	}
	
	/**
	 * Test whether the ontology still has deprecation annotations after 
	 * the conversion.
	 * 
	 * 
	 * PROBLEM: Depreciations are removed, due to the fact that the 
	 * boolean data type is not considered EL.
	 * Data ranges are not supported in EL and boolean in OWL is 
	 * defined as range. 
	 * 
	 * @throws Exception
	 */
	@Test
	public void testRetainDeprecated() throws Exception {
		ParserWrapper pw = new ParserWrapper();
		OWLGraphWrapper g = pw.parseToOWLGraph(getResourceIRIString("simple-deprecated.owl"));
		String label = "X:1";
		assertTrue(isDeprecated(label, g));
		OWLGraphWrapper gEL = InferenceBuilder.enforceEL(g);
		writeOWLOntolog(gEL, "simple-deprecated-el.owl");
		assertTrue(isDeprecated(label, gEL));
	}
	
	private boolean isDeprecated(String label, OWLGraphWrapper graph) {
		OWLObject owlObject = graph.getOWLObjectByLabel(label);
		assertNotNull(owlObject);
		if (owlObject instanceof OWLNamedObject) {
			IRI iri = ((OWLNamedObject) owlObject).getIRI();
			OWLOntology ontology = graph.getSourceOntology();
			Set<OWLAnnotationAssertionAxiom> assertionAxioms = ontology.getAnnotationAssertionAxioms(iri);
			assertNotNull(assertionAxioms);
			OWLDataFactory owlDataFactory = ontology.getOWLOntologyManager().getOWLDataFactory();
			OWLAnnotationProperty deprecated = owlDataFactory.getOWLDeprecated();
			for (OWLAnnotationAssertionAxiom axiom : assertionAxioms) {
				OWLAnnotationProperty property = axiom.getProperty();
				if (deprecated.equals(property)) {
					OWLAnnotationValue value = axiom.getValue();
					if (value instanceof OWLLiteral) {
						OWLLiteral literal = (OWLLiteral) value;
						String literalValue = literal.getLiteral();
						if(literal.getDatatype().isBoolean()) {
							boolean b = Boolean.parseBoolean(literalValue);
							return b;
						}
					}
				}
			}			
		}
		return false;
	}
	
	private void writeOWLOntolog(OWLGraphWrapper gmod, String fileName) throws OWLOntologyStorageException {
		File file = new File(FileUtils.getTempDirectory(), fileName);
		IRI iri = IRI.create(file);
		System.out.println("Saving ontology to file: "+file);
		gmod.getSourceOntology().getOWLOntologyManager().saveOntology(gmod.getSourceOntology(), new OWLXMLOntologyFormat(), iri);
	}
}

@@ -2877,963 +2877,42 @@
 ;%0A%09%09
-if (owlObject instanceof OWLNamedObject) %7B%0A%09%09%09IRI iri = ((OWLNamedObject) owlObject).getIRI();%0A%09%09%09OWLOntology ontology = graph.getSourceOntology();%0A%09%09%09Set%3COWLAnnotationAssertionAxiom%3E assertionAxioms = ontology.getAnnotationAssertionAxioms(iri);%0A%09%09%09assertNotNull(assertionAxioms);%0A%09%09%09OWLDataFactory owlDataFactory = ontology.getOWLOntologyManager().getOWLDataFactory();%0A%09%09%09OWLAnnotationProperty deprecated = owlDataFactory.getOWLDeprecated();%0A%09%09%09for (OWLAnnotationAssertionAxiom axiom : assertionAxioms) %7B%0A%09%09%09%09OWLAnnotationProperty property = axiom.getProperty();%0A%09%09%09%09if (deprecated.equals(property)) %7B%0A%09%09%09%09%09OWLAnnotationValue value = axiom.getValue();%0A%09%09%09%09%09if (value instanceof OWLLiteral) %7B%0A%09%09%09%09%09%09OWLLiteral literal = (OWLLiteral) value;%0A%09%09%09%09%09%09String literalValue = literal.getLiteral();%0A%09%09%09%09%09%09if(literal.getDatatype().isBoolean()) %7B%0A%09%09%09%09%09%09%09boolean b = Boolean.parseBoolean(literalValue);%0A%09%09%09%09%09%09%09return b;%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%09%09%09%0A%09%09%7D%0A%09%09return false
+return graph.isObsolete(owlObject)
 ;%0A%09%7D

simplify code, use wrapper to test for obsolete