AnimatLab  2
Test
NervousSystem.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Node.h"
13 #include "Link.h"
14 #include "IPhysicsMovableItem.h"
15 #include "IPhysicsBody.h"
16 #include "BoundingBox.h"
17 #include "MovableItem.h"
18 #include "BodyPart.h"
19 #include "Adapter.h"
20 #include "Joint.h"
21 #include "ReceptiveField.h"
22 #include "ContactSensor.h"
23 #include "RigidBody.h"
24 #include "Structure.h"
25 #include "NeuralModule.h"
26 #include "NervousSystem.h"
27 #include "Organism.h"
28 #include "ActivatedItem.h"
29 #include "ActivatedItemMgr.h"
30 #include "DataChartMgr.h"
31 #include "ExternalStimuliMgr.h"
32 #include "KeyFrame.h"
33 #include "SimulationRecorder.h"
34 #include "OdorType.h"
35 #include "Odor.h"
36 #include "Light.h"
37 #include "LightManager.h"
38 #include "Simulator.h"
39 
40 namespace AnimatSim
41 {
42  namespace Behavior
43  {
53 {
54  m_lpOrganism = NULL;
55 }
56 
66 {
67 
68 try
69 {
70  m_aryNeuralModules.RemoveAll();
71 }
72 catch(...)
73 {Std_TraceMsg(0, "Caught Error in desctructor of NervousSystem\r\n", "", -1, false, true);}
74 }
75 
76 
89 {
90  if(!lpModule)
91  THROW_ERROR(Al_Err_lNeuralModuleNotDefined, Al_Err_strNeuralModuleNotDefined);
92 
93  try
94  {
95  m_aryNeuralModules.Add(Std_CheckString(lpModule->ModuleName()), lpModule);
96  }
97  catch(CStdErrorInfo oError)
98  {
99  oError.m_strError += " Duplicate Neural Module Key: " + lpModule->ModuleName();
100  RELAY_ERROR(oError);
101  }
102 }
103 
119 void NervousSystem::AddNeuralModule(std::string strXml)
120 {
121  CStdXml oXml;
122  oXml.Deserialize(strXml);
123  oXml.FindElement("Root");
124  oXml.FindChildElement("NeuralModule");
125 
126  NeuralModule *lpModule = LoadNeuralModule(oXml);
127  lpModule->Initialize();
128 }
129 
144 void NervousSystem::RemoveNeuralModule(std::string strID)
145 {
146  m_aryNeuralModules.Remove(strID);
147 }
148 
149 
166 NeuralModule *NervousSystem::FindNeuralModule(std::string strModuleName, bool bThrowError)
167 {
168  NeuralModule *lpModule = NULL;
169  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
170  oPos = m_aryNeuralModules.find(Std_CheckString(strModuleName));
171 
172  if(oPos != m_aryNeuralModules.end())
173  lpModule = oPos->second;
174  else if(bThrowError)
175  THROW_PARAM_ERROR(Al_Err_lModuleNameNotFound, Al_Err_strModuleNameNotFound, "ModuleName", strModuleName);
176 
177  return lpModule;
178 }
179 
180 
181 void NervousSystem::Kill(bool bState)
182 {
183  NeuralModule *lpModule = NULL;
184  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
185  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
186  {
187  lpModule = oPos->second;
188  lpModule->Kill(bState);
189  }
190 }
191 
193 {
194  NeuralModule *lpModule = NULL;
195  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
196  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
197  {
198  lpModule = oPos->second;
199  lpModule->ResetSimulation();
200  }
201 }
202 
203 void NervousSystem::SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
204 {
205  AnimatBase::SetSystemPointers(lpSim, lpStructure, lpModule, lpNode, false);
206 
207  m_lpOrganism = dynamic_cast<Organism *>(lpStructure);
208 
209  if(bVerify) VerifySystemPointers();
210 }
211 
213 {
215 
216  if(!m_lpStructure)
217  THROW_PARAM_ERROR(Al_Err_lStructureNotDefined, Al_Err_strStructureNotDefined, "Link: ", m_strID);
218 
219  if(!m_lpOrganism)
220  THROW_PARAM_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Link: ", m_strID);
221 }
222 
224 {
226 
227  NeuralModule *lpModule = NULL;
228  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
229 
230  //Initialize the neural modules
231  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
232  {
233  lpModule = oPos->second;
234  lpModule->Initialize();
235  }
236 }
237 
238 void NervousSystem::MinTimeStep(float &fltMin)
239 {
240  NeuralModule *lpModule = NULL;
241  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
242 
243  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
244  {
245  lpModule = oPos->second;
246 
247  if(lpModule->TimeStep() < fltMin)
248  fltMin = lpModule->TimeStep();
249  }
250 }
251 
253 {
254  StepSim();
255  StepAdapters();
256 }
257 
258 void NervousSystem::StepSim()
259 {
260  NeuralModule *lpModule = NULL;
261  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
262 
263  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
264  {
265  lpModule = oPos->second;
266 
267  if(lpModule->NeedToStep(true))
268  lpModule->StepSimulation();
269  }
270 }
271 
272 void NervousSystem::StepAdapters()
273 {
274  NeuralModule *lpModule = NULL;
275  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
276 
277  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
278  {
279  lpModule = oPos->second;
280 
281  if(lpModule->NeedToStep(false))
282  {
283  lpModule->StepAdapters();
284  lpModule->ResetStepCounter();
285  }
286  }
287 }
288 
290 {
291  NeuralModule *lpModule = NULL;
292  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
293  long lSize = 0;
294 
295  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
296  {
297  lpModule = oPos->second;
298 
299  lSize += lpModule->CalculateSnapshotByteSize();
300  }
301 
302  return lSize;
303 }
304 
305 void NervousSystem::SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
306 {
307  NeuralModule *lpModule = NULL;
308  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
309 
310  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
311  {
312  lpModule = oPos->second;
313 
314  lpModule->SaveKeyFrameSnapshot(aryBytes, lIndex);
315  }
316 }
317 
318 void NervousSystem::LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
319 {
320  NeuralModule *lpModule = NULL;
321  CStdPtrMap<std::string, NeuralModule>::iterator oPos;
322 
323  for(oPos=m_aryNeuralModules.begin(); oPos!=m_aryNeuralModules.end(); ++oPos)
324  {
325  lpModule = oPos->second;
326 
327  lpModule->LoadKeyFrameSnapshot(aryBytes, lIndex);
328  }
329 }
330 
331 void NervousSystem::Load(CStdXml &oXml)
332 {
333  m_aryNeuralModules.RemoveAll();
334 
335  oXml.IntoChildElement("NeuralModules"); //Into NeuralModules Element
336 
337  int iCount = oXml.NumberOfChildren();
338  for(int iIndex=0; iIndex<iCount; iIndex++)
339  {
340  oXml.FindChildByIndex(iIndex);
341  LoadNeuralModule(oXml);
342  }
343 
344  oXml.OutOfElem(); //OutOf NeuralModules Element
345 }
346 
365 {
366  IStdClassFactory *lpFactory = NULL;
367  NeuralModule *lpModule = NULL;
368  std::string strModuleName, strModuleFileName, strModuleType;
369 
370 try
371 {
372  oXml.IntoElem(); //Into NeuralModule Element
373  strModuleName = oXml.GetChildString("ModuleName", "");
374  strModuleFileName = oXml.GetChildString("ModuleFileName", "");
375  strModuleType = oXml.GetChildString("Type");
376  oXml.OutOfElem(); //OutOf NeuralModule Element
377 
378  //Lets load the dynamic library and get a pointer to the class factory.
379  lpFactory = m_lpSim->LoadClassFactory(strModuleFileName);
380 
381  //Now create an instance of a neural module. There is only one type of
382  lpModule = dynamic_cast<NeuralModule *>(lpFactory->CreateObject("NeuralModule", strModuleType, true));
383  if(!lpModule)
384  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "NeuralModule");
385 
386  lpModule->SetSystemPointers(m_lpSim, m_lpStructure, NULL, NULL, true);
387 
388  //Clean up the original class factory. We will use the one in the NeuralModule from now on.
389  if(lpFactory)
390  {delete lpFactory; lpFactory = NULL;}
391 
392  //We must add the neural module factory before it is loaded because the module
393  //will need to use the standard Sim->CreateObject method and that requires
394  //that the sim have a pointer to this factory.
395  m_lpSim->AddNeuralModuleFactory(strModuleName, lpModule);
396 
397  lpModule->SetSystemPointers(m_lpSim, m_lpStructure, NULL, NULL, true);
398  lpModule->Load(oXml);
399 
400  AddNeuralModule(lpModule);
401 
402  return lpModule;
403 }
404 catch(CStdErrorInfo oError)
405 {
406  if(lpFactory) delete lpFactory;
407  if(lpModule) delete lpModule;
408  RELAY_ERROR(oError);
409  return NULL;
410 }
411 catch(...)
412 {
413  if(lpFactory) delete lpFactory;
414  if(lpModule) delete lpModule;
415  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
416  return NULL;
417 }
418 }
419 
420 
421  } //Behavior
422 } //AnimatSim
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
Base class file for all Animat simulation objects.
Declares the nervous system class.
Declares the simulation recorder class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
Simulates the entire environment.
Definition: Simulator.h:31
virtual void LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Loads a key frame snapshot.
Definition: AnimatBase.cpp:739
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
Root namespace for the base simulation library for AnimatLab.
virtual float TimeStep()
Gets the time step for this moudle in time units.
virtual void SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Saves a key frame snapshot.
Declares the body part class.
virtual void Kill(bool bState=true)
Kills.
Definition: AnimatBase.cpp:627
std::string m_strError
The error message.
Definition: StdErrorInfo.h:26
virtual void Initialize()
Initializes this object.
virtual bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual void Initialize()
Initializes this object.
virtual bool NeedToStep(bool bIncrement)
Tells whether this NeuralModule needs to call StepSimulation.
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
virtual void RemoveNeuralModule(std::string strID)
Removes the neural module based on its ID.
Declares the key frame class.
NervousSystem()
Default constructor.
Declares the joint class.
Declares the organism class.
Standard class factory.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
Declares a light object.
static IStdClassFactory * LoadClassFactory(std::string strModuleName, bool bThrowError=true)
Loads the class factory specified in the DLL module name.
Definition: Simulator.cpp:3019
Declares the activated item class.
virtual void ResetSimulation()
Resets the simulation back to time 0.
virtual std::string ModuleName()=0
Gets the module name.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
Declares a light manager object.
void AddNeuralModule(NeuralModule *lpModule)
Adds a neural module to the nervous system.
Declares the bounding box class.
virtual void StepSimulation()
Step the simulation for this object.
Organism * m_lpOrganism
The pointer to this node's organism.
Definition: NervousSystem.h:32
A standard xml manipulation class.
Definition: StdXml.h:19
A "static" structure in the simulation.
Definition: Structure.h:84
virtual void SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Saves a key frame snapshot.
Definition: AnimatBase.cpp:722
Declares the node class.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
NeuralModule * LoadNeuralModule(CStdXml &oXml)
Creates and loads a neural module.
virtual CStdSerialize * CreateObject(std::string strClassType, std::string strObjectType, bool bThrowError=true)=0
Creates an object of the specified class and object types.
virtual void LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Loads a key frame snapshot.
virtual long CalculateSnapshotByteSize()
Calculates the snapshot byte size.
virtual void Kill(bool bState=true)
Kills.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
virtual void ResetSimulation()
Resets the simulation back to time 0.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual ~NervousSystem()
Destructor.
virtual void StepAdapters()
Step adapters.
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
A dynamic organism that is controlled by a neural network.
Definition: Organism.h:31
Base class for body parts and neural network nodes.
Definition: Node.h:25
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void StepSimulation()
Step the simulation for this object.
Declares the data chart manager class.
Declares the rigid body class.
CStdPtrMap< std::string, NeuralModule > m_aryNeuralModules
The array of neural modules used within this nervous system.
Definition: NervousSystem.h:35
virtual bool IntoChildElement(std::string strElementName, bool bThrowError=true)
Goes into the child element with the specified name.
Definition: StdXml.cpp:278
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the structure class.
Declares the odor type class.
virtual long CalculateSnapshotByteSize()
Calculates the snapshot byte size.
Definition: AnimatBase.cpp:705
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
Definition: AnimatBase.cpp:315
virtual NeuralModule * FindNeuralModule(std::string strModuleName, bool bThrowError=true)
Searches for a neural module with a matching module name.
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
Declares the receptive field class.