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