mapstorage.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 10 Oct 2008 13:36:20 +0100
changeset 7 f227a74db59d
permissions -rw-r--r--
Update to compile with the latest LEMON (version 1.0 or [5e12d7734036])
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef MAPSTORAGE_H
    20 #define MAPSTORAGE_H
    21 
    22 class Mapstorage;
    23 
    24 #include <vector>
    25 #include <map>
    26 #include <string>
    27 #include "all_include.h"
    28 #include "xymap.h"
    29 #include <libgnomecanvasmm.h>
    30 #include "map_value.h"
    31 #include "map_value_map.h"
    32 
    33 ///class MapStorage handles NodeMaps and ArcMaps.
    34 
    35 ///Class MapStorage is responsible for storing
    36 ///NodeMaps and ArcMaps that can be shown later
    37 ///on GUI. Therefore maps can be added to it,
    38 ///and datas over the added maps can be queried.
    39 ///The maps will be stored in an std::map,
    40 ///referenced with their names. Unfortunately at
    41 ///the moment it works only with double type maps
    42 ///
    43 ///\todo too many things are public!!
    44 class MapStorage
    45 {
    46 private:
    47   std::string background_file_name;
    48   bool background_set;
    49   double background_scaling;
    50 public:
    51   class Error : public std::exception
    52   {
    53     private:
    54       std::string message;
    55     public:
    56       Error(const std::string& msg) : message(msg) {}
    57       virtual const char* what() const throw()
    58       {
    59         return message.c_str();
    60       }
    61       ~Error() throw() {}
    62   };
    63 
    64   void setBackground(const std::string& file_name);
    65   const std::string& getBackgroundFilename();
    66   bool isBackgroundSet();
    67   double getBackgroundScaling();
    68   void setBackgroundScaling(double scaling);
    69 
    70   enum MapSaveDest { GUI_SECT, NESET_SECT, DONT_SAVE };
    71   enum GuiSectSaveDest { LGF_FILE, CONF_FILE };
    72   struct SpecMapSaveOpts
    73   {
    74     enum Dest { GUI_SECT, NESET_SECT };
    75     enum MapNum { ONE_MAP, TWO_MAPS };
    76   };
    77 
    78   typedef Digraph::NodeMap<double> NumericNodeMap;
    79   typedef Digraph::NodeMap<std::string> StringNodeMap;
    80   typedef Digraph::ArcMap<double> NumericArcMap;
    81   typedef Digraph::ArcMap<std::string> StringArcMap;
    82   typedef Digraph::NodeMap<int> NodeLabelMap;
    83   typedef Digraph::ArcMap<int> ArcLabelMap;
    84   typedef XYMap<Digraph::NodeMap<double> > NodeCoordMap;
    85   typedef XYMap<Digraph::ArcMap<double> > ArrowCoordMap;
    86 
    87   struct ArcMapData
    88   {
    89     /// where to save the map
    90     MapSaveDest save_dest;
    91     /// read-only or read-write
    92     bool writeable;
    93     /// default value
    94     MapValue default_value;
    95     virtual MapValue::Type type() = 0;
    96     virtual MapValue get(Arc e) = 0;
    97     virtual void set(Arc e, MapValue v) = 0;
    98     ArcMapData(MapValue def_val) :
    99       save_dest(GUI_SECT),
   100       writeable(true),
   101       default_value(def_val)
   102     {}
   103   };
   104 
   105   struct NumericArcMapData : public ArcMapData
   106   {
   107     NumericArcMap map;
   108     MapValue::Type type() { return MapValue::NUMERIC; }
   109     MapValue get(Arc e) { return MapValue(map[e]); }
   110     void set(Arc e, MapValue v) { map.set(e, static_cast<double>(v)); }
   111     NumericArcMapData(Digraph& g, double def_val) :
   112       ArcMapData(MapValue(def_val)),
   113       map(g, def_val)
   114     {}
   115   };
   116 
   117   struct StringArcMapData : public ArcMapData
   118   {
   119     StringArcMap map;
   120     MapValue::Type type() { return MapValue::STRING; }
   121     MapValue get(Arc e) { return MapValue(map[e]); }
   122     void set(Arc e, MapValue v) { map.set(e, static_cast<std::string>(v)); }
   123     StringArcMapData(Digraph& g, std::string def_val) :
   124       ArcMapData(MapValue(def_val)),
   125       map(g, def_val)
   126     {}
   127   };
   128 
   129   struct NodeMapData
   130   {
   131     /// where to save the map
   132     MapSaveDest save_dest;
   133     /// read-only or read-write
   134     bool writeable;
   135     /// default value
   136     MapValue default_value;
   137     virtual MapValue::Type type() = 0;
   138     virtual MapValue get(Node e) = 0;
   139     virtual void set(Node e, MapValue v) = 0;
   140     NodeMapData(MapValue def_val) :
   141       save_dest(GUI_SECT),
   142       writeable(true),
   143       default_value(def_val)
   144     {}
   145   };
   146 
   147   struct NumericNodeMapData : public NodeMapData
   148   {
   149     NumericNodeMap map;
   150     MapValue::Type type() { return MapValue::NUMERIC; }
   151     MapValue get(Node e) { return MapValue(map[e]); }
   152     void set(Node e, MapValue v) { map.set(e, static_cast<double>(v)); }
   153     NumericNodeMapData(Digraph& g, double def_val) :
   154       NodeMapData(MapValue(def_val)),
   155       map(g, def_val)
   156     {}
   157   };
   158 
   159   struct StringNodeMapData : public NodeMapData
   160   {
   161     StringNodeMap map;
   162     MapValue::Type type() { return MapValue::STRING; }
   163     MapValue get(Node e) { return MapValue(map[e]); }
   164     void set(Node e, MapValue v) { map.set(e, static_cast<std::string>(v)); }
   165     StringNodeMapData(Digraph& g, std::string def_val) :
   166       NodeMapData(MapValue(def_val)),
   167       map(g, def_val)
   168     {}
   169   };
   170 
   171   typedef std::map<std::string, NodeMapData*> NodeMapStore;
   172   typedef std::map<std::string, ArcMapData*> ArcMapStore;
   173 
   174   struct GUISectData
   175   {
   176     std::vector<std::string> main_node_map_names;
   177     std::vector<std::string> main_arc_map_names;
   178 
   179     std::vector<std::string> gui_node_map_names;
   180     std::vector<std::string> gui_arc_map_names;
   181 
   182     std::map<std::string, MapValue::Type> node_map_types;
   183     std::map<std::string, MapValue::Type> arc_map_types;
   184 
   185     std::map<std::string, std::map<int, double>* > numeric_node_maps;
   186     std::map<std::string, std::map<int, std::string>* > string_node_maps;
   187 
   188     std::map<std::string, std::map<int, double>* > numeric_arc_maps;
   189     std::map<std::string, std::map<int, std::string>* > string_arc_maps;
   190 
   191     std::map<int, XY> node_coord_map;
   192     std::map<int, XY> arrow_coord_map;
   193 
   194     SpecMapSaveOpts::Dest node_coords_save_dest;
   195     SpecMapSaveOpts::MapNum node_coords_save_map_num;
   196     std::string node_coords_one_map_name;
   197     std::string node_coords_two_maps_1_name;
   198     std::string node_coords_two_maps_2_name;
   199 
   200     SpecMapSaveOpts::Dest arrow_coords_save_dest;
   201     SpecMapSaveOpts::MapNum arrow_coords_save_map_num;
   202     std::string arrow_coords_one_map_name;
   203     std::string arrow_coords_two_maps_1_name;
   204     std::string arrow_coords_two_maps_2_name;
   205 
   206     ~GUISectData()
   207     {
   208       using std::map;
   209       using std::vector;
   210       using std::pair;
   211       using std::string;
   212 
   213       for (map<string, map<int, double>* >::iterator it =
   214           numeric_node_maps.begin(); it != numeric_node_maps.end(); ++it)
   215       {
   216         delete it->second;
   217       }
   218       for (map<string, map<int, string>* >::iterator it =
   219           string_node_maps.begin(); it != string_node_maps.end(); ++it)
   220       {
   221         delete it->second;
   222       }
   223       for (map<string, map<int, double>* >::iterator it =
   224           numeric_arc_maps.begin(); it != numeric_arc_maps.end(); ++it)
   225       {
   226         delete it->second;
   227       }
   228       for (map<string, map<int, string>* >::iterator it =
   229           string_arc_maps.begin(); it != string_arc_maps.end(); ++it)
   230       {
   231         delete it->second;
   232       }
   233     }
   234   };
   235 public:
   236   ///The digraph for which the datas are stored.
   237   Digraph digraph;
   238   const Digraph& getDigraph();
   239 
   240 private:
   241   GuiSectSaveDest gui_sect_save_dest;
   242 
   243   SpecMapSaveOpts::Dest node_coords_save_dest;
   244   SpecMapSaveOpts::MapNum node_coords_save_map_num;
   245   SpecMapSaveOpts::Dest arrow_coords_save_dest;
   246   SpecMapSaveOpts::MapNum arrow_coords_save_map_num;
   247 
   248   NodeMapStore nodemaps;
   249   ArcMapStore arcmaps;
   250 
   251   NodeLabelMap node_label;
   252   ArcLabelMap arc_label;
   253 
   254   /// the coordinates of the nodes
   255   NodeCoordMap node_coords;
   256   Digraph::NodeMap<double> node_coords_x;
   257   Digraph::NodeMap<double> node_coords_y;
   258 
   259   /// the coordinates of the arrows on the arcs
   260   ArrowCoordMap arrow_coords;
   261   Digraph::ArcMap<double> arrow_coords_x;
   262   Digraph::ArcMap<double> arrow_coords_y;
   263 
   264   ///The content of the object has changed, update is needed.
   265   bool modified;
   266 
   267   ///Name of file loaded in object.
   268   std::string file_name;
   269 
   270   // the largest node label
   271   int max_node_label;
   272 
   273   // the largest arc label
   274   int max_arc_label;
   275 
   276   std::string node_coords_one_map_name;
   277   std::string node_coords_two_maps_1_name;
   278   std::string node_coords_two_maps_2_name;
   279 
   280   std::string arrow_coords_one_map_name;
   281   std::string arrow_coords_two_maps_1_name;
   282   std::string arrow_coords_two_maps_2_name;
   283 
   284 public:
   285   ///Stores the default values for the different visualization node attributes
   286   std::vector<Digraph::NodeMap<double> > default_nodemaps;
   287 
   288   ///Stores the default values for the different visualization arc attributes
   289   std::vector<Digraph::ArcMap<double> > default_arcmaps;
   290 
   291   ///Stores the active maps for the different visualization node attributes
   292   std::vector< std::string > active_nodemaps;
   293 
   294   /// Stores the active maps for the different visualization arc attributes
   295   std::vector< std::string > active_arcmaps;
   296 
   297 protected:
   298 
   299   /// Signal emitted on any change made on map values
   300 
   301   /// Signal emitted if the visualization of the maps might have to be updated.
   302   /// bool shows us whether the changed map is arc or nodemap.
   303   /// int tells us the refreshed property
   304   sigc::signal<void, bool, int> signal_prop;
   305 
   306   /// Signal emitted in the case of nodemap addition
   307 
   308   /// std::string is the
   309   ///name of the new map
   310   sigc::signal<void, std::string, MapValue::Type> signal_node_map;
   311 
   312   /// Signal emitted in the case of arcmap addition
   313 
   314   /// std::string is the
   315   ///name of the new map
   316   sigc::signal<void, std::string, MapValue::Type> signal_arc_map;
   317 
   318   /// Signal emitted, when entry in \ref MapWin should be changed.
   319   sigc::signal<void, bool, int, std::string> signal_map_win;
   320 
   321   /// Signal emitted, when entry in \ref DesignWin should be changed.
   322   sigc::signal<void, double, double, int> signal_design_win;
   323 
   324   ///Signal emitted when background should be set by \ref NoteBookTab
   325   sigc::signal<void> signal_background;
   326 
   327   ///Iteration number during digraph design
   328   int iterations;
   329 
   330   ///Attraction factor during digraph design
   331   double attraction;
   332 
   333   ///Propulsation factor during digraph design
   334   double propulsation;
   335 
   336 public:
   337   ///Constructor of MapStorage.
   338 
   339   ///Its all activity is initializing default values
   340   ///for different visualization attributes.
   341   MapStorage();
   342 
   343   ///Destructor of MapStorage
   344 
   345   ///Maps stored here are created with new. Destructor
   346   ///deletes them to free up the reserved memory.
   347   ~MapStorage();
   348 
   349   /// Registrates if the shown map by any attribute has changed to another.
   350 
   351   ///It handles the \ref active_arcmaps and
   352   ///\ref active_nodemaps vectors. It also emits \ref signal_prop signal to let
   353   ///know the interested objects that the visible map of a certain
   354   ///attribute has changed.
   355   ///\param itisarc arcmap or nodemap has changed
   356   ///\param prop the property of which the map is changed
   357   ///\param mapname the visible map
   358   void changeActiveMap(bool itisarc , int prop , std::string mapname);
   359 
   360   ///Emits signals that let change the active maps in \ref MapWin.
   361   void broadcastActiveMaps();
   362 
   363   /// Returns the active arcmap shown by a visualization property.
   364 
   365   /// \param prop is the property
   366   ///that shows the requested map.
   367   std::string getActiveArcMap(int prop);
   368 
   369   /// Returns the active nodemap shown by a visualization property.
   370 
   371   /// \param prop is the property
   372   ///that shows the requested map.
   373   std::string getActiveNodeMap(int prop);
   374 
   375   /// Returns the names of the arcmaps stored here.
   376   std::vector<std::string> getArcMapList(MapType type = ALL);
   377 
   378   /// Returns the names of the nodemaps stored here.
   379   std::vector<std::string> getNodeMapList(MapType type = ALL);
   380 
   381   ///returns \ref signal_prop to be able to connect functions to it
   382   sigc::signal<void, bool, int> signal_prop_ch();
   383 
   384   ///returns \ref signal_node_map to be able to connect functions to it
   385   sigc::signal<void, std::string, MapValue::Type> signal_node_map_ch(){return signal_node_map;};
   386 
   387   ///returns \ref signal_arc_map to be able to connect functions to it
   388   sigc::signal<void, std::string, MapValue::Type> signal_arc_map_ch(){return signal_arc_map;};
   389 
   390   ///returns \ref signal_map_win to be able to connect functions to it
   391   sigc::signal<void, bool, int, std::string> signal_map_win_ch(){return signal_map_win;};
   392 
   393   ///returns \ref signal_design_win to be able to connect functions to it
   394   sigc::signal<void, double, double, int> signal_design_win_ch(){return signal_design_win;};
   395 
   396   void createNodeMap(const std::string& name, MapValue::Type type,
   397     MapValue def_val);
   398   void createArcMap(const std::string& name, MapValue::Type type,
   399     MapValue def_val);
   400 
   401   ///returns \ref signal_background to be able to connect functions to it
   402   sigc::signal<void> signal_background_ch(){return signal_background;};
   403 
   404 
   405   ///Adds given map to storage.
   406 
   407   ///Emits \ref signal_prop if mapvalues have changed, and MapStorage gets to know it.
   408 
   409   ///If values in a map have changed, this function checks, whether it is displayed.
   410   ///This check means searching the given mapname between active maps
   411   ///(\ref active_nodemaps, \ref active_arcmaps). If it is there at a certain property,
   412   ///it emits a signal with the property, where the gotten mapname was found. One signal
   413   ///is emitted for each property displaying the given map.
   414   ///\param itisarc whether the map an arcmap or nodemap
   415   ///\param mapname name of map to visualize
   416   void mapChanged(bool itisarc, std::string mapname);
   417 
   418   ///Read datas from the given filename.
   419   int readFromFile(const std::string &);
   420 
   421   ///Save datas to the given filename.
   422   void writeToFile(const std::string &);
   423 
   424   ///Deletes all datastructures stored here.
   425   void clear();
   426 
   427   void get_design_data(double &, double &, int &);
   428   void set_attraction(double);
   429   void set_propulsation(double);
   430   void set_iteration(int);
   431 
   432   void redesign_data_changed();
   433 
   434   XY getNodeCoords(Node n) const;
   435   void setNodeCoords(Node n, XY c);
   436   XY getArrowCoords(Arc e) const;
   437   void setArrowCoords(Arc e, XY c);
   438 
   439   MapValue get(const std::string& name, Node node) const;
   440   void set(const std::string& name, Node node, MapValue val);
   441   MapValue get(const std::string& name, Arc arc) const;
   442   void set(const std::string& name, Arc arc, MapValue val);
   443 
   444   const std::string& getFileName() const;
   445   void setFileName(const std::string& fn);
   446 
   447   bool getModified() const;
   448   void setModified(bool m = true);
   449 
   450   Node addNode(XY);
   451   Arc addArc(Node, Node);
   452 
   453   NumericNodeMap& getNumericNodeMap(const std::string& name);
   454   StringNodeMap& getStringNodeMap(const std::string& name);
   455   NumericArcMap& getNumericArcMap(const std::string& name);
   456   StringArcMap& getStringArcMap(const std::string& name);
   457 
   458   MapValueArcMap getArcMap(const std::string& name);
   459   MapValueNodeMap getNodeMap(const std::string& name);
   460 
   461   int getLabel(Node) const;
   462   int getLabel(Arc) const;
   463 
   464   GuiSectSaveDest getGUIDataSaveLocation();
   465   void setGUIDataSaveLocation(GuiSectSaveDest dest);
   466 
   467   MapSaveDest getNodeMapSaveDest(std::string name) const;
   468   MapSaveDest getArcMapSaveDest(std::string name) const;
   469   void setNodeMapSaveDest(std::string name, MapSaveDest dest);
   470   void setArcMapSaveDest(std::string name, MapSaveDest dest);
   471 
   472   SpecMapSaveOpts::Dest getNodeCoordsSaveDest();
   473   SpecMapSaveOpts::Dest getArrowCoordsSaveDest();
   474   void setNodeCoordsSaveDest(SpecMapSaveOpts::Dest dest);
   475   void setArrowCoordsSaveDest(SpecMapSaveOpts::Dest dest);
   476 
   477   SpecMapSaveOpts::MapNum getNodeCoordsSaveMapNum();
   478   SpecMapSaveOpts::MapNum getArrowCoordsSaveMapNum();
   479   void setNodeCoordsSaveMapNum(SpecMapSaveOpts::MapNum num);
   480   void setArrowCoordsSaveMapNum(SpecMapSaveOpts::MapNum num);
   481 
   482   MapValue::Type getNodeMapElementType(std::string name) const;
   483   MapValue::Type getArcMapElementType(std::string name) const;
   484 
   485   const NodeLabelMap& getNodeLabelMap();
   486   const ArcLabelMap& getArcLabelMap();
   487 
   488   bool nodeMapExists(std::string name);
   489   bool arcMapExists(std::string name);
   490 
   491   std::vector<std::string> getArcMaps(MapType type = ALL);
   492   std::vector<std::string> getNodeMaps(MapType type = ALL);
   493 
   494   NodeCoordMap& getNodeCoordMap();
   495   ArrowCoordMap& getArrowCoordMap();
   496 
   497   const std::string& getNodeCoordsOneMapName();
   498   const std::string& getNodeCoordsTwoMaps1Name();
   499   const std::string& getNodeCoordsTwoMaps2Name();
   500   void setNodeCoordsOneMapName(const std::string& name);
   501   void setNodeCoordsTwoMaps1Name(const std::string& name);
   502   void setNodeCoordsTwoMaps2Name(const std::string& name);
   503 
   504   const std::string& getArrowCoordsOneMapName();
   505   const std::string& getArrowCoordsTwoMaps1Name();
   506   const std::string& getArrowCoordsTwoMaps2Name();
   507   void setArrowCoordsOneMapName(const std::string& name);
   508   void setArrowCoordsTwoMaps1Name(const std::string& name);
   509   void setArrowCoordsTwoMaps2Name(const std::string& name);
   510 
   511 private:
   512   ArcMapData* getArcMapData(std::string name) const;
   513   NodeMapData* getNodeMapData(std::string name) const;
   514   void readLGF(
   515       const std::string& filename,
   516       bool read_arc_label,
   517       const std::vector<std::string>& node_map_names,
   518       const std::vector<std::string>& arc_map_names,
   519       const std::map<std::string, MapValue::Type>& node_map_types,
   520       const std::map<std::string, MapValue::Type>& arc_map_types,
   521       const std::string& node_coord_xmap_name,
   522       const std::string& node_coord_ymap_name,
   523       const std::string& arrow_coord_xmap_name,
   524       const std::string& arrow_coord_ymap_name);
   525 
   526 public:
   527   void exportDigraphToEPS(std::vector<bool>, std::string, std::string);
   528 };
   529 
   530 #endif //MAPSTORAGE_H