Skip to content

Commit d21fbd7

Browse files
tobifalkcassava
authored andcommitted
runtime: Add SetVariable and SetData trigger actions
1 parent a00f64f commit d21fbd7

File tree

8 files changed

+402
-213
lines changed

8 files changed

+402
-213
lines changed

engine/src/simulation.cpp

+25-61
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#include <cloe/core/abort.hpp> // for AsyncAbort
8787
#include <cloe/registrar.hpp> // for DirectCallback
8888
#include <cloe/trigger/example_actions.hpp> // for CommandFactory, BundleFactory, ...
89-
#include <cloe/trigger/macros.hpp> // for DEFINE_SIMPLE_ACTION
89+
#include <cloe/trigger/set_action.hpp> // for DEFINE_SET_STATE_ACTION, SetDataActionFactory
9090
#include <cloe/utility/resource_handler.hpp> // for INCLUDE_RESOURCE, RESOURCE_HANDLER
9191
#include <fable/utility.hpp> // for pretty_print
9292

@@ -260,62 +260,26 @@ class SimulationMachine
260260
namespace actions {
261261

262262
// clang-format off
263-
DEFINE_SIMPLE_ACTION(Pause, "pause", "pause simulation", SimulationMachine, { ptr_->pause(); })
264-
DEFINE_SIMPLE_ACTION(Resume, "resume", "resume paused simulation", SimulationMachine, { ptr_->resume(); })
265-
DEFINE_SIMPLE_ACTION(Stop, "stop", "stop simulation with neither success nor failure", SimulationMachine, { ptr_->stop(); })
266-
DEFINE_SIMPLE_ACTION(Succeed, "succeed", "stop simulation with success", SimulationMachine, { ptr_->succeed(); })
267-
DEFINE_SIMPLE_ACTION(Fail, "fail", "stop simulation with failure", SimulationMachine, { ptr_->fail(); })
268-
DEFINE_SIMPLE_ACTION(Reset, "reset", "attempt to reset simulation", SimulationMachine, { ptr_->reset(); })
269-
270-
DEFINE_SIMPLE_ACTION(KeepAlive, "keep_alive", "keep simulation alive after termination",
271-
SimulationContext, { ptr_->config.engine.keep_alive = true; })
272-
273-
DEFINE_SIMPLE_ACTION(ResetStatistics, "reset_statistics", "reset simulation statistics",
274-
SimulationStatistics, { ptr_->reset(); })
275-
// clang-format on
263+
DEFINE_SET_STATE_ACTION(Pause, "pause", "pause simulation", SimulationMachine, { ptr_->pause(); })
264+
DEFINE_SET_STATE_ACTION(Resume, "resume", "resume paused simulation", SimulationMachine, { ptr_->resume(); })
265+
DEFINE_SET_STATE_ACTION(Stop, "stop", "stop simulation with neither success nor failure", SimulationMachine, { ptr_->stop(); })
266+
DEFINE_SET_STATE_ACTION(Succeed, "succeed", "stop simulation with success", SimulationMachine, { ptr_->succeed(); })
267+
DEFINE_SET_STATE_ACTION(Fail, "fail", "stop simulation with failure", SimulationMachine, { ptr_->fail(); })
268+
DEFINE_SET_STATE_ACTION(Reset, "reset", "attempt to reset simulation", SimulationMachine, { ptr_->reset(); })
276269

277-
class RealtimeFactor : public cloe::Action {
278-
public:
279-
explicit RealtimeFactor(const std::string& name, SimulationSync* s, double realtime_factor)
280-
: Action(name), sync_(s), realtime_factor_(realtime_factor) {}
281-
cloe::ActionPtr clone() const override {
282-
return std::make_unique<RealtimeFactor>(name(), sync_, realtime_factor_);
283-
}
284-
void operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override {
285-
logger()->info("Setting target simulation speed: {}", realtime_factor_);
286-
sync_->set_realtime_factor(realtime_factor_);
287-
}
288-
bool is_significant() const override { return false; }
289-
void to_json(cloe::Json& j) const override {
290-
j = cloe::Json{
291-
{"factor", realtime_factor_},
292-
};
293-
}
270+
DEFINE_SET_STATE_ACTION(KeepAlive, "keep_alive", "keep simulation alive after termination",
271+
SimulationContext, { ptr_->config.engine.keep_alive = true; })
294272

295-
private:
296-
SimulationSync* sync_;
297-
double realtime_factor_;
298-
};
273+
DEFINE_SET_STATE_ACTION(ResetStatistics, "reset_statistics", "reset simulation statistics",
274+
SimulationStatistics, { ptr_->reset(); })
299275

300-
class RealtimeFactorFactory : public cloe::ActionFactory {
301-
public:
302-
using ActionType = RealtimeFactor;
303-
explicit RealtimeFactorFactory(SimulationSync* s)
304-
: cloe::ActionFactory("realtime_factor", "modify the simulation speed"), sync_(s) {}
305-
cloe::ActionPtr make(const cloe::Conf& c) const override {
306-
auto factor = c.get<double>("factor");
307-
return std::make_unique<RealtimeFactor>(name(), sync_, factor);
308-
}
309-
cloe::ActionPtr make(const std::string& s) const override {
310-
auto factor = std::stod(s);
311-
return make(cloe::Conf{cloe::Json{
312-
{"factor", factor},
313-
}});
314-
}
276+
DEFINE_SET_DATA_ACTION(RealtimeFactor, "realtime_factor", "modify the simulation speed", SimulationSync, "factor", double,
277+
{
278+
logger()->info("Setting target simulation speed: {}", value_);
279+
ptr_->set_realtime_factor(value_);
280+
})
315281

316-
private:
317-
SimulationSync* sync_;
318-
};
282+
// clang-format on
319283

320284
} // namespace actions
321285

