Skip to content

Subtypes of graph_nodet can override DOT output #2913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/solvers/refinement/string_refinement_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ void string_dependenciest::output_dot(std::ostream &stream) const
return ostream.str();
};
stream << "digraph dependencies {\n";
output_dot_generic<nodet>(stream, for_each, for_each_succ, node_to_string);
output_dot_generic<nodet>(
stream, for_each, for_each_succ, node_to_string, node_to_string);
stream << '}' << std::endl;
}

Expand Down
46 changes: 43 additions & 3 deletions src/util/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Author: Daniel Kroening, [email protected]
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>

Expand Down Expand Up @@ -60,6 +61,39 @@ class graph_nodet
{
out.erase(n);
}

private:
/// \brief Node with attributes suitable for Graphviz DOT format
///
/// Derived types may override this function to produce more informative DOT
/// diagrams than the default implementation, which displays only the node
/// index. The return value should be a list of node attributes within square
/// brackets that can be parsed by `dot`. Here is a sample implementation for
/// a fictional node type with `is_evil()` and `is_pink()` functions:
///
/// std::stringstream ss;
/// ss << "[shape=\"" << is_evil() ? "box" : "diamond"
/// << "\", color=\"" << is_pink() ? "#e91e63" : "#9c27b0"
/// << "\", label=\"this is node " << std::to_string(idx)
/// << "\"]";
/// return ss.str();
///
virtual std::string dot_attributes(const node_indext &idx) const
{
return "";
}

public:
std::string pretty(const node_indext &idx) const
{
std::stringstream ss;
ss << std::to_string(idx) << " " << dot_attributes(idx);
return ss.str();
}

virtual ~graph_nodet()
{
}
};

/// A node type with an extra bit
Expand Down Expand Up @@ -877,9 +911,11 @@ void output_dot_generic(
const std::function<
void(const node_index_type &, std::function<void(const node_index_type &)>)>
&for_each_succ,
const std::function<std::string(const node_index_type &)> node_to_string)
const std::function<std::string(const node_index_type &)> node_to_string,
const std::function<std::string(const node_index_type &)> node_to_pretty)
{
for_each_node([&](const node_index_type &i) {
out << node_to_pretty(i) << ";\n";
for_each_succ(i, [&](const node_index_type &n) {
out << node_to_string(i) << " -> " << node_to_string(n) << '\n';
});
Expand Down Expand Up @@ -914,7 +950,7 @@ template <class N>
void grapht<N>::output_dot(std::ostream &out) const
{
const auto for_each_node =
[&](const std::function<void(const node_indext &)> &f) {
[this](const std::function<void(const node_indext &)> &f) {
for(node_indext i = 0; i < nodes.size(); ++i)
f(i);
};
Expand All @@ -925,7 +961,11 @@ void grapht<N>::output_dot(std::ostream &out) const
};

const auto to_string = [](const node_indext &i) { return std::to_string(i); };
output_dot_generic<node_indext>(out, for_each_node, for_each_succ, to_string);
const auto to_pretty_string = [this](const node_indext &i) {
return nodes[i].pretty(i);
};
output_dot_generic<node_indext>(
out, for_each_node, for_each_succ, to_string, to_pretty_string);
}

#endif // CPROVER_UTIL_GRAPH_H