Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
BasicGuide handle next_transition if asked to
[simgrid.git] / src / mc / api / State.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_STATE_HPP
7 #define SIMGRID_MC_STATE_HPP
8
9 #include "src/mc/api/ActorState.hpp"
10 #include "src/mc/api/RemoteApp.hpp"
11 #include "src/mc/api/guide/GuidedState.hpp"
12 #include "src/mc/sosp/Snapshot.hpp"
13 #include "src/mc/transition/Transition.hpp"
14
15 namespace simgrid::mc {
16
17 /* A node in the exploration graph (kind-of) */
18 class XBT_PRIVATE State : public xbt::Extendable<State> {
19   static long expended_states_; /* Count total amount of states, for stats */
20
21   /**
22    * @brief An empty transition that leads to this state by default
23    */
24   const std::unique_ptr<Transition> default_transition_ = std::make_unique<Transition>();
25
26   /**
27    * @brief The outgoing transition: what was the last transition that
28    * we took to leave this state?
29    *
30    * The owner of the transition is the `ActorState` instance which exists in this state,
31    * or a reference to the internal default transition `Transition()` if no transition has been
32    * set
33    */
34   Transition* transition_ = default_transition_.get();
35
36   /** Sequential state ID (used for debugging) */
37   long num_ = 0;
38
39   /** Snapshot of system state (if needed) */
40   std::shared_ptr<Snapshot> system_state_;
41
42   /** Unique parent of this state. Required both for sleep set computation
43       and for guided model-checking */
44   const State* parent_state_;
45
46   std::unique_ptr<GuidedState> guide;
47
48   /* Sleep sets are composed of the actor and the corresponding transition that made it being added to the sleep
49    * set. With this information, it is check whether it should be removed from it or not when exploring a new
50    * transition */
51   std::map<aid_t, Transition> sleep_set_;
52
53 public:
54   explicit State(RemoteApp& remote_app);
55   explicit State(RemoteApp& remote_app, const State* parent_state);
56   /* Returns a positive number if there is another transition to pick, or -1 if not */
57   aid_t next_transition() const;
58   /* Same as next_transition, but choice is now guided, and a double corresponding to the
59    internal cost of the transition is returned */
60   std::pair<aid_t, double> next_transition_guided() const;
61
62   /* Explore a new path on the remote app; the parameter 'next' must be the result of a previous call to
63    * next_transition() */
64   void execute_next(aid_t next, RemoteApp& app);
65
66   long get_num() const { return num_; }
67   std::size_t count_todo() const;
68   void mark_todo(aid_t actor) { guide->actors_to_run_.at(actor).mark_todo(); }
69   void mark_all_enabled_todo();
70   bool is_actor_done(aid_t actor) const { return guide->actors_to_run_.at(actor).is_done(); }
71   Transition* get_transition() const;
72   void set_transition(Transition* t) { transition_ = t; }
73   std::map<aid_t, ActorState> const& get_actors_list() const { return guide->actors_to_run_; }
74
75   unsigned long get_actor_count() const { return guide->actors_to_run_.size(); }
76   bool is_actor_enabled(aid_t actor) { return guide->actors_to_run_.at(actor).is_enabled(); }
77
78   Snapshot* get_system_state() const { return system_state_.get(); }
79   void set_system_state(std::shared_ptr<Snapshot> state) { system_state_ = std::move(state); }
80
81   std::map<aid_t, Transition> const& get_sleep_set() const { return sleep_set_; }
82   void add_sleep_set(Transition* t)
83   {
84     sleep_set_.insert_or_assign(t->aid_, Transition(t->type_, t->aid_, t->times_considered_));
85   }
86
87   /* Returns the total amount of states created so far (for statistics) */
88   static long get_expanded_states() { return expended_states_; }
89 };
90 } // namespace simgrid::mc
91
92 #endif