Skip to content

Commit e63e8d8

Browse files
authored
Adds support for ISystemReset in test fixture (#2647)
* Adds support for Reset in test fixture This PR adds support for the Reset API to the test fixture. As `TestFixture` is one of the main ways one can get access to the ECM in python when trying to write some scripts for Deep Reinforcement Learning I realized that without `Reset` supported in the `TestFixture` API, end users would have a very hard time using our python APIs (which are actually quite nice). For reference I'm hacking a demo template here: #2667 --------- Signed-off-by: Arjo Chakravarty <[email protected]>
1 parent f05cda2 commit e63e8d8

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

include/gz/sim/TestFixture.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ class GZ_SIM_VISIBLE TestFixture
9696
public: TestFixture &OnPostUpdate(std::function<void(
9797
const UpdateInfo &, const EntityComponentManager &)> _cb);
9898

99+
/// \brief Wrapper around a system's update callback
100+
/// \param[in] _cb Function to be called every reset
101+
/// \return Reference to self.
102+
public: TestFixture &OnReset(std::function<void(
103+
const UpdateInfo &, EntityComponentManager &)> _cb);
104+
99105
/// \brief Finalize all the functions and add fixture to server.
100106
/// Finalize must be called before running the server, otherwise none of the
101107
/// `On*` functions will be called.

python/src/gz/sim/TestFixture.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ defineSimTestFixture(pybind11::object module)
8383
),
8484
pybind11::return_value_policy::reference,
8585
"Wrapper around a system's post-update callback"
86+
)
87+
.def(
88+
"on_reset", WrapCallbacks(
89+
[](TestFixture* self, std::function<void(
90+
const UpdateInfo &, EntityComponentManager &)> _cb)
91+
{
92+
self->OnReset(_cb);
93+
}
94+
),
95+
pybind11::return_value_policy::reference,
96+
"Wrapper around a system's reset callback"
8697
);
8798
// TODO(ahcorde): This method is not compiling for the following reason:
8899
// The EventManager class has an unordered_map which holds a unique_ptr

src/TestFixture.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class HelperSystem :
2929
public ISystemConfigure,
3030
public ISystemPreUpdate,
3131
public ISystemUpdate,
32-
public ISystemPostUpdate
32+
public ISystemPostUpdate,
33+
public ISystemReset
3334
{
3435
// Documentation inherited
3536
public: void Configure(
@@ -50,6 +51,10 @@ class HelperSystem :
5051
public: void PostUpdate(const UpdateInfo &_info,
5152
const EntityComponentManager &_ecm) override;
5253

54+
// Documentation inherited
55+
public: void Reset(const UpdateInfo &_info,
56+
EntityComponentManager &_ecm) override;
57+
5358
/// \brief Function to call every time we configure a world
5459
public: std::function<void(const Entity &_entity,
5560
const std::shared_ptr<const sdf::Element> &_sdf,
@@ -68,6 +73,10 @@ class HelperSystem :
6873
/// \brief Function to call every post-update
6974
public: std::function<void(const UpdateInfo &,
7075
const EntityComponentManager &)> postUpdateCallback;
76+
77+
/// \brief Reset callback
78+
public: std::function<void(const UpdateInfo &,
79+
EntityComponentManager &)> resetCallback;
7180
};
7281

7382
/////////////////////////////////////////////////
@@ -105,6 +114,14 @@ void HelperSystem::PostUpdate(const UpdateInfo &_info,
105114
this->postUpdateCallback(_info, _ecm);
106115
}
107116

117+
/////////////////////////////////////////////////
118+
void HelperSystem::Reset(const UpdateInfo &_info,
119+
EntityComponentManager &_ecm)
120+
{
121+
if (this->resetCallback)
122+
this->resetCallback(_info, _ecm);
123+
}
124+
108125
//////////////////////////////////////////////////
109126
class gz::sim::TestFixture::Implementation
110127
{
@@ -200,6 +217,15 @@ TestFixture &TestFixture::OnPostUpdate(std::function<void(
200217
return *this;
201218
}
202219

220+
//////////////////////////////////////////////////
221+
TestFixture &TestFixture::OnReset(std::function<void(
222+
const UpdateInfo &, EntityComponentManager &)> _cb)
223+
{
224+
if (nullptr != this->dataPtr->helperSystem)
225+
this->dataPtr->helperSystem->resetCallback = std::move(_cb);
226+
return *this;
227+
}
228+
203229
//////////////////////////////////////////////////
204230
std::shared_ptr<Server> TestFixture::Server() const
205231
{

0 commit comments

Comments
 (0)