ExecPNGraph.hpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, LABUST, UNIZG-FER
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the LABUST nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 #ifndef EXECPNGRAPH_HPP_
00035 #define EXECPNGRAPH_HPP_
00036 #include <navcon_msgs/RegisterController.h>
00037 
00038 #include <boost/config.hpp>
00039 #include <boost/graph/adjacency_list.hpp>
00040 
00041 #include <list>
00042 #include <string>
00043 #include <map>
00044 #include <iosfwd>
00045 
00046 namespace labust
00047 {
00048         namespace control
00049         {
00053                 class ExecPNGraph
00054                 {
00055                         struct VertexProperty
00056                         {
00057                                 enum {p=0, t=1};
00058 
00059                                 VertexProperty():
00060                                         name("uninitialized"),
00061                                         type(0),
00062                                         marked(false){};
00063 
00064                                 VertexProperty(const std::string& name, int type):
00065                                         name(name),
00066                                         type(type),
00067                                         marked(false){};
00068 
00069                                 std::string name;
00070                                 int type;
00071                                 bool marked;
00072                         };
00073 
00074                         typedef boost::property < boost::edge_weight_t, int > EdgeProperty;
00075                         typedef boost::adjacency_list<boost::vecS, boost::vecS,
00076                                 boost::directedS, VertexProperty,
00077                                 EdgeProperty > GraphType;
00078 
00079                         struct PlaceInfo
00080                         {
00081                                 PlaceInfo():
00082                                         place_num(-1),
00083                                         enable_t(-1),
00084                                         disable_t(-1){};
00085 
00086                                 GraphType::vertex_descriptor
00087                                 place_num,
00088                                 enable_t,
00089                                 disable_t;
00090                         };
00091 
00092                 public:
00096                         ExecPNGraph();
00097 
00101                         void addToGraph(const navcon_msgs::RegisterControllerRequest& info);
00105                         void findPath(const std::string& start, const std::string& end,
00106                                         std::list<std::string>& path);
00110                         void getDotDesc(std::string& desc);
00111 
00112                 private:
00116                         inline GraphType::vertex_descriptor
00117                         add_place(const std::string& name)
00118                         {
00119                                 return boost::add_vertex(
00120                                                 VertexProperty(name,VertexProperty::p),graph);
00121                         }
00125                         inline GraphType::vertex_descriptor
00126                         add_transition(const std::string& name)
00127                         {
00128                                 return boost::add_vertex(
00129                                                 VertexProperty(name,VertexProperty::t),graph);
00130                         }
00134                         inline std::pair<GraphType::edge_descriptor, bool>
00135                         add_edge(GraphType::vertex_descriptor from,
00136                                         GraphType::vertex_descriptor to, int weight = 1)
00137                         {
00138                                 assert(((graph[from].type == VertexProperty::p) &&
00139                                                 (graph[to].type == VertexProperty::t)) ||
00140                                                 ((graph[from].type == VertexProperty::t)
00141                                                 && (graph[to].type == VertexProperty::p)) &&
00142                                                 "You can only connect places and transitions.");
00143                                 return boost::add_edge(from,to, weight, graph);
00144                         }
00148                         struct pn_writer {
00149                                 pn_writer(GraphType& graph):graph(graph){}
00150                                 template <class Vertex>
00151                                 void operator()(std::ostream &out, const Vertex& e) const
00152                                 {
00153 
00154                                         out << "[label="<< graph[e].name;
00155                                         out << ((graph[e].type == GraphType::vertex_property_type::value_type::t)?", shape=rectangle":"") << "]";
00156                                 }
00157                                 GraphType& graph;
00158                         };
00162                         GraphType graph;
00166                         std::map<std::string,
00167                                 PlaceInfo> nameMap;
00168                 };
00169         }
00170 }
00171 
00172 /* EXECPNGRAPH_HPP_ */
00173 #endif


labust_execution
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:36:24