From: <sha...@us...> - 2012-03-12 02:14:15
|
Revision: 3607 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3607&view=rev Author: shadowtm Date: 2012-03-12 02:14:08 +0000 (Mon, 12 Mar 2012) Log Message: ----------- Added OWLOntologyKnowledgeSource to support abstraction of the ontology creation so that we can introduce different implementations more easily. In particular, this will come in handy for the REST service interface. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/kb/KBFile.java trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.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-09 09:19:31 UTC (rev 3606) +++ trunk/components-core/src/main/java/org/dllearner/kb/KBFile.java 2012-03-12 02:14:08 UTC (rev 3607) @@ -36,61 +36,67 @@ import org.dllearner.parser.KBParser; import org.dllearner.parser.ParseException; import org.dllearner.reasoning.DIGConverter; -import org.springframework.beans.propertyeditors.StringTrimmerEditor; +import org.dllearner.utilities.owl.OWLAPIAxiomConvertVisitor; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; /** * KB files are an internal convenience format used in DL-Learner. Their * syntax is close to Description Logics and easy to use. KB files can be * exported to OWL for usage outside of DL-Learner. - * + * * @author Jens Lehmann - * */ @ComponentAnn(name = "KB File", shortName = "kbfile", version = 0.8) -public class KBFile extends AbstractKnowledgeSource { +public class KBFile extends AbstractKnowledgeSource implements OWLOntologyKnowledgeSource { - private static Logger logger = Logger.getLogger(KBFile.class); - - private KB kb; + private static Logger logger = Logger.getLogger(KBFile.class); - @ConfigOption(name = "url", description = "URL pointer to the KB file") + private KB kb; + + @ConfigOption(name = "url", description = "URL pointer to the KB file") private String url; private String baseDir; @ConfigOption(name = "fileName", description = "relative or absolute path to KB file") private String fileName; - /** - * Default constructor (needed for reflection in ComponentManager). - */ - public KBFile() { - } - - /** - * Constructor allowing you to treat an already existing KB object - * as a KBFile knowledge source. Use it sparingly, because the - * standard way to create components is via - * {@link org.dllearner.core.ComponentManager}. - * - * @param kb A KB object. - */ - public KBFile(KB kb) { - this.kb = kb; - } - - public static Collection<org.dllearner.core.options.ConfigOption<?>> createConfigOptions() { - Collection<org.dllearner.core.options.ConfigOption<?>> options = new LinkedList<org.dllearner.core.options.ConfigOption<?>>(); + private OWLOntology owlOntology; + + /** + * Default constructor (needed for reflection in ComponentManager). + */ + public KBFile() { + } + + /** + * Constructor allowing you to treat an already existing KB object + * as a KBFile knowledge source. Use it sparingly, because the + * standard way to create components is via + * {@link org.dllearner.core.ComponentManager}. + * + * @param kb A KB object. + */ + public KBFile(KB kb) { + this.kb = kb; + } + + public static Collection<org.dllearner.core.options.ConfigOption<?>> createConfigOptions() { + Collection<org.dllearner.core.options.ConfigOption<?>> options = new LinkedList<org.dllearner.core.options.ConfigOption<?>>(); // options.add(new StringConfigOption("filename", "pointer to the KB file on local file system",null, true, true)); - URLConfigOption urlOption = new URLConfigOption("url", "URL pointer to the KB file",null, false, true); - urlOption.setRefersToFile(true); - options.add(urlOption); - return options; - } - - public static String getName() { - return "KB file"; - } - + URLConfigOption urlOption = new URLConfigOption("url", "URL pointer to the KB file", null, false, true); + urlOption.setRefersToFile(true); + options.add(urlOption); + return options; + } + + public static String getName() { + return "KB file"; + } + @Override public void init() throws ComponentInitException { try { @@ -106,6 +112,7 @@ 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."); @@ -113,46 +120,69 @@ } catch (ParseException e) { throw new ComponentInitException("KB file " + getUrl() + " could not be parsed correctly.", e); - }catch (FileNotFoundException e) { + } catch (FileNotFoundException e) { throw new ComponentInitException("KB file " + getUrl() + " could not be found.", e); } catch (URISyntaxException e) { - throw new ComponentInitException("KB file " + getUrl() + " could not be found.", e); - } + throw new ComponentInitException("KB file " + getUrl() + " could not be found.", e); + } } - /* - * (non-Javadoc) - * - * @see org.dllearner.core.KnowledgeSource#toDIG() - */ - @Override - public String toDIG(URI kbURI) { - return DIGConverter.getDIGString(kb, kbURI).toString(); - } - - @Override - public String toString() { - if(kb==null) - return "KB file (not initialised)"; - else - return kb.toString(); - } - - @Override - public void export(File file, org.dllearner.core.OntologyFormat format){ - kb.export(file, format); - } - - public String getUrl() { - return url; - } + /** + * 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(); + IRI ontologyURI = IRI.create("http://example.com"); + OWLOntology ontology; + try { + ontology = manager.createOntology(ontologyURI); + OWLAPIAxiomConvertVisitor.fillOWLOntology(manager, ontology, kb); - @Override - public KB toKB() { - return kb; - } + } catch (OWLOntologyCreationException e) { + throw new RuntimeException(e); + } + return ontology; + } + /* + * (non-Javadoc) + * + * @see org.dllearner.core.KnowledgeSource#toDIG() + */ + @Override + public String toDIG(URI kbURI) { + return DIGConverter.getDIGString(kb, kbURI).toString(); + } + + @Override + public String toString() { + if (kb == null) + return "KB file (not initialised)"; + else + return kb.toString(); + } + + @Override + public void export(File file, org.dllearner.core.OntologyFormat format) { + kb.export(file, format); + } + + public String getUrl() { + return url; + } + + @Override + public KB toKB() { + return kb; + } + + public void setUrl(String url) { this.url = url; } @@ -165,11 +195,16 @@ this.baseDir = baseDir; } - public String getFileName() { - return fileName; - } + public String getFileName() { + return fileName; + } - public void setFileName(String fileName) { - this.fileName = fileName; - } + public void setFileName(String fileName) { + this.fileName = fileName; + } + + @Override + public OWLOntology getOWLOntology() { + return owlOntology; + } } Added: trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/kb/OWLOntologyKnowledgeSource.java 2012-03-12 02:14:08 UTC (rev 3607) @@ -0,0 +1,22 @@ +package org.dllearner.kb; + +import org.semanticweb.owlapi.model.OWLOntology; + +/** + * Created by IntelliJ IDEA. + * User: Chris Shellenbarger + * Date: 3/11/12 + * Time: 6:36 PM + * + * This interface represents objects which can return an OWLOntology representation of itself. + */ +public interface OWLOntologyKnowledgeSource { + + + /** + * Get the OWL Ontology that this object represents. + * + * @return The OWL ontology that this object represents. + */ + public OWLOntology getOWLOntology(); +} 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-09 09:19:31 UTC (rev 3606) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/OWLAPIReasoner.java 2012-03-12 02:14:08 UTC (rev 3607) @@ -30,6 +30,7 @@ import org.dllearner.core.owl.*; 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.owl.*; import org.semanticweb.HermiT.Reasoner.ReasonerFactory; @@ -209,17 +210,25 @@ // all other sources are converted to KB and then to an // OWL API ontology } else { + + //KB Files KB kb = source.toKB(); + + if(source instanceof OWLOntologyKnowledgeSource){ + ontology = ((OWLOntologyKnowledgeSource) source).getOWLOntology(); + } else { + // System.out.println(kb.toString(null,null)); - IRI ontologyURI = IRI.create("http://example.com"); - ontology = null; - try { - ontology = manager.createOntology(ontologyURI); - } catch (OWLOntologyCreationException e) { - throw new RuntimeException(e); + IRI ontologyURI = IRI.create("http://example.com"); + ontology = null; + try { + ontology = manager.createOntology(ontologyURI); + } catch (OWLOntologyCreationException e) { + throw new RuntimeException(e); + } + OWLAPIAxiomConvertVisitor.fillOWLOntology(manager, ontology, kb); } - OWLAPIAxiomConvertVisitor.fillOWLOntology(manager, ontology, kb); owlAPIOntologies.add(ontology); allImports.add(ontology); atomicConcepts.addAll(kb.findAllAtomicConcepts()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |