From: <sha...@us...> - 2012-03-12 04:28:29
|
Revision: 3608 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3608&view=rev Author: shadowtm Date: 2012-03-12 04:28:22 +0000 (Mon, 12 Mar 2012) Log Message: ----------- Modified interface to require the passing in of the OWL Manager so that a common manager instance could be used for OWLFiles and KBFiles. This will assist with multithreading in a threaded environment (webserver). Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/KBFile.java trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java Modified: trunk/components-core/src/main/java/org/dllearner/kb/KBFile.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/KBFile.java 2012-03-12 02:14:08 UTC (rev 3607) +++ trunk/components-core/src/main/java/org/dllearner/kb/KBFile.java 2012-03-12 04:28:22 UTC (rev 3608) @@ -64,7 +64,6 @@ @ConfigOption(name = "fileName", description = "relative or absolute path to KB file") private String fileName; - private OWLOntology owlOntology; /** * Default constructor (needed for reflection in ComponentManager). @@ -112,7 +111,6 @@ kb = KBParser.parseKBFile(f); } - owlOntology = createOWLOntology(kb); logger.trace("KB File " + getUrl() + " parsed successfully."); } else { throw new ComponentInitException("No URL option or kb object given. Cannot initialise KBFile component."); @@ -127,16 +125,9 @@ } } - /** - * Create the OWL Ontology. - * - * @param kb The kb to create the ontology on top of. - * @return The OWL Ontology - */ - private OWLOntology createOWLOntology(KB kb) { - //This call is potentially dangerous in a multi-threaded(web) environment - I believe it returns a singleton instance - // There are ways around this, but getting it to work single threaded first. - OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + @Override + public OWLOntology createOWLOntology(OWLOntologyManager manager) { + IRI ontologyURI = IRI.create("http://example.com"); OWLOntology ontology; try { @@ -151,10 +142,10 @@ } /* - * (non-Javadoc) - * - * @see org.dllearner.core.KnowledgeSource#toDIG() - */ + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#toDIG() + */ @Override public String toDIG(URI kbURI) { return DIGConverter.getDIGString(kb, kbURI).toString(); @@ -202,9 +193,4 @@ public void setFileName(String fileName) { this.fileName = fileName; } - - @Override - public OWLOntology getOWLOntology() { - return owlOntology; - } } Modified: trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java 2012-03-12 02:14:08 UTC (rev 3607) +++ trunk/components-core/src/main/java/org/dllearner/kb/OWLFile.java 2012-03-12 04:28:22 UTC (rev 3608) @@ -22,6 +22,7 @@ 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.LinkedList; @@ -33,7 +34,6 @@ import org.dllearner.core.ComponentInitException; import org.dllearner.core.OntologyFormat; import org.dllearner.core.OntologyFormatUnsupportedException; -import org.dllearner.core.config.ListStringEditor; import org.dllearner.core.options.ConfigEntry; import org.dllearner.core.options.ConfigOption; import org.dllearner.core.options.InvalidConfigOptionValueException; @@ -41,6 +41,10 @@ import org.dllearner.core.owl.KB; import org.dllearner.reasoning.OWLAPIDIGConverter; import org.dllearner.utilities.URLencodeUTF8; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; /** * @author Jens Lehmann @@ -49,7 +53,7 @@ * SH: I added SPARQL capabilities. Either URL is set directly or the basedir and filename is set or the URL and the SPARQL query is set */ @ComponentAnn(name = "OWL File", shortName = "owlfile", version = 0.9) -public class OWLFile extends AbstractKnowledgeSource { +public class OWLFile extends AbstractKnowledgeSource implements OWLOntologyKnowledgeSource { private static Logger logger = Logger.getLogger(OWLFile.class); @@ -133,9 +137,22 @@ throw new RuntimeException(e); } } + } + @Override + public OWLOntology createOWLOntology(OWLOntologyManager manager) { + try { + OWLOntology ontology = manager.loadOntologyFromOntologyDocument(IRI.create(getURL().toURI())); + return ontology; + } catch (OWLOntologyCreationException e) { + throw new RuntimeException(e); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + /* * (non-Javadoc) * Modified: trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java 2012-03-12 02:14:08 UTC (rev 3607) +++ trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java 2012-03-12 04:28:22 UTC (rev 3608) @@ -1,6 +1,7 @@ package org.dllearner.kb; import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyManager; /** * Created by IntelliJ IDEA. @@ -12,11 +13,11 @@ */ public interface OWLOntologyKnowledgeSource { - /** - * Get the OWL Ontology that this object represents. + * Create an OWL Ontology associated with the specified manager. * - * @return The OWL ontology that this object represents. + * @param manager The manager to associate the new ontology with. + * @return The result ontology */ - public OWLOntology getOWLOntology(); + public OWLOntology createOWLOntology(OWLOntologyManager manager); } 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-12 02:14:08 UTC (rev 3607) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java 2012-03-12 04:28:22 UTC (rev 3608) @@ -46,7 +46,6 @@ import java.io.File; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.util.*; import java.util.Map.Entry; @@ -153,25 +152,23 @@ 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(); manager = ontology.getOWLOntologyManager(); + owlAPIOntologies.add(ontology); } else if (source instanceof SparqlKnowledgeSource) { ontology = ((SparqlKnowledgeSource) source).getOWLAPIOntology(); manager = ontology.getOWLOntologyManager(); - } else { - ontology = manager.loadOntologyFromOntologyDocument(IRI.create(url.toURI())); + owlAPIOntologies.add(ontology); } - owlAPIOntologies.add(ontology); directImports.addAll(ontology.getImportsDeclarations()); try { // imports includes the ontology itself @@ -180,33 +177,28 @@ 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()); - } - - } catch (UnknownOWLOntologyException uooe) { - logger.error("UnknownOWLOntologyException occured, imports were not loaded! This is a bug, which has not been fixed yet."); + 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 (UnknownOWLOntologyException uooe) { + logger.error("UnknownOWLOntologyException occured, imports were not loaded! This is a bug, which has not been fixed yet."); + } - } catch (OWLOntologyCreationException e) { - throw new RuntimeException(e); - } catch (URISyntaxException e) { - throw new RuntimeException(e); + // 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 { @@ -214,12 +206,8 @@ //KB Files KB kb = source.toKB(); - if(source instanceof OWLOntologyKnowledgeSource){ - ontology = ((OWLOntologyKnowledgeSource) source).getOWLOntology(); - } else { - -// System.out.println(kb.toString(null,null)); - + if (!(source instanceof OWLOntologyKnowledgeSource)) { + //Not sure if this will ever get hit, but leaving in for backward compatibility. IRI ontologyURI = IRI.create("http://example.com"); ontology = null; try { @@ -228,8 +216,9 @@ throw new RuntimeException(e); } OWLAPIAxiomConvertVisitor.fillOWLOntology(manager, ontology, kb); + owlAPIOntologies.add(ontology); } - owlAPIOntologies.add(ontology); + allImports.add(ontology); atomicConcepts.addAll(kb.findAllAtomicConcepts()); atomicRoles.addAll(kb.findAllAtomicRoles()); @@ -238,6 +227,8 @@ } } try { + //The following line illustrates a problem with using different OWLOntologyManagers. This can manifest itself if we have multiple sources who were created with different manager instances. + //ontology = OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://dl-learner/all"), new HashSet<OWLOntology>(owlAPIOntologies)); ontology = manager.createOntology(IRI.create("http://dl-learner/all"), new HashSet<OWLOntology>(owlAPIOntologies)); //we have to add all import declarations manually here, because this are no axioms List<OWLOntologyChange> addImports = new ArrayList<OWLOntologyChange>(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |