PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // Copyright (C) 2002 Frederic Morin 00007 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc. 00008 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 /* ******************************************************* 00039 * $Id: OptionBase.cc 7042 2007-05-09 23:44:20Z saintmlx $ 00040 * AUTHORS: Pascal Vincent & Yoshua Bengio 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 #include "Object.h" 00045 #include "OptionBase.h" 00046 #include <plearn/base/stringutils.h> 00047 #include <plearn/base/tostring.h> 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00052 const OptionBase::flag_t OptionBase::buildoption = 1; 00053 const OptionBase::flag_t OptionBase::learntoption = 1 << 1; 00054 const OptionBase::flag_t OptionBase::tuningoption = 1 << 2; 00055 const OptionBase::flag_t OptionBase::nosave = 1 << 3; 00056 const OptionBase::flag_t OptionBase::nonparentable = 1 << 4; 00057 const OptionBase::flag_t OptionBase::nontraversable = 1 << 5; 00058 const OptionBase::flag_t OptionBase::remotetransmit = 1 << 6; 00059 OptionBase::flag_t OptionBase::current_flags_= (OptionBase::buildoption 00060 | OptionBase::learntoption 00061 | OptionBase::tuningoption 00062 | OptionBase::nosave 00063 | OptionBase::nonparentable 00064 | OptionBase::nontraversable 00065 | OptionBase::remotetransmit); 00066 00067 const OptionBase::OptionLevel OptionBase::basic_level= 200; 00068 const OptionBase::OptionLevel OptionBase::advanced_level= 400; 00069 const OptionBase::OptionLevel OptionBase::expert_level= 800; 00070 const OptionBase::OptionLevel OptionBase::experimental_level= 9999; 00071 const OptionBase::OptionLevel OptionBase::deprecated_level= 99999999; 00072 const OptionBase::OptionLevel OptionBase::default_level= OptionBase::basic_level; 00073 OptionBase::OptionLevel OptionBase::current_option_level_= OptionBase::default_level; 00074 00075 OptionBase::OptionBase(const string& optionname, flag_t flags, 00076 const string& optiontype, const string& defaultval, 00077 const string& description, const OptionLevel& level) 00078 : optionname_(optionname), flags_(flags), 00079 optiontype_(optiontype), defaultval_(defaultval), 00080 description_(description), level_(level) 00081 { 00082 /* 00083 if(defaultval_ != "") 00084 PLERROR("DEFAULT VAL: '%s'", defaultval_.c_str()); 00085 */ 00086 if (optionname.size() > 0 && optionname[0] == '_' ) 00087 PLERROR("OptionBase::OptionBase: options should not start with an underscore: '%s'", 00088 optionname.c_str()); 00089 } 00090 00091 00092 bool OptionBase::shouldBeSkipped() const 00093 { 00094 return (flags() & (buildoption | learntoption | tuningoption)) == 0; 00095 } 00096 00097 00098 string OptionBase::writeIntoString(const Object* o) const 00099 { 00100 string s; 00101 PStream out = openString(s, PStream::plearn_ascii, "w"); 00102 write(o, out); 00103 out.flush(); // May not be necessary ? 00104 return s; 00105 } 00106 00107 00108 void OptionBase::readIntoIndex(Object*, PStream&, const string&) 00109 { 00110 PLERROR("OptionBase::readIntoIndex: indexed reads are not supported for option '%s' " 00111 "of type '%s'", optionname().c_str(), optiontype().c_str()); 00112 } 00113 00114 00115 void OptionBase::writeAtIndex(const Object*, PStream&, const string&) const 00116 { 00117 PLERROR("OptionBase::writeAtIndex: indexed writes are not supported for option '%s' " 00118 "of type '%s'", optionname().c_str(), optiontype().c_str()); 00119 } 00120 00121 OptionBase::StrToFlagMap OptionBase::str_to_flag; 00122 const OptionBase::StrToFlagMap& OptionBase::getStrToFlagMap() 00123 { 00124 if(str_to_flag.size() == 0) 00125 { 00126 getFlagToStrMap();//make sure it is filled 00127 for(FlagToStrMap::iterator it= flag_to_str.begin(); 00128 it != flag_to_str.end(); ++it) 00129 str_to_flag[it->second]= it->first; 00130 } 00131 return str_to_flag; 00132 } 00133 00134 OptionBase::FlagToStrMap OptionBase::flag_to_str; 00135 const OptionBase::FlagToStrMap& OptionBase::getFlagToStrMap() 00136 { 00137 if (flag_to_str.size() == 0) 00138 { 00139 flag_to_str[buildoption ] = "buildoption"; 00140 flag_to_str[learntoption ] = "learntoption"; 00141 flag_to_str[tuningoption ] = "tuningoption"; 00142 flag_to_str[nosave ] = "nosave"; 00143 flag_to_str[nonparentable ] = "nonparentable"; 00144 flag_to_str[nontraversable] = "nontraversable"; 00145 flag_to_str[remotetransmit] = "remotetransmit"; 00146 } 00147 return flag_to_str; 00148 } 00149 vector<string> OptionBase::flagStrings() const 00150 { 00151 flag_t curflags = flags(); 00152 vector<string> fs; 00153 00154 const FlagToStrMap& flag_map= getFlagToStrMap(); 00155 00156 for (FlagToStrMap::const_iterator it = flag_map.begin(), 00157 end = flag_map.end() ; it != end ; ++it) 00158 { 00159 // As we process each option, turn it off in temporary copy of flags to 00160 // detect unprocessed flags 00161 if (curflags & it->first) { 00162 fs.push_back(it->second); 00163 curflags &= ~it->first; 00164 } 00165 } 00166 00167 if (curflags) 00168 PLERROR("OptionBase::flagStrings: unprocessed flags in option '%s' (%s);\n" 00169 "cannot interpret remaining bits %d", optionname().c_str(), 00170 optiontype().c_str(), curflags); 00171 00172 return fs; 00173 } 00174 00175 00176 OptionBase::StrToLevelMap OptionBase::str_to_level; 00177 const OptionBase::StrToLevelMap& OptionBase::getStrToLevelMap() 00178 { 00179 if(str_to_level.size() == 0) 00180 { 00181 str_to_level["basic"]= basic_level; 00182 str_to_level["advanced"]= advanced_level; 00183 str_to_level["expert"]= expert_level; 00184 str_to_level["experimental"]= experimental_level; 00185 str_to_level["deprecated"]= deprecated_level; 00186 } 00187 return str_to_level; 00188 } 00189 00190 OptionBase::OptionLevel OptionBase::optionLevelFromString(const string& s) 00191 { 00192 getStrToLevelMap();//make sure it is filled 00193 StrToLevelMap::iterator it= str_to_level.find(lowerstring(s)); 00194 if(it != str_to_level.end()) return it->second; 00195 return (OptionLevel)toint(s); 00196 } 00197 00198 OptionBase::LevelToStrMap OptionBase::level_to_str; 00199 const OptionBase::LevelToStrMap& OptionBase::getLevelToStrMap() 00200 { 00201 if(level_to_str.size() == 0) 00202 { 00203 getStrToLevelMap();//make sure it is filled 00204 for(StrToLevelMap::iterator it= str_to_level.begin(); 00205 it != str_to_level.end(); ++it) 00206 level_to_str[it->second]= it->first; 00207 } 00208 return level_to_str; 00209 } 00210 00211 string OptionBase::optionLevelToString(const OptionLevel& l) 00212 { 00213 getLevelToStrMap();//make sure it is filled 00214 LevelToStrMap::iterator it= level_to_str.find(l); 00215 if(it != level_to_str.end()) return it->second; 00216 return tostring(l); 00217 } 00218 00219 00220 } // end of namespace PLearn 00221 00222 00223 /* 00224 Local Variables: 00225 mode:c++ 00226 c-basic-offset:4 00227 c-file-style:"stroustrup" 00228 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00229 indent-tabs-mode:nil 00230 fill-column:79 00231 End: 00232 */ 00233 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :