AnimatLab  2
Test
PropertyControlAdapter.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 "PropertyControlAdapter.h"
22 #include "Joint.h"
23 #include "ReceptiveField.h"
24 #include "ContactSensor.h"
25 #include "RigidBody.h"
26 #include "Structure.h"
27 #include "NeuralModule.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 
43 namespace AnimatSim
44 {
45  namespace Adapters
46  {
54 {
55  m_lpTargetObject = NULL;
56  m_fltSetThreshold = 0.5;
57  m_fltPreviousSetVal = 0;
58  m_fltInitialValue = 0;
59  m_fltFinalValue = 1;
60 }
61 
69 {
70 
71 try
72 {
73  m_ePropertyType = AnimatPropertyType::Invalid;
74  m_lpTargetObject = NULL;
75 }
76 catch(...)
77 {Std_TraceMsg(0, "Caught Error in desctructor of PropertyControlAdapter\r\n", "", -1, false, true);}
78 }
79 
80 void PropertyControlAdapter::TargetID(std::string strID)
81 {
82  Adapter::TargetID(strID);
83  m_lpTargetObject = NULL;
84 }
85 
94 void PropertyControlAdapter::PropertyName(std::string strPropName)
95 {
96  //Reset the property name so we can get the property type setup correctly.
97  //If it is not set then we need to assume that they will set it later.
98  //Make sure the property type is set to invalid so the step sim method knows this.
99  if(m_lpTargetObject && !Std_IsBlank(strPropName))
100  {
101  if(Std_Trim(strPropName).length() == 0)
102  THROW_PARAM_ERROR(Al_Err_lPropertyNameBlank, Al_Err_strPropertyNameBlank, "Adapter ID", m_strID);
103 
104  if(!m_lpTargetObject->HasProperty(strPropName))
105  THROW_PARAM_ERROR(Al_Err_lTargetDoesNotHaveProperty, Al_Err_strTargetDoesNotHaveProperty, "Property name", strPropName);
106 
107  AnimatPropertyType ePropertyType = m_lpTargetObject->PropertyType(strPropName);
108  if(!(ePropertyType != AnimatPropertyType::Boolean || ePropertyType != AnimatPropertyType::Integer || ePropertyType != AnimatPropertyType::Float))
109  THROW_PARAM_ERROR(Al_Err_lTargetInvalidPropertyType, Al_Err_strTargetInvalidPropertyType, "Property name", strPropName);
110 
111  m_ePropertyType = ePropertyType;
112  }
113  else
114  m_ePropertyType = AnimatPropertyType::Invalid;
115 
116  m_strPropertyName = strPropName;
117 }
118 
128 {return m_strPropertyName;}
129 
141 void PropertyControlAdapter::SetThreshold(float fltThreshold)
142 {
143  if(fltThreshold < 0)
144  THROW_PARAM_ERROR(Al_Err_lInvalidSetThreshold, Al_Err_strInvalidSetThreshold, "Threshold", fltThreshold);
145 
146  m_fltSetThreshold = fltThreshold;
147 }
148 
158 {return m_fltSetThreshold;}
159 
169 {
170  m_fltInitialValue = fltVal;
171  m_fltPreviousSetVal = fltVal;
172 }
173 
183 {return m_fltInitialValue;}
184 
194 {
195  m_fltFinalValue = fltVal;
196 }
197 
207 {return m_fltFinalValue;}
208 
218 
219 bool PropertyControlAdapter::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
220 {
221  std::string strType = Std_CheckString(strDataType);
222 
223  if(Adapter::SetData(strDataType, strValue, false))
224  return true;
225 
226  if(strType == "PROPERTYNAME")
227  {
228  PropertyName(strValue);
229  return true;
230  }
231 
232  if(strType == "SETTHRESHOLD")
233  {
234  SetThreshold(atof(strValue.c_str()));
235  return true;
236  }
237 
238  if(strType == "INITIALVALUE")
239  {
240  InitialValue(atof(strValue.c_str()));
241  return true;
242  }
243 
244  if(strType == "FINALVALUE")
245  {
246  FinalValue(atof(strValue.c_str()));
247  return true;
248  }
249 
250  //If it was not one of those above then we have a problem.
251  if(bThrowError)
252  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Data Type", strDataType);
253 
254  return false;
255 }
256 
257 void PropertyControlAdapter::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
258 {
259  Adapter::QueryProperties(aryProperties);
260 
261  aryProperties.Add(new TypeProperty("PropertyName", AnimatPropertyType::String, AnimatPropertyDirection::Set));
262  aryProperties.Add(new TypeProperty("SetThreshold", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
263  aryProperties.Add(new TypeProperty("InitialValue", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
264  aryProperties.Add(new TypeProperty("FinalValue", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
265 }
266 
268 {
270 
271  if(m_bEnabled)
272  {
273  m_fltPreviousSetVal = m_fltInitialValue;
274  if(m_ePropertyType != AnimatPropertyType::Invalid)
275  m_lpTargetObject->SetData(m_strPropertyName, STR(m_fltFinalValue));
276  }
277 }
278 
280 {
281  if(m_bEnabled)
282  {
283  m_fltPreviousSetVal = m_fltInitialValue;
284  if(m_ePropertyType != AnimatPropertyType::Invalid)
285  m_lpTargetObject->SetData(m_strPropertyName, STR(m_fltPreviousSetVal));
286  }
287 }
288 
290 {
291  //Not calling Adapter initialize because we are doing a slightly different implementation
293 
294  m_lpSourceNode = dynamic_cast<Node *>(m_lpSim->FindByID(m_strSourceID));
295  if(!m_lpSourceNode)
296  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strSourceID);
297 
299 
300  if(!m_lpSourceData)
301  THROW_TEXT_ERROR(Al_Err_lDataPointNotFound, Al_Err_strDataPointNotFound,
302  ("Adapter: " + m_strID + " StructureID: " + m_lpStructure->ID() + "SourceID: " + m_strSourceID + " DataType: " + m_strSourceDataType));
303 
304  m_lpTargetNode = NULL; //This is always NULL for this object
306  if(!m_lpTargetObject)
307  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strTargetID);
308 
309  PropertyName(m_strPropertyName);
310 
311  m_fltPreviousSetVal = m_fltInitialValue;
312 
313  m_lpSim->AttachSourceAdapter(m_lpStructure, this);
314  m_lpSim->AttachTargetAdapter(m_lpStructure, this);
315 }
316 
317 void PropertyControlAdapter::SetPropertyValue(float fltVal)
318 {
319  //If they have not set the property name yet then we cannot set the property value
320  if(m_ePropertyType != AnimatPropertyType::Invalid)
321  {
322  float fltDiff = fltVal - m_fltPreviousSetVal;
323  if(fabs(fltDiff) > m_fltSetThreshold)
324  {
325  m_fltPreviousSetVal = fltVal;
326 
327  if(m_ePropertyType == AnimatPropertyType::Boolean)
328  {
329  if(fltDiff > 0)
330  m_lpTargetObject->SetData(m_strPropertyName, "1");
331  else
332  m_lpTargetObject->SetData(m_strPropertyName, "0");
333  }
334  else if(m_ePropertyType == AnimatPropertyType::Integer)
335  {
336  int iVal = (int) fltVal;
337  m_lpTargetObject->SetData(m_strPropertyName, STR(iVal));
338  }
339  else
340  m_lpTargetObject->SetData(m_strPropertyName, STR(fltVal));
341  }
342  }
343 }
344 
346 {
347  if(m_bEnabled)
348  {
349  float fltInput = m_lpGain->CalculateGain(*m_lpSourceData);
350  SetPropertyValue(fltInput);
351  }
352 }
353 
354 void PropertyControlAdapter::SetDestinationID(std::string strXml)
355 {
356  CStdXml oXml;
357  oXml.Deserialize(strXml);
358  oXml.FindElement("Root");
359  oXml.FindChildElement("Adapter");
360 
361  oXml.IntoElem(); //Into Adapter Element
362 
363  //Load Source Data
364  TargetModule(oXml.GetChildString("TargetModule"));
365  TargetID(oXml.GetChildString("TargetID"));
366  PropertyName(oXml.GetChildString("PropertyName", m_strPropertyName));
367  SetThreshold(oXml.GetChildFloat("SetThreshold", m_fltSetThreshold));
368  InitialValue(oXml.GetChildFloat("InitialValue", m_fltInitialValue));
369  FinalValue(oXml.GetChildFloat("FinalValue", m_fltFinalValue));
370 
371  oXml.OutOfElem(); //OutOf Adapter Element
372 
373  //Remove the adatper settings.
374  m_lpSim->RemoveSourceAdapter(m_lpStructure, this);
375  m_lpSim->RemoveTargetAdapter(m_lpStructure, this);
376 
377  Initialize();
378 }
379 
380 void PropertyControlAdapter::Load(CStdXml &oXml)
381 {
382  Adapter::Load(oXml);
383 
384  oXml.IntoElem(); //Into Adapter Element
385 
386  //Load Target Data
387  PropertyName(oXml.GetChildString("PropertyName", m_strPropertyName));
388  SetThreshold(oXml.GetChildFloat("SetThreshold", m_fltSetThreshold));
389  InitialValue(oXml.GetChildFloat("InitialValue", m_fltInitialValue));
390  FinalValue(oXml.GetChildFloat("FinalValue", m_fltFinalValue));
391 
392  oXml.OutOfElem(); //OutOf Adapter Element
393 }
394 
395 
396  } //Adapters
397 } //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.
Gain * m_lpGain
Pointer to the Gain that will be used to convert the source value into the target value...
Definition: Adapter.h:75
Declares the simulation recorder class.
virtual std::string TargetModule()
Gets the name of the target NeuralModule.
Definition: Adapter.cpp:189
virtual void SimStarting()
Called just before the simulation starts.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
virtual void StepSimulation()
Step the simulation for this object.
Root namespace for the base simulation library for AnimatLab.
Declares the body part class.
virtual AnimatBase * TargetObject()
Gets the target 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
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
virtual bool HasProperty(const std::string &strName)
Queries this object if it has a property with the given name.
Definition: AnimatBase.cpp:464
virtual std::string PropertyName()
Gets the name of the property that this adapter will be setting.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Definition: AnimatBase.cpp:340
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
virtual float FinalValue()
Gets the final value used to set this property when the simulation ends.
std::string m_strSourceDataType
DateType of the source variable that will be converted. This is retrieved using the GetDataPointer me...
Definition: Adapter.h:49
Declares the key frame class.
virtual float InitialValue()
Gets vthe initial value used to set this property when the simulation starts.
Node * m_lpSourceNode
Pointer to the source node.
Definition: Adapter.h:52
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
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: Adapter.cpp:626
std::string Std_Trim(std::string strVal)
Trims a string.
std::string m_strSourceID
GUID ID of the source node.
Definition: Adapter.h:46
std::string m_strTargetID
GUID ID of the target node.
Definition: Adapter.h:61
Declares a light object.
Node * m_lpTargetNode
Pointer to the target node.
Definition: Adapter.h:72
Declares the activated item class.
Declares a light manager object.
Declares the bounding box class.
Declares the gain base class.
A standard xml manipulation class.
Definition: StdXml.h:19
Declares the node class.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Adapter.cpp:753
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
virtual std::string TargetID()
Gets the GUID ID of the target node where we will add the transformed data variable.
Definition: Adapter.cpp:214
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 float SetThreshold()
Gets the threshold value used for determining when to set the value on the target object...
Animat base class.
Definition: AnimatBase.h:36
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
float * m_lpSourceData
Pointer to the source data varaible.
Definition: Adapter.h:55
virtual void ResetSimulation()
Resets the simulation back to time 0.
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
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the data chart manager class.
Declares the rigid body class.
virtual float CalculateGain(float fltInput)=0
Calculates the gain.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Declares the structure class.
Declares the odor type class.
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual void QueryProperties(CStdPtrArray< TypeProperty > &aryProperties)
Queries this object for a list of properties that can be changed using SetData.
Definition: Adapter.cpp:700
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
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
Declares the receptive field class.
AnimatBase * m_lpTargetObject
Pointer to the target node.
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617
virtual void Initialize()
Initializes this object.