PGROUTING  3.2
trsp_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: trsp_driver.cpp
4 
5 Copyright (c) 2017 pgRouting developers
7 
8 ------
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 
24  ********************************************************************PGR-GNU*/
25 
26 
28 #include <utility>
29 #include <vector>
30 #include <cstdint>
31 #include <sstream>
32 #include <algorithm>
33 #include "trsp/pgr_trspHandler.h"
34 #include "cpp_common/rule.h"
35 #include "cpp_common/pgr_assert.h"
36 #include "cpp_common/pgr_alloc.hpp"
37 
38 
39 
40 void
42  pgr_edge_t *edges,
43  size_t total_edges,
44 
45  Restriction_t *restrictions,
46  size_t restrictions_size,
47 
48  int64_t *start_vidsArr,
49  size_t size_start_vidsArr,
50 
51  int64_t *end_vidsArr,
52  size_t size_end_vidsArr,
53 
54  bool directed,
55 
56  General_path_element_t **return_tuples,
57  size_t *return_count,
58  char **log_msg,
59  char **notice_msg,
60  char **err_msg) {
61  std::ostringstream log;
62  std::ostringstream err;
63  std::ostringstream notice;
64  try {
65  pgassert(*return_tuples == NULL);
66  pgassert(*return_count == 0);
67  pgassert(*log_msg == NULL);
68  pgassert(*notice_msg == NULL);
69  pgassert(*err_msg == NULL);
70 
71  std::vector<pgrouting::trsp::Rule> ruleList;
72  for (size_t i = 0; i < restrictions_size; ++i) {
73  ruleList.push_back(pgrouting::trsp::Rule(*(restrictions + i)));
74  }
75 
76  /*
77  * Inserting vertices into a c++ vector structure
78  */
79  std::vector<int64_t>
80  sources(start_vidsArr, start_vidsArr + size_start_vidsArr);
81  std::vector< int64_t >
82  targets(end_vidsArr, end_vidsArr + size_end_vidsArr);
83 
84  /*
85  * ordering and removing duplicates
86  */
87  std::sort(sources.begin(), sources.end());
88  sources.erase(
89  std::unique(sources.begin(), sources.end()),
90  sources.end());
91 
92  std::sort(targets.begin(), targets.end());
93  targets.erase(
94  std::unique(targets.begin(), targets.end()),
95  targets.end());
96 
97 
98 
100  edges,
101  total_edges,
102  directed,
103  ruleList);
104 
105  auto paths = gdef.process(
106  sources,
107  targets);
108 
109 
110  size_t count(0);
111  count = count_tuples(paths);
112 
113  if (count == 0) {
114  (*return_tuples) = NULL;
115  (*return_count) = 0;
116  return;
117  }
118 
119  (*return_tuples) = pgr_alloc(count, (*return_tuples));
120  (*return_count) = collapse_paths(return_tuples, paths);
121 
122 
123  *log_msg = log.str().empty()?
124  *log_msg :
125  pgr_msg(log.str().c_str());
126  *notice_msg = notice.str().empty()?
127  *notice_msg :
128  pgr_msg(notice.str().c_str());
129 
130  return;
131  } catch (AssertFailedException &except) {
132  (*return_tuples) = pgr_free(*return_tuples);
133  (*return_count) = 0;
134  err << except.what();
135  *err_msg = pgr_msg(err.str().c_str());
136  *log_msg = pgr_msg(log.str().c_str());
137  } catch (std::exception &except) {
138  (*return_tuples) = pgr_free(*return_tuples);
139  (*return_count) = 0;
140  err << except.what();
141  *err_msg = pgr_msg(err.str().c_str());
142  *log_msg = pgr_msg(log.str().c_str());
143  } catch(...) {
144  (*return_tuples) = pgr_free(*return_tuples);
145  (*return_count) = 0;
146  err << "Caught unknown exception!";
147  *err_msg = pgr_msg(err.str().c_str());
148  *log_msg = pgr_msg(log.str().c_str());
149  }
150 }
151 
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgrouting::trsp::Pgr_trspHandler
Definition: pgr_trspHandler.h:52
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
pgr_edge_t
Definition: pgr_edge_t.h:37
trsp_driver.h
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
pgrouting::trsp::Pgr_trspHandler::process
Path process(const int64_t start_vertex, const int64_t end_vertex)
process
Definition: pgr_trspHandler.cpp:259
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
pgrouting::trsp::Rule
Definition: rule.h:39
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgr_trspHandler.h
pgr_alloc.hpp
do_trsp
void do_trsp(pgr_edge_t *edges, size_t total_edges, Restriction_t *restrictions, size_t restrictions_size, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: trsp_driver.cpp:41
rule.h
pgr_assert.h
An assert functionality that uses C++ throw().
General_path_element_t
Definition: general_path_element_t.h:37
Restriction_t
Definition: restriction_t.h:37
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139