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.LinkedList;
022
023import org.jetbrains.annotations.NotNull;
024import org.semanticweb.owlapi.model.OWLClass;
025import org.semanticweb.owlapi.model.OWLClassExpression;
026import org.semanticweb.owlapi.model.OWLDataFactory;
027import org.semanticweb.owlapi.model.OWLObjectProperty;
028import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
029
030import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;
031import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
032
033/**
034 * A property context is a utility class which specifies the
035 * position of constructs with respect to properties of a 
036 * construct in a class description. For instance, the A
037 * in \exists r.\exists s.A occurs in property context [r,s].
038 * 
039 * @author Jens Lehmann
040 *
041 */
042public class PropertyContext extends LinkedList<OWLObjectProperty> implements Comparable<PropertyContext> {
043
044        private static final long serialVersionUID = -4403308689522524077L;
045        private static final OWLClass OWL_THING = new OWLClassImpl(OWLRDFVocabulary.OWL_THING.getIRI());
046        private static final OWLDataFactory df = new OWLDataFactoryImpl();
047
048        /* (non-Javadoc)
049         * @see java.lang.Comparable#compareTo(java.lang.Object)
050         */
051        @Override
052        public int compareTo(@NotNull PropertyContext context) {
053                // we first distinguish on size - simpler contexts come first
054                int diff = context.size() - size();
055                if(diff != 0) {
056                        return diff;
057                }
058                        
059                for(int i=0; i<size(); i++) {
060                        int cmp = get(i).toStringID().compareTo(context.get(i).toStringID());
061                        if(cmp != 0) {
062                                return cmp;
063                        }
064                }
065                
066                return 0;
067        }
068
069        /**
070         * Transforms context [r,s] to \exists r.\exists s.\top.
071         * @return A OWLClassExpression with existential quantifiers and \top corresponding
072         * to the context.
073         */
074        public OWLClassExpression toExistentialContext() {
075                OWLClassExpression d = OWL_THING;
076                for(int i = size()-1; i>=0; i--) {
077                        d = df.getOWLObjectSomeValuesFrom(get(i), d);
078                }
079                return d;
080        }
081        
082}