diff --git a/cpp/BayesTree-inl.h b/cpp/BayesTree-inl.h index 0c0c7caa1..7eed3d8c7 100644 --- a/cpp/BayesTree-inl.h +++ b/cpp/BayesTree-inl.h @@ -33,7 +33,9 @@ namespace gtsam { template bool BayesTree::equals(const BayesTree& other, double tol) const { - return false; + return size()==other.size() && + equal(nodeMap_.begin(),nodeMap_.end(),other.nodeMap_.begin()) && + equal(nodes_.begin(),nodes_.end(),other.nodes_.begin(),equals_star); } /* ************************************************************************* */ diff --git a/cpp/BayesTree.h b/cpp/BayesTree.h index 0af1d2175..8ee46e47c 100644 --- a/cpp/BayesTree.h +++ b/cpp/BayesTree.h @@ -25,7 +25,7 @@ namespace gtsam { private: typedef boost::shared_ptr cond_ptr; std::list keys_; /** frontal keys */ - std::list nodes_; /** conditionals */ + std::list conditionals_; /** conditionals */ std::list separator_; /** separator keys */ public: @@ -50,13 +50,14 @@ namespace gtsam { /** check equality. TODO: only keys */ bool equals(const Front& other, double tol = 1e-9) const { - return (keys_ == other.keys_); + return (keys_ == other.keys_) && + equal(conditionals_.begin(),conditionals_.end(),other.conditionals_.begin(),equals_star); } /** add a frontal node */ void add(std::string key, cond_ptr conditional) { keys_.push_front(key); - nodes_.push_front(conditional); + conditionals_.push_front(conditional); } /** return size of the clique */ @@ -122,6 +123,9 @@ namespace gtsam { /** insert a new conditional */ void insert(std::string key, conditional_ptr conditional); + /** number of cliques */ + inline size_t size() const { return nodes_.size();} + /** return root clique */ const Front& root() const {return *(nodes_[0]);} diff --git a/cpp/testBayesTree.cpp b/cpp/testBayesTree.cpp index aadbefd88..6ebdbcae8 100644 --- a/cpp/testBayesTree.cpp +++ b/cpp/testBayesTree.cpp @@ -34,9 +34,9 @@ TEST( BayesTree, Front ) } /* ************************************************************************* */ -TEST( BayesTree, insert ) +TEST( BayesTree, constructor ) { - // Insert + // Create using insert BayesTree bayesTree; bayesTree.insert("B",B); bayesTree.insert("L",L); @@ -44,33 +44,25 @@ TEST( BayesTree, insert ) bayesTree.insert("S",S); bayesTree.insert("T",T); bayesTree.insert("X",X); - //bayesTree.print("bayesTree"); - //LONGS_EQUAL(1,bayesTree.size()); + // Check Size + LONGS_EQUAL(4,bayesTree.size()); // Check root Front expected_root("B",B); - //CHECK(assert_equal(expected_root,bayesTree.root())); -} + expected_root.add("L",L); + expected_root.add("E",E); + Front actual_root = bayesTree.root(); + CHECK(assert_equal(expected_root,actual_root)); -/* ************************************************************************* */ -TEST( BayesTree, constructor ) -{ - // Create Symbolic Bayes Chain in which we want to discover cliques + // Create from symbolic Bayes chain in which we want to discover cliques map nodes; insert(nodes)("B",B)("L",L)("E",E)("S",S)("T",T)("X",X); SymbolicBayesChain ASIA(nodes); + BayesTree bayesTree2(ASIA); - // Create Bayes Tree from Symbolic Bayes Chain - BayesTree bayesTree(ASIA); - bayesTree.insert("B",B); - //bayesTree.print("bayesTree"); - - //LONGS_EQUAL(1,bayesTree.size()); - - // Check root - Front expected_root("B",B); - //CHECK(assert_equal(expected_root,bayesTree.root())); + // Check whether the same + //CHECK(assert_equal(bayesTree,bayesTree2)); } /* ************************************************************************* */