Changeset 560bc4d in observatorio for simulacion/SimEscenariosEconomicos


Ignore:
Timestamp:
May 21, 2014, 9:30:33 AM (10 years ago)
Author:
Alejandro <amujica@…>
Branches:
simulacion
Children:
209ac10
Parents:
7bbc686
Message:

Subido a la rama simulacion, anteriormenete lo subi en una rama que cree erroneamente

Location:
simulacion/SimEscenariosEconomicos
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • simulacion/SimEscenariosEconomicos/Makefile

    r7bbc686 r560bc4d  
    44
    55XML = `pkg-config libxml++-2.6 --cflags --libs`
     6
     7GVI = `pkg-config libgvc --cflags`
     8
     9GVL = `pkg-config libgvc --libs`
    610
    711CXX = g++ -O3 -g
     
    1620       -Wno-parentheses -Wno-unused-function
    1721
    18 FLAGS = -std=c++0x -DDEBUG -D_GLIBCXX__PTHREADS $(WARN) $(XML)
     22FLAGS = -std=c++11 -DDEBUG -D_GLIBCXX__PTHREADS $(WARN) $(XML) $(GVC) $(GVL)
    1923
    2024INCLUDE = -I $(ALEPH) -I . -I $(PQ)
     
    2630
    2731SIM = simulator
     32
     33SB = svg_builder
    2834
    2935SOURCES = dbProperties.C dbConnection.C dbQuery.C common.C strQuery.C \
     
    3743PREFIX = ecosim_
    3844
    39 all: gb sim
     45all: obj gb sim sb
    4046
    41 gb: obj
    42         $(CXX) $(INCLUDE) $(GB).C -o $(PREFIX)$(GB) $(OBJECTS) $(LIBS) $(FLAGS)
     47gb:
     48        $(CXX) $(INCLUDE) $(GB).C -o $(PREFIX)$(GB) $(LIBS) $(FLAGS)
    4349
    44 sim: obj
    45         $(CXX) $(INCLUDE) $(SIM).C -o $(PREFIX)$(SIM) $(OBJECTS) $(LIBS) \
    46         $(FLAGS)
     50sim:
     51        $(CXX) $(INCLUDE) $(SIM).C -o $(PREFIX)$(SIM) $(LIBS) $(FLAGS)
     52
     53sb:
     54        $(CXX) $(INCLUDE) $(SB).C -o $(SB) $(LIBS) $(FLAGS)
    4755
    4856obj:
     
    5462        $(RM) *~ *.o
    5563clean-all:
    56         $(RM) *~ *.o $(PREFIX)$(GB) $(PREFIX)$(SIM) $(LOCALLIB)
     64        $(RM) *~ *.o $(PREFIX)$(GB) $(PREFIX)$(SIM) $(SB) $(LOCALLIB)
    5765
  • simulacion/SimEscenariosEconomicos/graph_builder.C

    r7bbc686 r560bc4d  
    2121*/
    2222
     23# include <argp.h>
     24
    2325# include <dbProperties.H>
    2426# include <ioManager.H>
    25 
    26 int main (int argc, char const * argv[]) {
    27 
    28     if (argc < 4) {
    29         std::cout << "usage: ./ecosim_graph_builder "
    30                   << "xmlRootsFileName xmlOutputFileName "
    31                   << "[db_server_info_file_name]\n";
     27# include <process.H>
     28
     29
     30class Fields
     31{
     32  Fields() { /* Empty */ }
     33
     34public:
     35  static std::string    dbFileName;
     36
     37  static std::string    xmlInputFileName;
     38
     39  static std::string    xmlOutputFileName;
     40
     41  static bool           generateXml;
     42
     43  static std::string    svgOutputFileName;
     44
     45  static bool           generateSvg;
     46
     47  static std::string    gvOutputFileName;
     48
     49  static std::string    dotOutputFileName;
     50
     51  static bool           generateGv;
     52
     53  static bool           generateDot;
     54
     55  static bool           verbose;
     56};
     57
     58std::string Fields::dbFileName = "";
     59
     60std::string Fields::xmlInputFileName = "";
     61
     62std::string Fields::xmlOutputFileName = "";
     63
     64bool Fields::generateXml = false;
     65
     66std::string Fields::svgOutputFileName = "";
     67
     68bool Fields::generateSvg = false;
     69
     70std::string Fields::gvOutputFileName = "";
     71
     72bool Fields::generateGv = false;
     73
     74std::string Fields::dotOutputFileName = "";
     75
     76bool Fields::generateDot = false;
     77
     78bool Fields::verbose = false;
     79
     80const char * argp_program_version =  "Graph Builder v1.1";
     81
     82const char * argp_program_bug_address = "amujica@cenditel.gob.ve";
     83
     84static char doc[] = "";
     85
     86static char argDoc[] = "";
     87
     88static struct argp_option options [] = {
     89  {"database", 'b', "database-filename", 0,
     90   "filename with database attributes", 0},
     91  {"xml-input", 'i', "xml-input-filename", 0, "xml input filename", 0},
     92  {"xml-output", 'o', "xml-output-filename", OPTION_ARG_OPTIONAL,
     93   "xml output filename", 0},
     94  {"svg-output", 's', "svg-output-filename", OPTION_ARG_OPTIONAL,
     95   "svg output filename", 0},
     96  {"gv-output", 'g', "gv-output-filename", OPTION_ARG_OPTIONAL,
     97   "gv output filename", 0},
     98  {"dot-output", 'd', "dot-output-filename", OPTION_ARG_OPTIONAL,
     99   "dot output filename", 0},
     100  {"keep-dot", 'k', 0, OPTION_ARG_OPTIONAL, "Don't remove temporal'", 0},
     101  {"verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Verbose mode", 0},
     102  { 0, 0, 0, 0, 0, 0 }
     103};
     104
     105static error_t parser_opt(int key, char * arg, struct argp_state *)
     106{
     107  switch (key)
     108    {
     109    case 'b':
     110      if (arg == NULL)
     111        {
     112          std::cout << "Waiting for filename with database attributes\n";
     113          exit(0);
     114        }
     115      Fields::dbFileName = arg;
     116      break;
     117
     118    case 'i':
     119      if (arg == NULL)
     120        {
     121          std::cout << "Waiting for xml input filename\n";
     122          exit(0);
     123        }
     124      Fields::xmlInputFileName = arg;
     125      break;
     126
     127    case 'o':
     128      if (arg == NULL)
     129        {
     130          std::cout << "Waiting for xml output filename\n";
     131          exit(0);
     132        }
     133      Fields::xmlOutputFileName = arg;
     134      Fields::generateXml = true;
     135      break;
     136
     137    case 's':
     138      if (arg == NULL)
     139        {
     140          std::cout << "Waiting for svg output filename\n";
     141          exit(0);
     142        }
     143      Fields::svgOutputFileName = arg;
     144      Fields::generateSvg = true;
     145      break;
     146
     147    case 'g':
     148      if (arg == NULL)
     149        {
     150          std::cout << "Waiting for gv output filename\n";
     151          exit(0);
     152        }
     153      Fields::gvOutputFileName = arg;
     154      Fields::generateGv = true;
     155      break;
     156
     157    case 'd':
     158      if (arg == NULL)
     159        {
     160          std::cout << "Waiting for dot output filename\n";
     161          exit(0);
     162        }
     163      Fields::dotOutputFileName = arg;
     164      Fields::generateDot = true;
     165      break;
     166
     167    case 'k':
     168      Fields::generateDot = true;
     169      break;
     170
     171    case 'v':
     172      Fields::verbose = true;
     173      break;
     174     
     175    default: return ARGP_ERR_UNKNOWN;
     176    }
     177
     178  return 0;
     179}
     180
     181static struct argp arg_defs = { options, parser_opt, argDoc, doc, 0, 0, 0 };
     182
     183int main (int argc, char * argv[]) {
     184
     185    argp_parse(&arg_defs, argc, argv, ARGP_IN_ORDER, 0, NULL);
     186
     187    if (Fields::dbFileName == "" or Fields::xmlInputFileName == "") {
     188
     189        std::cout << "filename with database attributes and xml input filename "
     190                     "are required\n";
    32191        return 1;
     192
    33193    }
    34194
    35     std::string xmlRootsFileName = argv[1];
    36 
    37     std::string xmlOutputFileName = argv[2];
    38 
    39     std::string dbFileName = argc > 3 ? argv[3] : "dbserver";
    40 
    41     DBProperties::getInstance().readFile(dbFileName);
     195    DBProperties::getInstance().readFile(Fields::dbFileName);
    42196
    43197    IOManager ioManager;
     
    47201    try {
    48202
    49         ioManager.buildGraph(xmlRootsFileName, graph);
    50         ioManager.addGraphToFile(graph, xmlRootsFileName, xmlOutputFileName,
    51                                  IOManager::GRAPH);
     203        ioManager.buildGraph(Fields::xmlInputFileName, graph);
     204
     205        if (Fields::generateXml) {
     206
     207            if (Fields::verbose) {
     208                std::cout << "Generating xml output...\n";
     209            }
     210
     211            ioManager.addGraphToFile(graph, Fields::xmlInputFileName,
     212                                     Fields::xmlOutputFileName,
     213                                     IOManager::GRAPH);
     214
     215            if (Fields::verbose) {
     216                std::cout << "Xml output generated.\n";
     217            }
     218        }
    52219
    53220    } catch(const std::exception & e) {
    54221
    55         std::cout << e.what() << std::endl;
     222        std::cout << "Exception catched: " << e.what() << std::endl;
    56223        return 2;
    57224    }
    58225
     226
     227    if (Fields::generateSvg or Fields::generateGv or Fields::generateDot) {
     228
     229        std::string dotFileName =
     230            Fields::generateDot ? Fields::dotOutputFileName : "temp.dot";
     231
     232        if (Fields::verbose) {
     233            std::cout << "Generating " << dotFileName << "...\n";
     234        }
     235
     236        ioManager.generateDot<IOManager::NodeLabel, IOManager::ArcLabel>
     237            (graph, dotFileName);
     238
     239        if (Fields::verbose) {
     240            std::cerr << dotFileName << " generated.\n";
     241        }
     242
     243        int returnValue = 0;
     244
     245        char const * path = "dot";
     246
     247        char * args[] = {"dot", "", (char *) dotFileName.c_str(), "-o", "", 0};
     248
     249            if (Fields::generateSvg) {
     250
     251                args[1] = "-Tsvg";
     252                args[4] = (char *) Fields::svgOutputFileName.c_str();
     253
     254                if (Fields::verbose) {
     255                    std::cout << "Generating " << Fields::svgOutputFileName
     256                              << "...\n";
     257                }
     258
     259                returnValue = Process::exec(path, args);
     260
     261                if (returnValue == 0) {
     262                    if (Fields::verbose) {
     263                        std::cerr << Fields::svgOutputFileName
     264                                  << " generated.\n";
     265                    }
     266                } else {
     267                    std::cout << "Error generating "
     268                              << Fields::svgOutputFileName << "\n";
     269                }
     270            }
     271
     272            if (Fields::generateGv) {
     273
     274                args[1] = "-Tgv";
     275                args[4] = (char *) Fields::gvOutputFileName.c_str();
     276
     277                if (Fields::verbose) {
     278                    std::cout << "Generating " << Fields::gvOutputFileName
     279                              << "...\n";
     280                }
     281
     282                returnValue = Process::exec(path, args);
     283
     284                if (returnValue == 0) {
     285                    if (Fields::verbose) {
     286                        std::cerr << Fields::gvOutputFileName
     287                                  << " generated.\n";
     288                    }
     289                } else {
     290                    std::cout << "Error generating "
     291                              << Fields::gvOutputFileName << "\n";
     292                }
     293            }
     294
     295            if (not Fields::generateDot) {
     296
     297                if (Fields::verbose) {
     298                    std::cout << "Removing " << dotFileName << "...\n";
     299                }
     300
     301                remove(dotFileName.c_str());
     302
     303                if (Fields::verbose) {
     304                    std::cerr << dotFileName << " removed.\n";
     305                }
     306            }
     307
     308        }
     309
    59310    return 0;
    60311}
  • simulacion/SimEscenariosEconomicos/ioManager.H

    r7bbc686 r560bc4d  
    2727*/
    2828
    29 
    3029# ifndef IO_MANAGER_H
    3130# define IO_MANAGER_H
     
    17391738                                    NodeSet & nodes, size_t & numClusters,
    17401739                                    size_t & numNodes,
    1741                                     NodeLabel & nodeLabel) {
     1740                                    NodeLabel & nodeLabel,
     1741                                    bool writeHref) {
    17421742
    17431743        std::string companyName = cinfo.companyName;
     
    17971797                 << " label = " << label
    17981798                 << " shape = box style = filled color = " << bgColor
    1799                  << " fontcolor = " << fontColor << "];\n";
     1799                 << " fontcolor = " << fontColor;
     1800
     1801            if (writeHref)
     1802                file << " href = \"#\""
     1803                     << " onclick = \"click_nodo_primario($(this), '"
     1804                     << cinfo.rif << "');\"";
     1805
     1806            file << "];\n";
    18001807
    18011808            map.insert(p, numNodes++);
     
    18821889        size_t numNodes;
    18831890
     1891        bool writeHref;
     1892
    18841893        ClusterMapOp(std::ostream & _file,
    18851894                     Map <Graph::Node *, size_t> & _map,
    1886                      NodeLabel & _nodeLabel)
     1895                     NodeLabel & _nodeLabel, bool _writeHref)
    18871896            : file(_file), map(_map), nodeLabel(_nodeLabel),
    1888               numClusters(0), numNodes(0) {
     1897              numClusters(0), numNodes(0), writeHref(_writeHref) {
    18891898
    18901899            // Empty
     
    18931902        void operator () (const ClusterByPlantInfo & cinfo, NodeSet & nodes) {
    18941903            writeClusterByPlant(file, map, cinfo, nodes, numClusters, numNodes,
    1895                                 nodeLabel);
     1904                                nodeLabel, writeHref);
    18961905        }
    18971906    };
     
    20902099     */
    20912100    template <class NodeLabel, class ArcLabel>
    2092     void generateDot(Graph & g, const std::string & fileName,
     2101    void generateDot(Graph & g, std::ostream & output, bool writeHref,
    20932102                     NodeLabel & nodeLabel, ArcLabel & arcLabel) {
    20942103
     
    21092118        Map <Graph::Node *, size_t> map;
    21102119
    2111         ofstream file(fileName.c_str());
    2112 
    2113         file << "/*\n" << COPYRIGHT << "\n*/\n\n";
    2114 
    2115         file << "/*\n"
    2116              << "  Archivo generado automáticamente por generateDot en"
    2117              << " ioManager.H\n\n"
    2118              << "  Autor: Alejandro J. Mujica\n"
    2119              << "*/\n\n";
    2120 
    2121         file << "digraph\n"
    2122              << "{\n"
    2123              << "  rankdir = LR;\n\n";
     2120        output << "/*\n" << COPYRIGHT << "\n*/\n\n";
     2121
     2122        output << "/*\n"
     2123               << "  Archivo generado automáticamente por generateDot en"
     2124               << " ioManager.H\n\n"
     2125               << "  Autor: Alejandro J. Mujica\n"
     2126               << "*/\n\n";
     2127
     2128        output << "digraph\n"
     2129               << "{\n"
     2130               << "  rankdir = LR;\n\n";
    21242131
    21252132        // Escribir nodos clusterizados por planta.
    2126         clustersByPlant.for_each(ClusterMapOp<NodeLabel>(file, map, nodeLabel));
     2133        clustersByPlant.for_each(ClusterMapOp<NodeLabel>(output, map,
     2134                                                         nodeLabel, writeHref));
    21272135
    21282136        size_t i = map.size();
     
    21372145            Good * good = p->get_info().get();
    21382146
    2139             file << "  " << i << "[label = \"Importar\\n" << good->getName()
    2140                  << "\\n" << numtostr(good->getQuantity()) << " "
    2141                  << good->getMeasurementUnit()
    2142                  << "\" style = filled color = pink shape = box]\n";
     2147            output << "  " << i << "[label = \"Importar\\n" << good->getName()
     2148                   << "\\n" << numtostr(good->getQuantity()) << " "
     2149                   << good->getMeasurementUnit()
     2150                   << "\" style = filled color = pink shape = box]\n";
    21432151
    21442152            map.insert(p, i);
     
    21612169            std::string label = arcLabel(g, a);
    21622170
    2163             std::string arc_color =
     2171            std::string arc_color = 
    21642172                dotColor(gsl_rng_uniform_int(rng, DotColor::NUM_COLORS));
    21652173
    2166             file << "  " << si << "->" << ti
    2167                  << "[label = \"" << label << "\" color = "
    2168                  << arc_color << " fontcolor = " << arc_color           ;
     2174            output << "  " << si << "->" << ti
     2175                   << "[label = \"" << label << "\" color = "
     2176                   << arc_color << " fontcolor = " << arc_color;
    21692177
    21702178            if (a->get_info().isMin)
    2171                 file << " style = dashed";
    2172 
    2173             file << "];\n";
    2174         }
    2175 
    2176         file << "}";
    2177 
    2178         file.close();
     2179                output << " style = dashed";
     2180
     2181            output << "];\n";
     2182        }
     2183
     2184        output << "}";
    21792185
    21802186        gsl_rng_free(rng);
     
    21832189    /// @override
    21842190    template <class NodeLabel = NodeLabel, class ArcLabel = ArcLabel>
    2185     void generateDot(Graph & g, const std::string & fileName,
     2191    void generateDot(Graph & g, std::ostream & output, bool writeHref = false,
    21862192                     NodeLabel && nodeLabel = NodeLabel(),
    21872193                     ArcLabel && arcLabel = ArcLabel()) {
    21882194
    2189         generateDot<NodeLabel, ArcLabel>(g, fileName, nodeLabel, arcLabel);
     2195        generateDot<NodeLabel, ArcLabel>(g, output, writeHref, nodeLabel,
     2196                                         arcLabel);
     2197    }
     2198
     2199    template <class NodeLabel = NodeLabel, class ArcLabel = ArcLabel>
     2200    void generateDot(Graph & g, const std::string & fileName, bool writeHref,
     2201                     NodeLabel & nodeLabel, ArcLabel & arcLabel) {
     2202
     2203        std::ofstream file(fileName.c_str());
     2204
     2205        generateDot<NodeLabel, ArcLabel>(g, file, writeHref, nodeLabel,
     2206                                         arcLabel);
     2207
     2208        file.close();
     2209    }
     2210
     2211    template <class NodeLabel = NodeLabel, class ArcLabel = ArcLabel>
     2212    void generateDot(Graph & g, const std::string & fileName,
     2213                     bool writeHref = false,
     2214                     NodeLabel && nodeLabel = NodeLabel(),
     2215                     ArcLabel && arcLabel = ArcLabel()) {
     2216
     2217        generateDot<NodeLabel, ArcLabel>(g, fileName, writeHref, nodeLabel,
     2218                                         arcLabel);
    21902219    }
    21912220};
Note: See TracChangeset for help on using the changeset viewer.