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;
020
021import org.apache.jena.datatypes.BaseDatatype;
022import org.apache.jena.datatypes.RDFDatatype;
023import org.apache.jena.datatypes.xsd.XSDDatatype;
024import org.apache.jena.graph.Node;
025import org.apache.jena.graph.NodeFactory;
026import org.apache.jena.graph.impl.LiteralLabel;
027import org.apache.jena.graph.impl.LiteralLabelFactory;
028import org.apache.jena.rdf.model.Literal;
029import org.apache.jena.rdf.model.Model;
030import org.apache.jena.rdf.model.ModelFactory;
031import org.apache.jena.rdf.model.Statement;
032import org.apache.jena.rdf.model.impl.LiteralImpl;
033import org.semanticweb.owlapi.apibinding.OWLManager;
034import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
035import org.semanticweb.owlapi.model.*;
036import org.semanticweb.owlapi.vocab.Namespaces;
037import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
038
039import java.io.IOException;
040import java.io.PipedInputStream;
041import java.io.PipedOutputStream;
042import java.util.List;
043import java.util.Set;
044
045/**
046 * @author Lorenz Buehmann
047 *
048 */
049public class OwlApiJenaUtils {
050        
051        private static OWLDataFactory dataFactory = new OWLDataFactoryImpl();
052
053        private static final OWLOntologyManager man = OWLManager.createOWLOntologyManager();
054
055        private static int ONT_COUNTER = 0;
056
057        /**
058         * Converts a JENA API model into an OWL API ontology.
059         * @param model the JENA API model
060         * @return the OWL API ontology
061         */
062        public static OWLOntology getOWLOntology(final Model model) {
063                OWLOntology ontology;
064
065                try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is)) {
066                        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
067                        new Thread(new Runnable() {
068                                @Override
069                                public void run() {
070                                        model.write(os, "TURTLE", null);
071                                        try {
072                                                os.close();
073                                        } catch (IOException e) {
074                                                e.printStackTrace();
075                                        }
076                                }
077                        }).start();
078                        ontology = man.loadOntologyFromOntologyDocument(is);
079                        return ontology;
080                } catch (Exception e) {
081                        throw new RuntimeException("Could not convert JENA API model to OWL API ontology.", e);
082                }
083        }
084
085        /**
086         * Converts an OWL API ontology into a JENA API model.
087         * @param ontology the OWL API ontology
088         * @return the JENA API model
089         */
090        public static Model getModel(final OWLOntology ontology) {
091                Model model = ModelFactory.createDefaultModel();
092
093                try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is)) {
094                        new Thread(new Runnable() {
095                                @Override
096                                public void run() {
097                                        try {
098                                                ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os);
099                                                os.close();
100                                        } catch (OWLOntologyStorageException | IOException e) {
101                                                e.printStackTrace();
102                                        }
103                                }
104                        }).start();
105                        model.read(is, null, "TURTLE");
106                        return model;
107                } catch (Exception e) {
108                        throw new RuntimeException("Could not convert OWL API ontology to JENA API model.", e);
109                }
110        }
111
112        /**
113         * Convert statements from JENA API  into OWL API axioms.
114         * @param statements the JENA API statements
115         * @return the set of axioms
116         */
117        public static Set<OWLAxiom> asOWLAxioms(List<Statement> statements) {
118                Model model = ModelFactory.createDefaultModel();
119                model.add(statements);
120                OWLOntology ontology = getOWLOntology(model);
121                return ontology.getAxioms();
122        }
123
124        /**
125         * Converts a JENA API literal into an OWL API literal.
126         * @param lit the JENA API literal
127         * @return the OWL API literal
128         */
129        public static OWLLiteral getOWLLiteral(Literal lit){
130                OWLLiteral literal = null;
131                if(lit.getDatatypeURI() != null){
132                        OWLDatatype datatype = dataFactory.getOWLDatatype(IRI.create(lit.getDatatypeURI()));
133                        literal = dataFactory.getOWLLiteral(lit.getLexicalForm(), datatype);
134                } else {
135                        if(lit.getLanguage() != null){
136                                literal = dataFactory.getOWLLiteral(lit.getLexicalForm(), lit.getLanguage());
137                        } else {
138                                literal = dataFactory.getOWLLiteral(lit.getLexicalForm());
139                        }
140                }
141                return literal;
142        }
143
144        /**
145         * Converts a JENA API literal into an OWL API literal.
146         * @param lit the JENA API literal
147         * @return the OWL API literal
148         */
149        public static OWLLiteral getOWLLiteral(LiteralLabel lit){
150                return getOWLLiteral(new LiteralImpl(NodeFactory.createLiteral(lit), null));
151        }
152
153        /**
154         * Converts an OWL API literal into a JENA API literal.
155         * @param lit the OWL API literal
156         * @return the JENA API literal
157         */
158        public static LiteralLabel getLiteral(OWLLiteral lit){
159                OWLDatatype owlDatatype = lit.getDatatype();
160
161                RDFDatatype datatype;
162                if(Namespaces.XSD.inNamespace(owlDatatype.getIRI())){
163                        datatype = new XSDDatatype(owlDatatype.getIRI().getRemainder().get());
164                } else {
165                        datatype = new BaseDatatype(lit.getDatatype().toStringID());
166                }
167                if(lit.hasLang()) {
168                        return LiteralLabelFactory.create(lit.getLiteral(), lit.getLang());
169                } else {
170                        return LiteralLabelFactory.create(lit.getLiteral(), datatype);
171                }
172        }
173        
174        /**
175         * Convert OWL axioms from OWL API into JENA API statements.
176         * @param axioms the OWL API axioms
177         * @return the JENA statements
178         */
179        public static Set<Statement> asStatements(Set<OWLAxiom> axioms) {
180                try {
181                        OWLOntology ontology = man.createOntology(axioms, IRI.create("http://dllearner.org/converter" + ONT_COUNTER++));
182                        Model model = getModel(ontology);
183                        return model.listStatements().toSet();
184                } catch (OWLOntologyCreationException e) {
185                        throw new RuntimeException("Conversion of axioms failed.", e);
186                }
187        }
188        
189        /**
190         * Convert an OWL API entity into a JENA API node.
191         * @param entity the OWL API entity
192         * @return the JENA API node
193         */
194        public static Node asNode(OWLEntity entity) {
195                return NodeFactory.createURI(entity.toStringID());
196        }
197        
198        /**
199         * Convert a JENA Node into an OWL entity of the given type.
200         * @param node the JENA node
201         * @param entityType the type of the OWL entity, e.g. class, property, etc.
202         * @return the OWL entity
203         */
204        public static <T extends OWLEntity> T asOWLEntity(Node node, EntityType<T> entityType) {
205                return dataFactory.getOWLEntity(entityType, IRI.create(node.getURI()));
206        }
207}