2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file src/regex/regex_test_graph.c
22 * @brief functions for creating .dot graphs from regexes
23 * @author Maximilian Szengel
26 #include "regex_internal_lib.h"
27 #include "regex_test_lib.h"
28 #include "regex_internal.h"
31 * Context for graph creation. Passed as the cls to
32 * REGEX_TEST_automaton_save_graph_step.
34 struct REGEX_TEST_Graph_Context
37 * File pointer to the dot file used for output.
42 * Verbose flag, if it's set to GNUNET_YES additional info will be printed in
48 * Coloring flag, if set to GNUNET_YES SCCs will be colored.
55 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
56 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
57 * SCCs inside an automaton.
59 * @param scc_counter counter for numbering the sccs
60 * @param v start vertex
61 * @param index current index
62 * @param stack stack for saving all SCCs
63 * @param stack_size current size of the stack
66 scc_tarjan_strongconnect (unsigned int *scc_counter,
67 struct REGEX_INTERNAL_State *v, unsigned int *index,
68 struct REGEX_INTERNAL_State **stack,
69 unsigned int *stack_size)
71 struct REGEX_INTERNAL_State *w;
72 struct REGEX_INTERNAL_Transition *t;
77 stack[(*stack_size)++] = v;
80 for (t = v->transitions_head; NULL != t; t = t->next)
89 scc_tarjan_strongconnect (scc_counter, w, index, stack, stack_size);
90 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
92 else if (1 == w->contained)
93 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
96 if (v->lowlink == v->index)
101 w = stack[--(*stack_size)];
103 w->scc_id = *scc_counter;
111 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
112 * SCCs will be marked using the scc_id on each state.
114 * @param a the automaton for which SCCs should be computed and assigned.
117 scc_tarjan (struct REGEX_INTERNAL_Automaton *a)
120 unsigned int scc_counter;
121 struct REGEX_INTERNAL_State *v;
122 struct REGEX_INTERNAL_State *stack[a->state_count];
123 unsigned int stack_size;
125 for (v = a->states_head; NULL != v; v = v->next)
136 for (v = a->states_head; NULL != v; v = v->next)
139 scc_tarjan_strongconnect (&scc_counter, v, &index, stack, &stack_size);
145 * Save a state to an open file pointer. cls is expected to be a file pointer to
146 * an open file. Used only in conjunction with
147 * REGEX_TEST_automaton_save_graph.
149 * @param cls file pointer.
150 * @param count current count of the state, not used.
154 REGEX_TEST_automaton_save_graph_step (void *cls, unsigned int count,
155 struct REGEX_INTERNAL_State *s)
157 struct REGEX_TEST_Graph_Context *ctx = cls;
158 struct REGEX_INTERNAL_Transition *ctran;
164 if (GNUNET_YES == ctx->verbose)
165 GNUNET_asprintf (&name, "%i (%s) (%s) (%s)", s->dfs_id, s->name, s->proof,
166 GNUNET_h2s (&s->hash));
168 GNUNET_asprintf (&name, "%i", s->dfs_id);
172 if (GNUNET_YES == ctx->coloring)
174 GNUNET_asprintf (&s_acc,
175 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
176 name, s->scc_id * s->scc_id);
180 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", name,
184 else if (GNUNET_YES == ctx->coloring)
186 GNUNET_asprintf (&s_acc,
187 "\"%s\" [shape=circle, color=\"0.%i 0.8 0.95\"];\n", name,
188 s->scc_id * s->scc_id);
192 GNUNET_asprintf (&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id);
195 GNUNET_assert (NULL != s_acc);
197 fwrite (s_acc, strlen (s_acc), 1, ctx->filep);
201 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
203 if (NULL == ctran->to_state)
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 "Transition from State %i has no state for transitioning\n",
211 if (GNUNET_YES == ctx->verbose)
213 GNUNET_asprintf (&to_name, "%i (%s) (%s) (%s)", ctran->to_state->dfs_id,
214 ctran->to_state->name, ctran->to_state->proof,
215 GNUNET_h2s (&ctran->to_state->hash));
218 GNUNET_asprintf (&to_name, "%i", ctran->to_state->dfs_id);
220 if (NULL == ctran->label)
222 if (GNUNET_YES == ctx->coloring)
224 GNUNET_asprintf (&s_tran,
225 "\"%s\" -> \"%s\" [label = \"ε\", color=\"0.%i 0.8 0.95\"];\n",
226 name, to_name, s->scc_id * s->scc_id);
230 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"ε\"];\n", name,
236 if (GNUNET_YES == ctx->coloring)
238 GNUNET_asprintf (&s_tran,
239 "\"%s\" -> \"%s\" [label = \"%s\", color=\"0.%i 0.8 0.95\"];\n",
240 name, to_name, ctran->label, s->scc_id * s->scc_id);
244 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%s\"];\n", name,
245 to_name, ctran->label, s->scc_id);
249 GNUNET_free (to_name);
251 GNUNET_assert (NULL != s_tran);
253 fwrite (s_tran, strlen (s_tran), 1, ctx->filep);
254 GNUNET_free (s_tran);
263 * Save the given automaton as a GraphViz dot file.
265 * @param a the automaton to be saved.
266 * @param filename where to save the file.
267 * @param options options for graph generation that include coloring or verbose
271 REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a,
272 const char *filename,
273 enum REGEX_TEST_GraphSavingOptions options)
277 struct REGEX_TEST_Graph_Context ctx;
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
285 if ((NULL == filename)||(strlen (filename) < 1))
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
291 ctx.filep = fopen (filename, "w");
293 (0 == (options & REGEX_TEST_GRAPH_VERBOSE)) ? GNUNET_NO : GNUNET_YES;
295 (0 == (options & REGEX_TEST_GRAPH_COLORING)) ? GNUNET_NO : GNUNET_YES;
297 if (NULL == ctx.filep)
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
304 /* First add the SCCs to the automaton, so we can color them nicely */
305 if (GNUNET_YES == ctx.coloring)
308 start = "digraph G {\nrankdir=LR\n";
309 fwrite (start, strlen (start), 1, ctx.filep);
311 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL,
312 ®EX_TEST_automaton_save_graph_step,
316 fwrite (end, strlen (end), 1, ctx.filep);