Skip to content

Commit

Permalink
Merge pull request #392 from VeriFIT/DOT_format_ASCII
Browse files Browse the repository at this point in the history
Dot format ascii
  • Loading branch information
Adda0 committed Nov 18, 2024
2 parents c92ee38 + 19625fb commit 1e2aa93
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
8 changes: 5 additions & 3 deletions include/mata/nfa/nfa.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 5 additions & 2 deletions include/mata/nft/nft.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
20 changes: 16 additions & 4 deletions src/nfa/nfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(symbol)) + "\\'";
};

output << "digraph finiteAutomaton {" << std::endl
<< "node [shape=circle];" << std::endl;

Expand All @@ -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;
}
}
}

Expand Down
30 changes: 26 additions & 4 deletions src/nft/nft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<eps>";
}
if (symbol == DONT_CARE) {
return "<dcare>";
}
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<char>(symbol)) + "\\'";
};
output << "digraph finiteAutomaton {" << std::endl
<< "node [shape=circle];" << std::endl;

Expand All @@ -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;
}
}
}

Expand Down

0 comments on commit 1e2aa93

Please sign in to comment.