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 org.semanticweb.owlapi.model.OWLClassExpression;
022import org.semanticweb.owlapi.model.OWLDataFactory;
023import org.semanticweb.owlapi.util.MaximumModalDepthFinder;
024import org.semanticweb.owlapi.util.OWLObjectDuplicator;
025import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
026
027import java.util.Set;
028
029/**
030 * A utility class for OWL class expressions.
031 * 
032 * @author Lorenz Buehmann
033 */
034public class OWLClassExpressionUtils {
035        
036        private static OWLDataFactory dataFactory = new OWLDataFactoryImpl();
037        private static OWLObjectDuplicator duplicator = new OWLObjectDuplicator(dataFactory);
038        private static final OWLClassExpressionLengthCalculator LENGTH_CALCULATOR= new OWLClassExpressionLengthCalculator();
039        private static final MaximumModalDepthFinder DEPTH_FINDER = new MaximumModalDepthFinder();
040        private static final OWLClassExpressionChildrenCollector CHILDREN_COLLECTOR = new OWLClassExpressionChildrenCollector();
041        
042        /**
043         * Returns the length of a given class expression. 
044         * @param ce the class expression
045         * @return the length of the class expression
046         */
047        public static int getLength(OWLClassExpression ce){
048                OWLClassExpressionLengthCalculator calculator = new OWLClassExpressionLengthCalculator();
049                return calculator.getLength(ce);
050        }
051
052        public static int getLength(OWLClassExpression ce, OWLClassExpressionLengthMetric metric) {
053                OWLClassExpressionLengthCalculator calculator = new OWLClassExpressionLengthCalculator(metric);
054                return calculator.getLength(ce);
055        }
056
057        /**
058         * Returns the depth of a class expression. 
059         * @param ce the class expression
060         * @return the depth of the class expression
061         */
062        public static synchronized int getDepth(OWLClassExpression ce){
063                return ce.accept(DEPTH_FINDER);
064        }
065        
066        /**
067         * Returns the arity of a class expression. 
068         * @param ce the class expression
069         * @return the arity of the class expression
070         */
071        public static synchronized int getArity(OWLClassExpression ce){
072                return getChildren(ce).size();
073        }
074        
075        /**
076         * Returns all direct child expressions of a class expression.
077         * @param ce the class expression
078         * @return the direct child expressions
079         */
080        public static Set<OWLClassExpression> getChildren(OWLClassExpression ce){
081                return ce.accept(CHILDREN_COLLECTOR);
082        }
083        
084        /**
085         * Returns a clone of the given class expression.
086         * @param ce the class expression
087         * @return a class expression clone
088         */
089        public static OWLClassExpression clone(OWLClassExpression ce) {
090                return duplicator.duplicateObject(ce);
091        }
092        
093        /**
094         * Determine whether a named class occurs on the outermost level of a class expression, i.e. property depth 0
095         * (it can still be at higher depth, e.g. if intersections are nested in unions)
096         * @param description the class expression
097         * @param cls the named class
098         * @return whether the named class occurs on the outermost level of the class expression
099         */
100        public static boolean occursOnFirstLevel(OWLClassExpression description, OWLClassExpression cls) {
101                return description.containsConjunct(cls);
102        }
103
104}