$darkmode
simulation.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 <filesystem> // for path
26 #include <functional> // for function<>
27 #include <optional> // for optional<>
28 
29 #include <cloe/stack.hpp> // for Stack
30 #include <sol/state_view.hpp> // for state_view
31 
32 namespace engine {
33 
34 class SimulationContext;
35 class SimulationMachine;
36 class SimulationResult;
37 class SimulationProbe;
38 
39 class Simulation {
40  public:
41  Simulation(const Simulation&) = default;
42  Simulation(Simulation&&) = delete;
43  Simulation& operator=(const Simulation&) = default;
44  Simulation& operator=(Simulation&&) = delete;
45  Simulation(cloe::Stack&& config, sol::state_view lua, const std::string& uuid);
46  ~Simulation() = default;
47 
51  cloe::Logger logger() const { return logger_; }
52 
60 
67 
71  size_t write_output(const SimulationResult&) const;
72 
76  bool write_output_file(const std::filesystem::path& filepath, const cloe::Json& j) const;
77 
81  bool is_writable(const std::filesystem::path& filepath) const;
82 
86  void set_report_progress(bool value) { report_progress_ = value; }
87 
93  void signal_abort();
94 
95  private:
101  void set_output_dir();
102 
116  std::filesystem::path get_output_filepath(const std::filesystem::path& filename) const;
117 
125  void set_abort_handler(SimulationMachine& machine, SimulationContext& ctx,
126  std::function<void()> hook);
127 
131  void reset_abort_handler() { abort_fn_ = nullptr; }
132 
133  private:
134  cloe::Stack config_;
135  sol::state_view lua_;
136  cloe::Logger logger_;
137  std::string uuid_;
138  std::optional<std::filesystem::path> output_dir_;
139  std::function<void()> abort_fn_;
140 
141  // Options:
142  bool report_progress_{false};
143 };
144 
145 } // namespace engine
Definition: simulation_machine.hpp:101
Definition: simulation.hpp:39
void signal_abort()
Definition: simulation.cpp:366
cloe::Logger logger() const
Definition: simulation.hpp:51
bool write_output_file(const std::filesystem::path &filepath, const cloe::Json &j) const
Definition: simulation.cpp:315
SimulationResult run()
Definition: simulation.cpp:178
void set_report_progress(bool value)
Definition: simulation.hpp:86
bool is_writable(const std::filesystem::path &filepath) const
Definition: simulation.cpp:332
SimulationProbe probe()
Definition: simulation.cpp:241
size_t write_output(const SimulationResult &) const
Definition: simulation.cpp:288
Definition: simulation_context.hpp:67
Definition: simulation_probe.hpp:42
Definition: simulation_result.hpp:35