$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 <functional> // for function<>
26 #include <memory> // for unique_ptr<>
27 
28 #include <boost/filesystem/path.hpp> // for path
29 
30 #include <fable/enum.hpp> // for ENUM_SERIALIZATION
31 #include <sol/state.hpp> // for state
32 
33 #include "simulation_context.hpp"
34 #include "stack.hpp" // for Stack
35 
36 namespace engine {
37 
39  cloe::Stack config;
40 
41  std::string uuid;
42  SimulationSync sync;
43  cloe::Duration elapsed;
44  SimulationOutcome outcome;
45  std::vector<std::string> errors;
46  SimulationStatistics statistics;
47  cloe::Json triggers;
48  cloe::Json report;
49  cloe::Json signals; // dump of all signals in DataBroker right before the simulation started
50  std::vector<std::string>
51  signals_autocompletion; // pseudo lua file used for vscode autocompletion
52  std::optional<std::filesystem::path> output_dir;
53 
54  public:
68  std::filesystem::path get_output_filepath(const std::filesystem::path& filename) const {
69  std::filesystem::path filepath;
70  if (filename.is_absolute()) {
71  filepath = filename;
72  } else if (output_dir) {
73  filepath = *output_dir / filename;
74  } else {
75  throw cloe::ModelError{"cannot determine output path for '{}'", filename.native()};
76  }
77 
78  return filepath;
79  }
80 
86  void set_output_dir() {
87  if (config.engine.output_path) {
88  // For $registry to be of value, output_path (~= $id) here needs to be set.
89  if (config.engine.output_path->is_absolute()) {
90  // If it's absolute, then registry_path doesn't matter.
91  output_dir = *config.engine.output_path;
92  } else if (config.engine.registry_path) {
93  // Now, since output_dir is relative, we need the registry path.
94  // We don't care here whether the registry is relative or not.
95  output_dir = *config.engine.registry_path / *config.engine.output_path;
96  }
97  }
98  }
99 
100  friend void to_json(cloe::Json& j, const SimulationResult& r) {
101  j = cloe::Json{
102  {"elapsed", r.elapsed},
103  {"errors", r.errors},
104  {"outcome", r.outcome},
105  {"report", r.report},
106  {"simulation", r.sync},
107  {"statistics", r.statistics},
108  {"uuid", r.uuid},
109  };
110  }
111 };
112 
113 class Simulation {
114  public:
115  Simulation(cloe::Stack&& config, sol::state&& lua, const std::string& uuid);
116  ~Simulation() = default;
117 
121  cloe::Logger logger() const { return logger_; }
122 
130 
134  size_t write_output(const SimulationResult&) const;
135 
139  bool write_output_file(const std::filesystem::path& filepath, const cloe::Json& j) const;
140 
144  bool is_writable(const std::filesystem::path& filepath) const;
145 
149  void set_report_progress(bool value) { report_progress_ = value; }
150 
156  void signal_abort();
157 
158  private:
159  cloe::Stack config_;
160  sol::state lua_;
161  cloe::Logger logger_;
162  std::string uuid_;
163  std::function<void()> abort_fn_;
164 
165  // Options:
166  bool report_progress_{false};
167 };
168 
169 } // namespace engine
Definition: model.hpp:62
Definition: stack.hpp:879
Definition: simulation_context.hpp:55
Definition: simulation.hpp:113
void signal_abort()
Definition: simulation.cpp:1677
cloe::Logger logger() const
Definition: simulation.hpp:121
bool write_output_file(const std::filesystem::path &filepath, const cloe::Json &j) const
Definition: simulation.cpp:1626
SimulationResult run()
Definition: simulation.cpp:1471
void set_report_progress(bool value)
Definition: simulation.hpp:149
bool is_writable(const std::filesystem::path &filepath) const
Definition: simulation.cpp:1643
size_t write_output(const SimulationResult &) const
Definition: simulation.cpp:1599
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
SimulationOutcome
Definition: simulation_context.hpp:150
Definition: simulation.hpp:38
void set_output_dir()
Definition: simulation.hpp:86
std::filesystem::path get_output_filepath(const std::filesystem::path &filename) const
Definition: simulation.hpp:68
Definition: simulation_context.hpp:121