Commit 6280c891 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

gui: defines editor state machine

parent d6beb5f7
......@@ -2236,13 +2236,7 @@ editor::show_model_dynamics(model& mdl) noexcept
ImGui::PopItemWidth();
if (simulation_show_value &&
match(st,
editor_status::success,
editor_status::running_once,
editor_status::running_once_need_join,
editor_status::running_step)) {
if (simulation_show_value && st != editor_status::editing) {
sim.dispatch(mdl.type, [&](const auto& d_array) {
const auto& dyn = d_array.get(mdl.id);
add_input_attribute(*this, dyn);
......@@ -2272,11 +2266,7 @@ editor::show_top() noexcept
if (top.children[i].first.index() == 0) {
const auto id = std::get<model_id>(top.children[i].first);
if (auto* mdl = sim.models.try_to_get(id); mdl) {
if (match(st,
editor_status::success,
editor_status::running_once,
editor_status::running_once_need_join,
editor_status::running_step) &&
if (st != editor_status::editing &&
models_make_transition[get_index(id)]) {
imnodes::PushColorStyle(imnodes::ColorStyle_TitleBar,
......@@ -2815,26 +2805,20 @@ show_plot_box(bool* show_plot)
static editor_id current = undefined<editor_id>();
if (auto* ed = make_combo_editor_name(current); ed) {
if (match(ed->st,
editor_status::success,
editor_status::running_once,
editor_status::running_once_need_join,
editor_status::running_step)) {
if (ImPlot::BeginPlot("simulation", "t", "s")) {
ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, 1.f);
for (const auto& obs : ed->observation_outputs) {
if (obs.observation_type ==
observation_output::type::multiplot &&
obs.xs.data()) {
ImPlot::PlotLine(obs.name.c_str(),
obs.xs.data(),
obs.ys.data(),
static_cast<int>(obs.xs.size()));
}
if (ImPlot::BeginPlot("simulation", "t", "s")) {
ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, 1.f);
for (const auto& obs : ed->observation_outputs) {
if (obs.observation_type ==
observation_output::type::multiplot &&
obs.xs.data()) {
ImPlot::PlotLine(obs.name.c_str(),
obs.xs.data(),
obs.ys.data(),
static_cast<int>(obs.xs.size()));
}
ImPlot::PopStyleVar(1);
ImPlot::EndPlot();
}
ImPlot::PopStyleVar(1);
ImPlot::EndPlot();
}
}
......
......@@ -115,10 +115,11 @@ using child_id = std::variant<model_id, cluster_id>;
enum class editor_status
{
success,
running_once,
running_once_need_join,
running_step,
editing,
initializing,
running_debug,
running_thread,
running_thread_need_join
};
static inline constexpr int not_found = -1;
......@@ -258,12 +259,13 @@ struct editor
double simulation_current = 10.0;
double simulation_next_time = 0.0;
long simulation_bag_id = 0;
double simulation_during_date;
int simulation_during_bag;
std::thread simulation_thread;
editor_status st = editor_status::success;
editor_status st = editor_status::editing;
status sim_st = status::success;
bool simulation_show_value = false;
bool stop = false;
......
......@@ -163,43 +163,45 @@ run_simulation(window_logger& log_w,
double end,
double& current,
editor_status& st,
status& sim_st,
const bool& stop) noexcept
{
current = begin;
if (auto ret = sim.initialize(current); irt::is_bad(ret)) {
st = editor_status::initializing;
if (sim_st = sim.initialize(current); irt::is_bad(sim_st)) {
log_w.log(3,
"Simulation initialization failure (%s)\n",
irt::status_string(ret));
st = editor_status::success;
irt::status_string(sim_st));
st = editor_status::editing;
return;
}
st = editor_status::running_thread;
do {
if (auto ret = sim.run(current); irt::is_bad(ret)) {
log_w.log(3, "Simulation failure (%s)\n", irt::status_string(ret));
st = editor_status::success;
if (sim_st = sim.run(current); irt::is_bad(sim_st)) {
log_w.log(
3, "Simulation failure (%s)\n", irt::status_string(sim_st));
st = editor_status::editing;
return;
}
} while (current < end && !stop);
sim.clean();
st = editor_status::running_once_need_join;
st = editor_status::running_thread_need_join;
}
static void
show_simulation_run_once(window_logger& log_w, editor& ed)
{
if (ed.st == editor_status::running_once_need_join) {
if (ed.st == editor_status::running_thread_need_join) {
if (ed.simulation_thread.joinable()) {
ed.simulation_thread.join();
ed.st = editor_status::success;
ed.st = editor_status::editing;
}
} else {
if (ImGui::Button("run")) {
initialize_observation(log_w, ed);
ed.st = editor_status::running_once;
ed.stop = false;
ed.simulation_thread = std::thread(&run_simulation,
......@@ -209,11 +211,12 @@ show_simulation_run_once(window_logger& log_w, editor& ed)
ed.simulation_end,
std::ref(ed.simulation_current),
std::ref(ed.st),
std::ref(ed.sim_st),
std::cref(ed.stop));
}
}
if (ed.st == editor_status::running_once) {
if (ed.st == editor_status::running_thread) {
ImGui::SameLine();
if (ImGui::Button("Force stop"))
ed.stop = true;
......@@ -229,119 +232,132 @@ simulation_init(window_logger& log_w, editor& ed)
ed.simulation_current = ed.simulation_begin;
ed.simulation_during_date = ed.simulation_begin;
ed.st = editor_status::initializing;
std::fill_n(
ed.models_make_transition.data(), ed.sim.models.max_used(), false);
if (auto ret = ed.sim.initialize(ed.simulation_current); irt::is_bad(ret)) {
if (ed.sim_st = ed.sim.initialize(ed.simulation_current);
irt::is_bad(ed.sim_st)) {
log_w.log(3,
"Simulation initialisation failure (%s)\n",
irt::status_string(ret));
ed.st = editor_status::success;
irt::status_string(ed.sim_st));
ed.st = editor_status::editing;
} else {
ed.simulation_next_time = ed.sim.sched.empty()
? time_domain<time>::infinity
: ed.sim.sched.tn();
ed.simulation_bag_id = 0;
ed.st = editor_status::running_step;
ed.st = editor_status::running_debug;
}
}
static void
show_simulation_run_debug(window_logger& log_w, editor& ed)
{
if (ed.st == editor_status::success) {
ImGui::Text("simulation finished");
if (ImGui::Button("init."))
simulation_init(log_w, ed);
} else {
ImGui::Text("Current time %g", ed.simulation_current);
ImGui::Text("Current bag %ld", ed.simulation_bag_id);
ImGui::Text("Next time %g", ed.simulation_next_time);
ImGui::Text("Model %lu", (unsigned long)ed.sim.sched.size());
if (ImGui::Button("re-init."))
simulation_init(log_w, ed);
ImGui::SameLine();
if (ImGui::Button("Next bag")) {
if (auto ret = ed.sim.run(ed.simulation_current); is_bad(ret)) {
ed.st = editor_status::success;
}
++ed.simulation_bag_id;
ImGui::Text("Current time %g", ed.simulation_current);
ImGui::Text("Current bag %ld", ed.simulation_bag_id);
ImGui::Text("Next time %g", ed.simulation_next_time);
ImGui::Text("Model %lu", (unsigned long)ed.sim.sched.size());
if (ImGui::Button("init."))
simulation_init(log_w, ed);
ImGui::SameLine();
if (ImGui::Button("Next bag")) {
if (ed.sim_st = ed.sim.run(ed.simulation_current); is_bad(ed.sim_st)) {
ed.st = editor_status::editing;
log_w.log(3,
"Simulation next bag failure (%s)\n",
irt::status_string(ed.sim_st));
}
++ed.simulation_bag_id;
}
ImGui::SameLine();
if (ImGui::Button("Bag >> 10")) {
for (int i = 0; i < 10; ++i) {
if (auto ret = ed.sim.run(ed.simulation_current); is_bad(ret)) {
ed.st = editor_status::success;
break;
}
++ed.simulation_bag_id;
ImGui::SameLine();
if (ImGui::Button("Bag >> 10")) {
for (int i = 0; i < 10; ++i) {
if (ed.sim_st = ed.sim.run(ed.simulation_current);
is_bad(ed.sim_st)) {
ed.st = editor_status::editing;
log_w.log(3,
"Simulation next bag failure (%s)\n",
irt::status_string(ed.sim_st));
break;
}
++ed.simulation_bag_id;
}
}
ImGui::SameLine();
if (ImGui::Button("Bag >> 100")) {
for (int i = 0; i < 100; ++i) {
if (auto ret = ed.sim.run(ed.simulation_current); is_bad(ret)) {
ed.st = editor_status::success;
break;
}
++ed.simulation_bag_id;
ImGui::SameLine();
if (ImGui::Button("Bag >> 100")) {
for (int i = 0; i < 100; ++i) {
if (ed.sim_st = ed.sim.run(ed.simulation_current);
is_bad(ed.sim_st)) {
ed.st = editor_status::editing;
log_w.log(3,
"Simulation next bag failure (%s)\n",
irt::status_string(ed.sim_st));
break;
}
++ed.simulation_bag_id;
}
}
ImGui::InputDouble("##date", &ed.simulation_during_date);
ImGui::InputDouble("##date", &ed.simulation_during_date);
ImGui::SameLine();
ImGui::SameLine();
if (ImGui::Button("run##date")) {
const auto end = ed.simulation_current + ed.simulation_during_date;
while (ed.simulation_current < end) {
if (auto ret = ed.sim.run(ed.simulation_current); is_bad(ret)) {
ed.st = editor_status::success;
break;
}
++ed.simulation_bag_id;
if (ImGui::Button("run##date")) {
const auto end = ed.simulation_current + ed.simulation_during_date;
while (ed.simulation_current < end) {
if (ed.sim_st = ed.sim.run(ed.simulation_current);
is_bad(ed.sim_st)) {
ed.st = editor_status::editing;
log_w.log(3,
"Simulation next bag failure (%s)\n",
irt::status_string(ed.sim_st));
break;
}
++ed.simulation_bag_id;
}
}
ImGui::InputInt("##bag", &ed.simulation_during_bag);
ImGui::InputInt("##bag", &ed.simulation_during_bag);
ImGui::SameLine();
ImGui::SameLine();
if (ImGui::Button("run##bag")) {
for (int i = 0, e = ed.simulation_during_bag; i != e; ++i) {
if (auto ret = ed.sim.run(ed.simulation_current); is_bad(ret)) {
ed.st = editor_status::success;
break;
}
++ed.simulation_bag_id;
if (ImGui::Button("run##bag")) {
for (int i = 0, e = ed.simulation_during_bag; i != e; ++i) {
if (ed.sim_st = ed.sim.run(ed.simulation_current);
is_bad(ed.sim_st)) {
ed.st = editor_status::editing;
log_w.log(3,
"Simulation next bag failure (%s)\n",
irt::status_string(ed.sim_st));
break;
}
++ed.simulation_bag_id;
}
}
if (ed.st == editor_status::running_step) {
ed.simulation_next_time = ed.sim.sched.empty()
? time_domain<time>::infinity
: ed.sim.sched.tn();
if (ed.st == editor_status::running_debug) {
ed.simulation_next_time = ed.sim.sched.empty()
? time_domain<time>::infinity
: ed.sim.sched.tn();
const auto& l = ed.sim.sched.list_model_id();
const auto& l = ed.sim.sched.list_model_id();
std::fill_n(ed.models_make_transition.data(),
ed.sim.models.max_used(),
false);
std::fill_n(
ed.models_make_transition.data(), ed.sim.models.max_used(), false);
for (auto it = l.begin(), e = l.end(); it != e; ++it)
ed.models_make_transition[get_index(*it)] = true;
} else {
ed.simulation_next_time = time_domain<time>::infinity;
}
for (auto it = l.begin(), e = l.end(); it != e; ++it)
ed.models_make_transition[get_index(*it)] = true;
} else {
ed.simulation_next_time = time_domain<time>::infinity;
}
}
......@@ -484,11 +500,7 @@ show_simulation_box(window_logger& log_w, bool* show_simulation)
// }
// }
if (match(ed->st,
editor_status::success,
editor_status::running_once,
editor_status::running_once_need_join,
editor_status::running_step)) {
if (ed->st != editor_status::editing) {
ImGui::Text("Current: %g", ed->simulation_current);
const double duration = ed->simulation_end - ed->simulation_begin;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment