Agent.C

Go to the documentation of this file.
00001 /*!@file SeaBee/Agent.C  base class for agents (has run function)       */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Michael Montalbo <montalbo@usc.edu>
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/Agent.C $
00034 // $Id: Agent.C 10794 2009-02-08 06:21:09Z itti $
00035 //
00036 //////////////////////////////////////////////////////////////////////////
00037 
00038 #include <iostream>
00039 #include <unistd.h>
00040 #include "Util/log.H"
00041 
00042 #include "Agent.H"
00043 
00044 // ######################################################################
00045 //Constructors
00046 Agent::Agent(std::string name)
00047 {
00048   itsName = name;
00049   itsLastAction = "";
00050   pthread_mutex_init(&itsStateChangedLock, NULL);
00051   stateChange = 0;
00052 }
00053 
00054 // ######################################################################
00055 Agent::~Agent() { }
00056 
00057 // ######################################################################
00058 //Prints an action being performed by an agent
00059 void Agent::Do(std::string msg)
00060 {
00061   if(itsLastAction != msg)
00062     {
00063       std::cout<<">>>"<<itsName<<"::"<<msg<<"<<<"<<std::endl;
00064       itsLastAction = msg;
00065     }
00066 }
00067 
00068 // ######################################################################
00069 //Dummy scheduler. Descendent agents implement something meaningful here
00070 bool Agent::pickAndExecuteAnAction()
00071 {
00072   return false;
00073 }
00074 
00075 // ######################################################################
00076 //Indicates that something has occured to change the state of the agent
00077 void Agent::stateChanged()
00078 {
00079   pthread_mutex_lock(&itsStateChangedLock);
00080   stateChange++;
00081   pthread_mutex_unlock(&itsStateChangedLock);
00082 }
00083 
00084 // ######################################################################
00085 //Used to put agent to sleep until stateChanged is called
00086 void Agent::acquire()
00087 {
00088   pthread_mutex_lock(&itsStateChangedLock);
00089   stateChange--;
00090   if(stateChange < 0)
00091   {
00092     stateChange = 0;
00093   }
00094   pthread_mutex_unlock(&itsStateChangedLock);
00095 }
00096 
00097 // ######################################################################
00098 // starts running the agent
00099 void Agent::run()
00100 {
00101   bool goOn = true;
00102   while(goOn)
00103   {
00104     // while the agent has stuff to do,
00105     // call the scheduler
00106     while(pickAndExecuteAnAction());
00107 
00108     // puts the agent to sleep
00109     // until another agent wakes it up with a message
00110     acquire();
00111     while(isAsleep()) { usleep(10000); }
00112   }
00113 }
00114 
00115 // ######################################################################
00116 //Returns whether or not the agent is asleep as indicated by stateChange
00117 bool Agent::isAsleep()
00118 {
00119 
00120   pthread_mutex_lock(&itsStateChangedLock);
00121   bool sleep = (stateChange == 0);
00122   pthread_mutex_unlock(&itsStateChangedLock);
00123 
00124   return sleep;
00125 }
00126 
00127 // ######################################################################
00128 /* So things look consistent in everyone's emacs... */
00129 /* Local Variables: */
00130 /* indent-tabs-mode: nil */
00131 /* End: */
00132 
00133 
00134 
00135 
Generated on Sun May 8 08:40:15 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3