From 19625fb1c34a3fa83d5f16421f6ef95a47ad915a Mon Sep 17 00:00:00 2001 From: koniksedy Date: Fri, 1 Mar 2024 12:04:55 +0100 Subject: [PATCH] print_DOT() can translate numbers to ASCII chars --- include/mata/nfa/nfa.hh | 8 +++++--- include/mata/nft/nft.hh | 7 +++++-- src/nfa/nfa.cc | 20 ++++++++++++++++---- src/nft/nft.cc | 30 ++++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/include/mata/nfa/nfa.hh b/include/mata/nfa/nfa.hh index 26545b57..ba3f7c66 100644 --- a/include/mata/nfa/nfa.hh +++ b/include/mata/nfa/nfa.hh @@ -283,14 +283,16 @@ public: /** * @brief Prints the automaton in DOT format * + * @param[in] ascii Whether to use ASCII characters for the output. * @return automaton in DOT format */ - std::string print_to_dot() const; + std::string print_to_dot(const bool ascii = false) const; /** * @brief Prints the automaton to the output stream in DOT format + * + * @param[in] ascii Whether to use ASCII characters for the output. */ - void print_to_dot(std::ostream &output) const; - + void print_to_dot(std::ostream &output, const bool ascii = false) const; /** * @brief Prints the automaton in mata format * diff --git a/include/mata/nft/nft.hh b/include/mata/nft/nft.hh index d312e113..95fba48c 100644 --- a/include/mata/nft/nft.hh +++ b/include/mata/nft/nft.hh @@ -240,13 +240,16 @@ public: /** * @brief Prints the automaton in DOT format * + * @param[in] ascii Whether to use ASCII characters for the output. * @return automaton in DOT format */ - std::string print_to_DOT() const; + std::string print_to_DOT(const bool ascii = false) const; /** * @brief Prints the automaton to the output stream in DOT format + * + * @param[in] ascii Whether to use ASCII characters for the output. */ - void print_to_DOT(std::ostream &output) const; + void print_to_DOT(std::ostream &output, const bool ascii = false) const; /** * @brief Prints the automaton in mata format * diff --git a/src/nfa/nfa.cc b/src/nfa/nfa.cc index 00cbb1c2..3f89600a 100644 --- a/src/nfa/nfa.cc +++ b/src/nfa/nfa.cc @@ -426,13 +426,21 @@ bool Nfa::is_flat() const { return flat; } -std::string Nfa::print_to_dot() const { +std::string Nfa::print_to_dot(const bool ascii) const { std::stringstream output; - print_to_dot(output); + print_to_dot(output, ascii); return output.str(); } -void Nfa::print_to_dot(std::ostream &output) const { +void Nfa::print_to_dot(std::ostream &output, const bool ascii) const { + auto to_ascii = [&](const Symbol symbol) { + // Translate only printable ASCII characters. + if (symbol < 33) { + return std::to_string(symbol); + } + return "\\'" + std::string(1, static_cast(symbol)) + "\\'"; + }; + output << "digraph finiteAutomaton {" << std::endl << "node [shape=circle];" << std::endl; @@ -447,7 +455,11 @@ void Nfa::print_to_dot(std::ostream &output) const { for (State target: move.targets) { output << target << " "; } - output << "} [label=" << move.symbol << "];" << std::endl; + if (ascii) { + output << "} [label=\"" << to_ascii(move.symbol) << "\"];" << std::endl; + } else { + output << "} [label=\"" << move.symbol << "\"];" << std::endl; + } } } diff --git a/src/nft/nft.cc b/src/nft/nft.cc index 6fbaedeb..0200a8f5 100644 --- a/src/nft/nft.cc +++ b/src/nft/nft.cc @@ -76,13 +76,30 @@ Nft& Nft::trim(StateRenaming* state_renaming) { return *this; } -std::string Nft::print_to_DOT() const { +std::string Nft::print_to_DOT(const bool ascii) const { std::stringstream output; - print_to_DOT(output); + print_to_DOT(output, ascii); return output.str(); } -void Nft::print_to_DOT(std::ostream &output) const { +void Nft::print_to_DOT(std::ostream &output, const bool ascii) const { + auto translate_special_symbols = [&](const Symbol symbol) -> std::string { + if (symbol == EPSILON) { + return ""; + } + if (symbol == DONT_CARE) { + return ""; + } + return std::to_string(symbol); + }; + + auto to_ascii = [&](const Symbol symbol) { + // Translate only printable ASCII characters. + if (symbol < 33) { + return std::to_string(symbol); + } + return "\\'" + std::string(1, static_cast(symbol)) + "\\'"; + }; output << "digraph finiteAutomaton {" << std::endl << "node [shape=circle];" << std::endl; @@ -97,7 +114,12 @@ void Nft::print_to_DOT(std::ostream &output) const { for (State target: move.targets) { output << target << " "; } - output << "} [label=" << move.symbol << "];" << std::endl; + + if (ascii && move.symbol < 128) { + output << "} [label=\"" << to_ascii(move.symbol) << "\"];" << std::endl; + } else { + output << "} [label=\"" << translate_special_symbols(move.symbol) << "\"];" << std::endl; + } } }