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.utilities.owl;
020
021import org.dllearner.utilities.StringFormatter;
022import org.semanticweb.owlapi.apibinding.OWLManager;
023import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
024import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
025import org.semanticweb.owlapi.model.*;
026import org.semanticweb.owlapi.owlxml.renderer.OWLXMLObjectRenderer;
027import org.semanticweb.owlapi.owlxml.renderer.OWLXMLWriter;
028
029import java.io.ByteArrayOutputStream;
030import java.io.StringWriter;
031import java.io.UnsupportedEncodingException;
032
033/**
034 * A collection of various render methods provided by 
035 * OWL API.
036 * 
037 * @author Jens Lehmann
038 *
039 */
040public class OWLAPIRenderers {
041        
042        private static final ManchesterOWLSyntaxOWLObjectRendererImplExt manchesterRenderer = new ManchesterOWLSyntaxOWLObjectRendererImplExt();
043        private static final DLSyntaxObjectRendererExt dlSyntaxRenderer = new DLSyntaxObjectRendererExt();
044        
045        static {
046                manchesterRenderer.setUseWrapping(false);
047                manchesterRenderer.setUseTabbing(false);
048        }
049        
050        /**
051         * Converts an OWL API object to a DL syntax string.
052         * 
053         * @param owlObject input OWL API object
054         * @return Manchester OWL syntax string.
055         */
056        public static String toDLSyntax(OWLObject owlObject) {
057                return dlSyntaxRenderer.render(owlObject);
058        }
059        
060        /**
061         * Converts an OWL API axiom to a Manchester OWL syntax string.
062         * 
063         * @param axiom input OWL axiom
064         * @return Manchester OWL syntax string.
065         */
066        public static String toManchesterOWLSyntax(OWLAxiom axiom) {
067                return manchesterRenderer.render(axiom);
068        }       
069        
070        /**
071         * Converts an OWL API OWLClassExpression to a Manchester OWL syntax string.
072         * 
073         * @param ce input OWL class expression
074         * @return Manchester OWL syntax string.
075         */
076        public static String toManchesterOWLSyntax(OWLClassExpression ce) {
077                return manchesterRenderer.render(ce);
078        }
079        
080        /**
081         * Converts an OWL API object to an OWL/XML syntax string.
082         * 
083         * @param obj Input OWL object.
084         * @return OWL/XML syntax string.
085         */
086        public static String toOWLXMLSyntax(OWLObject obj) {
087                StringWriter sw = new StringWriter();
088                try {
089                        OWLXMLWriter oxw = new OWLXMLWriter(sw, OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://example.com/")));
090                        OWLXMLObjectRenderer renderer = new OWLXMLObjectRenderer(oxw);
091                        obj.accept(renderer);
092                } catch (OWLOntologyCreationException e) {
093                        e.printStackTrace();
094                }
095                return sw.toString();
096        }       
097
098        public static String toRDFXMLSyntax(OWLAxiom axiom) {
099                ByteArrayOutputStream out = new ByteArrayOutputStream();
100                String str = "";
101                try {
102                        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
103                        OWLOntology ontology = manager.createOntology(IRI.create("http://example.com/"));
104                        manager.applyChange(new AddAxiom(ontology, axiom));
105                        manager.saveOntology(ontology, new RDFXMLDocumentFormat(), out);
106                        str = new String(out.toByteArray(), "UTF-8");
107                } catch (OWLOntologyCreationException | OWLOntologyStorageException | UnsupportedEncodingException e) {
108                        e.printStackTrace();
109                }
110                return str;
111        }               
112        
113        public static String toTurtleSyntax(OWLAxiom axiom, boolean shortVersion) {
114                ByteArrayOutputStream out = new ByteArrayOutputStream();
115                String str = "";
116                try {
117                        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
118                        OWLOntology ontology = manager.createOntology(IRI.create("http://example.com/"));
119                        manager.applyChange(new AddAxiom(ontology, axiom));
120                        manager.saveOntology(ontology, new TurtleDocumentFormat(), out);
121                        str = new String(out.toByteArray(), "UTF-8");
122                } catch (OWLOntologyCreationException | UnsupportedEncodingException | OWLOntologyStorageException e) {
123                        e.printStackTrace();
124                }
125                if(shortVersion) {
126                        String[] lines = str.split("\n");
127                        String shortStr = "";
128                        for(String line : lines) {
129                                if(!line.startsWith("@prefix") && 
130                                   !line.startsWith("@base") && 
131                                   !line.startsWith("#") &&
132                                   !line.startsWith("<http://example.com/>") &&
133                                   !(StringFormatter.isWhitespace(line))) {
134                                        shortStr += line + "\n";
135                                }
136                        }
137                        return shortStr;
138                }
139                
140                return str;
141        }       
142        
143}