00001 #ifndef HEURISTIC_H 00002 #define HEURISTIC_H 00003 00004 //-------------------------------------------------------------------- 00005 // 00006 // This file is part of PEACE. 00007 // 00008 // PEACE is free software: you can redistribute it and/or modify it 00009 // under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation, either version 3 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // PEACE is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with PEACE. If not, see <http://www.gnu.org/licenses/>. 00020 // 00021 // Miami University makes no representations or warranties about the 00022 // suitability of the software, either express or implied, including 00023 // but not limited to the implied warranties of merchantability, 00024 // fitness for a particular purpose, or non-infringement. Miami 00025 // University shall not be liable for any damages suffered by licensee 00026 // as a result of using, result of using, modifying or distributing 00027 // this software or its derivatives. 00028 // 00029 // By using or copying this Software, Licensee agrees to abide by the 00030 // intellectual property laws, and all other applicable laws of the 00031 // U.S., and the terms of GNU General Public License (version 3). 00032 // 00033 // Authors: Dhananjai M. Rao raodm@muohio.edu 00034 // 00035 //--------------------------------------------------------------------- 00036 00037 #include "arg_parser.h" 00038 00039 /** The base class of all heuristics. 00040 00041 This class must be the base class of all heuristics in the 00042 system. This class provides some default functionality that can be 00043 readily used by the heuristics. This class enables the 00044 HeuristicChain to manage a list of heuristics and dispatch method 00045 calls to various heuristics. 00046 */ 00047 class Heuristic { 00048 public: 00049 /** Display valid command line arguments for this heuristic. 00050 00051 This method must be used to display all valid command line 00052 options that are supported by this heuristic. Note that 00053 derived classes may override this method to display additional 00054 command line options that are applicable to it. This method 00055 is typically used in the main() method when displaying usage 00056 information. 00057 00058 \note Derived heuristic classes <b>must</b> override this 00059 method to display help for their custom command line 00060 arguments. When this method is overridden don't forget to 00061 call the corresponding base class implementation to display 00062 common options. 00063 00064 \param[out] os The output stream to which the valid command 00065 line arguments must be written. 00066 */ 00067 virtual void showArguments(std::ostream& os) = 0; 00068 00069 /** Process command line arguments. 00070 00071 This method is used to process command line arguments specific 00072 to this heuristic. This method is typically used from the 00073 main method just after the heuristic has been instantiated. 00074 This method consumes all valid command line arguments. If the 00075 command line arguments were valid and successfully processed, 00076 then this method returns \c true. 00077 00078 \note Derived heuristic classes <b>must</b> override this 00079 method to process any command line arguments that are custom 00080 to their operation. When this method is overridden don't 00081 forget to call the corresponding base class implementation to 00082 display common options. 00083 00084 \param[in,out] argc The number of command line arguments to be 00085 processed. 00086 00087 \param[in,out] argv The array of command line arguments. 00088 00089 \return This method returns \c true if the command line 00090 arguments were successfully processed. Otherwise this method 00091 returns \c false. 00092 */ 00093 virtual bool parseArguments(int& argc, char **argv) = 0; 00094 00095 /** Method to begin heuristic analysis (if any). 00096 00097 This method is invoked just before commencement of EST 00098 analysis. This method typically loads additional information 00099 that may be necessary for a given heuristic from data files. 00100 In addition, it may perform any pre-processing as the case may 00101 be. 00102 00103 \note Derived classes must override this method. 00104 00105 \return If the initialization process was sucessful, then this 00106 method returns 0. Otherwise this method returns with a 00107 non-zero error code. 00108 */ 00109 virtual int initialize() = 0; 00110 00111 /** Set the reference EST id for analysis. 00112 00113 This method is invoked just before a batch of ESTs are 00114 analyzed via a call to the analyze(EST *) method. Setting the 00115 reference EST provides heuristics an opportunity to optimize 00116 certain operations, if possible. 00117 00118 \note This method must be called only after the initialize() 00119 method is called. 00120 00121 \return If the initialization process was sucessful, then this 00122 method returns 0. Otherwise this method returns an error code. 00123 */ 00124 virtual int setReferenceEST(const int estIdx) = 0; 00125 00126 /** Determine whether the analyzer should analyze, according to 00127 this heuristic. 00128 00129 This method can be used to compare a given EST with the 00130 reference EST (set via the call to the setReferenceEST()) 00131 method. 00132 00133 \param[in] otherEST The index (zero based) of the EST with 00134 which the reference EST is to be compared. 00135 00136 \return This method returns true if the heuristic says the 00137 EST pair should be analyzed, and false if it should not. 00138 */ 00139 bool shouldAnalyze(const int otherEST); 00140 00141 /** Obtain human-readable name for this heuristic. 00142 00143 This method must be used to obtain the human readable name set 00144 for this heuristic. This method essentially returns the value 00145 set when this heuristic class was instantiated. 00146 00147 \return A human readable name associated with this heuristic. 00148 */ 00149 const std::string& getName() const { return heuristicName; } 00150 00151 /** The destructor. 00152 00153 The destructor frees memory allocated for holding any dynamic 00154 data in the base class. 00155 */ 00156 virtual ~Heuristic(); 00157 00158 /** Method to display statistics regarding operation of this heuristic. 00159 00160 This method can be used to obtain a dump of the statistics 00161 gathered regarding the operation of this heuristic. The 00162 typical statistic generated by heuristics includes: 00163 00164 <ul> 00165 00166 <li>The number of times the heuristic was called. More 00167 specifically this value indicates the number of times the \c 00168 shouldAnalyze() method was invoked.</li> 00169 00170 <li>The number of successful matches reported by this 00171 heuristic. This number indirectly indicates the number of 00172 times other heuristics or the actual heavy weight algorithm 00173 was invoked.</li> 00174 00175 </ul> 00176 00177 \note Derived heuristic classes may override this method to 00178 display additional statistics. However, the additional 00179 information must be displayed after the base class method has 00180 completed its task. 00181 00182 \param[out] os The output stream to which the statistics 00183 regarding the heuristic is to be dumped. 00184 */ 00185 virtual void printStats(std::ostream& os) const; 00186 00187 /** Method to obtain the count of times this heuristic was run. 00188 00189 \return The number of times this heuristic was called. 00190 */ 00191 inline int getRunCount() { 00192 return runCount; 00193 } 00194 00195 /** Method to obtain the count of times this heuristic was 00196 successful (i.e. result indicated that heavy weight analysis 00197 should be undertaken). 00198 00199 \return The number of times calls to this heuristic succeeded 00200 -- that is, the number of times the heuristic reported that 00201 two ESTs are related. 00202 */ 00203 inline int getSuccessCount() { 00204 return successCount; 00205 } 00206 00207 protected: 00208 /** The default constructor. 00209 00210 The constructor has been made protected to ensure that this 00211 class is never directly instantiated. Instead one of the 00212 derived Heuristic classes must be instantiated via the 00213 HeuristicFactory API methods. 00214 00215 \param[in] heuristicName The human readable name for this 00216 heuristic. This name is used when generating errors, 00217 warnings, and other output messages for this heuristic. 00218 */ 00219 Heuristic(const std::string& heuristicName); 00220 00221 /** Determine whether the analyzer should analyze, according to 00222 this heuristic. 00223 00224 This method is invoked from the shouldAnalyze() method to 00225 perform the actual heuristic analysis. The analysis is 00226 performed between the refESTidx (set via earlier call to 00227 setReferenceEST) and otherEST. 00228 00229 \note Derived heuristic classes must override this method and 00230 provide a proper implementation. 00231 00232 \param[in] otherEST The index (zero based) of the EST with 00233 which the reference EST is to be compared. 00234 00235 \return This method must return \c true if the heuristic says 00236 the EST pair should be analyzed, and \c false otherwise. 00237 */ 00238 virtual bool runHeuristic(const int otherEST) = 0; 00239 00240 /** The index of the reference EST in a given file. 00241 00242 This member object is used to hold the index of a reference 00243 EST in a given file. The index values begin from 0 (zero). 00244 This member is initialized in the constructor and is changed 00245 by the setReferenceEST() id. 00246 */ 00247 int refESTidx; 00248 00249 /** The name of this heuristic. 00250 00251 This instance variable contains the human recognizable name 00252 for this heuristic. This value is set when the heuristic is 00253 instantiated (in the constructor) and is never changed during 00254 the life time of this heuristic. This information is used when 00255 generating errors, warnings, and other output messages. 00256 */ 00257 const std::string heuristicName; 00258 00259 private: 00260 /** Variable to track the number of times this heuristic was run. 00261 00262 This instance variable is used to track the number of times 00263 this heuristic was run. This variable is initialized to zero 00264 in the constructor. It is incremented each time the 00265 shouldAnalyze() method is invoked to run the the heuristic. 00266 */ 00267 int runCount; 00268 00269 /** Variable to track the number of times this heuristic passed. 00270 00271 This instance vairable tracks the number of times the 00272 heuristic passed. This number indirectly indicates the number 00273 of times other heuristics or the actual heavy weight algorithm 00274 was invoked. This value is incremented in the \c 00275 shouldAnalyze() method each time the runHeuristic method 00276 returns a \c true value. 00277 */ 00278 int successCount; 00279 00280 /** A dummy operator= 00281 00282 The operator=() is supressed for this class as it has constant 00283 members whose value is set when the object is created. These 00284 values cannot be changed during the lifetime of this object. 00285 00286 \param[in] src The source object from where data is to be 00287 copied. Currently this value is ignored. 00288 00289 \return Reference to this. 00290 */ 00291 Heuristic& operator=(const Heuristic& src); 00292 }; 00293 00294 #endif