$darkmode
simulation_context.hpp
Go to the documentation of this file.
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  */
23 #pragma once
24 
25 #include <functional> // for function<>
26 #include <map> // for map<>
27 #include <memory> // for unique_ptr<>, shared_ptr<>
28 #include <optional> // for optional<>
29 #include <string> // for string
30 #include <vector> // for vector<>
31 
32 #include <sol/state_view.hpp> // for state_view
33 
34 #include <cloe/cloe_fwd.hpp> // for Simulator, Controller, Registrar, Vehicle, Duration
35 #include <cloe/stack.hpp> // for Stack
36 #include <cloe/utility/timer.hpp> // for DurationTimer
37 
38 #include "simulation_events.hpp" // for LoopCallback, ...
39 #include "simulation_outcome.hpp" // for SimulationOutcome
40 #include "simulation_probe.hpp" // for SimulationProbe
41 #include "simulation_progress.hpp" // for SimulationProgress
42 #include "simulation_result.hpp" // for SimulationResult
43 #include "simulation_statistics.hpp" // for SimulationStatistics
44 #include "simulation_sync.hpp" // for SimulationSync
45 
46 namespace engine {
47 
48 // Forward-declarations:
49 class CommandExecuter;
50 class Registrar;
51 class Coordinator;
52 class Server;
53 class SimulationResult;
54 class SimulationProbe;
55 
68  SimulationContext(cloe::Stack conf, sol::state_view l);
69 
70  // Configuration -----------------------------------------------------------
71  //
72  // These values are meant to be set before starting the simulation in order
73  // to affect how the simulation is run.
74  //
75  // The other values in this struct should not be directly modified unless
76  // you really know what you are doing.
77  //
78 
79  cloe::Stack config;
80  std::string uuid{};
81 
83  bool report_progress{false};
84 
89  bool probe_simulation{false};
90 
91  // Setup -------------------------------------------------------------------
92  //
93  // These are functional parts of the simulation framework that mostly come
94  // from the engine. They are all initialized in the constructor.
95  //
96  sol::state_view lua;
97  std::unique_ptr<cloe::DataBroker> db;
98  std::unique_ptr<Server> server;
99  std::shared_ptr<Coordinator> coordinator;
100  std::shared_ptr<Registrar> registrar;
101 
103  std::unique_ptr<CommandExecuter> commander;
104 
105  // State -------------------------------------------------------------------
106  //
107  // These are the types that represent the simulation state and have no
108  // functionality of their own, directly. They may change during the
109  // simulation.
110  //
111 
114 
117 
123 
124  std::map<std::string, std::unique_ptr<cloe::Simulator>> simulators;
125  std::map<std::string, std::shared_ptr<cloe::Vehicle>> vehicles;
126  std::map<std::string, std::unique_ptr<cloe::Controller>> controllers;
127 
129 
134  bool pause_execution{false};
135 
136  // Output ------------------------------------------------------------------
137  SimulationStatistics statistics;
138  std::optional<SimulationOutcome> outcome;
139  std::optional<SimulationResult> result;
140  std::optional<SimulationProbe> probe;
141 
142  // Events ------------------------------------------------------------------
143  //
144  // The following callbacks store listeners on the given events.
145  // In the state where an event occurs, the callback is then triggered.
146  // There is generally only one place where each of these callbacks is
147  // triggered.
148  //
149  std::shared_ptr<events::LoopCallback> callback_loop;
150  std::shared_ptr<events::PauseCallback> callback_pause;
151  std::shared_ptr<events::ResumeCallback> callback_resume;
152  std::shared_ptr<events::StartCallback> callback_start;
153  std::shared_ptr<events::StopCallback> callback_stop;
154  std::shared_ptr<events::SuccessCallback> callback_success;
155  std::shared_ptr<events::FailureCallback> callback_failure;
156  std::shared_ptr<events::ResetCallback> callback_reset;
157  std::shared_ptr<events::TimeCallback> callback_time;
158 
159  public:
160  // Helper Methods ----------------------------------------------------------
161  //
162  // These methods encapsulate methods on the data in this struct that can be
163  // used by various states. They constitute implementation details and may
164  // be refactored out of this struct at some point.
165  //
166  std::string version() const;
167  cloe::Logger logger() const;
168 
169  std::shared_ptr<cloe::Registrar> simulation_registrar();
170 
171  std::vector<std::string> model_ids() const;
172  std::vector<std::string> simulator_ids() const;
173  std::vector<std::string> controller_ids() const;
174  std::vector<std::string> vehicle_ids() const;
175  std::vector<std::string> plugin_ids() const;
176 
177  bool foreach_model(std::function<bool(cloe::Model&, const char* type)> f);
178  bool foreach_model(std::function<bool(const cloe::Model&, const char* type)> f) const;
179  bool foreach_simulator(std::function<bool(cloe::Simulator&)> f);
180  bool foreach_simulator(std::function<bool(const cloe::Simulator&)> f) const;
181  bool foreach_controller(std::function<bool(cloe::Controller&)> f);
182  bool foreach_controller(std::function<bool(const cloe::Controller&)> f) const;
183  bool foreach_vehicle(std::function<bool(cloe::Vehicle&)> f);
184  bool foreach_vehicle(std::function<bool(const cloe::Vehicle&)> f) const;
185 };
186 
187 } // namespace engine
Definition: controller.hpp:126
Definition: model.hpp:203
Definition: simulator.hpp:139
Definition: vehicle.hpp:106
Definition: simulation_progress.hpp:37
Definition: simulation_sync.hpp:34
Definition: simulation_context.hpp:67
cloe::Stack config
Input configuration.
Definition: simulation_context.hpp:79
std::string uuid
UUID to use for simulation.
Definition: simulation_context.hpp:80
bool pause_execution
Definition: simulation_context.hpp:134
SimulationProgress progress
Track the approximate progress of the simulation.
Definition: simulation_context.hpp:116
bool report_progress
Report simulation progress to the console.
Definition: simulation_context.hpp:83
bool probe_simulation
Definition: simulation_context.hpp:89
std::unique_ptr< CommandExecuter > commander
Configurable system command executer for triggers.
Definition: simulation_context.hpp:103
SimulationSync sync
Track the simulation timing.
Definition: simulation_context.hpp:113
cloe::Model * now_initializing
Definition: simulation_context.hpp:122
Definition: simulation_statistics.hpp:29