PGROUTING  3.2
astar.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne.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 #include "c_common/edges_input.h"
38 #include "c_common/arrays_input.h"
41 
43 
44 PGDLLEXPORT Datum _pgr_astar(PG_FUNCTION_ARGS);
46 
47 #if 0
48 void
50  int heuristic,
51  double factor,
52  double epsilon) {
53  if (heuristic > 5 || heuristic < 0) {
54  ereport(ERROR,
55  (errmsg("Unknown heuristic"),
56  errhint("Valid values: 0~5")));
57  }
58  if (factor <= 0) {
59  ereport(ERROR,
60  (errmsg("Factor value out of range"),
61  errhint("Valid values: positive non zero")));
62  }
63  if (epsilon < 1) {
64  ereport(ERROR,
65  (errmsg("Epsilon value out of range"),
66  errhint("Valid values: 1 or greater than 1")));
67  }
68 }
69 #endif
70 
71 static
72 void
73 process(char* edges_sql,
74  char* combinations_sql,
75  ArrayType *starts,
76  ArrayType *ends,
77  bool directed,
78  int heuristic,
79  double factor,
80  double epsilon,
81  bool only_cost,
82  bool normal,
83  General_path_element_t **result_tuples,
84  size_t *result_count) {
85  check_parameters(heuristic, factor, epsilon);
86 
88 
89  int64_t* start_vidsArr = NULL;
90  size_t size_start_vidsArr = 0;
91 
92  int64_t* end_vidsArr = NULL;
93  size_t size_end_vidsArr = 0;
94 
95  Pgr_edge_xy_t *edges = NULL;
96  size_t total_edges = 0;
97 
98  pgr_combination_t *combinations = NULL;
99  size_t total_combinations = 0;
100 
101  if (normal) {
102  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
103  if (starts && ends) {
104  start_vidsArr = (int64_t*)
105  pgr_get_bigIntArray(&size_start_vidsArr, starts);
106  end_vidsArr = (int64_t*)
107  pgr_get_bigIntArray(&size_end_vidsArr, ends);
108  } else if (combinations_sql) {
109  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
110  }
111  } else {
112  pgr_get_edges_xy_reversed(edges_sql, &edges, &total_edges);
113  end_vidsArr = (int64_t*)
114  pgr_get_bigIntArray(&size_end_vidsArr, starts);
115  start_vidsArr = (int64_t*)
116  pgr_get_bigIntArray(&size_start_vidsArr, ends);
117  }
118 
119  if (total_edges == 0) {
120  PGR_DBG("No edges found");
121  (*result_count) = 0;
122  (*result_tuples) = NULL;
123  pgr_SPI_finish();
124  return;
125  }
126 
127  PGR_DBG("Starting processing");
128  char *log_msg = NULL;
129  char *notice_msg = NULL;
130  char *err_msg = NULL;
131  clock_t start_t = clock();
133  edges, total_edges,
134 
135  combinations, total_combinations,
136 
137  start_vidsArr, size_start_vidsArr,
138  end_vidsArr, size_end_vidsArr,
139  directed,
140  heuristic,
141  factor,
142  epsilon,
143  only_cost,
144  normal,
145  result_tuples, result_count,
146  &log_msg,
147  &notice_msg,
148  &err_msg);
149 
150  if (only_cost) {
151  time_msg("processing pgr_astarCost", start_t, clock());
152  } else {
153  time_msg("processing pgr_astar", start_t, clock());
154  }
155 
156 
157  if (err_msg && (*result_tuples)) {
158  pfree(*result_tuples);
159  (*result_tuples) = NULL;
160  (*result_count) = 0;
161  }
162 
163  pgr_global_report(log_msg, notice_msg, err_msg);
164 
165  if (log_msg) pfree(log_msg);
166  if (notice_msg) pfree(notice_msg);
167  if (err_msg) pfree(err_msg);
168  if (edges) pfree(edges);
169  if (start_vidsArr) pfree(start_vidsArr);
170  if (end_vidsArr) pfree(end_vidsArr);
171 
172  pgr_SPI_finish();
173 }
174 
175 PGDLLEXPORT Datum
176 _pgr_astar(PG_FUNCTION_ARGS) {
177  FuncCallContext *funcctx;
178  TupleDesc tuple_desc;
179 
180  /**********************************************************************/
181  General_path_element_t *result_tuples = NULL;
182  size_t result_count = 0;
183  /**********************************************************************/
184 
185  if (SRF_IS_FIRSTCALL()) {
186  MemoryContext oldcontext;
187  funcctx = SRF_FIRSTCALL_INIT();
188  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
189 
190 
191  if (PG_NARGS() == 9) {
192  /*
193  * many to many
194  */
195 
196  process(
197  text_to_cstring(PG_GETARG_TEXT_P(0)),
198  NULL,
199  PG_GETARG_ARRAYTYPE_P(1),
200  PG_GETARG_ARRAYTYPE_P(2),
201  PG_GETARG_BOOL(3),
202  PG_GETARG_INT32(4),
203  PG_GETARG_FLOAT8(5),
204  PG_GETARG_FLOAT8(6),
205  PG_GETARG_BOOL(7),
206  PG_GETARG_BOOL(8),
207  &result_tuples,
208  &result_count);
209 
210  } else if (PG_NARGS() == 7) {
211  /*
212  * Combinations
213  */
214 
215  process(
216  text_to_cstring(PG_GETARG_TEXT_P(0)),
217  text_to_cstring(PG_GETARG_TEXT_P(1)),
218  NULL,
219  NULL,
220  PG_GETARG_BOOL(2),
221  PG_GETARG_INT32(3),
222  PG_GETARG_FLOAT8(4),
223  PG_GETARG_FLOAT8(5),
224  PG_GETARG_BOOL(6),
225  true,
226  &result_tuples,
227  &result_count);
228  }
229 
230 
231 #if PGSQL_VERSION > 95
232  funcctx->max_calls = result_count;
233 #else
234  funcctx->max_calls = (uint32_t)result_count;
235 #endif
236  funcctx->user_fctx = result_tuples;
237  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
238  != TYPEFUNC_COMPOSITE)
239  ereport(ERROR,
240  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
241  errmsg("function returning record called in context "
242  "that cannot accept type record")));
243 
244  funcctx->tuple_desc = tuple_desc;
245  MemoryContextSwitchTo(oldcontext);
246  }
247 
248  funcctx = SRF_PERCALL_SETUP();
249  tuple_desc = funcctx->tuple_desc;
250  result_tuples = (General_path_element_t*) funcctx->user_fctx;
251 
252  if (funcctx->call_cntr < funcctx->max_calls) {
253  HeapTuple tuple;
254  Datum result;
255  Datum *values;
256  bool* nulls;
257 
258  /*********************************************************************
259  OUT seq INTEGER,
260  OUT path_seq INTEGER,
261  OUT start_vid BIGINT,
262  OUT end_vid BIGINT,
263  OUT node BIGINT,
264  OUT edge BIGINT,
265  OUT cost FLOAT,
266  OUT agg_cost FLOAT
267  **********************************************************************/
268 
269 
270  size_t numb = 8;
271  values = palloc(numb * sizeof(Datum));
272  nulls = palloc(numb * sizeof(bool));
273 
274  size_t i;
275  for (i = 0; i < numb; ++i) {
276  nulls[i] = false;
277  }
278 
279 
280  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
281  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
282  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
283  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
284  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
285  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
286  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
287  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
288 
289 
290  tuple = heap_form_tuple(tuple_desc, values, nulls);
291  result = HeapTupleGetDatum(tuple);
292  SRF_RETURN_NEXT(funcctx, result);
293  } else {
294  SRF_RETURN_DONE(funcctx);
295  }
296 }
combinations_input.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
check_parameters
void check_parameters(int heuristic, double factor, double epsilon)
Definition: check_parameters.c:34
time_msg.h
postgres_connection.h
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, bool normal, General_path_element_t **result_tuples, size_t *result_count)
Definition: astar.c:73
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_astar)
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
_pgr_astar
PGDLLEXPORT Datum _pgr_astar(PG_FUNCTION_ARGS)
Definition: astar.c:176
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_get_edges_xy_reversed
void pgr_get_edges_xy_reversed(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
for many to 1 on aStar
Definition: edges_input.c:757
do_pgr_astarManyToMany
void do_pgr_astarManyToMany(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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: astar_driver.cpp:86
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
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
General_path_element_t::end_id
int64_t end_id
Definition: general_path_element_t.h:40
astar_driver.h
check_parameters.h
edges_input.h
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
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
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44