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.net.URI;
023    import java.util.Set;
024    import java.util.SortedSet;
025    import java.util.TreeSet;
026    
027    import org.dllearner.core.owl.Axiom;
028    import org.dllearner.core.owl.Constant;
029    import org.dllearner.core.owl.Datatype;
030    import org.dllearner.core.owl.DatatypeProperty;
031    import org.dllearner.core.owl.Description;
032    import org.dllearner.core.owl.Entity;
033    import org.dllearner.core.owl.Individual;
034    import org.dllearner.core.owl.NamedClass;
035    import org.dllearner.core.owl.Nothing;
036    import org.dllearner.core.owl.OWL2Datatype;
037    import org.dllearner.core.owl.ObjectProperty;
038    import org.dllearner.core.owl.Thing;
039    import org.dllearner.core.owl.TypedConstant;
040    import org.dllearner.core.owl.UntypedConstant;
041    import org.semanticweb.owlapi.apibinding.OWLManager;
042    import org.semanticweb.owlapi.model.IRI;
043    import org.semanticweb.owlapi.model.OWLAxiom;
044    import org.semanticweb.owlapi.model.OWLClass;
045    import org.semanticweb.owlapi.model.OWLClassExpression;
046    import org.semanticweb.owlapi.model.OWLDataFactory;
047    import org.semanticweb.owlapi.model.OWLDataProperty;
048    import org.semanticweb.owlapi.model.OWLDatatype;
049    import org.semanticweb.owlapi.model.OWLEntity;
050    import org.semanticweb.owlapi.model.OWLIndividual;
051    import org.semanticweb.owlapi.model.OWLLiteral;
052    import org.semanticweb.owlapi.model.OWLNamedIndividual;
053    import org.semanticweb.owlapi.model.OWLObjectProperty;
054    
055    /**
056     * A collection of methods for exchanging objects between OWL API and
057     * DL-Learner.
058     * 
059     * @author Jens Lehmann
060     *
061     */
062    public final class OWLAPIConverter {
063    
064            private static OWLDataFactory staticFactory = OWLManager.createOWLOntologyManager().getOWLDataFactory();
065            
066            /**
067             * Converts a DL-Learner axiom into an OWL API axiom.
068             * 
069             * @see OWLAPIAxiomConvertVisitor#convertAxiom(Axiom)
070             * @param axiom The axiom to convert.
071             * @return An OWL API axiom.
072             */
073            public static OWLAxiom getOWLAPIAxiom(Axiom axiom) {
074                    return OWLAPIAxiomConvertVisitor.convertAxiom(axiom);
075            }       
076            
077            /**
078             * Converts a DL-Learner description into an OWL API description.
079             * 
080             * @see OWLAPIDescriptionConvertVisitor#getOWLClassExpression(org.dllearner.core.owl.Description)
081             * @param description DL-Learner description.
082             * @return Corresponding OWL API description.
083             */
084            public static OWLClassExpression getOWLAPIDescription(Description description) {
085                    return OWLAPIDescriptionConvertVisitor.getOWLClassExpression(description);
086            }
087            
088            public static OWLIndividual getOWLAPIIndividual(Individual individual) {
089                    return staticFactory.getOWLNamedIndividual(IRI.create(individual.getName()));
090            }       
091            
092            public static Set<OWLIndividual> getOWLAPIIndividuals(Set<Individual> individuals) {
093                    Set<OWLIndividual> inds = new TreeSet<OWLIndividual>();
094                    for(Individual individual : individuals) {
095                            inds.add(getOWLAPIIndividual(individual));
096                    }
097                    return inds;
098            }       
099            
100            public static OWLObjectProperty getOWLAPIObjectProperty(ObjectProperty role) {
101                    return staticFactory.getOWLObjectProperty(IRI.create(role.getName()));
102            }
103            
104            public static OWLDataProperty getOWLAPIDataProperty(DatatypeProperty datatypeProperty) {
105                    return staticFactory.getOWLDataProperty(IRI.create(datatypeProperty.getName()));
106            }
107            
108            public static OWLEntity getOWLAPIEntity(Entity entity) {
109                    if(entity instanceof ObjectProperty) {
110                            return staticFactory.getOWLObjectProperty(IRI.create(entity.getName()));
111                    } else if(entity instanceof DatatypeProperty) {
112                            return staticFactory.getOWLDataProperty(IRI.create(entity.getName()));  
113                    } else if(entity instanceof NamedClass) {
114                            return staticFactory.getOWLClass(IRI.create(entity.getName()));                 
115                    } else if(entity instanceof Individual) {
116                            return staticFactory.getOWLNamedIndividual(IRI.create(entity.getName()));                                               
117                    }
118                    // should never happen
119                    throw new Error("OWL API entity conversion for " + entity + " not supported.");
120            }
121            
122            public static Individual convertIndividual(OWLNamedIndividual individual) {
123                    return new Individual(individual.getIRI().toString());
124            }
125            
126            public static Set<Individual> convertIndividuals(Set<? extends OWLIndividual> individuals) {
127                    Set<Individual> inds = new TreeSet<Individual>();
128                    for(OWLIndividual individual : individuals) {
129                            inds.add(convertIndividual(individual.asOWLNamedIndividual()));
130                    }
131                    return inds;
132            }       
133            
134            public static ObjectProperty convertObjectProperty(OWLObjectProperty property) {
135                    return new ObjectProperty(property.getIRI().toString());
136            }
137            
138            public static DatatypeProperty convertIndividual(OWLDataProperty property) {
139                    return new DatatypeProperty(property.getIRI().toString());
140            }       
141            
142            public static Description convertClass(OWLClass owlClass) {
143                    if(owlClass.isOWLThing()) {
144                            return Thing.instance;
145                    } else if(owlClass.isOWLNothing()) {
146                            return Nothing.instance;
147                    } else {
148                            return new NamedClass(owlClass.getIRI().toString());
149                    }
150            }       
151            
152            public static Constant convertConstant(OWLLiteral constant) {
153                    Constant c;
154                    // for typed constants we have to figure out the correct
155                    // data type and value
156    
157                    if(constant.isRDFPlainLiteral()){
158                            if(constant.hasLang()){
159                                    c = new UntypedConstant(constant.getLiteral(), constant.getLang());
160                            } else {
161                                    c = new UntypedConstant(constant.getLiteral());
162                            }
163                    } else {
164                            Datatype dt = OWLAPIConverter.convertDatatype(constant.getDatatype());
165                    c = new TypedConstant(constant.getLiteral(),dt);
166                    }
167           
168                    return c;
169            }
170    
171            public static Set<Constant> convertConstants(Set<OWLLiteral> constants) {
172                    SortedSet<Constant> is = new TreeSet<Constant>();
173                    for(OWLLiteral oi : constants) {
174                            is.add(convertConstant(oi));
175                    }               
176                    return is;                      
177            }               
178            
179            public static Datatype convertDatatype(OWLDatatype dataType) {
180                    URI uri = dataType.getIRI().toURI();
181                    if(uri.equals(OWL2Datatype.BOOLEAN.getURI()))
182                            return OWL2Datatype.BOOLEAN.getDatatype();
183                    else if(uri.equals(OWL2Datatype.DOUBLE.getURI()))
184                            return OWL2Datatype.DOUBLE.getDatatype();
185                    else if(uri.equals(OWL2Datatype.INT.getURI()))
186                            return OWL2Datatype.INT.getDatatype();                  
187                    else if(uri.equals(OWL2Datatype.INTEGER.getURI()))
188                            return OWL2Datatype.INTEGER.getDatatype();                      
189                    else if(uri.equals(OWL2Datatype.STRING.getURI()))
190                            return OWL2Datatype.STRING.getDatatype();                       
191                    else if(uri.equals(OWL2Datatype.DATE.getURI()))
192                            return OWL2Datatype.DATE.getDatatype();
193                    else if(uri.equals(OWL2Datatype.DATETIME.getURI()))
194                            return OWL2Datatype.DATETIME.getDatatype();
195                    throw new Error("Unsupported datatype " + dataType + ". Please inform a DL-Learner developer to add it.");
196            }
197    
198    }