@@ -477,8 +441,8 @@ StateId SimulationMachine::Connect::impl(SimulationContext& ctx) {
477441
ctx.now_initializing = x.get();
478442

479443
// Configure simulator:
480-
auto r = ctx.registrar->with_trigger_prefix(name)
481-
->with_api_prefix(std::string("/simulators/") + name);
444+
auto r = ctx.registrar->with_trigger_prefix(name)->with_api_prefix(
445+
std::string("/simulators/") + name);
482446
x->connect();
483447
x->enroll(*r);
484448

@@ -531,8 +495,8 @@ StateId SimulationMachine::Connect::impl(SimulationContext& ctx) {
531495
ctx.now_initializing = x.get();
532496

533497
// Configure component:
534-
auto r = ctx.registrar->with_trigger_prefix(name)
535-
->with_api_prefix(std::string("/components/") + name);
498+
auto r = ctx.registrar->with_trigger_prefix(name)->with_api_prefix(
499+
std::string("/components/") + name);
536500
x->connect();
537501
x->enroll(*r);
538502

@@ -625,8 +589,8 @@ StateId SimulationMachine::Connect::impl(SimulationContext& ctx) {
625589
}
626590

627591
// Configure vehicle:
628-
auto r = ctx.registrar->with_trigger_prefix(c.name)
629-
->with_api_prefix(std::string("/vehicles/") + c.name);
592+
auto r = ctx.registrar->with_trigger_prefix(c.name)->with_api_prefix(
593+
std::string("/vehicles/") + c.name);
630594
x->connect();
631595
x->enroll(*r);
632596

@@ -707,8 +671,8 @@ StateId SimulationMachine::Connect::impl(SimulationContext& ctx) {
707671
ctx.now_initializing = x.get();
708672

709673
// Configure
710-
auto r = ctx.registrar->with_trigger_prefix(name)
711-
->with_api_prefix(std::string("/controllers/") + name);
674+
auto r = ctx.registrar->with_trigger_prefix(name)->with_api_prefix(
675+
std::string("/controllers/") + name);
712676
x->set_vehicle(ctx.vehicles.at(c.vehicle));
713677
x->connect();
714678
x->enroll(*r);

engine/src/simulation_context.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <cloe/registrar.hpp> // for Registrar
3737
#include <cloe/simulator.hpp> // for Simulator
3838
#include <cloe/sync.hpp> // for Sync
39-
#include <cloe/trigger/macros.hpp> // for DEFINE_NIL_EVENT
39+
#include <cloe/trigger/nil_event.hpp> // for DEFINE_NIL_EVENT
4040
#include <cloe/utility/statistics.hpp> // for Accumulator
4141
#include <cloe/utility/timer.hpp> // for DurationTimer
4242
#include <cloe/vehicle.hpp> // for Vehicle

plugins/virtue/src/virtue.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include <cloe/plugin.hpp> // for EXPORT_CLOE_PLUGIN
3535
#include <cloe/registrar.hpp> // for DirectCallback
3636
#include <cloe/sync.hpp> // for Sync
37-
#include <cloe/trigger/macros.hpp> // for DEFINE_NIL_EVENT
37+
#include <cloe/trigger/nil_event.hpp> // for DEFINE_NIL_EVENT
3838
#include <cloe/vehicle.hpp> // for Vehicle
3939

4040
namespace cloe {

runtime/include/cloe/trigger.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* \see cloe/trigger.cpp
2121
* \see cloe/registrar.hpp
2222
* \see cloe/trigger/example_actions.hpp
23-
* \see cloe/trigger/macros.hpp
23+
* \see cloe/trigger/set_action.hpp
24+
* \see cloe/trigger/nil_event.hpp
2425
*/
2526

2627
#pragma once
@@ -481,7 +482,7 @@ class TriggerRegistrar {
481482
* interface, in which Triggers with the corresponding event are contained.
482483
*
483484
* \see cloe/registrar.hpp
484-
* \see cloe/trigger/macros.hpp
485+
* \see cloe/trigger/nil_event.hpp
485486
*/
486487
class Event : public Entity {
487488
public:
@@ -510,7 +511,7 @@ class Event : public Entity {
510511
* An EventFactory parses a single JSON object or string into an Event.
511512
*
512513
* \see cloe/registrar.hpp
513-
* \see cloe/trigger/macros.hpp
514+
* \see cloe/trigger/nil_event.hpp
514515
*/
515516
using EventFactory = TriggerFactory<Event>;
516517
using EventFactoryPtr = std::unique_ptr<EventFactory>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
/**
19+
* \file cloe/trigger/helper_macros.hpp
20+
*/
21+
22+
#pragma once
23+
#ifndef CLOE_TRIGGER_HELPER_MACROS_HPP_
24+
#define CLOE_TRIGGER_HELPER_MACROS_HPP_
25+
26+
#define _X_FACTORY(xName) xName##Factory
27+
#define _X_CALLBACK(xName) xName##Callback
28+
29+
#endif // CLOE_TRIGGER_HELPER_MACROS_HPP_

runtime/include/cloe/trigger/macros.hpp

-147
This file was deleted.

0 commit comments

Comments
 (0)