PGROUTING  3.2
bdAstar.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdAstar.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail:
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 #include <stdbool.h>
32 #include "utils/array.h"
33 
34 #include "c_common/debug_macro.h"
35 #include "c_common/e_report.h"
36 #include "c_common/time_msg.h"
37 
38 #include "c_common/edges_input.h"
39 #include "c_common/arrays_input.h"
42 
43 
44 
47 
48 PGDLLEXPORT Datum _pgr_bdastar(PG_FUNCTION_ARGS);
50 
51 
52 static
53 void
54 process(char* edges_sql,
55  char* combinations_sql,
56  ArrayType *starts,
57  ArrayType *ends,
58  bool directed,
59  int heuristic,
60  double factor,
61  double epsilon,
62  bool only_cost,
63  General_path_element_t **result_tuples,
64  size_t *result_count) {
65  check_parameters(heuristic, factor, epsilon);
66 
68 
69  int64_t* start_vidsArr = NULL;
70  size_t size_start_vidsArr = 0;
71 
72  int64_t* end_vidsArr = NULL;
73  size_t size_end_vidsArr = 0;
74 
75  pgr_combination_t *combinations = NULL;
76  size_t total_combinations = 0;
77 
78  if (starts && ends) {
79  start_vidsArr = (int64_t*)
80  pgr_get_bigIntArray(&size_start_vidsArr, starts);
81  end_vidsArr = (int64_t*)
82  pgr_get_bigIntArray(&size_end_vidsArr, ends);
83  } else if (combinations_sql) {
84  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
85  }
86 
87  Pgr_edge_xy_t *edges = NULL;
88  size_t total_edges = 0;
89 
90  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
91  PGR_DBG("Total %ld edges in query:", total_edges);
92 
93  if (total_edges == 0) {
94  PGR_DBG("No edges found");
95  (*result_count) = 0;
96  (*result_tuples) = NULL;
98  return;
99  }
100 
101  PGR_DBG("Starting processing");
102  char* log_msg = NULL;
103  char* notice_msg = NULL;
104  char* err_msg = NULL;
105  clock_t start_t = clock();
107  edges, total_edges,
108  combinations, total_combinations,
109  start_vidsArr, size_start_vidsArr,
110  end_vidsArr, size_end_vidsArr,
111 
112  directed,
113  heuristic,
114  factor,
115  epsilon,
116  only_cost,
117 
118  result_tuples,
119  result_count,
120  &log_msg,
121  &notice_msg,
122  &err_msg);
123 
124  if (only_cost) {
125  time_msg("pgr_bdAstarCost()", start_t, clock());
126  } else {
127  time_msg("pgr_bdAstar()", start_t, clock());
128  }
129 
130  if (err_msg && (*result_tuples)) {
131  pfree(*result_tuples);
132  (*result_count) = 0;
133  (*result_tuples) = NULL;
134  }
135 
136  pgr_global_report(log_msg, notice_msg, err_msg);
137 
138  if (log_msg) pfree(log_msg);
139  if (notice_msg) pfree(notice_msg);
140  if (err_msg) pfree(err_msg);
141  if (edges) pfree(edges);
142 
143  pgr_SPI_finish();
144 }
145 
146 PGDLLEXPORT Datum
147 _pgr_bdastar(PG_FUNCTION_ARGS) {
148  FuncCallContext *funcctx;
149  TupleDesc tuple_desc;
150 
151  General_path_element_t *result_tuples = 0;
152  size_t result_count = 0;
153 
154  if (SRF_IS_FIRSTCALL()) {
155  MemoryContext oldcontext;
156  funcctx = SRF_FIRSTCALL_INIT();
157  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
158 
159 
160  if (PG_NARGS() == 8) {
161  /*
162  * many to many
163  */
164  process(
165  text_to_cstring(PG_GETARG_TEXT_P(0)),
166  NULL,
167  PG_GETARG_ARRAYTYPE_P(1),
168  PG_GETARG_ARRAYTYPE_P(2),
169 
170  PG_GETARG_BOOL(3),
171  PG_GETARG_INT32(4),
172  PG_GETARG_FLOAT8(5),
173  PG_GETARG_FLOAT8(6),
174  PG_GETARG_BOOL(7),
175  &result_tuples,
176  &result_count);
177 
178  } else if (PG_NARGS() == 7) {
179  /*
180  * combinations
181  */
182  process(
183  text_to_cstring(PG_GETARG_TEXT_P(0)),
184  text_to_cstring(PG_GETARG_TEXT_P(1)),
185  NULL,
186  NULL,
187 
188  PG_GETARG_BOOL(2),
189  PG_GETARG_INT32(3),
190  PG_GETARG_FLOAT8(4),
191  PG_GETARG_FLOAT8(5),
192  PG_GETARG_BOOL(6),
193  &result_tuples,
194  &result_count);
195  }
196 
197 
198 
199 #if PGSQL_VERSION > 95
200  funcctx->max_calls = result_count;
201 #else
202  funcctx->max_calls = (uint32_t)result_count;
203 #endif
204  funcctx->user_fctx = result_tuples;
205  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
206  != TYPEFUNC_COMPOSITE)
207  ereport(ERROR,
208  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
209  errmsg("function returning record called in context "
210  "that cannot accept type record")));
211 
212  funcctx->tuple_desc = tuple_desc;
213  MemoryContextSwitchTo(oldcontext);
214  }
215 
216  funcctx = SRF_PERCALL_SETUP();
217  tuple_desc = funcctx->tuple_desc;
218  result_tuples = (General_path_element_t*) funcctx->user_fctx;
219 
220  if (funcctx->call_cntr < funcctx->max_calls) {
221  HeapTuple tuple;
222  Datum result;
223  Datum *values;
224  bool* nulls;
225  size_t call_cntr = funcctx->call_cntr;
226 
227 
228  /**********************************************************************
229  OUT seq INTEGER,
230  OUT path_seq INTEGER,
231  OUT node BIGINT,
232  OUT edge BIGINT,
233  OUT cost FLOAT,
234  OUT agg_cost FLOAT
235  *********************************************************************/
236 
237  size_t numb = 8;
238  values = palloc(numb * sizeof(Datum));
239  nulls = palloc(numb * sizeof(bool));
240 
241 
242  size_t i;
243  for (i = 0; i < numb; ++i) {
244  nulls[i] = false;
245  }
246 
247  values[0] = Int32GetDatum(call_cntr + 1);
248  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
249  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
250  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
251  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
252  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
253  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
254  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
255 
256 
257  tuple = heap_form_tuple(tuple_desc, values, nulls);
258  result = HeapTupleGetDatum(tuple);
259  SRF_RETURN_NEXT(funcctx, result);
260  } else {
261  SRF_RETURN_DONE(funcctx);
262  }
263 }
264 
combinations_input.h
check_parameters
void check_parameters(int heuristic, double factor, double epsilon)
Definition: check_parameters.c:34
do_pgr_bdAstar
void do_pgr_bdAstar(Pgr_edge_xy_t *edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: bdAstar_driver.cpp:141
bdAstar_driver.h
time_msg.h
postgres_connection.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_bdastar)
pgr_get_edges_xy
void pgr_get_edges_xy(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
Edges with x, y vertices values.
Definition: edges_input.c:744
process
static void process(char *edges_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
Definition: bdAstar.c:54
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
edge
Definition: trsp.h:41
debug_macro.h
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
_pgr_bdastar
PGDLLEXPORT Datum _pgr_bdastar(PG_FUNCTION_ARGS)
Definition: bdAstar.c:147
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
astar_driver.h
check_parameters.h
edges_input.h
Pgr_edge_xy_t
Definition: pgr_edge_xy_t.h:38
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93