001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2011-01-28 01:55:07 +0000 (Fri, 28 Jan 2011) $' 
007 * '$Revision: 26856 $'
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.gui;
031
032import java.util.Hashtable;
033import java.util.Iterator;
034import java.util.List;
035import java.util.Stack;
036import java.util.Vector;
037
038import javax.swing.JTree;
039import javax.swing.tree.TreeModel;
040import javax.swing.tree.TreePath;
041
042import org.apache.commons.logging.Log;
043import org.apache.commons.logging.LogFactory;
044import org.ecoinformatics.seek.sms.AnnotationEngine;
045import org.kepler.moml.NamedObjId;
046
047import ptolemy.kernel.util.NamedObj;
048
049/**
050 * A very simple, first draft ontology-based library search engine
051 * 
052 *@author berkley
053 *@since October 30, 2006
054 *
055 *@deprecated This class is unused, so deprecating. Consider 
056 *for deletion? -01/27/11 derik
057 */
058public class OntLibrarySearcher extends LibrarySearcher {
059        private static final Log log = LogFactory.getLog(OntLibrarySearcher.class.getName());
060        private static final boolean isDebugging = log.isDebugEnabled();
061
062        private AnnotationEngine _engine = null;
063        // for searching
064        private Stack<NamedObj> _pathStack;
065        // the stack to create the return paths with
066        private Hashtable<String, Vector<TreePath>> _hashTable;
067
068        // key is lsid of leaf, object is vector of treepaths to leaf
069
070        /**
071         * Constructor
072         * 
073         *@param library
074         *            Description of the Parameter
075         *@param searchPane
076         *            Description of the Parameter
077         */
078        public OntLibrarySearcher(JTree library, LibrarySearchPane searchPane) {
079                super(library, searchPane);
080                _engine = AnnotationEngine.instance();
081        }
082
083        /**
084         * Search for ontology-keyword in the library
085         * 
086         *@param value
087         *            the keyword in the ontology to search for
088         *@param authenticate
089         *                        if true prompt user to login if necessary
090         *@return Description of the Return Value
091         */
092        public LibrarySearchResults search(String value, boolean authenticate) {
093                // assign the new results of the search
094                _results = new LibrarySearchResults();
095                // this is a protected class member
096                if (value.trim().equals("")) {
097                        return _results;
098                }
099
100                // build the hash table
101                NamedObj root = (NamedObj) _library.getModel().getRoot();
102                _pathStack = new Stack<NamedObj>();
103                _pathStack.push(root);
104                _hashTable = new Hashtable<String, Vector<TreePath>>();
105                buildHashTable(root, _library.getModel());
106
107                // get the ontology search results
108                Vector<NamedObj> ont_results = _engine.search(value, true);
109                // find the appropriate results in the library
110                Iterator<NamedObj> iter = ont_results.iterator();
111                while (iter.hasNext()) {
112                        NamedObj obj = iter.next();
113                        Iterator<String> idIter = getIds(obj).iterator();
114                        // for each id, add the tree path to the result
115                        while (idIter.hasNext()) {
116                                String id = idIter.next();
117                                Iterator<TreePath> pathIter = hashTableGet(id);
118                                while (pathIter.hasNext()) {
119                                        TreePath path = pathIter.next();
120                                        if (!_results.contains(path)) {
121                                                _results.add(path);
122                                        }
123                                }
124                        }
125                }
126                return _results;
127        }
128
129        //
130        // helper function to build the hash table of paths leading to NamedObj's
131        // with ids
132        private void buildHashTable(NamedObj parent, TreeModel model) {
133                for (int i = 0; i < model.getChildCount(parent); i++) {
134                        NamedObj child = (NamedObj) model.getChild(parent, i);
135                        _pathStack.push(child);
136                        if (model.isLeaf(child)) {
137                                Iterator<String> iter = getIds(child).iterator();
138                                while (iter.hasNext()) {
139                                        TreePath path = new TreePath(_pathStack.toArray());
140                                        hashTablePut(iter.next(), path);
141                                }
142                        } else {
143                                buildHashTable(child, model);
144                        }
145
146                        _pathStack.pop();
147                }
148
149        }
150
151        //
152        // helper function: given named object returns it's ids
153        //
154        /**
155         * Gets the ids attribute of the OntLibrarySearcher object
156         * 
157         *@param obj
158         *            Description of the Parameter
159         *@return The ids value
160         */
161        private Vector<String> getIds(NamedObj obj) {
162                Vector<String> ids = new Vector<String>();
163                List<NamedObjId> idAtts = obj.attributeList(NamedObjId.class);
164                Iterator<NamedObjId> iter = idAtts.iterator();
165                while (iter.hasNext()) {
166                        NamedObjId id = (NamedObjId) iter.next();
167                        ids.add(id.getExpression());
168                }
169                return ids;
170        }
171
172        //
173        // helper function: add an (id, path) to _hashtable
174        //
175        private void hashTablePut(String id, TreePath path) {
176                if (!_hashTable.containsKey(id)) {
177                        Vector<TreePath> paths = new Vector<TreePath>();
178                        paths.add(path);
179                        _hashTable.put(id, paths);
180                } else {
181                        Vector<TreePath> paths = _hashTable.get(id);
182                        if (!paths.contains(path)) {
183                                paths.add(path);
184                        }
185                }
186        }
187
188        //
189        // helper function: given an id, returns an iterator of its paths
190        //
191        private Iterator<TreePath> hashTableGet(String id) {
192                Vector<TreePath> ids = _hashTable.get(id);
193                if (ids == null) {
194                        return new Vector<TreePath>().iterator();
195                }
196                return ids.iterator();
197        }
198
199        /**
200         * provides any initialization needed prior to searching. It is called when
201         * the class is constructed. Note that any local variables of extending
202         * classes should be initialized in init since it is called be the
203         * constructor of the super class (LibrarySearcher).
204         */
205        protected void init() {
206        }
207
208}
209// OntLibrarySearcher