00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00079 TLeafList perceptors;
00080 ListChildrenSupportingClass<Perceptor>(perceptors,true);
00081
00082 shared_ptr<PredicateList> predList(new PredicateList());
00083
00084
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
00114 TLeafList effectors;
00115 ListChildrenSupportingClass<Effector>(effectors,true);
00116
00117
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
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 }