ARGoS
3
A parallel, multi-engine simulator for swarm robotics
|
00001 00008 #include "entity.h" 00009 #include "composable_entity.h" 00010 #include <argos3/core/utility/logging/argos_log.h> 00011 #include <argos3/core/simulator/space/space.h> 00012 00013 namespace argos { 00014 00015 /****************************************/ 00016 /****************************************/ 00017 00018 CEntity::CEntity(CComposableEntity* pc_parent) : 00019 m_pcParent(pc_parent), 00020 m_bEnabled(true), 00021 m_bCanBeEnabledIfDisabled(true) { 00022 } 00023 00024 /****************************************/ 00025 /****************************************/ 00026 00027 CEntity::CEntity(CComposableEntity* pc_parent, 00028 const std::string& str_id) : 00029 m_pcParent(pc_parent), 00030 m_strId(str_id), 00031 m_bEnabled(true), 00032 m_bCanBeEnabledIfDisabled(true) { 00033 } 00034 00035 /****************************************/ 00036 /****************************************/ 00037 00038 void CEntity::Init(TConfigurationNode& t_tree) { 00039 try { 00040 /* 00041 * Set the id of the entity from XML or type description 00042 */ 00043 /* Was an id specified explicitly? */ 00044 if(NodeAttributeExists(t_tree, "id")) { 00045 /* Yes, use that */ 00046 GetNodeAttribute(t_tree, "id", m_strId); 00047 } 00048 else { 00049 /* No, derive it from the parent */ 00050 if(m_pcParent != NULL) { 00051 UInt32 unIdCount = 0; 00052 while(GetParent().HasComponent(GetTypeDescription() + 00053 "[" + GetTypeDescription() + 00054 "_" + ToString(unIdCount) + 00055 "]")) { 00056 ++unIdCount; 00057 } 00058 m_strId = GetTypeDescription() + "_" + ToString(unIdCount); 00059 } 00060 else { 00061 THROW_ARGOSEXCEPTION("Root entities must provide the identifier tag"); 00062 } 00063 } 00064 } 00065 catch(CARGoSException& ex) { 00066 THROW_ARGOSEXCEPTION_NESTED("Failed to initialize an entity.", ex); 00067 } 00068 } 00069 00070 /****************************************/ 00071 /****************************************/ 00072 00073 std::string CEntity::GetContext() const { 00074 if(m_pcParent != NULL) { 00075 return GetParent().GetContext() + GetParent().GetId() + "."; 00076 } 00077 else { 00078 return ""; 00079 } 00080 } 00081 00082 /****************************************/ 00083 /****************************************/ 00084 00085 CComposableEntity& CEntity::GetParent() { 00086 if(m_pcParent != NULL) { 00087 return *m_pcParent; 00088 } 00089 else { 00090 THROW_ARGOSEXCEPTION("Entity \"" << GetId() << "\" has no parent"); 00091 } 00092 } 00093 00094 /****************************************/ 00095 /****************************************/ 00096 00097 const CComposableEntity& CEntity::GetParent() const { 00098 if(m_pcParent != NULL) { 00099 return *m_pcParent; 00100 } 00101 else { 00102 THROW_ARGOSEXCEPTION("Entity \"" << GetId() << "\" has no parent"); 00103 } 00104 } 00105 00106 /****************************************/ 00107 /****************************************/ 00108 00109 CEntity& CEntity::GetRootEntity() { 00110 if(m_pcParent != NULL) { 00111 return m_pcParent->GetRootEntity(); 00112 } 00113 else { 00114 return *this; 00115 } 00116 } 00117 00118 /****************************************/ 00119 /****************************************/ 00120 00121 const CEntity& CEntity::GetRootEntity() const { 00122 if(m_pcParent != NULL) { 00123 return m_pcParent->GetRootEntity(); 00124 } 00125 else { 00126 return *this; 00127 } 00128 } 00129 00130 /****************************************/ 00131 /****************************************/ 00132 00133 void CEntity::SetEnabled(bool b_enabled) { 00134 /* No need to set the state if the new one is the same as the old one */ 00135 if(m_bEnabled != b_enabled) { 00136 /* The wanted state if different from the current one */ 00137 if(! b_enabled) { 00138 /* We can always disable an entity */ 00139 m_bEnabled = false; 00140 } 00141 else if(m_bCanBeEnabledIfDisabled) { 00142 /* We can enable the entity */ 00143 m_bEnabled = true; 00144 } 00145 /* The 'else' branch is not necessary, 00146 it corresponds to not enabling the entity when we can't */ 00147 } 00148 } 00149 00150 /****************************************/ 00151 /****************************************/ 00152 00153 INIT_VTABLE_FOR(CEntity); 00154 00155 /****************************************/ 00156 /****************************************/ 00157 00158 REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CEntity); 00159 00160 /****************************************/ 00161 /****************************************/ 00162 00163 }