AnimatLab  2
Test
PhysicsNeuralModule.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 "Gain.h"
20 #include "Adapter.h"
21 #include "Joint.h"
22 #include "ReceptiveField.h"
23 #include "ContactSensor.h"
24 #include "RigidBody.h"
25 #include "Structure.h"
26 #include "NeuralModule.h"
27 #include "PhysicsNeuralModule.h"
28 #include "NervousSystem.h"
29 #include "Organism.h"
30 #include "ActivatedItem.h"
31 #include "ActivatedItemMgr.h"
32 #include "DataChartMgr.h"
33 #include "ExternalStimuliMgr.h"
34 #include "KeyFrame.h"
35 #include "SimulationRecorder.h"
36 #include "OdorType.h"
37 #include "Odor.h"
38 #include "Light.h"
39 #include "LightManager.h"
40 #include "Simulator.h"
41 
42 namespace AnimatSim
43 {
44  namespace Behavior
45  {
46 
54 {
55 }
56 
64 {
65 
66 try
67 {
68  m_aryAdapters.RemoveAll();
69 }
70 catch(...)
71 {Std_TraceMsg(0, "Caught Error in desctructor of PhysicsNeuralModule\r\n", "", -1, false, true);}
72 }
73 
74 //The physics neural module should always return and be set to the physics time step of the simulation.
76 {
77  if(m_lpSim)
78  return m_lpSim->PhysicsTimeStep();
79  return 0;
80 }
81 
83 {
84  if(m_lpSim)
86 }
87 
88 void PhysicsNeuralModule::Kill(bool bState)
89 {
90  int iCount = m_aryAdapters.GetSize();
91  for(int iIndex=0; iIndex<iCount; iIndex++)
92  if(m_aryAdapters[iIndex])
93  m_aryAdapters[iIndex]->Kill(bState);
94 }
95 
97 {
98  int iCount = m_aryAdapters.GetSize();
99  for(int iIndex=0; iIndex<iCount; iIndex++)
100  if(m_aryAdapters[iIndex])
101  m_aryAdapters[iIndex]->ResetSimulation();
102 }
103 
105 {
107 
108  Organism *lpOrganism = dynamic_cast<Organism *>(m_lpStructure);
109  if(!lpOrganism)
110  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Organism");
111 
112  int iCount = m_aryAdapters.GetSize();
113  for(int iIndex=0; iIndex<iCount; iIndex++)
114  if(m_aryAdapters[iIndex])
115  m_aryAdapters[iIndex]->Initialize();
116 }
117 
118 #pragma region DataAccesMethods
119 
120 bool PhysicsNeuralModule::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
121 {
122  std::string strType = Std_CheckString(strDataType);
123 
124  if(NeuralModule::SetData(strDataType, strValue, false))
125  return true;
126 
127  if(strType == "TIMESTEP")
128  {
129  TimeStep((float) atof(strValue.c_str()));
130  return true;
131  }
132 
133  //If it was not one of those above then we have a problem.
134  if(bThrowError)
135  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
136 
137  return false;
138 }
139 
140 void PhysicsNeuralModule::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
141 {
142  NeuralModule::QueryProperties(aryProperties);
143 
144  aryProperties.Add(new TypeProperty("TimeStep", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
145 }
146 
147 void PhysicsNeuralModule::AddAdapter(std::string strXml, bool bDoNotInit)
148 {
149  CStdXml oXml;
150  oXml.Deserialize(strXml);
151  oXml.FindElement("Root");
152  oXml.FindChildElement("Adapter");
153 
154  Adapter *lpAdapter = LoadAdapter(oXml);
155  if(!bDoNotInit)
156  lpAdapter->Initialize();
157 }
158 
159 void PhysicsNeuralModule::RemoveAdapter(std::string strID)
160 {
161  int iIdx = FindAdapterListPos(strID);
162  m_aryAdapters[iIdx]->DetachAdaptersFromSimulation();
163  m_aryAdapters.RemoveAt(iIdx);
164 }
165 
166 int PhysicsNeuralModule::FindAdapterListPos(std::string strID, bool bThrowError)
167 {
168  std::string sID = Std_ToUpper(Std_Trim(strID));
169 
170  int iCount = m_aryAdapters.GetSize();
171  for(int iIndex=0; iIndex<iCount; iIndex++)
172  if(m_aryAdapters[iIndex]->ID() == sID)
173  return iIndex;
174 
175  if(bThrowError)
176  THROW_PARAM_ERROR(Al_Err_lAdapterIDNotFound, Al_Err_strAdapterIDNotFound, "ID", m_strID);
177 
178  return -1;
179 }
180 
181 bool PhysicsNeuralModule::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
182 {
183  std::string strType = Std_CheckString(strItemType);
184 
185  if(strType == "ADAPTER")
186  {
187  try
188  {
189  AddAdapter(strXml, bDoNotInit);
190  return true;
191  }
192  catch(CStdErrorInfo oError)
193  {
194  if(bThrowError)
195  RELAY_ERROR(oError);
196  }
197  }
198 
199 
200  //If it was not one of those above then we have a problem.
201  if(bThrowError)
202  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
203 
204  return false;
205 }
206 
207 bool PhysicsNeuralModule::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
208 {
209  std::string strType = Std_CheckString(strItemType);
210 
211  if(strType == "ADAPTER")
212  {
213  RemoveAdapter(strID);
214  return true;
215  }
216  //Synapses are stored in the destination neuron. They will be removed there.
217 
218 
219  //If it was not one of those above then we have a problem.
220  if(bThrowError)
221  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
222 
223  return false;
224 }
225 
226 #pragma endregion
227 
229 {
230  long lByteSize = 0;
231  int iCount = m_aryAdapters.GetSize();
232  for(int iIndex=0; iIndex<iCount; iIndex++)
233  if(m_aryAdapters[iIndex])
234  lByteSize += m_aryAdapters[iIndex]->CalculateSnapshotByteSize();
235 
236  return lByteSize;
237 }
238 
239 void PhysicsNeuralModule::SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
240 {
241  int iCount = m_aryAdapters.GetSize();
242  for(int iIndex=0; iIndex<iCount; iIndex++)
243  if(m_aryAdapters[iIndex])
244  m_aryAdapters[iIndex]->SaveKeyFrameSnapshot(aryBytes, lIndex);
245 }
246 
247 void PhysicsNeuralModule::LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
248 {
249  int iCount = m_aryAdapters.GetSize();
250  for(int iIndex=0; iIndex<iCount; iIndex++)
251  if(m_aryAdapters[iIndex])
252  m_aryAdapters[iIndex]->LoadKeyFrameSnapshot(aryBytes, lIndex);
253 }
254 
255 void PhysicsNeuralModule::Load(CStdXml &oXml)
256 {
258 
259  CStdXml oNetXml;
260 
261  oXml.IntoElem(); //Into NeuralModule Element
262 
263  ID(oXml.GetChildString("ID", m_strID));
264  Type(oXml.GetChildString("Type", m_strType));
265  Name(oXml.GetChildString("Name", m_strName));
266 
267  //We do NOT call the TimeStep mutator here because we need to call it only after all modules are loaded so we can calculate the min time step correctly.
268  m_fltTimeStep = oXml.GetChildFloat("TimeStep", m_fltTimeStep);
269 
270  //This will add this object to the object list of the simulation.
271  m_lpSim->AddToObjectList(this);
272 
273  m_arySourceAdapters.RemoveAll();
274  m_aryTargetAdapters.RemoveAll();
275 
276  if(oXml.FindChildElement("Adapters", false))
277  {
278  oXml.IntoElem(); //Into Adapters Element
279  int iCount = oXml.NumberOfChildren();
280 
281  for(int iIndex=0; iIndex<iCount; iIndex++)
282  {
283  oXml.FindChildByIndex(iIndex);
284  LoadAdapter(oXml);
285  }
286  oXml.OutOfElem(); //OutOf Adapters Element
287  }
288 
289  oXml.OutOfElem(); //OutOf NeuralModule Element
290 
291  TRACE_DEBUG("Finished loading nervous system config file.");
292 }
293 
311 Adapter *PhysicsNeuralModule::LoadAdapter(CStdXml &oXml)
312 {
313  Adapter *lpAdapter = NULL;
314  std::string strModuleName, strType;
315 
316 try
317 {
318  oXml.IntoElem(); //Into Child Element
319  strModuleName = oXml.GetChildString("ModuleName", "");
320  strType = oXml.GetChildString("Type");
321  oXml.OutOfElem(); //OutOf Child Element
322 
323  lpAdapter = dynamic_cast<Adapter *>(m_lpSim->CreateObject(strModuleName, "Adapter", strType));
324  if(!lpAdapter)
325  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Adapter");
326 
327  lpAdapter->SetSystemPointers(m_lpSim, m_lpStructure, NULL, NULL, true);
328  lpAdapter->Load(oXml);
329 
330  m_aryAdapters.Add(lpAdapter);
331 
332  return lpAdapter;
333 }
334 catch(CStdErrorInfo oError)
335 {
336  if(lpAdapter) delete lpAdapter;
337  RELAY_ERROR(oError);
338  return NULL;
339 }
340 catch(...)
341 {
342  if(lpAdapter) delete lpAdapter;
343  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
344  return NULL;
345 }
346 }
347 
348  } //Behavior
349 } //AnimatSim
350 
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
CStdArray< Adapter * > m_arySourceAdapters
An array of source adapters for this module.
Definition: NeuralModule.h:56
Base class file for all Animat simulation objects.
Declares the nervous system class.
virtual void PhysicsTimeStep(float fltVal)
Sets the integration time step for the physics engine.
Definition: Simulator.cpp:1126
Declares the simulation recorder class.
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 void ResetSimulation()
Resets the simulation back to time 0.
virtual float TimeStep()
Gets the time step for this moudle in time units.
virtual void Initialize()
Initializes this object.
Declares the body part class.
virtual std::string Type()
returns the string type name of this object.
Definition: AnimatBase.cpp:221
virtual void Kill(bool bState=true)
Kills.
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 std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
Declares the key frame class.
virtual void Initialize()
Initializes this object.
Definition: Adapter.cpp:786
virtual void LoadKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Loads a key frame snapshot.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
Declares the joint class.
Declares the organism class.
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
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
std::string Std_Trim(std::string strVal)
Trims a string.
CStdPtrArray< Adapter > m_aryAdapters
The array of adapters in this module.
Declares a light object.
Declares the activated item class.
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
Declares a light manager object.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual float TimeStep()
Gets the time step for this moudle in time units.
Declares the bounding box class.
CStdArray< Adapter * > m_aryTargetAdapters
An array of target adapters for this module.
Definition: NeuralModule.h:59
virtual std::string Name()
Gets the name of this object.
Definition: AnimatBase.cpp:195
Declares the gain base class.
A standard xml manipulation class.
Definition: StdXml.h:19
Declares the node class.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Definition: AnimatBase.cpp:447
virtual long CalculateSnapshotByteSize()
Calculates the snapshot byte size.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
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 bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
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
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
Declares the data chart manager class.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
Declares the structure class.
Declares the odor type class.
float m_fltTimeStep
The DT time step for this neural module in seconds.
Definition: NeuralModule.h:40
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
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
std::string m_strType
The type for this object. Examples are Box, Plane, Neuron, etc..
Definition: AnimatBase.h:58
virtual void AddToObjectList(AnimatBase *lpItem)
Adds an object to the list of all simulation objects.
Definition: Simulator.cpp:4068
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
Definition: AnimatBase.cpp:371
virtual void SaveKeyFrameSnapshot(byte *aryBytes, long &lIndex)
Saves a key frame snapshot.
Declares the receptive field class.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617