PLearn 0.1
InstanceSnippetTest.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // InstanceSnippetTest.h
00004 //
00005 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc.
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 // Authors: Xavier Saint-Mleux
00036 
00040 #ifndef InstanceSnippetTest_INC
00041 #define InstanceSnippetTest_INC
00042 
00043 #include <plearn/python/PythonCodeSnippet.h> //python includes have to come first
00044 #include <pytest/defines.h>
00045 #include <plearn/misc/PTest.h>
00046 #include <plearn_learners/regressors/LinearRegressor.h>
00047 
00048 
00049 namespace PLearn {
00050 
00062 class TestSnippet : public PythonCodeSnippet
00063 {
00064 public:
00065     typedef PythonCodeSnippet inherited;
00066     
00067     PP<PLearner> ll;
00068 
00069     TestSnippet(const string& xtra= "")
00070         :PythonCodeSnippet(string("from plearn.pybridge.test_embedded_code_snippet import *\n")
00071                            + xtra,
00072                            true),
00073          ll(new LinearRegressor())
00074     {}
00075 
00076     virtual void build()
00077     {
00078         inherited::build();
00079         build_();
00080     }
00081 
00082     void build_()
00083     {
00084         inject("getLearner", this, &TestSnippet::getLearner);
00085         inject("recTest", this, &TestSnippet::recTest);
00086         inject("recTest2", this, &TestSnippet::recTest2);
00087         inject("recTestCrash", this, &TestSnippet::recTestCrash);
00088     }
00089 
00090     PythonObjectWrapper getLearner(const TVec<PythonObjectWrapper>& args)
00091     {
00092         Object* p= ll;
00093         return PythonObjectWrapper(p);
00094     }
00095 
00096     PythonObjectWrapper recTest(const TVec<PythonObjectWrapper>& args)
00097     {
00098         int n= args[0];
00099         if(n%100 == 0)
00100             pout << "recTest\t" << n << endl;
00101         invoke("testRec", args);
00102         return PythonObjectWrapper(n);
00103     }
00104 
00105     PythonObjectWrapper recTest2(const TVec<PythonObjectWrapper>& args)
00106     {
00107         PP<PLearner> l= args[0];
00108         --l->nstages;
00109         if(l->nstages%100 == 0)
00110             pout << "recTest2\t" << l->nstages << "\tnrefs=" << l->usage() << endl;
00111         invoke("testRec2", args);
00112         return args[0];
00113     }
00114 
00115     PythonObjectWrapper recTestCrash(const TVec<PythonObjectWrapper>& args)
00116     {
00117         int n= args[0];
00118         pout << "recTestCrash\t" << n << endl;
00119         invoke("testRecCrash", n-1);
00120         return PythonObjectWrapper();
00121     }
00122 
00123 
00124 };
00125 
00126 
00127 class InstanceSnippetTest : public PTest
00128 {
00129     typedef PTest inherited;
00130 
00131 public:
00132     //#####  Public Build Options  ############################################
00133 
00134     // ### declare public option fields (such as build options) here
00135     // Start your comments with Doxygen-compatible comments such as //!
00136     // ### Typically, a PTest options are used to store the test results.
00137 
00138 
00139 public:
00140     //#####  Public Member Functions  #########################################
00141 
00143     // ### Make sure the implementation in the .cc
00144     // ### initializes all fields to reasonable default values.
00145     InstanceSnippetTest();
00146 
00147     // Your other public member functions go here
00148 
00149 
00150     //#####  PLearn::Object Protocol  #########################################
00151 
00152     // Declares other standard object methods.
00153     // ### If your class is not instantiatable (it has pure virtual methods)
00154     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT
00155     PLEARN_DECLARE_OBJECT(InstanceSnippetTest);
00156 
00157     // Simply calls inherited::build() then build_()
00158     virtual void build();
00159 
00161     // (PLEASE IMPLEMENT IN .cc)
00162     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00163 
00164     //#####  PLearn::PTest Protocol  ##########################################
00165 
00168     virtual void perform();
00169 
00170 protected:
00171     //#####  Protected Options  ###############################################
00172 
00173     // ### Declare protected option fields (such as learned parameters) here
00174     // ...
00175 
00176 protected:
00177     //#####  Protected Member Functions  ######################################
00178 
00180     // (PLEASE IMPLEMENT IN .cc)
00181     static void declareOptions(OptionList& ol);
00182 
00183 private:
00184     //#####  Private Member Functions  ########################################
00185 
00187     // (PLEASE IMPLEMENT IN .cc)
00188     void build_();
00189 
00190 private:
00191     //#####  Private Data Members  ############################################
00192 
00193     // The rest of the private stuff goes here
00194 };
00195 
00196 // Declares a few other classes and functions related to this class
00197 DECLARE_OBJECT_PTR(InstanceSnippetTest);
00198 
00199 } // end of namespace PLearn
00200 
00201 #endif
00202 
00203 
00204 /*
00205   Local Variables:
00206   mode:c++
00207   c-basic-offset:4
00208   c-file-style:"stroustrup"
00209   c-file-offsets:((innamespace . 0)(inline-open . 0))
00210   indent-tabs-mode:nil
00211   fill-column:79
00212   End:
00213 */
00214 // 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