sml_graph_demo.cpp
Go to the documentation of this file.
1 // Copyright 2021 PickNik Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // * Neither the name of the PickNik Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 
29 #include <boost_sml/example.h>
31 
32 using StateMachine = boost::sml::sm<sml_example::StateMachineLogic>;
33 
34 int main() {
35  StateMachine sml;
37 
38  const std::string output_filename_dot = "/tmp/sml_transition_diagram.dot";
39  const std::string output_filename_png = "/tmp/sml_transition_diagram.png";
40  std::ofstream dot_file(output_filename_dot);
41  std::cout << "Saving sml tranistion diagram to: `" << output_filename_dot
42  << "`\n";
43  graph.write_graphiz(dot_file);
44  std::cout << "You can visualize it by running `dot -Tpng "
45  << output_filename_dot << " > " << output_filename_png
46  << " && xdg-open " << output_filename_png << "`\n";
47 
48  std::cout << "----------------------------------------\n";
49  graph.write_graphiz();
50 
51  std::cout << "----------------------------------------\n";
52  graph.write_all_reachable_states(graph.get_vertex_index("idle"));
53 
54  std::cout << "----------------------------------------\n";
56  graph.get_vertex_index("executing"));
57 
58  std::cout << "----------------------------------------\n";
59  auto path_1 = graph.find_path(graph.get_vertex_index("idle"),
60  graph.get_vertex_index("executing"));
61  std::reverse(path_1.begin(), path_1.end());
62  for (size_t i = 0; i < path_1.size() - 1; i++)
63  std::cout << graph.get_vertex_name(path_1.at(i)) << " -> ";
64  std::cout << graph.get_vertex_name(path_1.back()) << "\n";
65 
66  std::cout << "----------------------------------------\n";
67  auto path_2 = graph.find_path(graph.get_vertex_index("executing"),
68  graph.get_vertex_index("idle"));
69  if (path_2.size() == 1)
70  std::cout << "There's no path from 'executing' to 'idle'\n";
71 }
void write_path_between_two_states(const vertex_descriptor &start_vertex, const vertex_descriptor &goal_vertex, std::ostream &out=std::cout)
boost::sml::sm< sml_example::StateMachineLogic > StateMachine
void write_graphiz(std::ostream &out=std::cout) const
vertex_descriptor get_vertex_index(const std::string &vertex_name) const
std::string get_vertex_name(const vertex_descriptor &vertex_index) const
std::vector< vertex_descriptor > find_path(const vertex_descriptor &start_vertex, const vertex_descriptor &goal_vertex)
void write_all_reachable_states(const vertex_descriptor &start_vertex, std::ostream &out=std::cout)
int main()


boost_sml
Author(s): boost-experimental (https://github.com/boost-experimental)
autogenerated on Thu Jan 26 2023 03:14:02