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.ecoinformatics.seek.sms;
031
032/**
033 *@author    Shawn Bowers
034 *
035 *  Documentation coming soon
036 */
037
038import java.io.FileOutputStream;
039import java.util.Vector;
040
041import org.kepler.util.StaticResources;
042
043import com.hp.hpl.jena.ontology.OntClass;
044import com.hp.hpl.jena.ontology.OntModel;
045import com.hp.hpl.jena.ontology.OntModelSpec;
046import com.hp.hpl.jena.ontology.tidy.Checker;
047import com.hp.hpl.jena.rdf.model.ModelFactory;
048import com.hp.hpl.jena.util.iterator.ExtendedIterator;
049
050/**
051 * Description of the Class
052 * 
053 *@author berkley
054 *@created February 17, 2005
055 */
056public class OntologyCatalog {
057
058        private static OntologyCatalog _catalog = null;
059        // singleton instance
060        private String KEPLER = System.getProperty("KEPLER");
061        //TODO FIXME hardcoded path:
062        private String LOCAL_PATH = KEPLER + "/common/configs/" + StaticResources.RESOURCEBUNDLE_DIR + "/";
063        private String DEFAULT_NSPREFIX = "http://seek.ecoinformatics.org/ontology#";
064        // this should be obtained from the onto
065
066        // list of OntModel objects
067        private Vector<OntModel> _ontologyModels = new Vector<OntModel>();
068
069        /** Constructor for the OntologyCatalog object */
070        protected OntologyCatalog() {
071                initialize();
072        }
073
074        /**
075         *@return The unique instance of this class This must be called to
076         *         create/obtain an instance of the catalog
077         */
078        public static OntologyCatalog instance() {
079                if (_catalog == null) {
080                        _catalog = new OntologyCatalog();
081                }
082                return _catalog;
083        }
084
085        /**
086         * Returns the default ontology model for the actor library.
087         * 
088         *@return The defaultOntology value
089         */
090        public OntModel getDefaultOntology() {
091                try {
092                        return (OntModel) getOntModels().elementAt(0);
093                } catch (Exception e) {
094                }
095                return null;
096        }
097
098        /**
099         * Gets the ontModels attribute of the OntologyCatalog object
100         * 
101         *@return The ontModels value
102         */
103        public Vector<OntModel> getOntModels() {
104                return _ontologyModels;
105        }
106
107        /**
108         * Returns the first concept in the default onto with the given label
109         * 
110         *@param label
111         *            Description of the Parameter
112         *@return The conceptNameWithLabel value
113         */
114        public String getConceptNameWithLabel(String label) {
115                OntModel defaultOnt = getDefaultOntology();
116                if (defaultOnt == null || label == null) {
117                        return null;
118                }
119                ExtendedIterator iter = defaultOnt.listClasses();
120                while (iter.hasNext()) {
121                        OntClass c = (OntClass) iter.next();
122                        if (label.equals(c.getLabel(null))) {
123                                return c.getLocalName();
124                        }
125                }
126                return null;
127        }
128
129        /**
130         * Adds a concept to the default ontology
131         * 
132         *@param conceptName
133         *            The feature to be added to the Concept attribute
134         */
135        public void addConcept(String conceptName) {
136                addConcept(conceptName, conceptName);
137        }
138
139        /**
140         * Adds a concept to the default ontology
141         * 
142         *@param conceptName
143         *            The feature to be added to the Concept attribute
144         *@param conceptLabel
145         *            The feature to be added to the Concept attribute
146         */
147        public void addConcept(String conceptName, String conceptLabel) {
148                OntModel defaultOnt = getDefaultOntology();
149                if (defaultOnt == null) {
150                        return;
151                }
152                OntClass c = defaultOnt.createClass(DEFAULT_NSPREFIX + conceptName);
153                c.addLabel(conceptLabel, null);
154                // no "language"
155                writeDefaultModel();
156        }
157
158        /**
159         * Assigns a concept as a subconcept to a superconcept
160         * 
161         *@param subConceptName
162         *            Description of the Parameter
163         *@param superConceptName
164         *            Description of the Parameter
165         */
166        public void assignSuperConcept(String subConceptName,
167                        String superConceptName) {
168                OntModel defaultOnt = getDefaultOntology();
169                if (defaultOnt == null) {
170                        return;
171                }
172                // check if sub and sup have been created already
173                OntClass sub = defaultOnt.getOntClass(subConceptName);
174                if (sub == null) {
175                        sub = defaultOnt.createClass(DEFAULT_NSPREFIX + subConceptName);
176                }
177                OntClass sup = defaultOnt.getOntClass(superConceptName);
178                if (sup == null) {
179                        sup = defaultOnt.createClass(DEFAULT_NSPREFIX + superConceptName);
180                }
181                sub.addSuperClass(sup);
182                writeDefaultModel();
183        }
184
185        /** Description of the Method */
186        protected void writeDefaultModel() {
187                OntModel defaultOnt = getDefaultOntology();
188                if (defaultOnt == null) {
189                        return;
190                }
191                try {
192                        FileOutputStream hndl = new FileOutputStream(LOCAL_PATH
193                                        + "ontology.owl");
194                        defaultOnt.write(hndl);
195                        hndl.close();
196                } catch (Exception e) {
197                        e.printStackTrace();
198                }
199        }
200
201        /** Description of the Method */
202        protected void initialize() {
203                // the default ontology is now "ontology.owl"
204                String ONTO_FILE = LOCAL_PATH + "ontology.owl";
205                try {
206                        Checker validate = new Checker(false);
207                        // create and read the ontology model
208                        OntModel _ontModel = ModelFactory.createOntologyModel(
209                                        OntModelSpec.OWL_MEM_RDFS_INF, null);
210                        _ontModel.read("file:" + ONTO_FILE);
211                        _ontologyModels.add(_ontModel);
212                } catch (Exception e) {
213                        e.printStackTrace();
214                }
215        }
216
217        /**
218         * The main program for the OntologyCatalog class
219         * 
220         *@param args
221         *            The command line arguments
222         */
223        public static void main(String[] args) {
224                OntologyCatalog catalog = OntologyCatalog.instance();
225                catalog.addConcept("MyClass", "My Class Label");
226                catalog.assignSuperConcept("MyClass", "WorkflowComponent");
227        }
228
229}