whitebox-Brain.C

Go to the documentation of this file.
00001 /*!@file TestSuite/whitebox-Brain.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/TestSuite/whitebox-Brain.C $
00035 // $Id: whitebox-Brain.C 10827 2009-02-11 09:40:02Z itti $
00036 //
00037 
00038 #ifndef TESTSUITE_WHITEBOX_BRAIN_C_DEFINED
00039 #define TESTSUITE_WHITEBOX_BRAIN_C_DEFINED
00040 
00041 #include "Component/ModelManager.H"
00042 #include "Neuro/AttentionGuidanceMap.H"
00043 #include "Neuro/GistEstimator.H"
00044 #include "Neuro/InferoTemporal.H"
00045 #include "Neuro/Retina.H"
00046 #include "Neuro/EyeHeadController.H"
00047 #include "Neuro/SaliencyMap.H"
00048 #include "Neuro/ShapeEstimator.H"
00049 #include "Neuro/SimulationViewer.H"
00050 #include "Neuro/StdBrain.H"
00051 #include "Neuro/TargetChecker.H"
00052 #include "Neuro/TaskRelevanceMap.H"
00053 #include "Neuro/VisualCortex.H"
00054 #include "Neuro/WinnerTakeAll.H"
00055 #include "TestSuite/TestSuite.H"
00056 
00057 
00058 #include "rutz/demangle.h"
00059 
00060 namespace
00061 {
00062   // these classes represent a potential future implementation of a
00063   // Brain base class that basically just represents the
00064   // collection-of-modules interface
00065 
00066   class Visitor
00067   {
00068   public:
00069     virtual ~Visitor() {}
00070 
00071     virtual void visit(ModelComponent* p) = 0;
00072   };
00073 
00074   template <class T>
00075   class TypeVisitor : public Visitor
00076   {
00077   public:
00078     TypeVisitor(bool strict = true) : itsResult(0), itsStrict(strict) {}
00079 
00080     virtual void visit(ModelComponent* p)
00081     {
00082       T* t = dynamic_cast<T*>(p);
00083       if (t != 0)
00084         {
00085           if (itsResult != 0 && itsStrict)
00086             LFATAL("ambiguous type match while searching for a %s:\n"
00087                    "\tfirst match was a %s, second match was a %s",
00088                    rutz::demangled_name(typeid(T)),
00089                    rutz::demangled_name(typeid(*itsResult)),
00090                    rutz::demangled_name(typeid(*t)));
00091           else
00092             itsResult = t;
00093         }
00094     }
00095 
00096     T* result() const { return itsResult; }
00097 
00098   private:
00099     T* itsResult;
00100     bool itsStrict;
00101   };
00102 
00103   class Aggregate
00104   {
00105   public:
00106 
00107     virtual ~Aggregate() {}
00108 
00109     virtual void accept(Visitor& c) = 0;
00110 
00111     template <class T>
00112     T* findModuleSoft()
00113     {
00114       TypeVisitor<T> checker(false);
00115       this->accept(checker);
00116       return checker.result();
00117     }
00118 
00119     template <class T>
00120     T* findModule()
00121     {
00122       TypeVisitor<T> checker(true);
00123       this->accept(checker);
00124       if (checker.result() == 0)
00125         LFATAL("no module found of type %s",
00126                rutz::demangled_name(typeid(T)));
00127       return checker.result();
00128     }
00129   };
00130 
00131   class ProtoBrain : public Aggregate
00132   {
00133   public:
00134 
00135     virtual void accept(Visitor& c)
00136     {
00137       /*
00138       c.visit(itsBrain->getRET().get());
00139       c.visit(itsBrain->getVC().get());
00140       c.visit(itsBrain->getEHC().get());
00141       c.visit(itsBrain->getSV().get());
00142       c.visit(itsBrain->getSE().get());
00143       c.visit(itsBrain->getSM().get());
00144       c.visit(itsBrain->getTRM().get());
00145       c.visit(itsBrain->getAGM().get());
00146       c.visit(itsBrain->getWTA().get());
00147       c.visit(itsBrain->getIT().get());
00148       c.visit(itsBrain->getGE().get());
00149       c.visit(itsBrain->getTC().get());
00150       */
00151     }
00152 
00153     nub::soft_ref<Brain> itsBrain;
00154   };
00155 
00156 }
00157 
00158 static void Brain_xx_modules_xx_1(TestSuite& suite)
00159 {
00160   ModelManager mm("dummy test model manager");
00161 
00162   nub::ref<StdBrain> brain(new StdBrain(mm));
00163   mm.addSubComponent(brain);
00164 
00165   nub::ref<SimEventQueue> seq(new SimEventQueue(mm));
00166   mm.addSubComponent(seq);
00167 
00168   const char* args[] =
00169     { __FILE__, "--ehc-type=Simple", "--esc-type=Trivial",
00170       "--hsc-type=None", "--it-type=Std", "--ge-type=Std", 0 };
00171 
00172   REQUIRE(mm.parseCommandLine(4, args, "", 0, 0) == true);
00173 
00174   // here we're testing the local "ProtoBrain" class, which is a
00175   // prototype implementation of a Brain::findModule<ModuleType>()
00176   // interface
00177   ProtoBrain pbrain;
00178   pbrain.itsBrain = brain;
00179 
00180   /*
00181 
00182     the visiting should recurse
00183 
00184   REQUIRE(pbrain.findModule<Retina>() != 0);
00185   REQUIRE(pbrain.findModule<VisualCortex>() != 0);
00186   REQUIRE(pbrain.findModule<EyeHeadController>() != 0);
00187   REQUIRE(pbrain.findModule<SimulationViewer>() != 0);
00188   REQUIRE(pbrain.findModule<ShapeEstimator>() != 0);
00189   REQUIRE(pbrain.findModule<SaliencyMap>() != 0);
00190   REQUIRE(pbrain.findModule<TaskRelevanceMap>() != 0);
00191   REQUIRE(pbrain.findModule<AttentionGuidanceMap>() != 0);
00192   REQUIRE(pbrain.findModule<WinnerTakeAll>() != 0);
00193   REQUIRE(pbrain.findModule<InferoTemporal>() != 0);
00194   REQUIRE(pbrain.findModule<GistEstimator>() != 0);
00195   REQUIRE(pbrain.findModule<TargetChecker>() != 0);
00196 
00197   REQUIRE(pbrain.findModuleSoft<Brain>() == 0);
00198 
00199   REQUIRE(pbrain.findModuleSoft<ModelComponent>() != 0);
00200 
00201   bool caught = false;
00202 
00203   try {
00204     // we expect this to fail, because the findModule() call is
00205     // ambiguous -- ProtoBrain has more than one module of type
00206     // (derived from) ModelComponent
00207     (void) pbrain.findModule<ModelComponent>();
00208   } catch (lfatal_exception& e) {
00209     caught = true;
00210   }
00211   REQUIRE(caught);
00212   */
00213   mm.start();
00214 }
00215 
00216 ///////////////////////////////////////////////////////////////////////
00217 //
00218 // main
00219 //
00220 ///////////////////////////////////////////////////////////////////////
00221 
00222 int main(int argc, const char** argv)
00223 {
00224   TestSuite suite;
00225 
00226   suite.ADD_TEST(Brain_xx_modules_xx_1);
00227 
00228   suite.parseAndRun(argc, argv);
00229 
00230   return 0;
00231 }
00232 
00233 // ######################################################################
00234 /* So things look consistent in everyone's emacs... */
00235 /* Local Variables: */
00236 /* indent-tabs-mode: nil */
00237 /* End: */
00238 
00239 #endif // TESTSUITE_WHITEBOX_BRAIN_C_DEFINED
Generated on Sun May 8 08:42:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3