From: <sha...@us...> - 2012-03-15 02:34:14
|
Revision: 3611 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3611&view=rev Author: shadowtm Date: 2012-03-15 02:34:07 +0000 (Thu, 15 Mar 2012) Log Message: ----------- Added support of the OWLAPIOntology to implement the OWLOntologyKnowledgeSource interface to provide thread safe access to its underlying ontology. Also added a convenience class in support of this which can convert an OWL Ontology to a byte array and back again. Using this we can ensure disconnection occurs from OWL Ontology Managers. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java trunk/components-core/src/main/java/org/dllearner/reasoning/PelletReasoner.java trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java trunk/components-core/src/main/java/org/dllearner/reasoning/fuzzydll/FuzzyOWLAPIReasoner.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java trunk/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java trunk/components-core/src/test/java/org/dllearner/kb/ trunk/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java trunk/components-core/src/test/java/org/dllearner/utilities/ trunk/components-core/src/test/java/org/dllearner/utilities/owl/ trunk/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java trunk/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java trunk/components-core/src/test/resources/org/ trunk/components-core/src/test/resources/org/dllearner/ trunk/components-core/src/test/resources/org/dllearner/kb/ trunk/components-core/src/test/resources/org/dllearner/kb/owl-api-ontology-data.owl trunk/components-core/src/test/resources/org/dllearner/utilities/ trunk/components-core/src/test/resources/org/dllearner/utilities/owl/ trunk/components-core/src/test/resources/org/dllearner/utilities/owl/byte-conversion-data.owl Modified: trunk/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java 2012-03-14 17:27:16 UTC (rev 3610) +++ trunk/components-core/src/main/java/org/dllearner/kb/OWLAPIOntology.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -21,57 +21,45 @@ import java.io.File; import java.net.URI; -import java.util.Iterator; -import java.util.Set; import org.dllearner.core.AbstractKnowledgeSource; import org.dllearner.core.OntologyFormat; import org.dllearner.core.options.ConfigEntry; import org.dllearner.core.options.InvalidConfigOptionValueException; import org.dllearner.core.owl.KB; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataProperty; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLOntology; +import org.dllearner.utilities.owl.OntologyToByteConverter; +import org.dllearner.utilities.owl.SimpleOntologyToByteConverter; +import org.semanticweb.owlapi.model.*; -public class OWLAPIOntology extends AbstractKnowledgeSource { +/** + * This class provides a wrapper around a single OWL Ontology. However, due to threading issues it is not safe + * to allow access to ontologies created with an Ontology Manager which we do not control. + */ +public class OWLAPIOntology extends AbstractKnowledgeSource implements OWLOntologyKnowledgeSource{ - private OWLOntology ontology; - private Set<OWLOntology> ontologies; - private Set<OWLClass> classes; - private Set<OWLObjectProperty> prop; - private Set<OWLDataProperty> dataProp; - private Set<OWLNamedIndividual> individuals; + private byte[] ontologyBytes; + private OntologyToByteConverter converter = new SimpleOntologyToByteConverter(); + - public OWLAPIOntology() { - this(null); - } + public OWLAPIOntology(OWLOntology onto) { + ontologyBytes = converter.convert(onto); + } - public OWLAPIOntology(OWLOntology onto) - { - this.ontology = onto; - classes = ontology.getClassesInSignature(); - prop = ontology.getObjectPropertiesInSignature(); - dataProp = ontology.getDataPropertiesInSignature(); - individuals = ontology.getIndividualsInSignature(); - } - public static String getName() { return "OWL API Ontology"; } - - @Override + + @Override + public OWLOntology createOWLOntology(OWLOntologyManager manager) { + return converter.convert(ontologyBytes, manager); + } + + @Override public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { } - public OWLOntology getOWLOntolgy() - { - return ontology; - } - @Override public KB toKB() { @@ -95,45 +83,22 @@ { return null; } - - public void setOWLOntologies(Set<OWLOntology> onto) { - ontologies = onto; - System.out.println("ONTO: " + ontologies); - Iterator<OWLOntology> it = ontologies.iterator(); - while(it.hasNext()) { - OWLOntology ont = it.next(); - if(ont.getClassesInSignature() != null) { - classes.addAll(ont.getClassesInSignature()); - } - if(ont.getObjectPropertiesInSignature() != null) { - prop.addAll(ont.getObjectPropertiesInSignature()); - } - if(ont.getDataPropertiesInSignature() != null) { - dataProp.addAll(ont.getDataPropertiesInSignature()); - } - if(ont.getIndividualsInSignature() != null) { - individuals.addAll(ont.getIndividualsInSignature()); - } - } - } - - public Set<OWLOntology> getOWLOnntologies() { - return ontologies; - } - - public Set<OWLClass> getOWLClasses() { - return classes; - } - - public Set<OWLObjectProperty> getOWLObjectProperies() { - return prop; - } - - public Set<OWLDataProperty> getOWLDataProperies() { - return dataProp; - } - - public Set<OWLNamedIndividual> getOWLIndividuals() { - return individuals; - } + + /** + * Get the OntologyToByteConverter associated with this object. + * + * @return The OntologyToByteConverter associated with this object. + */ + public OntologyToByteConverter getConverter() { + return converter; + } + + /** + * Set the OntologyToByteConverter associated with this object. + * + * @param converter the OntologyToByteConverter to associate with this object. + */ + public void setConverter(OntologyToByteConverter converter) { + this.converter = converter; + } } Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java 2012-03-14 17:27:16 UTC (rev 3610) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -161,11 +161,7 @@ if (source instanceof OWLFile || source instanceof SparqlKnowledgeSource || source instanceof SparqlSimpleExtractor || source instanceof OWLAPIOntology) { - if (source instanceof OWLAPIOntology) { - ontology = ((OWLAPIOntology) source).getOWLOntolgy(); - manager = ontology.getOWLOntologyManager(); - owlAPIOntologies.add(ontology); - } else if (source instanceof SparqlKnowledgeSource) { + if (source instanceof SparqlKnowledgeSource) { ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); manager = ontology.getOWLOntologyManager(); owlAPIOntologies.add(ontology); Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/PelletReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/PelletReasoner.java 2012-03-14 17:27:16 UTC (rev 3610) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/PelletReasoner.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -22,7 +22,6 @@ import java.io.File; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -78,6 +77,7 @@ import org.dllearner.core.owl.UntypedConstant; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.OWLFile; +import org.dllearner.kb.OWLOntologyKnowledgeSource; import org.dllearner.kb.sparql.SparqlKnowledgeSource; import org.dllearner.utilities.Helper; import org.dllearner.utilities.owl.ConceptComparator; @@ -200,35 +200,30 @@ for (AbstractKnowledgeSource source : sources) { + if (source instanceof OWLOntologyKnowledgeSource) { + ontology = ((OWLOntologyKnowledgeSource) source).createOWLOntology(manager); + owlAPIOntologies.add(ontology); + } + if (source instanceof OWLFile || source instanceof SparqlKnowledgeSource || source instanceof OWLAPIOntology) { - URL url = null; - if (source instanceof OWLFile) { - url = ((OWLFile) source).getURL(); - } -// try { + if (source instanceof SparqlKnowledgeSource) { + ontology = ((SparqlKnowledgeSource) source) + .getOWLAPIOntology(); + manager = ontology.getOWLOntologyManager(); + owlAPIOntologies.add(ontology); + } - if (source instanceof OWLAPIOntology) { - ontology = ((OWLAPIOntology) source).getOWLOntolgy(); - manager = ontology.getOWLOntologyManager(); - } else if (source instanceof SparqlKnowledgeSource) { - ontology = ((SparqlKnowledgeSource) source) - .getOWLAPIOntology(); - manager = ontology.getOWLOntologyManager(); - } else { - ontology = manager.loadOntologyFromOntologyDocument(IRI.create(url)); - } - - owlAPIOntologies.add(ontology); - // imports includes the ontology itself - Set<OWLOntology> imports = manager - .getImportsClosure(ontology); - allImports.addAll(imports); - loadedOntologies.addAll(imports); - // System.out.println(imports); - classes.addAll(ontology.getClassesInSignature(true)); + // imports includes the ontology itself + Set<OWLOntology> imports = manager + .getImportsClosure(ontology); + allImports.addAll(imports); + loadedOntologies.addAll(imports); + + // System.out.println(imports); + classes.addAll(ontology.getClassesInSignature(true)); owlObjectProperties.addAll(ontology.getObjectPropertiesInSignature(true)); owlDatatypeProperties.addAll(ontology.getDataPropertiesInSignature(true)); owlIndividuals.addAll(ontology.getIndividualsInSignature(true)); @@ -474,42 +469,26 @@ for (AbstractKnowledgeSource source : sources) { + if (source instanceof OWLOntologyKnowledgeSource) { + ontology = ((OWLOntologyKnowledgeSource) source).createOWLOntology(manager); + owlAPIOntologies.add(ontology); + } if (source instanceof OWLFile || source instanceof SparqlKnowledgeSource || source instanceof OWLAPIOntology) { - URL url = null; - if (source instanceof OWLFile) { - url = ((OWLFile) source).getURL(); - } -// try { + if (source instanceof SparqlKnowledgeSource) { + ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); + manager = ontology.getOWLOntologyManager(); + owlAPIOntologies.add(ontology); + } - if (source instanceof OWLAPIOntology) { - ontology = ((OWLAPIOntology) source).getOWLOntolgy(); - manager = ontology.getOWLOntologyManager(); - } else if (source instanceof SparqlKnowledgeSource) { - ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); - manager = ontology.getOWLOntologyManager(); - } else { - try { - ontology = manager.loadOntologyFromOntologyDocument(IRI.create(url - .toURI())); - } catch (OWLOntologyCreationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - owlAPIOntologies.add(ontology); - // imports includes the ontology itself - Set<OWLOntology> imports = manager - .getImportsClosure(ontology); - allImports.addAll(imports); - loadedOntologies.addAll(imports); - // System.out.println(imports); + // imports includes the ontology itself + Set<OWLOntology> imports = manager + .getImportsClosure(ontology); + allImports.addAll(imports); + loadedOntologies.addAll(imports); + // System.out.println(imports); classes.addAll(ontology.getClassesInSignature(true)); owlObjectProperties.addAll(ontology.getObjectPropertiesInSignature(true)); owlDatatypeProperties.addAll(ontology.getDataPropertiesInSignature(true)); Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java 2012-03-14 17:27:16 UTC (rev 3610) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/ProtegeReasoner.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -20,8 +20,6 @@ package org.dllearner.reasoning; import java.io.File; -import java.net.URISyntaxException; -import java.net.URL; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -73,6 +71,7 @@ import org.dllearner.core.owl.UntypedConstant; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.OWLFile; +import org.dllearner.kb.OWLOntologyKnowledgeSource; import org.dllearner.kb.sparql.SparqlKnowledgeSource; import org.dllearner.utilities.Helper; import org.dllearner.utilities.owl.ConceptComparator; @@ -250,36 +249,22 @@ for (AbstractKnowledgeSource source : sources) { + if (source instanceof OWLOntologyKnowledgeSource) { + ontology = ((OWLOntologyKnowledgeSource) source).createOWLOntology(manager); + owlAPIOntologies.add(ontology); + } + if (source instanceof OWLFile || source instanceof SparqlKnowledgeSource || source instanceof OWLAPIOntology) { - URL url = null; - if (source instanceof OWLFile) { - url = ((OWLFile) source).getURL(); - } -// try { + if (source instanceof SparqlKnowledgeSource) { + ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); + manager = ontology.getOWLOntologyManager(); + owlAPIOntologies.add(ontology); + } - if (source instanceof OWLAPIOntology) { - ontology = ((OWLAPIOntology) source).getOWLOntolgy(); - } else if (source instanceof SparqlKnowledgeSource) { - ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); - manager = ontology.getOWLOntologyManager(); - } else { - try { - ontology = manager.loadOntologyFromOntologyDocument(IRI.create(url - .toURI())); - } catch (OWLOntologyCreationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - owlAPIOntologies.add(ontology); - classes.addAll(ontology.getClassesInSignature(true)); + classes.addAll(ontology.getClassesInSignature(true)); owlObjectProperties.addAll(ontology.getObjectPropertiesInSignature(true)); owlDatatypeProperties.addAll(ontology.getDataPropertiesInSignature(true)); owlIndividuals.addAll(ontology.getIndividualsInSignature(true)); Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/fuzzydll/FuzzyOWLAPIReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/fuzzydll/FuzzyOWLAPIReasoner.java 2012-03-14 17:27:16 UTC (rev 3610) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/fuzzydll/FuzzyOWLAPIReasoner.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -22,7 +22,6 @@ import java.io.File; import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.util.Collection; import java.util.Collections; @@ -66,6 +65,7 @@ import org.dllearner.core.owl.fuzzydll.FuzzyIndividual; import org.dllearner.kb.OWLAPIOntology; import org.dllearner.kb.OWLFile; +import org.dllearner.kb.OWLOntologyKnowledgeSource; import org.dllearner.kb.sparql.SparqlKnowledgeSource; import org.dllearner.reasoning.ReasonerType; import org.dllearner.utilities.owl.ConceptComparator; @@ -74,7 +74,6 @@ import org.dllearner.utilities.owl.OWLAPIConverter; import org.dllearner.utilities.owl.OWLAPIDescriptionConvertVisitor; import org.dllearner.utilities.owl.RoleComparator; -import org.semanticweb.HermiT.Reasoner.ReasonerFactory; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.AddAxiom; import org.semanticweb.owlapi.model.IRI; @@ -246,53 +245,43 @@ prefixes = new TreeMap<String,String>(); for(AbstractKnowledgeSource source : sources) { - + + if (source instanceof OWLOntologyKnowledgeSource) { + ontology = ((OWLOntologyKnowledgeSource) source).createOWLOntology(manager); + owlAPIOntologies.add(ontology); + } + if(source instanceof OWLFile || source instanceof SparqlKnowledgeSource || source instanceof OWLAPIOntology) { - URL url=null; - if(source instanceof OWLFile){ - url = ((OWLFile)source).getURL(); - } - try { - - if(source instanceof OWLAPIOntology) { - ontology = ((OWLAPIOntology)source).getOWLOntolgy(); - } else if (source instanceof SparqlKnowledgeSource) { - ontology = ((SparqlKnowledgeSource)source).getOWLAPIOntology(); - manager = ontology.getOWLOntologyManager(); - } else { - ontology = manager.loadOntologyFromOntologyDocument(IRI.create(url.toURI())); - } - - owlAPIOntologies.add(ontology); - // imports includes the ontology itself - Set<OWLOntology> imports = manager.getImportsClosure(ontology); - allImports.addAll(imports); + if (source instanceof SparqlKnowledgeSource) { + ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); + manager = ontology.getOWLOntologyManager(); + owlAPIOntologies.add(ontology); + } + + // imports includes the ontology itself + Set<OWLOntology> imports = manager.getImportsClosure(ontology); + allImports.addAll(imports); // System.out.println(imports); - for(OWLOntology ont : imports) { - classes.addAll(ont.getClassesInSignature()); - owlObjectProperties.addAll(ont.getObjectPropertiesInSignature()); - owlDatatypeProperties.addAll(ont.getDataPropertiesInSignature()); - owlIndividuals.addAll(ont.getIndividualsInSignature()); - } - - // if several knowledge sources are included, then we can only - // guarantee that the base URI is from one of those sources (there - // can't be more than one); but we will take care that all prefixes are - // correctly imported - OWLOntologyFormat format = manager.getOntologyFormat(ontology); - if(format instanceof PrefixOWLOntologyFormat) { - prefixes.putAll(((PrefixOWLOntologyFormat)format).getPrefixName2PrefixMap()); - baseURI = ((PrefixOWLOntologyFormat) format).getDefaultPrefix(); - prefixes.remove(""); - } - - } catch (OWLOntologyCreationException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - // all other sources are converted to KB and then to an + for (OWLOntology ont : imports) { + classes.addAll(ont.getClassesInSignature()); + owlObjectProperties.addAll(ont.getObjectPropertiesInSignature()); + owlDatatypeProperties.addAll(ont.getDataPropertiesInSignature()); + owlIndividuals.addAll(ont.getIndividualsInSignature()); + } + + // if several knowledge sources are included, then we can only + // guarantee that the base URI is from one of those sources (there + // can't be more than one); but we will take care that all prefixes are + // correctly imported + OWLOntologyFormat format = manager.getOntologyFormat(ontology); + if (format instanceof PrefixOWLOntologyFormat) { + prefixes.putAll(((PrefixOWLOntologyFormat) format).getPrefixName2PrefixMap()); + baseURI = ((PrefixOWLOntologyFormat) format).getDefaultPrefix(); + prefixes.remove(""); + } + + // all other sources are converted to KB and then to an // OWL API ontology } else { KB kb = source.toKB(); Added: trunk/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/OntologyToByteConverter.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,37 @@ +package org.dllearner.utilities.owl; + +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyManager; + +/** + * Created by IntelliJ IDEA. + * User: Chris Shellenbarger + * Date: 3/14/12 + * Time: 7:30 PM + * <p/> + * Interface to allow the conversion of an OWL Ontology into a byte array and back. + * <p/> + * The purpose of the interface is to allow the association of an OWLOntology object with a specified OWLOntologyManager. + * <p/> + * If someone hands us an OWLOntology, we may not want to use the associated OWLOntologyManager. Rather, we can serialize it out + * to a byte array and then read it back in with a different OWLOntologyManager. + */ +public interface OntologyToByteConverter { + + /** + * Convert the ontology into a byte array. + * + * @param ontology The ontology to convert to a byte array. + * @return The byte array representing the ontology + */ + byte[] convert(OWLOntology ontology); + + /** + * Convert bytes into an Ontology registered with manager. + * + * @param bytes The bytes to convert to an OWLOntology + * @param manager The Ontology Manager to load the ontology with. + * @return The ontology derived from bytes. + */ + OWLOntology convert(byte[] bytes, OWLOntologyManager manager); +} Added: trunk/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverter.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,55 @@ +package org.dllearner.utilities.owl; + +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.OWLOntologyStorageException; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * Created by IntelliJ IDEA. + * User: Chris Shellenbarger + * Date: 3/13/12 + * Time: 6:24 PM + * + * A Byte Array based implementation of the OntologyToByteConverter interface. + */ +public class SimpleOntologyToByteConverter implements OntologyToByteConverter { + + @Override + public byte[] convert(OWLOntology ontology) { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + OWLOntologyManager manager = ontology.getOWLOntologyManager(); + try { + manager.saveOntology(ontology,baos); + baos.close(); + } catch (OWLOntologyStorageException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return baos.toByteArray(); + } + + @Override + public OWLOntology convert(byte[] bytes, OWLOntologyManager manager) { + + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + + try { + OWLOntology ontology = manager.loadOntologyFromOntologyDocument(bais); + bais.close(); + return ontology; + } catch (OWLOntologyCreationException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} Added: trunk/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/kb/OWLAPIOntologyTest.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,47 @@ +package org.dllearner.kb; + +import org.junit.Test; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.io.IOException; + +import static org.junit.Assert.*; + +/** + * Created by IntelliJ IDEA. + * User: Chris Shellenbarger + * Date: 3/14/12 + * Time: 7:57 PM + */ +public class OWLAPIOntologyTest { + + private OWLOntology createOntology() throws OWLOntologyCreationException, IOException { + // Set up the ontology here and hide its manager - the test needs to use a different ontology manager on reconstitution + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(new OWLDataFactoryImpl()); + Resource owlFile = new ClassPathResource("/org/dllearner/kb/owl-api-ontology-data.owl"); + return manager.loadOntologyFromOntologyDocument(owlFile.getInputStream()); + } + + + @Test + public void testMethods() throws Exception { + OWLOntology ontology = createOntology(); + assertNotNull(ontology); + + OWLAPIOntology testSubject = new OWLAPIOntology(ontology); + + OWLOntology result = testSubject.createOWLOntology(OWLManager.createOWLOntologyManager(new OWLDataFactoryImpl())); + + assertNotNull(result); + assertNotSame(ontology,result); + + // Basic Equality Check - for some reason axiom count is different - the result looks more complete than the original. + assertEquals(ontology.getIndividualsInSignature().size(), result.getIndividualsInSignature().size()); + } +} Added: trunk/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/utilities/owl/OntologyToByteConverterTest.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,56 @@ +package org.dllearner.utilities.owl; + +import org.junit.Test; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Created by IntelliJ IDEA. + * User: Chris Shellenbarger + * Date: 3/14/12 + * Time: 7:29 PM + * + * Test the interface level apis for the ontology to byte converter. + */ +public abstract class OntologyToByteConverterTest { + + public abstract OntologyToByteConverter getInstance(); + + private OWLOntology createOntology() throws OWLOntologyCreationException, IOException { + // Set up the ontology here and hide its manager - the test needs to use a different ontology manager on reconstitution + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(new OWLDataFactoryImpl()); + Resource owlFile = new ClassPathResource("/org/dllearner/utilities/owl/byte-conversion-data.owl"); + return manager.loadOntologyFromOntologyDocument(owlFile.getInputStream()); + } + + @Test + public void testConversion() throws Exception { + OntologyToByteConverter converter = getInstance(); + OWLOntology ontology = createOntology(); + assertNotNull(ontology); + + byte[] bytes = converter.convert(ontology); + + assertNotNull(bytes); + assertTrue(bytes.length > 0); + + // Use a new manager so that the IRIs don't get messed up + OWLOntologyManager newManager = OWLManager.createOWLOntologyManager(new OWLDataFactoryImpl()); + OWLOntology result = converter.convert(bytes, newManager); + assertNotNull(result); + + // Basic Equality Check - for some reason axiom count is different - the result looks more complete than the original. + assertEquals(ontology.getIndividualsInSignature().size(), result.getIndividualsInSignature().size()); + } +} Added: trunk/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/utilities/owl/SimpleOntologyToByteConverterTest.java 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,17 @@ +package org.dllearner.utilities.owl; + +/** + * Created by IntelliJ IDEA. + * User: Chris Shellenbarger + * Date: 3/13/12 + * Time: 6:28 PM + * + * Test instance for a particular implementation. + */ +public class SimpleOntologyToByteConverterTest extends OntologyToByteConverterTest{ + + @Override + public OntologyToByteConverter getInstance() { + return new SimpleOntologyToByteConverter(); + } +} Added: trunk/components-core/src/test/resources/org/dllearner/kb/owl-api-ontology-data.owl =================================================================== --- trunk/components-core/src/test/resources/org/dllearner/kb/owl-api-ontology-data.owl (rev 0) +++ trunk/components-core/src/test/resources/org/dllearner/kb/owl-api-ontology-data.owl 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,33 @@ +<?xml version="1.0"?> +<rdf:RDF + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns="http://example.com/father#" + xmlns:owl="http://www.w3.org/2002/07/owl#" + xml:base="http://example.com/father"> + <owl:Ontology rdf:about=""/> + <owl:Class rdf:ID="female"/> + <owl:Class rdf:ID="male"> + <owl:equivalentClass> + <owl:Class> + <owl:complementOf rdf:resource="#female"/> + </owl:Class> + </owl:equivalentClass> + </owl:Class> + <owl:ObjectProperty rdf:ID="hasChild"/> + <male rdf:ID="markus"> + <hasChild> + <female rdf:ID="anna"> + <hasChild> + <male rdf:ID="heinz"/> + </hasChild> + </female> + </hasChild> + </male> + <male rdf:ID="stefan"> + <hasChild rdf:resource="#markus"/> + </male> + <female rdf:ID="michelle"/> + <male rdf:ID="martin"> + <hasChild rdf:resource="#heinz"/> + </male> +</rdf:RDF> Added: trunk/components-core/src/test/resources/org/dllearner/utilities/owl/byte-conversion-data.owl =================================================================== --- trunk/components-core/src/test/resources/org/dllearner/utilities/owl/byte-conversion-data.owl (rev 0) +++ trunk/components-core/src/test/resources/org/dllearner/utilities/owl/byte-conversion-data.owl 2012-03-15 02:34:07 UTC (rev 3611) @@ -0,0 +1,35 @@ +<?xml version="1.0"?> +<rdf:RDF + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:xsd="http://www.w3.org/2001/XMLSchema#" + xmlns="http://example.com/father#" + xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" + xmlns:owl="http://www.w3.org/2002/07/owl#" + xml:base="http://example.com/father"> + <owl:Ontology rdf:about=""/> + <owl:Class rdf:ID="female"/> + <owl:Class rdf:ID="male"> + <owl:equivalentClass> + <owl:Class> + <owl:complementOf rdf:resource="#female"/> + </owl:Class> + </owl:equivalentClass> + </owl:Class> + <owl:ObjectProperty rdf:ID="hasChild"/> + <male rdf:ID="markus"> + <hasChild> + <female rdf:ID="anna"> + <hasChild> + <male rdf:ID="heinz"/> + </hasChild> + </female> + </hasChild> + </male> + <male rdf:ID="stefan"> + <hasChild rdf:resource="#markus"/> + </male> + <female rdf:ID="michelle"/> + <male rdf:ID="martin"> + <hasChild rdf:resource="#heinz"/> + </male> +</rdf:RDF> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |