OWLIO.java
Go to the documentation of this file.
00001 /* \file OWLIO.java
00002  * \brief I/O class for OWL ontolgies
00003  *
00004  * The OWLIO class provides means to load/save OWL ontologies to/from various sources/targets.
00005  * 
00006  * This file is part of the RoboEarth ROS re_comm_core package.
00007  * 
00008  * It was originally created for <a href="http://www.roboearth.org/">RoboEarth</a>.
00009  * The research leading to these results has received funding from the 
00010  * European Union Seventh Framework Programme FP7/2007-2013 
00011  * under grant agreement no248942 RoboEarth.
00012  *
00013  * Copyright (C) 2010 by 
00014  * <a href=" mailto:perzylo@cs.tum.edu">Alexander Perzylo</a>
00015  * Technische Universitaet Muenchen
00016  * 
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions are met:
00019  * 
00020  *    <UL>
00021  *     <LI> Redistributions of source code must retain the above copyright
00022  *       notice, this list of conditions and the following disclaimer.
00023  *     <LI> Redistributions in binary form must reproduce the above copyright
00024  *       notice, this list of conditions and the following disclaimer in the
00025  *       documentation and/or other materials provided with the distribution.
00026  *     <LI> Neither the name of Willow Garage, Inc. nor the names of its
00027  *       contributors may be used to endorse or promote products derived from
00028  *       this software without specific prior written permission.
00029  *    </UL>
00030  * 
00031  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00032  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00034  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00035  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00036  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00037  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00038  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00039  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00040  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00041  * POSSIBILITY OF SUCH DAMAGE.
00042  *
00043  * \author Alexander Perzylo
00044  * \version 1.0
00045  * \date 2010
00046  * \image html http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00047  * \image latex http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00048  */
00049 package roboearth.wp5.owl;
00050 
00051 import java.io.ByteArrayInputStream;
00052 import java.io.ByteArrayOutputStream;
00053 import java.io.File;
00054 import java.io.FileNotFoundException;
00055 import java.io.IOException;
00056 import java.io.InputStream;
00057 import java.io.OutputStream;
00058 import java.io.UnsupportedEncodingException;
00059 import java.net.UnknownHostException;
00060 import java.util.Map;
00061 
00062 import org.semanticweb.owlapi.apibinding.OWLManager;
00063 import org.semanticweb.owlapi.io.OWLOntologyCreationIOException;
00064 import org.semanticweb.owlapi.io.OWLParser;
00065 import org.semanticweb.owlapi.io.OWLParserException;
00066 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
00067 import org.semanticweb.owlapi.io.UnparsableOntologyException;
00068 import org.semanticweb.owlapi.model.IRI;
00069 import org.semanticweb.owlapi.model.OWLOntology;
00070 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
00071 import org.semanticweb.owlapi.model.OWLOntologyFormat;
00072 import org.semanticweb.owlapi.model.OWLOntologyManager;
00073 import org.semanticweb.owlapi.model.UnloadableImportException;
00074 import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
00075 
00084 public class OWLIO {
00085 
00086 
00090         public static final OWLOntologyFormat ONTOLOGY_FORMAT_RDFXML = new RDFXMLOntologyFormat();
00091 
00092 
00098         public static OWLOntology loadOntologyFromWeb(String url) {
00099 
00100                 OWLOntology ontology = null;
00101 
00102                 try {
00103                         // Get hold of an ontology manager
00104                         OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00105 
00106                         // Load the ontology from the web
00107                         IRI iri = IRI.create(url);
00108                         ontology = manager.loadOntologyFromOntologyDocument(iri);
00109                 } catch (OWLOntologyCreationIOException e) {
00110                         // IOExceptions during loading get wrapped in an OWLOntologyCreationIOException
00111                         IOException ioException = e.getCause();
00112                         if(ioException instanceof UnknownHostException) {
00113                                 System.out.println("Could not load ontology. Unknown host: " + ioException.getMessage());
00114                         }
00115                         else {
00116                                 System.out.println("Could not load ontology: " + ioException.getClass().getSimpleName() + " " + ioException.getMessage());    
00117                         }
00118                 } catch (UnparsableOntologyException e) {
00119                         // If there was a problem loading an ontology because there are syntax errors in the document (file) that
00120                         // represents the ontology then an UnparsableOntologyException is thrown
00121                         System.out.println("Could not parse the ontology: " + e.getMessage());
00122                         // A map of errors can be obtained from the exception
00123                         Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
00124                         // The map describes which parsers were tried and what the errors were
00125                         for(OWLParser parser : exceptions.keySet()) {
00126                                 System.out.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
00127                                 System.out.println("Failed because: " + exceptions.get(parser).getMessage());
00128                         }
00129                 } catch (UnloadableImportException e) {
00130                         // If our ontology contains imports and one or more of the imports could not be loaded then an
00131                         // UnloadableImportException will be thrown (depending on the missing imports handling policy)
00132                         System.out.println("Could not load import: " + e.getImportsDeclaration());
00133                         // The reason for this is specified and an OWLOntologyCreationException
00134                         OWLOntologyCreationException cause = e.getOntologyCreationException();
00135                         System.out.println("Reason: " + cause.getMessage());
00136                 } catch (OWLOntologyCreationException e) {
00137                         System.out.println("Could not load ontology: " + e.getMessage());
00138                 } catch (Exception e) {
00139                         System.out.println("Could not load ontology due to unknown error: " + e.getMessage());
00140                 }
00141 
00142                 return ontology;
00143 
00144         }
00145 
00146 
00152         public static OWLOntology loadOntologyFromFile(String file) {
00153 
00154                 OWLOntology ontology = null;
00155 
00156                 try {
00157                         // Build File object
00158                         File f = new File(file);
00159 
00160                         // Get hold of an ontology manager
00161                         OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00162 
00163                         // Now load the local copy
00164                         ontology = manager.loadOntologyFromOntologyDocument(f);
00165 
00166                 } catch (OWLOntologyCreationIOException e) {
00167                         // IOExceptions during loading get wrapped in an OWLOntologyCreationIOException
00168                         IOException ioException = e.getCause();
00169                         if(ioException instanceof FileNotFoundException) {
00170                                 System.err.println("Could not load ontology. File not found: " + ioException.getMessage());
00171                         }
00172                         else {
00173                                 System.err.println("Could not load ontology: " + ioException.getClass().getSimpleName() + " " + ioException.getMessage());    
00174                         }
00175                 } catch (UnparsableOntologyException e) {
00176                         // If there was a problem loading an ontology because there are syntax errors in the document (file) that
00177                         // represents the ontology then an UnparsableOntologyException is thrown
00178                         System.err.println("Could not parse the ontology: " + e.getMessage());
00179                         // A map of errors can be obtained from the exception
00180                         Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
00181                         // The map describes which parsers were tried and what the errors were
00182                         for(OWLParser parser : exceptions.keySet()) {
00183                                 System.err.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
00184                                 System.err.println("Failed because: " + exceptions.get(parser).getMessage());
00185                         }
00186                 } catch (UnloadableImportException e) {
00187                         // If our ontology contains imports and one or more of the imports could not be loaded then an
00188                         // UnloadableImportException will be thrown (depending on the missing imports handling policy)
00189                         System.err.println("Could not load import: " + e.getImportsDeclaration());
00190                         // The reason for this is specified and an OWLOntologyCreationException
00191                         OWLOntologyCreationException cause = e.getOntologyCreationException();
00192                         System.err.println("Reason: " + cause.getMessage());
00193                 } catch (OWLOntologyCreationException e) {
00194                         System.err.println("Could not load ontology: " + e.getMessage());
00195                 } catch (Exception e) {
00196                         System.err.println("Could not load ontology due to unknown error: " + e.getMessage());
00197                 }
00198 
00199                 return ontology;
00200 
00201         }
00202 
00203 
00209         public static OWLOntology loadOntologyFromStream(InputStream stream) {
00210 
00211                 OWLOntology ontology = null;
00212 
00213                 try {
00214 
00215                         // Get hold of an ontology manager
00216                         OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00217 
00218                         // Now load the local copy
00219                         ontology = manager.loadOntologyFromOntologyDocument(stream);
00220 
00221                 } catch (OWLOntologyCreationIOException e) {
00222                         // IOExceptions during loading get wrapped in an OWLOntologyCreationIOException
00223                         IOException ioException = e.getCause();
00224                         if(ioException instanceof FileNotFoundException) {
00225                                 System.out.println("Could not load ontology. File not found: " + ioException.getMessage());
00226                         }
00227                         else {
00228                                 System.out.println("Could not load ontology: " + ioException.getClass().getSimpleName() + " " + ioException.getMessage());    
00229                         }
00230                 } catch (UnparsableOntologyException e) {
00231                         // If there was a problem loading an ontology because there are syntax errors in the document that
00232                         // represents the ontology then an UnparsableOntologyException is thrown
00233                         System.out.println("Could not parse the ontology: " + e.getMessage());
00234                         // A map of errors can be obtained from the exception
00235                         Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
00236                         // The map describes which parsers were tried and what the errors were
00237                         for(OWLParser parser : exceptions.keySet()) {
00238                                 System.out.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
00239                                 System.out.println("Failed because: " + exceptions.get(parser).getMessage());
00240                         }
00241                 } catch (UnloadableImportException e) {
00242                         // If our ontology contains imports and one or more of the imports could not be loaded then an
00243                         // UnloadableImportException will be thrown (depending on the missing imports handling policy)
00244                         System.out.println("Could not load import: " + e.getImportsDeclaration());
00245                         // The reason for this is specified and an OWLOntologyCreationException
00246                         OWLOntologyCreationException cause = e.getOntologyCreationException();
00247                         System.out.println("Reason: " + cause.getMessage());
00248                 } catch (OWLOntologyCreationException e) {
00249                         System.out.println("Could not load ontology: " + e.getMessage());
00250                 } catch (Exception e) {
00251                         System.out.println("Could not load ontology due to unknown error: " + e.getMessage());
00252                 }
00253 
00254                 return ontology;
00255 
00256         }
00257 
00258 
00264         public static OWLOntology loadOntologyFromString(String s) {
00265 
00266                 InputStream inStream = new ByteArrayInputStream(s.getBytes());
00267                 OWLOntology ontology = OWLIO.loadOntologyFromStream(inStream);
00268 
00269                 return ontology;
00270 
00271         }
00272 
00273 
00281         public static boolean saveOntologyToFile(OWLOntology ontology, String file) {
00282 
00283                 boolean ok = false;
00284 
00285                 try {
00286                         OWLOntologyFormat format = ontology.getOWLOntologyManager().getOntologyFormat(ontology);
00287                         ok = saveOntologyToFile(ontology, file, format);                        
00288                 } catch (NullPointerException e) {
00289                         System.out.println("Could not save ontology: null pointer argument found\n" + e.getMessage());
00290                 }
00291 
00292                 return ok; 
00293 
00294         }
00295 
00296 
00305         public static boolean saveOntologyToFile(OWLOntology ontology, String file, OWLOntologyFormat format) {
00306 
00307                 boolean ok = false;
00308 
00309                 try {
00310 
00311                         // Build File object
00312                         File f = new File(file);
00313 
00314                         // Get hold of the ontology manager
00315                         OWLOntologyManager manager = ontology.getOWLOntologyManager();
00316 
00317                         // By default ontologies are saved in the format from which they were loaded.
00318                         // We can get information about the format of an ontology from its manager
00319                         OWLOntologyFormat currFormat = manager.getOntologyFormat(ontology);
00320 
00321                         // The Document IRI, where the file should be saved
00322                         IRI documentIRI = IRI.create(f.toURI());
00323 
00324                         if (currFormat.equals(format)) {
00325 
00326                                 // Save a local copy of the ontology.
00327                                 manager.saveOntology(ontology, documentIRI);
00328 
00329                         } else {
00330 
00331                                 // Some ontology formats support prefix names and prefix IRIs. When we save the ontology in
00332                                 // the new format we will copy the prefixes over so that we have nicely abbreviated IRIs in
00333                                 // the new ontology document
00334                                 if (format.isPrefixOWLOntologyFormat() && currFormat.isPrefixOWLOntologyFormat()) {
00335                                         ((PrefixOWLOntologyFormat)format).copyPrefixesFrom(currFormat.asPrefixOWLOntologyFormat());
00336                                 }
00337                                 manager.saveOntology(ontology, format, documentIRI);
00338 
00339                         }
00340 
00341                         ok = true;
00342 
00343                 } catch (Exception e) {
00344                         System.out.println("Could not save ontology: " + e.getMessage());
00345                 }
00346 
00347                 return ok;
00348         }
00349 
00350 
00358         public static boolean saveOntologyToStream(OWLOntology ontology, OutputStream stream) {
00359 
00360                 boolean ok = false;
00361 
00362                 try {
00363                         OWLOntologyFormat format = ontology.getOWLOntologyManager().getOntologyFormat(ontology);
00364                         ok = saveOntologyToStream(ontology, stream, format);                    
00365                 } catch (NullPointerException e) {
00366                         System.out.println("Could not save ontology: null pointer argument found\n" + e.getMessage());
00367                 }
00368 
00369                 return ok; 
00370 
00371         }
00372 
00373 
00382         public static boolean saveOntologyToStream(OWLOntology ontology, OutputStream stream, OWLOntologyFormat format) {
00383 
00384                 boolean ok = false;
00385 
00386                 if (stream != null) {
00387 
00388                         try {
00389 
00390                                 // Get hold of the ontology manager
00391                                 OWLOntologyManager manager = ontology.getOWLOntologyManager();
00392 
00393                                 // By default ontologies are saved in the format from which they were loaded.
00394                                 // We can get information about the format of an ontology from its manager
00395                                 OWLOntologyFormat currFormat = manager.getOntologyFormat(ontology);
00396 
00397                                 if (currFormat.equals(format)) {
00398 
00399                                         // Save a copy of the ontology to the given stream.
00400                                         manager.saveOntology(ontology, stream);
00401 
00402                                 } else {
00403 
00404                                         // Some ontology formats support prefix names and prefix IRIs. When we save the ontology in
00405                                         // the new format we will copy the prefixes over so that we have nicely abbreviated IRIs in
00406                                         // the new ontology document
00407                                         if (format.isPrefixOWLOntologyFormat() && currFormat.isPrefixOWLOntologyFormat()) {
00408                                                 ((PrefixOWLOntologyFormat)format).copyPrefixesFrom(currFormat.asPrefixOWLOntologyFormat());
00409                                         }
00410                                         manager.saveOntology(ontology, format, stream);
00411 
00412                                 }
00413 
00414                                 ok = true;
00415 
00416                         } catch (Exception e) {
00417                                 System.out.println("Could not save ontology: " + e.getMessage());
00418                         }
00419 
00420                 }
00421 
00422                 return ok;
00423         }
00424 
00425 
00431         public static String saveOntologyToString(OWLOntology ontology) {
00432 
00433                 return saveOntologyToString(ontology, ontology.getOWLOntologyManager().getOntologyFormat(ontology));
00434 
00435         }       
00436 
00437 
00444         public static String saveOntologyToString(OWLOntology ontology, OWLOntologyFormat format) {
00445 
00446                 String s = null;
00447                 ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
00448 
00449                 if (saveOntologyToStream(ontology, os, format)) {
00450                         try {
00451                                 s = new String(os.toByteArray(), "UTF-8");
00452                         } catch (UnsupportedEncodingException e) {
00453                                 System.out.println("UTF-8 encoding is unsupported: " + e.getMessage());
00454                         }                       
00455                 }
00456 
00457                 return s;
00458 
00459         }
00460 
00461 
00462 }


re_comm_core
Author(s): Alexander Perzylo
autogenerated on Sun Jan 5 2014 11:29:33