001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.kb;
020
021import org.apache.log4j.Logger;
022import org.dllearner.core.AbstractKnowledgeSource;
023import org.dllearner.core.ComponentAnn;
024import org.dllearner.core.ComponentInitException;
025import org.dllearner.core.config.ConfigOption;
026import org.dllearner.parser.KBParser;
027import org.dllearner.parser.ParseException;
028import org.semanticweb.owlapi.model.OWLOntology;
029import org.semanticweb.owlapi.model.OWLOntologyCreationException;
030import org.semanticweb.owlapi.model.OWLOntologyManager;
031
032import java.io.FileNotFoundException;
033import java.io.IOException;
034import java.net.MalformedURLException;
035import java.net.URI;
036import java.net.URL;
037import java.nio.file.Path;
038import java.nio.file.Paths;
039
040/**
041 * KB files are an internal convenience format used in DL-Learner. Their
042 * syntax is close to Description Logics and easy to use. KB files can be
043 * exported to OWL for usage outside of DL-Learner.
044 *
045 * @author Jens Lehmann
046 */
047@ComponentAnn(name = "KB File", shortName = "kbfile", version = 0.8)
048public class KBFile extends AbstractKnowledgeSource implements OWLOntologyKnowledgeSource {
049
050    private static Logger logger = Logger.getLogger(KBFile.class);
051
052    private OWLOntology kb;
053
054    @ConfigOption(description = "URL pointer to the KB file")
055    private String url;
056    
057    @ConfigOption(description="change the base directory (must be absolute)",defaultValue="directory of conf file")
058    private String baseDir;
059    @ConfigOption(description = "relative or absolute path to KB file")
060    private String fileName;
061
062    /**
063     * Default constructor (needed for reflection in ComponentManager).
064     */
065    public KBFile() {
066    }
067
068    /**
069     * Constructor allowing you to treat an already existing OWL ontology
070     * as a KBFile knowledge source.
071     *
072     * @param kb A KB object.
073     */
074    public KBFile(OWLOntology kb) {
075        this.kb = kb;
076    }
077
078    @Override
079    public void init() throws ComponentInitException {
080        try {
081                if (url == null) {
082                        /* Copyied from OWLFile */
083                                Path path = Paths.get(fileName);
084
085                                if(!path.isAbsolute() && baseDir != null) {// else relative to base directory
086                                    path = Paths.get(baseDir, fileName);
087                                }
088
089                        URI uri = path.normalize().toUri();
090                        setUrl(uri.toURL().toString());
091                }
092
093                kb = KBParser.parseKBFile(new URL(getUrl()));
094                logger.trace("KB File " + getUrl() + " parsed successfully.");
095
096        } catch (ParseException e) {
097                throw new ComponentInitException("KB file " + getUrl() + " could not be parsed correctly.", e);
098        } catch (FileNotFoundException e) {
099                throw new ComponentInitException("KB file " + getUrl() + " could not be found.", e);
100        } catch (OWLOntologyCreationException e) {
101                throw new ComponentInitException("KB file " + getUrl() + " could not converted to OWL ontology.", e);
102        } catch (MalformedURLException e) {
103                throw new ComponentInitException("KB file URL " + getUrl() + " is invalid.", e);
104        } catch (IOException e) {
105                throw new ComponentInitException("KB file " + getUrl() + " could not be read.", e);
106        }
107        
108        initialized = true;
109    }
110
111    @Override
112    public OWLOntology createOWLOntology(OWLOntologyManager manager) {
113        return kb;
114    }
115
116    @Override
117    public String toString() {
118        if (kb == null)
119            return "KB file (not initialised)";
120        else
121            return kb.toString();
122    }
123
124    public String getUrl() {
125        return url;
126    }
127
128    public void setUrl(String url) {
129        this.url = url;
130    }
131
132    public String getBaseDir() {
133        return baseDir;
134    }
135
136    public void setBaseDir(String baseDir) {
137        this.baseDir = baseDir;
138    }
139
140    public String getFileName() {
141        return fileName;
142    }
143
144    public void setFileName(String fileName) {
145        this.fileName = fileName;
146    }
147
148}