Skip to content

Commit

Permalink
print_DOT() can translate numbers to ASCII chars
Browse files Browse the repository at this point in the history
  • Loading branch information
koniksedy committed Mar 1, 2024
1 parent fcb8507 commit 485b91e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
7 changes: 5 additions & 2 deletions include/mata/nfa/nfa.hh
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,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 @@ -379,13 +379,21 @@ bool Nfa::is_acyclic() const {
return acyclic;
}

std::string Nfa::print_to_DOT() const {
std::string Nfa::print_to_DOT(const bool ascii) const {

Check warning on line 382 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L382

Added line #L382 was not covered by tests
std::stringstream output;
print_to_DOT(output);
print_to_DOT(output, ascii);

Check warning on line 384 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L384

Added line #L384 was not covered by tests
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) {

Check warning on line 389 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L388-L389

Added lines #L388 - L389 were not covered by tests
// Translate only printable ASCII characters.
if (symbol < 33) {
return std::to_string(symbol);

Check warning on line 392 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L392

Added line #L392 was not covered by tests
}
return "\\'" + std::string(1, static_cast<char>(symbol)) + "\\'";

Check warning on line 394 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L394

Added line #L394 was not covered by tests
};

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

Expand All @@ -400,7 +408,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;

Check warning on line 412 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L412

Added line #L412 was not covered by tests
} else {
output << "} [label=\"" << move.symbol << "\"];" << std::endl;

Check warning on line 414 in src/nfa/nfa.cc

View check run for this annotation

Codecov / codecov/patch

src/nfa/nfa.cc#L414

Added line #L414 was not covered by tests
}
}
}

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 {

Check warning on line 79 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L79

Added line #L79 was not covered by tests
std::stringstream output;
print_to_DOT(output);
print_to_DOT(output, ascii);

Check warning on line 81 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L81

Added line #L81 was not covered by tests
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 {

Check warning on line 86 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L85-L86

Added lines #L85 - L86 were not covered by tests
if (symbol == EPSILON) {
return "<eps>";

Check warning on line 88 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L88

Added line #L88 was not covered by tests
}
if (symbol == DONT_CARE) {
return "<dcare>";

Check warning on line 91 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L91

Added line #L91 was not covered by tests
}
return std::to_string(symbol);

Check warning on line 93 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L93

Added line #L93 was not covered by tests
};

auto to_ascii = [&](const Symbol symbol) {

Check warning on line 96 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L96

Added line #L96 was not covered by tests
// Translate only printable ASCII characters.
if (symbol < 33) {
return std::to_string(symbol);

Check warning on line 99 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L99

Added line #L99 was not covered by tests
}
return "\\'" + std::string(1, static_cast<char>(symbol)) + "\\'";

Check warning on line 101 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L101

Added line #L101 was not covered by tests
};
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;

Check warning on line 119 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L119

Added line #L119 was not covered by tests
} else {
output << "} [label=\"" << translate_special_symbols(move.symbol) << "\"];" << std::endl;

Check warning on line 121 in src/nft/nft.cc

View check run for this annotation

Codecov / codecov/patch

src/nft/nft.cc#L121

Added line #L121 was not covered by tests
}
}
}

Expand Down

0 comments on commit 485b91e

Please sign in to comment.