| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    Conditional.h | 
					
						
							|  |  |  |  * @brief   Base class for conditional densities | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/utility.hpp> // for noncopyable
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | #include <boost/serialization/string.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Testable.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Base class for conditional densities | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * We make it noncopyable so we enforce the fact that factors are | 
					
						
							|  |  |  |  * kept in pointer containers. To be safe, you should make them | 
					
						
							|  |  |  |  * immutable, i.e., practicing functional programming. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Conditional: boost::noncopyable, public Testable<Conditional> { | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** key of random variable */ | 
					
						
							|  |  |  | 	std::string key_; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:55:07 +08:00
										 |  |  | 	/** empty constructor for serialization */ | 
					
						
							|  |  |  | 	Conditional() : | 
					
						
							|  |  |  | 		key_("__unitialized__") { | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** constructor */ | 
					
						
							|  |  |  | 	Conditional(const std::string& key) : | 
					
						
							|  |  |  | 		key_(key) { | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/* destructor */ | 
					
						
							|  |  |  | 	virtual ~Conditional() { | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** check equality */ | 
					
						
							|  |  |  | 	bool equals(const Conditional& c, double tol = 1e-9) const { | 
					
						
							|  |  |  | 		return key_ == c.key_; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** return key */ | 
					
						
							|  |  |  | 	inline const std::string& key() const { | 
					
						
							|  |  |  | 		return key_; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** return parent keys */ | 
					
						
							|  |  |  | 	virtual std::list<std::string> parents() const = 0; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** return the number of parents */ | 
					
						
							|  |  |  | 	virtual std::size_t nrParents() const = 0; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | private: | 
					
						
							|  |  |  | 	/** Serialization function */ | 
					
						
							|  |  |  | 	friend class boost::serialization::access; | 
					
						
							|  |  |  | 	template<class Archive> | 
					
						
							|  |  |  | 	void serialize(Archive & ar, const unsigned int version) { | 
					
						
							|  |  |  | 		ar & BOOST_SERIALIZATION_NVP(key_); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | // predicate to check whether a conditional has the sought key
 | 
					
						
							|  |  |  | template<class Conditional> | 
					
						
							|  |  |  | class onKey { | 
					
						
							|  |  |  | 	const std::string& key_; | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	onKey(const std::string& key) : | 
					
						
							|  |  |  | 		key_(key) { | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	bool operator()(const typename Conditional::shared_ptr& conditional) { | 
					
						
							|  |  |  | 		return (conditional->key() == key_); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | } |