PGROUTING  3.2
turnRestrictedPath.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: turnRestrictedPath.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 vicky Vergara
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 
32 #include <stdbool.h>
34 
35 
36 #include "c_common/debug_macro.h"
37 #include "c_common/e_report.h"
38 #include "c_common/time_msg.h"
39 
40 #include "c_common/edges_input.h"
42 
44 
45 PGDLLEXPORT Datum _pgr_turnrestrictedpath(PG_FUNCTION_ARGS);
47 
48 
49 static
50 void
52  char* edges_sql,
53  char *restrictions_sql,
54  int64_t start_vid,
55  int64_t end_vid,
56 
57  int p_k,
58  bool directed,
59  bool heap_paths,
60  bool stop_on_first,
61  bool strict,
62 
64  size_t *result_count) {
65  (*path) = NULL;
66  (*result_count) = 0;
67 
68  if (p_k < 0) {
69  return;
70  }
71 
72  size_t k = (size_t)p_k;
73 
74  if (start_vid == end_vid) {
75  PGR_DBG("Source and target are the same");
76  return;
77  }
78 
80 
81 
82  pgr_edge_t *edges = NULL;
83  size_t total_edges = 0;
84 
85 
86  pgr_get_edges(edges_sql, &edges, &total_edges);
87 
88  Restriction_t *restrictions = NULL;
89  size_t total_restrictions = 0;
90 
91  pgr_get_restrictions(restrictions_sql, &restrictions,
92  &total_restrictions);
93 
94  if (total_edges == 0) {
95  PGR_DBG("No edges found");
97  return;
98  }
99 
100  clock_t start_t = clock();
101  char *log_msg = NULL;
102  char *notice_msg = NULL;
103  char *err_msg = NULL;
105  edges,
106  total_edges,
107  restrictions,
108  total_restrictions,
109  start_vid,
110  end_vid,
111  k,
112  directed,
113  heap_paths,
114  stop_on_first,
115  strict,
116 
117  path,
118  result_count,
119  &log_msg,
120  &notice_msg,
121  &err_msg);
122 
123  time_msg(" processing pgr_turnRestrictedPath", start_t, clock());
124 
125  if (err_msg) {
126  if (*path) pfree(*path);
127  }
128  pgr_global_report(log_msg, notice_msg, err_msg);
129 
130  if (edges) {pfree(edges); edges = NULL;}
131  if (log_msg) {pfree(log_msg); log_msg = NULL;}
132  if (notice_msg) {pfree(notice_msg); notice_msg = NULL;}
133  if (err_msg) {pfree(err_msg); err_msg = NULL;}
134  if (restrictions) {pfree(restrictions); edges = NULL;}
135 
136  pgr_SPI_finish();
137 }
138 
139 PGDLLEXPORT Datum
140 _pgr_turnrestrictedpath(PG_FUNCTION_ARGS) {
141  FuncCallContext *funcctx;
142  TupleDesc tuple_desc;
143 
144  General_path_element_t *path = NULL;
145  size_t result_count = 0;
146 
147  if (SRF_IS_FIRSTCALL()) {
148  MemoryContext oldcontext;
149  funcctx = SRF_FIRSTCALL_INIT();
150  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
151 
152 
153  /**********************************************************************/
154  /*
155  TEXT, -- edges_sql
156  TEXT, -- restrictions_sql
157  BIGINT, -- start_vertex
158  BIGINT, -- end_vertex
159  INTEGER,-- K cycles
160  directed BOOLEAN DEFAULT true,
161  heap_paths BOOLEAN DEFAULT false,
162  stop_on_first BOOLEAN DEFAULT true,
163 
164  OUT seq INTEGER,
165  OUT path_seq INTEGER,
166  OUT node BIGINT,
167  OUT edge BIGINT,
168  OUT cost FLOAT,
169  OUT agg_cost FLOAT)
170  **********************************************************************/
171 
172 
173  PGR_DBG("Calling process");
174  process(
175  text_to_cstring(PG_GETARG_TEXT_P(0)),
176  text_to_cstring(PG_GETARG_TEXT_P(1)),
177  PG_GETARG_INT64(2),
178  PG_GETARG_INT64(3),
179  PG_GETARG_INT32(4),
180  PG_GETARG_BOOL(5),
181  PG_GETARG_BOOL(6),
182  PG_GETARG_BOOL(7),
183  PG_GETARG_BOOL(8),
184  &path,
185  &result_count);
186 
187 #if PGSQL_VERSION > 95
188  funcctx->max_calls = result_count;
189 #else
190  funcctx->max_calls = (uint32_t)result_count;
191 #endif
192  funcctx->user_fctx = path;
193  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
194  != TYPEFUNC_COMPOSITE) {
195  ereport(ERROR,
196  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
197  errmsg("function returning record called in context "
198  "that cannot accept type record")));
199  }
200 
201  funcctx->tuple_desc = tuple_desc;
202  MemoryContextSwitchTo(oldcontext);
203  }
204 
205  funcctx = SRF_PERCALL_SETUP();
206  tuple_desc = funcctx->tuple_desc;
207  path = (General_path_element_t*) funcctx->user_fctx;
208 
209  if (funcctx->call_cntr < funcctx->max_calls) {
210  HeapTuple tuple;
211  Datum result;
212  Datum *values;
213  bool* nulls;
214 
215  /**********************************************************************/
216  /* MODIFY AS NEEDED */
217  /*
218  OUT seq INTEGER,
219  OUT path_seq INTEGER,
220  OUT node BIGINT,
221  OUT edge BIGINT,
222  OUT cost FLOAT,
223  OUT agg_cost FLOAT
224  ***********************************************************************/
225 
226  size_t v_count = 7;
227 
228  values = palloc(v_count * sizeof(Datum));
229  nulls = palloc(v_count * sizeof(bool));
230 
231 
232  size_t i;
233  for (i = 0; i < v_count; ++i) {
234  nulls[i] = false;
235  }
236 
237  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
238  values[1] = Int32GetDatum(path[funcctx->call_cntr].start_id + 1);
239  values[2] = Int32GetDatum(path[funcctx->call_cntr].seq);
240  values[3] = Int64GetDatum(path[funcctx->call_cntr].node);
241  values[4] = Int64GetDatum(path[funcctx->call_cntr].edge);
242  values[5] = Float8GetDatum(path[funcctx->call_cntr].cost);
243  values[6] = Float8GetDatum(path[funcctx->call_cntr].agg_cost);
244 
245 
246  tuple = heap_form_tuple(tuple_desc, values, nulls);
247  result = HeapTupleGetDatum(tuple);
248  SRF_RETURN_NEXT(funcctx, result);
249  } else {
250  SRF_RETURN_DONE(funcctx);
251  }
252 }
turnRestrictedPath_driver.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
do_pgr_turnRestrictedPath
void do_pgr_turnRestrictedPath(pgr_edge_t *data_edges, size_t total_edges, Restriction_t *restrictions, size_t total_restrictions, int64_t start_vid, int64_t end_vid, size_t k, bool directed, bool heap_paths, bool stop_on_first, bool strict, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: turnRestrictedPath_driver.cpp:77
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_get_restrictions
void pgr_get_restrictions(char *restrictions_sql, Restriction_t **restrictions, size_t *total_restrictions)
Definition: restrictions_input.c:63
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
debug_macro.h
_pgr_turnrestrictedpath
PGDLLEXPORT Datum _pgr_turnrestrictedpath(PG_FUNCTION_ARGS)
Definition: turnRestrictedPath.c:140
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
restrictions_input.h
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
process
static void process(char *edges_sql, char *restrictions_sql, int64_t start_vid, int64_t end_vid, int p_k, bool directed, bool heap_paths, bool stop_on_first, bool strict, General_path_element_t **path, size_t *result_count)
Definition: turnRestrictedPath.c:51
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
Restriction_t
Definition: restriction_t.h:37
edges_input.h
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_turnrestrictedpath)