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 com.google.common.collect.Maps;
022import org.apache.jena.vocabulary.OWL;
023import org.dllearner.core.AbstractReasonerComponent;
024import org.semanticweb.owlapi.expression.OWLEntityChecker;
025import org.semanticweb.owlapi.model.*;
026import org.semanticweb.owlapi.util.IRIShortFormProvider;
027import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider;
028import org.semanticweb.owlapi.vocab.OWL2Datatype;
029import org.semanticweb.owlapi.vocab.XSDVocabulary;
030import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
031
032import java.util.Collection;
033import java.util.Map;
034import java.util.Set;
035
036/**
037 * @author Lorenz Buehmann
038 *
039 */
040public class SimpleOWLEntityChecker implements OWLEntityChecker{
041        
042        private static OWLDataFactory df = new OWLDataFactoryImpl();
043        private AbstractReasonerComponent rc;
044        
045        IRIShortFormProvider sfp = new SimpleIRIShortFormProvider();
046        
047        private boolean allowShortForm = false;
048
049        static Map<String, OWLDatatype> datatypeNames = Maps.newHashMap();
050        static {
051                datatypeNames.put("double", df.getDoubleOWLDatatype());
052                datatypeNames.put("int", df.getIntegerOWLDatatype());
053                datatypeNames.put("integer", df.getIntegerOWLDatatype());
054                datatypeNames.put("date", df.getOWLDatatype(XSDVocabulary.DATE.getIRI()));
055                for (OWL2Datatype o2d : OWL2Datatype.values()) {
056                        if (!o2d.getShortForm().toUpperCase().equals(o2d.getShortForm()))
057                                datatypeNames.put(o2d.getShortForm(), o2d.getDatatype(df));
058                }
059        }
060
061        public SimpleOWLEntityChecker(AbstractReasonerComponent rc) {
062                this.rc = rc;
063        }
064
065        private <T extends HasIRI> T find(String name, Collection<? extends T> c) {
066                for (T x : c) {
067                        if(allowShortForm && sfp.getShortForm(x.getIRI()).equals(name) ||
068                                        x.getIRI().toString().equals(name) ||
069                                        x.getIRI().toQuotedString().equals(name)) {
070                                return x;
071                        }
072                }
073                return null;
074        }
075
076        /* (non-Javadoc)
077         * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLClass(java.lang.String)
078         */
079        @Override
080        public OWLClass getOWLClass(String name) {
081                if ("owl:Thing".equals(name) || IRI.create(OWL.NS + "Thing").toQuotedString().equals(name)) {
082                        return df.getOWLThing();
083                }
084                OWLClass cls = find(name, rc.getClasses());
085                if (cls != null) {
086                        return cls;
087                }
088                if (allowShortForm && "Thing".equals(name)) {
089                        return df.getOWLThing();
090                }
091                return null;
092        }
093
094        /* (non-Javadoc)
095         * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLObjectProperty(java.lang.String)
096         */
097        @Override
098        public OWLObjectProperty getOWLObjectProperty(String name) {
099                return find(name, rc.getObjectProperties());
100        }
101
102        /* (non-Javadoc)
103         * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLDataProperty(java.lang.String)
104         */
105        @Override
106        public OWLDataProperty getOWLDataProperty(String name) {
107                return find(name, rc.getDatatypeProperties());
108        }
109
110        /* (non-Javadoc)
111         * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLIndividual(java.lang.String)
112         */
113        @Override
114        public OWLNamedIndividual getOWLIndividual(String name) {
115                return find(name, (Set<OWLNamedIndividual>)(Set<?>)rc.getIndividuals());
116        }
117
118        /* (non-Javadoc)
119         * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLDatatype(java.lang.String)
120         */
121        @Override
122        public OWLDatatype getOWLDatatype(String name) {
123                return datatypeNames.get(name);
124        }
125
126        /* (non-Javadoc)
127         * @see org.semanticweb.owlapi.expression.OWLEntityChecker#getOWLAnnotationProperty(java.lang.String)
128         */
129        @Override
130        public OWLAnnotationProperty getOWLAnnotationProperty(String name) {
131                return null;
132        }
133        
134        /**
135         * @param allowShortForm the allowShortForm to set
136         */
137        public void setAllowShortForm(boolean allowShortForm) {
138                this.allowShortForm = allowShortForm;
139        }
140
141}