dot methods

release/4.3a0
Frank Dellaert 2021-12-18 23:48:23 -05:00
parent 81b9724225
commit d41ab8addb
2 changed files with 39 additions and 10 deletions

View File

@ -35,21 +35,39 @@ void BayesNet<CONDITIONAL>::print(
/* ************************************************************************* */ /* ************************************************************************* */
template <class CONDITIONAL> template <class CONDITIONAL>
void BayesNet<CONDITIONAL>::saveGraph(const std::string& s, void BayesNet<CONDITIONAL>::dot(std::ostream& os,
const KeyFormatter& keyFormatter) const { const KeyFormatter& keyFormatter) const {
std::ofstream of(s.c_str()); os << "digraph G{\n";
of << "digraph G{\n";
for (auto conditional : boost::adaptors::reverse(*this)) { for (auto conditional : boost::adaptors::reverse(*this)) {
typename CONDITIONAL::Frontals frontals = conditional->frontals(); typename CONDITIONAL::Frontals frontals = conditional->frontals();
Key me = frontals.front(); const Key me = frontals.front();
typename CONDITIONAL::Parents parents = conditional->parents(); auto parents = conditional->parents();
for (Key p : parents) for (const Key& p : parents)
of << keyFormatter(p) << "->" << keyFormatter(me) << std::endl; os << keyFormatter(p) << "->" << keyFormatter(me) << "\n";
} }
of << "}"; os << "}";
std::flush(os);
}
/* ************************************************************************* */
template <class CONDITIONAL>
std::string BayesNet<CONDITIONAL>::dot(const KeyFormatter& keyFormatter) const {
std::stringstream ss;
dot(ss, keyFormatter);
return ss.str();
}
/* ************************************************************************* */
template <class CONDITIONAL>
void BayesNet<CONDITIONAL>::saveGraph(const std::string& filename,
const KeyFormatter& keyFormatter) const {
std::ofstream of(filename.c_str());
dot(of, keyFormatter);
of.close(); of.close();
} }
/* ************************************************************************* */
} // namespace gtsam } // namespace gtsam

View File

@ -67,8 +67,19 @@ namespace gtsam {
/// @name Standard Interface /// @name Standard Interface
/// @{ /// @{
void saveGraph(const std::string& s, /// Output to graphviz format, stream version.
virtual void dot(std::ostream& os, const KeyFormatter& keyFormatter =
DefaultKeyFormatter) const;
/// Output to graphviz format string.
std::string dot(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
/// output to file with graphviz format.
void saveGraph(const std::string& filename,
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const; const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
/// @}
}; };
} }