LoBaylogAnalyzer.C

00001 /**
00002    \file  Robots/LoBot/control/LoBaylogAnalyzer.C
00003    \brief This file defines the non-inline member functions of the
00004    lobot::BaylogAnalyzer class.
00005 */
00006 
00007 // //////////////////////////////////////////////////////////////////// //
00008 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00009 // by the University of Southern California (USC) and the iLab at USC.  //
00010 // See http://iLab.usc.edu for information about this project.          //
00011 // //////////////////////////////////////////////////////////////////// //
00012 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00013 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00014 // in Visual Environments, and Applications'' by Christof Koch and      //
00015 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00016 // pending; application number 09/912,225 filed July 23, 2001; see      //
00017 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00018 // //////////////////////////////////////////////////////////////////// //
00019 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00022 // redistribute it and/or modify it under the terms of the GNU General  //
00023 // Public License as published by the Free Software Foundation; either  //
00024 // version 2 of the License, or (at your option) any later version.     //
00025 //                                                                      //
00026 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00027 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00028 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00029 // PURPOSE.  See the GNU General Public License for more details.       //
00030 //                                                                      //
00031 // You should have received a copy of the GNU General Public License    //
00032 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00033 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00034 // Boston, MA 02111-1307 USA.                                           //
00035 // //////////////////////////////////////////////////////////////////// //
00036 //
00037 // Primary maintainer for this file: mviswana usc edu
00038 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/baylog/LoBaylogAnalyzer.C $
00039 // $Id: LoBaylogAnalyzer.C 14083 2010-09-30 13:59:37Z mviswana $
00040 //
00041 
00042 //------------------------------ HEADERS --------------------------------
00043 
00044 // lobot headers
00045 #include "Robots/LoBot/baylog/LoBaylogAnalyzer.H"
00046 #include "Robots/LoBot/baylog/LoTTIMap.H"
00047 #include "Robots/LoBot/baylog/LoDirList.H"
00048 
00049 #include "Robots/LoBot/config/LoConfigHelpers.H"
00050 #include "Robots/LoBot/thread/LoMutex.H"
00051 
00052 #include "Robots/LoBot/util/LoFile.H"
00053 #include "Robots/LoBot/util/LoString.H"
00054 
00055 // DEVNOTE: We must supply the definition of YY_EXTRA_TYPE prior to
00056 // including LoBaylogParser.H, which is generated by flex. Otherwise, the
00057 // generated header will define YY_EXTRA_TYPE as void* and the linker
00058 // will fail to find the lobay_parser_set_extra function, which will end
00059 // up being declared in this module as taking a void* rather than a
00060 // lobot::TTIMap* as it actually does.
00061 //
00062 // Moreover, we must include the flex-generated LoBaylogParser.H header
00063 // after LoTTIMap.H. Otherwise, the lobot::TTIMap class will not be
00064 // properly declared or defined and the compiler really detests that.
00065 #define YY_EXTRA_TYPE lobot::TTIMap*
00066 #include "Robots/LoBot/baylog/LoBaylogParser.H"
00067 
00068 // Standard C++ headers
00069 #include <iostream>
00070 #include <fstream>
00071 #include <string>
00072 #include <algorithm>
00073 #include <vector>
00074 
00075 // Standard C headers
00076 #include <stdio.h>
00077 
00078 //-------------------------- KNOB TWIDDLING -----------------------------
00079 
00080 namespace {
00081 
00082 // Retrieve settings from global section of config file
00083 template<typename T>
00084 inline T conf(const std::string& key, const T& default_value)
00085 {
00086    return lobot::global_conf<T>(key, default_value) ;
00087 }
00088 
00089 /// This inner class encapsulates various parameters that can be used to
00090 /// tweak different aspects of the Bayesian TTI prediction analysis.
00091 class AnalyzerParams : public lobot::singleton<AnalyzerParams> {
00092    /// Each dataset for the Bayesian time-to-impact prediction
00093    /// experiments consists of ten (or more) log files. This setting
00094    /// specifies a regular expression that defines the pattern for the
00095    /// names of these log files.
00096    std::string m_log_name ;
00097 
00098    /// Once all the log files for a dataset have been parsed, the results
00099    /// will be written to a file under the same directory as the
00100    /// dataset's log files with the name specified by this setting.
00101    std::string m_result_file ;
00102 
00103    /// Private constructor because this is a singleton.
00104    AnalyzerParams() ;
00105 
00106    // Boilerplate code to make generic singleton design pattern work
00107    friend class lobot::singleton<AnalyzerParams> ;
00108 
00109 public:
00110    /// Accessing the various parameters.
00111    //@{
00112    static const std::string& log_name()    {return instance().m_log_name    ;}
00113    static const std::string& result_file() {return instance().m_result_file ;}
00114    //@}
00115 } ;
00116 
00117 // Parameters initialization
00118 AnalyzerParams::AnalyzerParams()
00119    : m_log_name(conf<std::string>("log_file_name",
00120                                   "(metlog-[[:digit:]]{8}-[[:digit:]]{6})$")),
00121      m_result_file(conf<std::string>("result_file_name", "result"))
00122 {}
00123 
00124 // Shortcut
00125 typedef AnalyzerParams Params ;
00126 
00127 } // end of local anonymous namespace encapsulating above helpers
00128 
00129 //----------------------------- NAMESPACE -------------------------------
00130 
00131 namespace lobot {
00132 
00133 //-------------------------- INITIALIZATION -----------------------------
00134 
00135 BaylogAnalyzer* BaylogAnalyzer::create(const DirList& L)
00136 {
00137    return new BaylogAnalyzer(L) ;
00138 }
00139 
00140 BaylogAnalyzer::BaylogAnalyzer(const DirList& L)
00141    : m_dirs(L)
00142 {
00143    static int n ;
00144    static Mutex m ;
00145 
00146    AutoMutex M(m) ;
00147    start(std::string("lobay_analyzer_") + to_string(++n)) ;
00148 }
00149 
00150 //-------------------------- METLOG PARSING -----------------------------
00151 
00152 // A helper function object for loading and parsing the log file for an
00153 // individual experiment in a dataset.
00154 namespace {
00155 
00156 class process_log {
00157    std::string m_thread_name ;
00158    yyscan_t m_parser ;
00159 public:
00160    process_log(const std::string& thread_name, yyscan_t parser) ;
00161    void operator()(const std::string& log_file_name) const ;
00162 } ;
00163 
00164 process_log::process_log(const std::string& thread_name, yyscan_t parser)
00165    : m_thread_name(thread_name), m_parser(parser)
00166 {}
00167 
00168 void process_log::operator()(const std::string& log_file_name) const
00169 {
00170    FILE* file = fopen(log_file_name.c_str(), "r") ;
00171    if (! file) {
00172       std::cerr << m_thread_name << ": "
00173                 << log_file_name << ": unable to open\n" ;
00174       return ;
00175    }
00176 
00177    lobay_parser_set_in(file, m_parser) ;
00178    lobay_parser_lex(m_parser) ;
00179    lobay_parser_set_in(stdin, m_parser) ;
00180    fclose(file) ;
00181 }
00182 
00183 } // end of local anonymous namespace encapsulating above helper
00184 
00185 // This function analyzes a dataset and writes the results of the
00186 // analysis to a file.
00187 //
00188 // Since the lobay program is designed to process multiple datasets in
00189 // parallel, there will be multiple instances of this class, each one
00190 // executing this function in a separate thread. All of them will use a
00191 // shared lobot::DirList object to figure out which dataset to work on
00192 // next and process the datasets specified therein one-by-one.
00193 void BaylogAnalyzer::run()
00194 {
00195    yyscan_t parser ;
00196    try
00197    {
00198       lobay_parser_lex_init(&parser) ;
00199       for(;;)
00200       {
00201          std::string dir = m_dirs.next() ;
00202          std::vector<std::string> logs = find_file(dir, Params::log_name()) ;
00203 
00204          TTIMap result ;
00205          lobay_parser_set_extra(&result, parser) ;
00206          std::for_each(logs.begin(), logs.end(),
00207                        process_log(Thread::name(), parser)) ;
00208          lobay_parser_set_extra(0, parser) ;
00209 
00210          std::string result_file_name = dir + "/" + Params::result_file() ;
00211          std::ofstream result_file(result_file_name.c_str()) ;
00212          result_file << result ;
00213       }
00214    }
00215    catch (DirList::eol&)
00216    {
00217       lobay_parser_lex_destroy(parser) ;
00218    }
00219 }
00220 
00221 //-----------------------------------------------------------------------
00222 
00223 } // end of namespace encapsulating this file's definitions
00224 
00225 /* So things look consistent in everyone's emacs... */
00226 /* Local Variables: */
00227 /* indent-tabs-mode: nil */
00228 /* End: */
Generated on Sun May 8 08:05:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3