001    /**
002     * Copyright (C) 2007-2008, 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    package org.dllearner.scripts;
019    
020    import java.io.File;
021    import java.net.URI;
022    import java.util.HashSet;
023    import java.util.Set;
024    
025    import org.dllearner.core.KnowledgeSource;
026    import org.dllearner.kb.OWLFile;
027    import org.dllearner.reasoning.OWLAPIReasoner;
028    import org.dllearner.utilities.owl.OntologyCloserOWLAPI;
029    
030    /**
031     * Script for closing an ontology OWLAPI produces extensive filesizes, when
032     * exporting output file ist named like input file, but recieves a
033     * "_closedConcise" at the end.
034     * 
035     * Counts all roles of individuals and adds an Intersection (Concise) of
036     * ExactCardinalityRestriction to the ABox
037     * 
038     */
039    public class CloseOntology {
040    
041            /**
042             * @param args
043             *            simply the path to the owl ontology "examples/test.owl"
044             */
045            public static void main(String[] args) {
046                    String ontopath=""; 
047                    //ontopath="examples/carcinogenesis/carcinogenesis.owl";
048                    // inputURI
049                    //ontopath = args[0];
050                    File file = new File(ontopath);
051                    URI inputURI = file.toURI();
052    
053                    // outputURI
054                    String ending = ontopath.substring(ontopath.lastIndexOf(".") + 1);
055                    ontopath = ontopath.replace("." + ending, "_closedConcise." + ending);
056                    file = new File(ontopath);
057                    URI outputURI = file.toURI();
058    
059                    try {
060                            // initializing reasoner
061                            OWLFile owlFile = new OWLFile();
062                            owlFile.setURL(inputURI.toURL());
063                            Set<KnowledgeSource> ks = new HashSet<KnowledgeSource>();
064                            ks.add(owlFile);
065                            OWLAPIReasoner owlapireasoner = new OWLAPIReasoner(ks);
066                            owlapireasoner.init();
067    
068                            // close
069                            OntologyCloserOWLAPI oc = new OntologyCloserOWLAPI(owlapireasoner);
070                            oc.testForTransitiveProperties(true);
071                            System.out.println("Attempting to close");
072                            oc.applyNumberRestrictionsConcise();
073                            System.out.println("Finished, preparing output");
074    
075                            // save
076                            oc.writeOWLFile(outputURI);
077    
078                    } catch (Exception e) {
079                            e.printStackTrace();
080                    }
081            }
082    
083    }