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 java.util.HashSet;
022import java.util.Set;
023
024import org.semanticweb.owlapi.model.IRI;
025import org.semanticweb.owlapi.model.OWLClass;
026import org.semanticweb.owlapi.model.OWLNamedIndividual;
027import org.semanticweb.owlapi.model.OWLObjectProperty;
028import org.semanticweb.owlapi.model.OWLOntology;
029
030import uk.ac.manchester.cs.owl.owlapi.OWLNamedIndividualImpl;
031import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl;
032
033/**
034 * @author Lorenz Buehmann
035 *
036 */
037public class OWLPunningDetector {
038        
039        /**
040         * This object property is used to connect individuals with classes that are also individuals, thus, lead to punning.
041         */
042        public static final OWLObjectProperty punningProperty = 
043                        new OWLObjectPropertyImpl(IRI.create("http://dl-learner.org/punning/relatedTo"));
044        
045        /**
046         * Checks whether the class is also used as individual in the ontology.
047         * @param ontology the ontology
048         * @param cls the class
049         * @return whether the class is also used as individual in the ontology
050         */
051        public static boolean hasPunning(OWLOntology ontology, OWLClass cls){
052                return hasPunning(ontology, cls.getIRI());
053        }
054        
055        /**
056         * Checks whether the same IRI denotes both a class and an individual in the ontology.
057         * @param ontology ontology the ontology
058         * @param iri the IRI
059         * @return whether the IRI denotes both a class and an individual
060         */
061        public static boolean hasPunning(OWLOntology ontology, IRI iri){
062                boolean isClass = ontology.getClassesInSignature().contains(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(iri));
063                boolean isIndividual = ontology.getIndividualsInSignature().contains(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLNamedIndividual(iri));
064                return isClass && isIndividual;
065        }
066        
067        /**
068         * Returns the classes of the ontology that are also used as individuals, i.e. types of other classes.
069         * @param ontology the ontology
070         * @return the classes
071         */
072        public static Set<OWLClass> getPunningClasses(OWLOntology ontology){
073                Set<OWLClass> classes = new HashSet<>();
074                Set<OWLNamedIndividual> individualsInSignature = ontology.getIndividualsInSignature();
075                for (OWLClass cls : ontology.getClassesInSignature()) {
076                        if(individualsInSignature.contains(new OWLNamedIndividualImpl(cls.getIRI()))){
077                                classes.add(cls);
078                        }
079//                      for (OWLNamedIndividual ind : ontology.getIndividualsInSignature()) {
080//                              if(cls.getIRI().equals(ind.getIRI())){
081//                                      classes.add(cls);
082//                                      break;
083//                              }
084//                      }
085                }
086                return classes;
087        }
088        
089        /**
090         * Returns the classes of the ontology that are also used as individuals, i.e. types of other classes.
091         * @param ontology the ontology
092         * @return the classes
093         */
094        public static Set<IRI> getPunningIRIs(OWLOntology ontology){
095                Set<IRI> classes = new HashSet<>();
096                Set<OWLNamedIndividual> individualsInSignature = ontology.getIndividualsInSignature();
097                for (OWLClass cls : ontology.getClassesInSignature()) {
098                        if(individualsInSignature.contains(new OWLNamedIndividualImpl(cls.getIRI()))){
099                                classes.add(cls.getIRI());
100                        }
101//                      for (OWLNamedIndividual ind : ontology.getIndividualsInSignature()) {
102//                              if(cls.getIRI().equals(ind.getIRI())){
103//                                      classes.add(cls);
104//                                      break;
105//                              }
106//                      }
107                }
108                return classes;
109        }
110        
111        /**
112         * Checks whether the same IRI denotes both a class and an individual in the ontology.
113         * @param ontology the ontology
114         * @param iri the IRI
115         * @return whether the same IRI denotes both a class and an individual
116         */
117        public static boolean hasPunning(OWLOntology ontology, String iri){
118                return hasPunning(ontology, IRI.create(iri));
119        }
120
121}