PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ObjectGraphIteratorTest.cc 00004 // 00005 // Copyright (C) 2005 Nicolas Chapados 00006 // Copyright (C) 2005 Olivier Delalleau 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00038 ******************************************************* */ 00039 00040 // Authors: Nicolas Chapados, Olivier Delalleau 00041 00045 #include "ObjectGraphIteratorTest.h" 00046 00047 #include <string> 00048 #include <iostream> 00049 #include <plearn/base/ObjectGraphIterator.h> 00050 #include <plearn/base/Object.h> 00051 #include <plearn/math/TVec.h> 00052 #include <plearn/io/openString.h> 00053 00054 namespace PLearn { 00055 using namespace std; 00056 00057 PLEARN_IMPLEMENT_OBJECT( 00058 ObjectGraphIteratorTest, 00059 "Test the object graph traversal functions.", 00060 "" 00061 ); 00062 00064 // ObjectGraphIteratorTest // 00066 ObjectGraphIteratorTest::ObjectGraphIteratorTest() 00067 /* ### Initialize all fields to their default value */ 00068 { 00069 // ... 00070 00071 // ### You may (or not) want to call build_() to finish building the object 00072 // ### (doing so assumes the parent classes' build_() have been called too 00073 // ### in the parent classes' constructors, something that you must ensure) 00074 } 00075 00077 // build // 00079 void ObjectGraphIteratorTest::build() 00080 { 00081 inherited::build(); 00082 build_(); 00083 } 00084 00086 // makeDeepCopyFromShallowCopy // 00088 void ObjectGraphIteratorTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00089 { 00090 inherited::makeDeepCopyFromShallowCopy(copies); 00091 00092 // ### Call deepCopyField on all "pointer-like" fields 00093 // ### that you wish to be deepCopied rather than 00094 // ### shallow-copied. 00095 // ### ex: 00096 // deepCopyField(trainvec, copies); 00097 00098 // ### Remove this line when you have fully implemented this method. 00099 PLERROR("ObjectGraphIteratorTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00100 } 00101 00103 // declareOptions // 00105 void ObjectGraphIteratorTest::declareOptions(OptionList& ol) 00106 { 00107 // ### Declare all of this object's options here 00108 // ### For the "flags" of each option, you should typically specify 00109 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00110 // ### OptionBase::tuningoption. Another possible flag to be combined with 00111 // ### is OptionBase::nosave 00112 00113 // ### ex: 00114 // declareOption(ol, "myoption", &ObjectGraphIteratorTest::myoption, OptionBase::buildoption, 00115 // "Help text describing this option"); 00116 // ... 00117 00118 // Now call the parent class' declareOptions 00119 inherited::declareOptions(ol); 00120 } 00121 00123 // build_ // 00125 void ObjectGraphIteratorTest::build_() 00126 { 00127 // ### This method should do the real building of the object, 00128 // ### according to set 'options', in *any* situation. 00129 // ### Typical situations include: 00130 // ### - Initial building of an object from a few user-specified options 00131 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00132 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00133 // ### You should assume that the parent class' build_() has already been called. 00134 } 00135 00136 class X : public Object 00137 { 00138 typedef Object inherited; 00139 PLEARN_DECLARE_OBJECT(X); 00140 00141 public: 00142 string name; 00143 PP<X> child; 00144 00145 public: 00146 X() {} 00147 00148 static void declareOptions(OptionList& ol) 00149 { 00150 declareOption(ol, "name", &X::name, OptionBase::buildoption, ""); 00151 declareOption(ol, "child", &X::child, OptionBase::buildoption, ""); 00152 } 00153 00154 virtual void printName() 00155 { 00156 cout << "X::printName: " << name << endl; 00157 } 00158 00159 virtual void method1(string s) 00160 { 00161 cout << "X::method1: name='" << name << "' arg1='" << s << "'" << endl; 00162 } 00163 00164 virtual void method2(string s, int i) 00165 { 00166 cout << "X::method1: name='" << name 00167 << "' arg1='" << s << "'" 00168 << " arg2='" << i << "'" << endl; 00169 } 00170 }; 00171 00172 DECLARE_OBJECT_PTR(X); 00173 PLEARN_IMPLEMENT_OBJECT(X, "", ""); 00174 00175 00176 //##### Y ################################################################### 00177 00178 class Y : public X 00179 { 00180 typedef X inherited; 00181 PLEARN_DECLARE_OBJECT(Y); 00182 00183 public: 00184 Y() {} 00185 00186 virtual void printName() 00187 { 00188 cout << "Y::printName: " << name << endl; 00189 } 00190 }; 00191 00192 DECLARE_OBJECT_PTR(Y); 00193 PLEARN_IMPLEMENT_OBJECT(Y, "", ""); 00194 00195 00196 //##### Z ################################################################### 00197 00198 class Z : public Object 00199 { 00200 typedef Object inherited; 00201 PLEARN_DECLARE_OBJECT(Z); 00202 00203 public: 00204 string dummy_option1; 00205 TVec< PP<X> > sub_objects; 00206 int dummy_option2; 00207 00208 public: 00209 Z() { dummy_option2 = 0; } 00210 00211 static void declareOptions(OptionList& ol) 00212 { 00213 declareOption(ol, "dummy_option1", &Z::dummy_option1, OptionBase::buildoption, ""); 00214 declareOption(ol, "sub_objects", &Z::sub_objects, OptionBase::buildoption, ""); 00215 declareOption(ol, "dummy_option2", &Z::dummy_option2, OptionBase::buildoption, ""); 00216 } 00217 }; 00218 00219 DECLARE_OBJECT_PTR(Z); 00220 PLEARN_IMPLEMENT_OBJECT(Z, "", ""); 00221 00222 00223 00224 //##### main ################################################################ 00225 00226 void iterate(ObjectGraphIterator grit, ObjectGraphIterator grend) 00227 { 00228 for ( ; grit != grend ; ++grit ) { 00229 const Object* curobj = *grit; 00230 cout << "Encountered class \"" << curobj->classname() << "\"" 00231 << " at option \"" << grit.getCurrentOptionName() << "\"" << endl; 00232 if (const X* x = dynamic_cast<const X*>(curobj)) { 00233 cout << "... and name is: " << x->name << endl; 00234 } 00235 } 00236 } 00237 00238 00239 const char* test_objects[] = { 00240 "Z(sub_objects = [])", 00241 00242 "Z(sub_objects = [X(name=\"X1\"), \n" 00243 " *1->X(name=\"X2\"), \n" 00244 " Y(name=\"Y3\"), \n" 00245 " *1, \n" 00246 " *2->Y(name=\"Y5\"), \n" 00247 " X(name=\"X6\", \n" 00248 " child = X(child = Y(name = \"innerY\"))) \n" 00249 " *2])" 00250 }; 00251 00252 const int num_tests = sizeof(test_objects) / sizeof(test_objects[0]); 00253 00254 00256 // perform // 00258 void ObjectGraphIteratorTest::perform() 00259 { 00260 try { 00261 pout.setMode(PStream::plearn_ascii); 00262 for (int i=0 ; i<num_tests ; ++i) { 00263 string test_object = test_objects[i]; 00264 cout << endl 00265 << "- - - - - - - - - - -" << endl 00266 << "Building object structure from:" << endl 00267 << test_object << endl; 00268 00269 PStream strin = openString(test_object, PStream::plearn_ascii); 00270 PP<Object> o; 00271 strin >> o; 00272 00273 cout << endl << "Built structure: " << endl; 00274 pout << o << flush; 00275 pout.copies_map_out.clear(); 00276 00277 cout << endl << "Now traversing the graph in breadth-first:" << endl; 00278 iterate(ObjectGraphIterator(o, ObjectGraphIterator::BreadthOrder, true), 00279 ObjectGraphIterator()); 00280 00281 cout << endl << "Traversing only Y objects in depth-first:" << endl; 00282 iterate(ObjectGraphIterator(o, ObjectGraphIterator::DepthPreOrder, true, "Y"), 00283 ObjectGraphIterator()); 00284 00285 cout << endl << "Broadcast a call to X::printName()" << endl; 00286 memfun_broadcast(o, &X::printName); 00287 00288 cout << endl << "Broadcast a call to Y::printName()" << endl; 00289 memfun_broadcast(o, &Y::printName); 00290 00291 cout << endl << "Broadcast a breadth-first call to X::method1(\"foo\")" << endl; 00292 memfun_broadcast(o, &X::method1, string("foo"), ObjectGraphIterator::BreadthOrder); 00293 00294 cout << endl << "Broadcast a breadth-first call to X::method1(option_name)" << endl; 00295 memfun_broadcast_optname(o, &X::method1, ObjectGraphIterator::BreadthOrder); 00296 00297 cout << endl << "Broadcast a breadth-first call to X::method2(\"foo\",42)" << endl; 00298 memfun_broadcast(o, &X::method2, string("foo"), 42, ObjectGraphIterator::BreadthOrder); 00299 } 00300 } 00301 00302 catch(const PLearnError& e) 00303 { 00304 cerr << "FATAL ERROR: " << e.message() << endl; 00305 } 00306 catch (...) 00307 { 00308 cerr << "FATAL ERROR: uncaught unknown exception " 00309 << "(ex: out-of-memory when allocating a matrix)" << endl; 00310 } 00311 } 00312 00313 } // end of namespace PLearn 00314 00315 00316 /* 00317 Local Variables: 00318 mode:c++ 00319 c-basic-offset:4 00320 c-file-style:"stroustrup" 00321 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00322 indent-tabs-mode:nil 00323 fill-column:79 00324 End: 00325 */ 00326 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :