2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file src/regex/regex_graph.c
22 * @brief functions for creating .dot graphs from regexes
23 * @author Maximilian Szengel
26 #include "gnunet_regex_lib.h"
27 #include "regex_internal.h"
30 * Context for graph creation. Passed as the cls to
31 * GNUNET_REGEX_automaton_save_graph_step.
33 struct GNUNET_REGEX_Graph_Context
36 * File pointer to the dot file used for output.
41 * Verbose flag, if it's set to GNUNET_YES additional info will be printed in
47 * Coloring flag, if set to GNUNET_YES SCCs will be colored.
54 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
55 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
56 * SCCs inside an automaton.
58 * @param scc_counter counter for numbering the sccs
59 * @param v start vertex
60 * @param index current index
61 * @param stack stack for saving all SCCs
62 * @param stack_size current size of the stack
65 scc_tarjan_strongconnect (unsigned int *scc_counter,
66 struct GNUNET_REGEX_State *v, unsigned int *index,
67 struct GNUNET_REGEX_State **stack,
68 unsigned int *stack_size)
70 struct GNUNET_REGEX_State *w;
71 struct GNUNET_REGEX_Transition *t;
76 stack[(*stack_size)++] = v;
79 for (t = v->transitions_head; NULL != t; t = t->next)
88 scc_tarjan_strongconnect (scc_counter, w, index, stack, stack_size);
89 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
91 else if (0 != w->contained)
92 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
95 if (v->lowlink == v->index)
97 w = stack[--(*stack_size)];
105 w->scc_id = *scc_counter;
106 w = stack[--(*stack_size)];
109 w->scc_id = *scc_counter;
116 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
117 * SCCs will be marked using the scc_id on each state.
119 * @param a the automaton for which SCCs should be computed and assigned.
122 scc_tarjan (struct GNUNET_REGEX_Automaton *a)
125 unsigned int scc_counter;
126 struct GNUNET_REGEX_State *v;
127 struct GNUNET_REGEX_State *stack[a->state_count];
128 unsigned int stack_size;
130 for (v = a->states_head; NULL != v; v = v->next)
141 for (v = a->states_head; NULL != v; v = v->next)
144 scc_tarjan_strongconnect (&scc_counter, v, &index, stack, &stack_size);
150 * Save a state to an open file pointer. cls is expected to be a file pointer to
151 * an open file. Used only in conjunction with
152 * GNUNET_REGEX_automaton_save_graph.
154 * @param cls file pointer.
155 * @param count current count of the state, not used.
159 GNUNET_REGEX_automaton_save_graph_step (void *cls, unsigned int count,
160 struct GNUNET_REGEX_State *s)
162 struct GNUNET_REGEX_Graph_Context *ctx = cls;
163 struct GNUNET_REGEX_Transition *ctran;
169 if (GNUNET_YES == ctx->verbose)
170 GNUNET_asprintf (&name, "%i (%s) (%s) (%s)", s->dfs_id, s->name, s->proof,
171 GNUNET_h2s (&s->hash));
173 GNUNET_asprintf (&name, "%i", s->dfs_id);
177 if (GNUNET_YES == ctx->coloring)
179 GNUNET_asprintf (&s_acc,
180 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
185 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", name,
189 else if (GNUNET_YES == ctx->coloring)
191 GNUNET_asprintf (&s_acc,
192 "\"%s\" [shape=circle, color=\"0.%i 0.8 0.95\"];\n", name,
197 GNUNET_asprintf (&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id);
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
205 fwrite (s_acc, strlen (s_acc), 1, ctx->filep);
209 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
211 if (NULL == ctran->to_state)
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 "Transition from State %i has no state for transitioning\n",
219 if (GNUNET_YES == ctx->verbose)
221 GNUNET_asprintf (&to_name, "%i (%s) (%s) (%s)", ctran->to_state->dfs_id,
222 ctran->to_state->name, ctran->to_state->proof,
223 GNUNET_h2s (&ctran->to_state->hash));
226 GNUNET_asprintf (&to_name, "%i", ctran->to_state->dfs_id);
228 if (NULL == ctran->label)
230 if (GNUNET_YES == ctx->coloring)
232 GNUNET_asprintf (&s_tran,
233 "\"%s\" -> \"%s\" [label = \"ε\", color=\"0.%i 0.8 0.95\"];\n",
234 name, to_name, s->scc_id);
238 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"ε\"];\n", name,
244 if (GNUNET_YES == ctx->coloring)
246 GNUNET_asprintf (&s_tran,
247 "\"%s\" -> \"%s\" [label = \"%s\", color=\"0.%i 0.8 0.95\"];\n",
248 name, to_name, ctran->label, s->scc_id);
252 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%s\"];\n", name,
253 to_name, ctran->label, s->scc_id);
257 GNUNET_free (to_name);
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
266 fwrite (s_tran, strlen (s_tran), 1, ctx->filep);
267 GNUNET_free (s_tran);
276 * Save the given automaton as a GraphViz dot file.
278 * @param a the automaton to be saved.
279 * @param filename where to save the file.
280 * @param options options for graph generation that include coloring or verbose
284 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
285 const char *filename,
286 enum GNUNET_REGEX_GraphSavingOptions options)
290 struct GNUNET_REGEX_Graph_Context ctx;
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
298 if (NULL == filename || strlen (filename) < 1)
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
304 ctx.filep = fopen (filename, "w");
306 (0 == (options & GNUNET_REGEX_GRAPH_VERBOSE)) ? GNUNET_NO : GNUNET_YES;
308 (0 == (options & GNUNET_REGEX_GRAPH_COLORING)) ? GNUNET_NO : GNUNET_YES;
310 if (NULL == ctx.filep)
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
317 /* First add the SCCs to the automaton, so we can color them nicely */
318 if (GNUNET_YES == ctx.coloring)
321 start = "digraph G {\nrankdir=LR\n";
322 fwrite (start, strlen (start), 1, ctx.filep);
324 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL,
325 &GNUNET_REGEX_automaton_save_graph_step,
329 fwrite (end, strlen (end), 1, ctx.filep);