PLearn 0.1
RunObject.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RunObject.cc
00004 //
00005 // Copyright (C) 2004-2005 Olivier Delalleau 
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: RunObject.cc 6351 2006-10-25 19:05:45Z chapados $ 
00037  ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "RunObject.h"
00045 #include <plearn/io/load_and_save.h>
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00051 // RunObject //
00053 RunObject::RunObject():
00054     run_objects(false)
00055 {}
00056 
00057 PLEARN_IMPLEMENT_OBJECT(RunObject,
00058     "Allows to build non-runnable objects or run multiple objects in scripts",
00059     "A RunObject can be used for:\n"
00060     " - Defining non-runnable objects in PLearn scripts, without PLearn\n"
00061     "   complaining (this assumes these objects do something interesting at\n"
00062     "   build time, e.g. a PrecomputedVMatrix will precompute its data)\n"
00063     " - Running multiple objects in PLearn scripts, when the 'run_objects'\n"
00064     "   option is set to 'true': this is especially useful when a script is\n"
00065     "   generated by a Python PyPLearn script\n"
00066     " - Saving the resulting objects to files\n"
00067 );
00068 
00069 void RunObject::declareOptions(OptionList& ol)
00070 {
00071     TVec<PPath>         save_files;
00072 
00073     declareOption(ol, "objects", &RunObject::objects,
00074         OptionBase::buildoption,
00075         "The objects that need being built, run and / or saved.");
00076 
00077     declareOption(ol, "run_objects", &RunObject::run_objects,
00078         OptionBase::buildoption,
00079         "If set to 'true', objects will be run with this RunObject.");
00080 
00081     declareOption(ol, "save_files", &RunObject::save_files,
00082         OptionBase::buildoption,
00083         "If provided, the resulting objects will be saved. This vector must\n"
00084         "either have same length as 'objects', or be of length one, in which\n"
00085         "case each object 'i' will be saved as '<basename>_i.<extension>'.");
00086 
00087     declareOption(ol, "underlying_object", &RunObject::underlying_object,
00088         OptionBase::learntoption,
00089         "DEPRECATED: The underlying object to be built.");
00090 
00091     declareOption(ol, "save_object_name", &RunObject::save_object_name,
00092         OptionBase::learntoption,
00093         "DEPRECATED: If provided, the object will be saved to this file after it is built.");
00094 
00095     // Now call the parent class' declareOptions
00096     inherited::declareOptions(ol);
00097 }
00098 
00099 void RunObject::build_()
00100 {
00101     // Deal with deprecated options.
00102     if (underlying_object) {
00103         PLDEPRECATED("In RunObject::build_ - Option 'underlying_object' is now"
00104                      " deprecated, please use the 'objects' option (note that "
00105                      "this is still going to work as usual)");
00106         PLASSERT( objects.isEmpty() && !run_objects );
00107         run_objects = false;
00108         objects.resize(0);
00109         objects.append(underlying_object);
00110     }
00111     if (!save_object_name.isEmpty()) {
00112         PLDEPRECATED("In RunObject::build_ - Option 'save_object_name' is now"
00113                      "deprecated, please use the 'save_files' option (note "
00114                      "that this is still going to work as usual)");
00115         PLASSERT(save_files.isEmpty() && !run_objects && objects.length() == 1);
00116         save_files.resize(0);
00117         save_files.append(save_object_name);
00118     }
00119 }
00120 
00121 void RunObject::build()
00122 {
00123     inherited::build();
00124     build_();
00125 }
00126 
00127 void RunObject::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00128 {
00129     inherited::makeDeepCopyFromShallowCopy(copies);
00130 
00131     // ### Call deepCopyField on all "pointer-like" fields 
00132     // ### that you wish to be deepCopied rather than 
00133     // ### shallow-copied.
00134     // ### ex:
00135     // deepCopyField(trainvec, copies);
00136 
00137     // ### Remove this line when you have fully implemented this method.
00138     PLERROR("RunObject::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00139 }
00140 
00142 // run //
00144 void RunObject::run() {
00145     if (run_objects)
00146         for (int i = 0; i < objects.length(); i++)
00147             objects[i]->run();
00148 
00149     PLASSERT( save_files.isEmpty() || save_files.length() == 1
00150                                  || save_files.length() == objects.length() );
00151     if (!save_files.isEmpty()) {
00152         TVec<PPath> paths = save_files;
00153         if (paths.length() != objects.length()) {
00154             // Automatically generate paths with an increasing counter.
00155             PLASSERT( paths.length() == 1 );
00156             PPath basepath = paths[0];
00157             PPath no_ext = basepath.no_extension();
00158             string ext = basepath.extension(true);
00159             paths = TVec<PPath>(objects.length());
00160             for (int i = 0; i < paths.length(); i++)
00161                 paths[i] = no_ext + "_" + tostring(i) + ext;
00162         }
00163         PLASSERT( objects.length() == paths.length() );
00164         for (int i = 0; i < objects.length(); i++)
00165             PLearn::save(paths[i], objects[i]);
00166     }
00167 }
00168 
00169 } // end of namespace PLearn
00170 
00171 
00172 /*
00173   Local Variables:
00174   mode:c++
00175   c-basic-offset:4
00176   c-file-style:"stroustrup"
00177   c-file-offsets:((innamespace . 0)(inline-open . 0))
00178   indent-tabs-mode:nil
00179   fill-column:79
00180   End:
00181 */
00182 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines