Added serialization of base class

release/4.3a0
Manohar Paluri 2009-11-10 14:44:53 +00:00
parent ddc0173671
commit 1f15650da0
3 changed files with 68 additions and 46 deletions

View File

@ -9,57 +9,71 @@
#pragma once
#include <boost/utility.hpp> // for noncopyable
#include <boost/serialization/string.hpp>
#include "Testable.h"
namespace gtsam {
/**
* 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:
/**
* 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_;
/** key of random variable */
std::string key_;
public:
public:
/** constructor */
Conditional(const std::string& key):key_(key) {}
/** constructor */
Conditional(const std::string& key) :
key_(key) {
}
/* destructor */
virtual ~Conditional() {};
/* destructor */
virtual ~Conditional() {
}
;
/** check equality */
bool equals(const Conditional& c, double tol = 1e-9) const {
return key_ == c.key_;
}
/** check equality */
bool equals(const Conditional& c, double tol = 1e-9) const {
return key_ == c.key_;
}
/** return key */
inline const std::string& key() const { return key_;}
/** return parent keys */
virtual std::list<std::string> parents() const = 0;
/** return the number of parents */
virtual std::size_t nrParents() const = 0;
};
/** return key */
inline const std::string& key() const {
return key_;
}
// 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_);
}
};
/** return parent keys */
virtual std::list<std::string> parents() const = 0;
/** return the number of parents */
virtual std::size_t nrParents() const = 0;
private:
/** Serialization function */
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(key_);
}
};
// 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_);
}
};
}

View File

@ -13,7 +13,6 @@
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include "Conditional.h"
@ -44,12 +43,12 @@ protected:
/** the names and the matrices connecting to parent nodes */
Parents parents_;
/** vector of standard deviations */
Vector sigmas_;
/** the RHS vector */
Vector d_;
/** vector of standard deviations */
Vector sigmas_;
public:
/** default constructor needed for serialization */
@ -141,10 +140,11 @@ private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this));
ar & BOOST_SERIALIZATION_NVP(R_);
ar & BOOST_SERIALIZATION_NVP(parents_);
ar & BOOST_SERIALIZATION_NVP(d_);
ar & BOOST_SERIALIZATION_NVP(sigmas_);
ar & BOOST_SERIALIZATION_NVP(parents_);
}
};
}

View File

@ -13,6 +13,7 @@
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp> // TODO: make cpp file
#include <boost/serialization/list.hpp>
#include "Conditional.h"
namespace gtsam {
@ -88,6 +89,13 @@ namespace gtsam {
return parents_.size();
}
private:
/** Serialization function */
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this));
ar & BOOST_SERIALIZATION_NVP(parents_);
}
};
} /// namespace gtsam