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.utilities.owl;
021
022 import java.io.ByteArrayOutputStream;
023 import java.io.StringWriter;
024 import java.io.UnsupportedEncodingException;
025
026 import org.coode.owlapi.owlxml.renderer.OWLXMLObjectRenderer;
027 import org.coode.owlapi.owlxml.renderer.OWLXMLWriter;
028 import org.coode.owlapi.turtle.TurtleOntologyFormat;
029 import org.dllearner.core.owl.Axiom;
030 import org.dllearner.utilities.StringFormatter;
031 import org.semanticweb.owlapi.apibinding.OWLManager;
032 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
033 import org.semanticweb.owlapi.model.AddAxiom;
034 import org.semanticweb.owlapi.model.IRI;
035 import org.semanticweb.owlapi.model.OWLAxiom;
036 import org.semanticweb.owlapi.model.OWLClassExpression;
037 import org.semanticweb.owlapi.model.OWLOntology;
038 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
039 import org.semanticweb.owlapi.model.OWLOntologyManager;
040 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
041 import org.semanticweb.owlapi.util.ShortFormProvider;
042 import org.semanticweb.owlapi.util.SimpleShortFormProvider;
043
044 import uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxObjectRenderer;
045
046 /**
047 * A collection of various render methods provided by
048 * OWL API.
049 *
050 * @author Jens Lehmann
051 *
052 */
053 public class OWLAPIRenderers {
054
055 /**
056 * Converts an OWL API axiom to a Manchester OWL syntax string.
057 *
058 * @param description Input OWLAxiom.
059 * @return Manchester OWL syntax string.
060 */
061 public static String toManchesterOWLSyntax(OWLAxiom description) {
062 StringWriter sw = new StringWriter();
063 ShortFormProvider sfp = new SimpleShortFormProvider();
064 ManchesterOWLSyntaxObjectRenderer renderer = new ManchesterOWLSyntaxObjectRenderer(sw, sfp);
065 description.accept(renderer);
066 return sw.toString();
067 }
068
069 /**
070 * Converts an OWL API description to a Manchester OWL syntax string.
071 *
072 * @param description Input OWLDescription.
073 * @return Manchester OWL syntax string.
074 */
075 public static String toManchesterOWLSyntax(OWLClassExpression description) {
076 StringWriter sw = new StringWriter();
077 ShortFormProvider sfp = new SimpleShortFormProvider();
078 ManchesterOWLSyntaxObjectRenderer renderer = new ManchesterOWLSyntaxObjectRenderer(sw, sfp);
079 description.accept(renderer);
080 return sw.toString();
081 }
082
083 /**
084 * Converts an OWL API description to an OWL/XML syntax string.
085 *
086 * @param description Input OWLDescription.
087 * @return OWL/XML syntax string.
088 */
089 public static String toOWLXMLSyntax(OWLClassExpression description) {
090 StringWriter sw = new StringWriter();
091 try {
092 OWLXMLWriter oxw = new OWLXMLWriter(sw, OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://example.com/")));
093 OWLXMLObjectRenderer renderer = new OWLXMLObjectRenderer(oxw);
094 description.accept(renderer);
095 } catch (OWLOntologyCreationException e) {
096 e.printStackTrace();
097 }
098 return sw.toString();
099 }
100
101 public static String toOWLXMLSyntax(Axiom axiom) {
102 return toOWLXMLSyntax(OWLAPIAxiomConvertVisitor.convertAxiom(axiom));
103 }
104
105 public static String toOWLXMLSyntax(OWLAxiom axiom) {
106 StringWriter sw = new StringWriter();
107 try {
108 OWLXMLWriter oxw = new OWLXMLWriter(sw, OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://example.com/")));
109 OWLXMLObjectRenderer renderer = new OWLXMLObjectRenderer(oxw);
110 axiom.accept(renderer);
111 } catch (OWLOntologyCreationException e) {
112 e.printStackTrace();
113 }
114 return sw.toString();
115 }
116
117 public static String toRDFXMLSyntax(Axiom axiom) {
118 return toRDFXMLSyntax(OWLAPIAxiomConvertVisitor.convertAxiom(axiom));
119 }
120
121 public static String toRDFXMLSyntax(OWLAxiom axiom) {
122 ByteArrayOutputStream out = new ByteArrayOutputStream();
123 String str = "";
124 try {
125 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
126 OWLOntology ontology = manager.createOntology(IRI.create("http://example.com/"));
127 manager.applyChange(new AddAxiom(ontology, axiom));
128 manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
129 str = new String(out.toByteArray(), "UTF-8");
130 } catch (OWLOntologyCreationException e) {
131 e.printStackTrace();
132 } catch (OWLOntologyStorageException e) {
133 e.printStackTrace();
134 } catch (UnsupportedEncodingException e) {
135 e.printStackTrace();
136 }
137 return str;
138 }
139
140 public static String toTurtleSyntax(Axiom axiom) {
141 return toTurtleSyntax(OWLAPIAxiomConvertVisitor.convertAxiom(axiom), false);
142 }
143
144 // short version = a lot of stuff thrown out (not a standalone Turtle file anymore)
145 public static String toTurtleSyntax(Axiom axiom, boolean shortVersion) {
146 return toTurtleSyntax(OWLAPIAxiomConvertVisitor.convertAxiom(axiom), shortVersion);
147 }
148
149 public static String toTurtleSyntax(OWLAxiom axiom, boolean shortVersion) {
150 ByteArrayOutputStream out = new ByteArrayOutputStream();
151 String str = "";
152 try {
153 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
154 OWLOntology ontology = manager.createOntology(IRI.create("http://example.com/"));
155 manager.applyChange(new AddAxiom(ontology, axiom));
156 manager.saveOntology(ontology, new TurtleOntologyFormat(), out);
157 str = new String(out.toByteArray(), "UTF-8");
158 } catch (OWLOntologyCreationException e) {
159 e.printStackTrace();
160 } catch (OWLOntologyStorageException e) {
161 e.printStackTrace();
162 } catch (UnsupportedEncodingException e) {
163 e.printStackTrace();
164 }
165 if(shortVersion) {
166 String[] lines = str.split("\n");
167 String shortStr = "";
168 for(String line : lines) {
169 if(!line.startsWith("@prefix") &&
170 !line.startsWith("@base") &&
171 !line.startsWith("#") &&
172 !line.startsWith("<http://example.com/>") &&
173 !(StringFormatter.isWhitespace(line))) {
174 shortStr += line + "\n";
175 }
176 }
177 return shortStr;
178 }
179
180 return str;
181 }
182
183 }