diff --git a/cpp/BayesNet-inl.h b/cpp/BayesNet-inl.h index ad7d1f618..2f7af67a4 100644 --- a/cpp/BayesNet-inl.h +++ b/cpp/BayesNet-inl.h @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -59,6 +60,21 @@ namespace gtsam { return ord; } + /* ************************************************************************* */ + template + void BayesNet::saveGraph(const std::string &s) const { + ofstream of(s.c_str()); + of<< "digraph G{\n"; + BOOST_FOREACH(sharedConditional conditional,conditionals_) { + Symbol child = conditional->key(); + BOOST_FOREACH(const Symbol& parent,conditional->parents()) { + of << (string)parent << "->" << (string)child << endl; + } + } + of<<"}"; + of.close(); + } + /* ************************************************************************* */ template diff --git a/cpp/BayesNet.h b/cpp/BayesNet.h index 2a20fa0fa..462874a33 100644 --- a/cpp/BayesNet.h +++ b/cpp/BayesNet.h @@ -97,6 +97,9 @@ namespace gtsam { inline const_reverse_iterator const rbegin() const {return conditionals_.rbegin();} inline const_reverse_iterator const rend() const {return conditionals_.rend();} + /** saves the bayes to a text file in GraphViz format */ + void saveGraph(const std::string& s) const; + private: /** Serialization function */ friend class boost::serialization::access;