PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bdDijkstra_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdDijkstra_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Celia Virginia Vergara Castillo
10 Mail: vicky_vergara@hotmail.com
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
30 
31 #include <sstream>
32 #include <deque>
33 #include <vector>
34 
35 #include "./pgr_bdDijkstra.hpp"
36 #include "./bdDijkstra_driver.h"
37 
38 
39 #include "./../../common/src/pgr_alloc.hpp"
40 #include "./../../common/src/pgr_assert.h"
41 #include "./../../common/src/pgr_types.h"
42 
43 #include "./../../common/src/pgr_base_graph.hpp"
44 
45 
46 
47 
48 /************************************************************
49  edges_sql TEXT,
50  start_vid BIGINT,
51  end_vid BIGINT,
52  directed BOOLEAN DEFAULT true,
53  only_cost BOOLEAN DEFAULT false,
54  ***********************************************************/
55 
56 template < class G >
57 static
58 Path
60  G &graph,
61  int64_t source,
62  int64_t target,
63  std::ostream &log,
64  bool only_cost = false) {
65  log << "entering static function\n";
66  Pgr_bdDijkstra<G> fn_bdDijkstra(graph);
67 
68  auto path = fn_bdDijkstra.pgr_bdDijkstra(
69  graph.get_V(source), graph.get_V(target), only_cost);
70  log << fn_bdDijkstra.log();
71 
72  return path;
73 }
74 
75 
76 void
78  pgr_edge_t *data_edges,
79  size_t total_edges,
80  int64_t start_vid,
81  int64_t end_vid,
82  bool directed,
83  bool only_cost,
84  General_path_element_t **return_tuples,
85  size_t *return_count,
86  char ** log_msg,
87  char ** notice_msg,
88  char ** err_msg) {
89  std::ostringstream log;
90  std::ostringstream err;
91  std::ostringstream notice;
92  try {
93  pgassert(!(*log_msg));
94  pgassert(!(*notice_msg));
95  pgassert(!(*err_msg));
96  pgassert(!(*return_tuples));
97  pgassert(*return_count == 0);
98  pgassert(total_edges != 0);
99 
100  graphType gType = directed? DIRECTED: UNDIRECTED;
101 
102  Path path;
103 
104  log << "starting process\n";
105  if (directed) {
106  log << "Working with directed Graph\n";
107  pgrouting::DirectedGraph digraph(gType);
108  digraph.insert_edges(data_edges, total_edges);
109  path = pgr_bdDijkstra(digraph,
110  start_vid,
111  end_vid,
112  log,
113  only_cost);
114  } else {
115  log << "Working with Undirected Graph\n";
116  pgrouting::UndirectedGraph undigraph(gType);
117  undigraph.insert_edges(data_edges, total_edges);
118  path = pgr_bdDijkstra(
119  undigraph,
120  start_vid,
121  end_vid,
122  log,
123  only_cost);
124  }
125 
126  auto count = path.size();
127 
128  if (count == 0) {
129  (*return_tuples) = NULL;
130  (*return_count) = 0;
131  notice <<
132  "No paths found between start_vid and end_vid vertices";
133  } else {
134  (*return_tuples) = pgr_alloc(count, (*return_tuples));
135  size_t sequence = 0;
136  path.generate_postgres_data(return_tuples, sequence);
137  (*return_count) = sequence;
138  }
139 
140  pgassert(*err_msg == NULL);
141  *log_msg = log.str().empty()?
142  nullptr :
143  strdup(log.str().c_str());
144  *notice_msg = notice.str().empty()?
145  nullptr :
146  strdup(notice.str().c_str());
147  } catch (AssertFailedException &except) {
148  if (*return_tuples) free(*return_tuples);
149  (*return_count) = 0;
150  err << except.what();
151  *err_msg = strdup(err.str().c_str());
152  *log_msg = strdup(log.str().c_str());
153  } catch (std::exception& except) {
154  if (*return_tuples) free(*return_tuples);
155  (*return_count) = 0;
156  err << except.what();
157  *err_msg = strdup(err.str().c_str());
158  *log_msg = strdup(log.str().c_str());
159  } catch(...) {
160  if (*return_tuples) free(*return_tuples);
161  (*return_count) = 0;
162  err << "Caught unknown exception!";
163  *err_msg = strdup(err.str().c_str());
164  *log_msg = strdup(log.str().c_str());
165  }
166 }
void do_pgr_bdDijkstra(pgr_edge_t *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
std::string log() const
static Path pgr_bdDijkstra(G &graph, int64_t source, int64_t target, std::ostream &log, bool only_cost=false)
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
void generate_postgres_data(General_path_element_t **postgres_data, size_t &sequence) const
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
graphType
Definition: pgr_types.h:222
Path pgr_bdDijkstra(V start_vertex, V end_vertex, bool only_cost)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:62
path_element_t * path
Definition: BDATester.cpp:49
virtual const char * what() const
Definition: pgr_assert.cpp:53
char * err_msg
Definition: BDATester.cpp:50
size_t size() const