001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.sms;
031
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.Iterator;
035import java.util.List;
036import java.util.Set;
037import java.util.Vector;
038
039import org.semanticweb.owl.model.OWLAnnotation;
040import org.semanticweb.owl.model.OWLDescription;
041import org.semanticweb.owl.model.OWLOntology;
042import org.semanticweb.owl.model.OWLProperty;
043import org.semanticweb.owl.vocab.OWLRDFVocabulary;
044
045/**
046 * 
047 */
048public class NamedOntProperty implements Comparable {
049
050        private OWLProperty _ontProperty;
051        private String _name;
052        private OWLOntology _ontology;
053        private NamedOntModel _model;
054
055        public NamedOntProperty(OWLProperty ontProperty, OWLOntology ontology, NamedOntModel model) {
056                _ontProperty = ontProperty;
057                _ontology = ontology;
058                _model = model;
059                String str = ontProperty.getAnnotations(ontology, OWLRDFVocabulary.RDFS_LABEL.getURI()).toArray(new OWLAnnotation[1])[0].getAnnotationValue().toString();
060//              String str = ontProperty.getLabel(null);
061                if (str != null)
062                        _name = str;
063                else
064                        _name = ontProperty.toString();
065        }
066
067        public String toString() {
068                return getName();
069        }
070
071        /**
072         * @return The domain of the property (a set of NamedOntClasses)
073         */
074        public Iterator getDomain(boolean sorted) {
075                Set<OWLDescription> domains = _ontProperty.getDomains(_ontology);
076                List<NamedOntClass> classes = new ArrayList<NamedOntClass>();
077                for (OWLDescription domain : domains) {
078                        classes.add(new NamedOntClass(domain.asOWLClass(), _ontology));
079                }
080                if (sorted) {
081                        Collections.sort(classes);
082                }
083                return classes.iterator();
084        }
085
086        /**
087         * FIXME: Need to support non class ranges
088         */
089        public Iterator getRange(boolean sorted) {
090                List<NamedOntClass> classes = new ArrayList<NamedOntClass>();
091                Set<OWLDescription> ranges = _ontProperty.getRanges(_ontology);
092                for (OWLDescription range : ranges) {
093                        classes.add(new NamedOntClass(range.asOWLClass(), _ontology));
094                }
095
096                if (sorted) {
097                        Collections.sort(classes);
098                }
099                return classes.iterator();
100        }
101
102//      /**
103//       * Given a resource, returns its ontology class if it has one. and null
104//       * otherwise.
105//       */
106//      // NOTE: Not used
107//      private NamedOntClass getOntClass(OntResource res) {
108//              String lname = res.getLocalName();
109//              String nspace = res.getNameSpace();
110//              OntologyCatalog catalog = OntologyCatalog.instance();
111//              return catalog.getNamedOntClass(nspace, lname);
112//      }
113
114//      // NOTE: Not used
115//      private NamedOntClass getOntClass(OWLDescription res) {
116//              res.
117//              String lname = res.getLocalName();
118//              String nspace = res.getNameSpace();
119//              OntologyCatalog catalog = OntologyCatalog.instance();
120//              return catalog.getNamedOntClass(nspace, lname);
121//      }
122        
123        public String getName() {
124                return _name;
125        }
126
127        /**
128     */
129        public Iterator getNamedSuperProperties(boolean sorted) {
130                Vector<NamedOntProperty> result = new Vector<NamedOntProperty>();
131                Set<OWLProperty> properties = _ontProperty.getSuperProperties(_ontology);
132                for (OWLProperty property : properties) {
133                        result.add(new NamedOntProperty(property, _ontology, _model));
134                }
135                if (sorted) {
136                        Collections.sort(result);
137                }
138                return result.iterator();
139        }
140
141        /**
142     */
143        public Iterator getNamedSubProperties(boolean sorted) {
144                Vector<NamedOntProperty> result = new Vector<NamedOntProperty>();
145                Set<OWLProperty> properties = _ontProperty.getSubProperties(_ontology);
146                for (OWLProperty property : properties) {
147                        result.add(new NamedOntProperty(property, _ontology, _model));
148                }
149                if (sorted) {
150                        Collections.sort(result);
151                }
152                return result.iterator();
153        }
154
155        public int compareTo(Object obj) {
156                String str1 = toString();
157                String str2 = obj.toString();
158                return str1.compareTo(str2);
159        }
160
161        /**
162         * @return The (first) comment associated with the property
163         */
164        public String getComment() {
165                OWLAnnotation[] comments = _ontProperty.getAnnotations(_ontology, OWLRDFVocabulary.RDFS_COMMENT.getURI()).toArray(new OWLAnnotation[1]);
166                if (comments.length == 0) {
167                        return null;
168                }
169                return NamedOntModel.parseLabel(comments[0]);
170        }
171
172        protected OWLProperty ontProperty() {
173                return _ontProperty;
174        }
175
176        public boolean equals(Object obj) {
177                if (!(obj instanceof NamedOntProperty))
178                        return false;
179                NamedOntProperty tmp = (NamedOntProperty) obj;
180                if (!tmp.ontProperty().equals(this.ontProperty()))
181                        return false;
182                return true;
183        }
184
185} // NamedOntProperty