Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

agentaspect.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: agentaspect.cpp,v 1.7 2004/04/24 12:02:23 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "agentaspect.h"
00023 #include <zeitgeist/logserver/logserver.h>
00024 
00025 using namespace boost;
00026 using namespace oxygen;
00027 using namespace salt;
00028 using namespace std;
00029 
00030 AgentAspect::AgentAspect() : Transform()
00031 {
00032     SetName("agentAspect");
00033 }
00034 
00035 AgentAspect::~AgentAspect()
00036 {
00037 }
00038 
00039 bool
00040 AgentAspect::RealizeActions(boost::shared_ptr<ActionObject::TList> actions)
00041 {
00042     UpdateEffectorMap();
00043 
00044     for (
00045          ActionObject::TList::iterator iter = actions->begin();
00046          iter != actions->end();
00047          ++iter
00048          )
00049         {
00050             shared_ptr<ActionObject> action = (*iter);
00051             std::string predicate = action->GetPredicate();
00052 
00053             shared_ptr<Effector> effector = GetEffector(predicate);
00054             if (effector.get() == 0)
00055                 {
00056                     GetLog()->Warning()
00057                         <<  "(AgentAspect) No effector found for predicate "
00058                         << predicate << "\n";
00059                     continue;
00060                 }
00061 
00062             bool realized = effector->Realize(action);
00063 
00064             if (! realized)
00065                 {
00066                     GetLog()->Warning()
00067                         << "(AgentAspect) Failed to realize predicate "
00068                         << predicate << "\n";
00069                 }
00070         }
00071 
00072     return true;
00073 }
00074 
00075 shared_ptr<PredicateList>
00076 AgentAspect::QueryPerceptors()
00077 {
00078     // build list of perceptors, searching recursively
00079     TLeafList perceptors;
00080     ListChildrenSupportingClass<Perceptor>(perceptors,true);
00081 
00082     shared_ptr<PredicateList> predList(new PredicateList());
00083 
00084     // query the perceptors for new data
00085     for (
00086          TLeafList::iterator iter = perceptors.begin();
00087          iter != perceptors.end();
00088          ++iter
00089          )
00090         {
00091             shared_static_cast<Perceptor>(*iter)->Percept(predList);
00092         }
00093 
00094     return predList;
00095 }
00096 
00097 shared_ptr<Effector>
00098 AgentAspect::GetEffector(const std::string predicate) const
00099 {
00100     TEffectorMap::const_iterator iter = mEffectorMap.find(predicate);
00101 
00102     if (iter == mEffectorMap.end())
00103         {
00104             return shared_ptr<Effector>();
00105         }
00106 
00107     return (*iter).second;
00108 }
00109 
00110 void
00111 AgentAspect::UpdateEffectorMap()
00112 {
00113     // build list of effectors, searching recursively
00114     TLeafList effectors;
00115     ListChildrenSupportingClass<Effector>(effectors,true);
00116 
00117     // build the effector map
00118     mEffectorMap.clear();
00119 
00120     for (
00121          TLeafList::iterator iter = effectors.begin();
00122          iter != effectors.end();
00123          ++iter
00124          )
00125         {
00126             shared_ptr<Effector> effector = shared_static_cast<Effector>(*iter);
00127             mEffectorMap[effector->GetPredicate()] = effector;
00128         }
00129 }
00130 
00131 bool
00132 AgentAspect::Init(const string& createEffector)
00133 {
00134     shared_ptr<Effector> create = shared_dynamic_cast<Effector>
00135         (GetCore()->New(createEffector));
00136 
00137     if (create.get() == 0)
00138         {
00139             GetLog()->Error()
00140                 << "ERROR: (AgentAspect) Could not construct a createEffector '"
00141                 << createEffector << "'\n";
00142             return false;
00143         }
00144 
00145     create->SetName("_CreateEffector");
00146 
00147     // link it into our hierarchy
00148     bool added = AddChildReference(create);
00149 
00150     if (! added)
00151         {
00152             GetLog()->Error()
00153                 << "ERROR: (AgentAspect) failed to set up the CreateEffector '"
00154                 << createEffector << "'\n";
00155             return false;
00156         } else
00157             {
00158                 GetLog()->Debug() << "(AgentAspect) created CreateEffector '"
00159                                   << createEffector << "'\n";
00160             }
00161 
00162     return added;
00163 }

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4