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.extraction;
020
021import org.apache.log4j.Logger;
022import org.semanticweb.owlapi.apibinding.OWLManager;
023import org.semanticweb.owlapi.model.*;
024import org.semanticweb.owlapi.util.SimpleIRIMapper;
025
026import java.io.File;
027
028public class OWLAPIOntologyCollector {
029        
030        private static Logger logger = Logger.getLogger(OWLAPIOntologyCollector.class);
031         
032        private OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
033        private OWLDataFactory factory;
034        private OWLOntology currentOntology;
035        private IRI ontologyIRI;
036        private IRI physicalIRI;
037         
038        
039
040        public OWLAPIOntologyCollector(){
041                 this("http://www.fragment.org/fragment", "cache/"+System.currentTimeMillis()+".owl");
042         }
043         
044         public OWLAPIOntologyCollector(String ontologyIRI, String physicalIRI){
045                 this.ontologyIRI = IRI.create(ontologyIRI);
046                 this.physicalIRI = IRI.create(new File(physicalIRI));
047                 SimpleIRIMapper mapper = new SimpleIRIMapper(this.ontologyIRI, this.physicalIRI);
048                 this.manager.getIRIMappers().add(mapper);
049                 try{
050                 this.currentOntology = manager.createOntology(this.ontologyIRI);
051                 }catch(OWLOntologyCreationException e){
052                         logger.error("FATAL failed to create Ontology " + this.ontologyIRI);
053                         e.printStackTrace();
054                 }
055                 this.factory = manager.getOWLDataFactory();
056                 
057         }
058
059         public void addAxiom(OWLAxiom axiom){
060                 AddAxiom addAxiom = new AddAxiom(currentOntology, axiom);
061                 try{
062                 manager.applyChange(addAxiom);
063                 }catch (OWLOntologyChangeException e) {
064                         e.printStackTrace();
065                }
066         }
067         
068         public OWLDataFactory getFactory() {
069                        return factory;
070                }
071         
072         public void saveOntology(){
073                 try{
074                 manager.saveOntology(currentOntology);
075                 //manager.s
076                 }catch (Exception e) {
077                        e.printStackTrace();
078                        
079                }
080         }
081
082        public OWLOntology getCurrentOntology() {
083                return currentOntology;
084        }
085
086        public IRI getPhysicalIRI() {
087                return physicalIRI;
088        }
089        
090        public int getNrOfExtractedAxioms(){
091                return currentOntology.getAxioms().size();
092        }
093
094   
095
096}