[Echempp-devel] Experiment/Experiment Experiment.hpp,NONE,1.1
Status: Beta
Brought to you by:
berndspeiser
|
From: beeblbrox <bee...@us...> - 2007-04-06 10:12:14
|
Update of /cvsroot/echempp/Experiment/Experiment In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv13087 Added Files: Experiment.hpp Log Message: Initial version. Still needs template parameters for realism, dynamics and checking of invariants for containers. --- NEW FILE: Experiment.hpp --- #ifndef __EXPERIMENT_HPP__ #define __EXPERIMENT_HPP__ // STL includes #include <vector> #include <iostream> #include <algorithm> #include <functional> #include <fstream> // Boost includes #include <boost/serialization/serialization.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/is_abstract.hpp> #include <boost/serialization/nvp.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/vector.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/time_serialize.hpp> // Local includes #include "ExcitationFunction/excitationFunction.hpp" namespace experiment { // Unary functor for deletion template<typename Obj> class functor_delete { public: typedef Obj argument_type; typedef void result_type; result_type operator()(argument_type obj) { delete obj; } }; // Unary functor to call perform template<typename Obj> class functor_perform { public: typedef Obj argument_type; typedef void result_type; result_type operator()(argument_type obj) { obj->perform(); } }; enum Realism{ Simulated, Real, Dual }; enum Dynamics{ Dynamic, Static }; class ExperimentException { public: ExperimentException(const std::string &msg) : msg_(msg) {}; const std::string what() { return msg_; } private: std::string msg_; }; class Experiment { public: virtual ~Experiment() {} virtual void perform() = 0; virtual const int dynamics_kind() const = 0; virtual const int realism_kind() const = 0; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version){} }; class Atomic : public Experiment { public: Atomic(int dynamics, int realism) : dynamics_(dynamics), realism_(realism) {} virtual ~Atomic() {}; const int dynamics_kind() const { return dynamics_; } const int realism_kind() const { return realism_; } private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Experiment) & BOOST_SERIALIZATION_NVP(dynamics_) & BOOST_SERIALIZATION_NVP(realism_); } int dynamics_; int realism_; // TODO: insert ExcitationFunction protected: Atomic() {} // For serialization }; template<class I, class D> class Action : public Atomic { public: Action(int dynamics, int realism) : Atomic(dynamics, realism){} virtual ~Action() {}; virtual void perform() {}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Atomic) & BOOST_SERIALIZATION_NVP(execution_start) & BOOST_SERIALIZATION_NVP(execution_end); } Action(){} // For serialization boost::posix_time::ptime execution_start; boost::posix_time::ptime execution_end; // TODO: insert ExcitationFunc ExcitationFunctions::DIExcitationFunction<I,D> exc_func; }; class Observation : public Atomic { public: Observation(int dynamics, int realism) : Atomic(dynamics, realism){} virtual ~Observation() {}; virtual void perform() {}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Atomic) & BOOST_SERIALIZATION_NVP(recording_start) & BOOST_SERIALIZATION_NVP(recording_end); } Observation(){} // For serialization boost::posix_time::ptime recording_start; boost::posix_time::ptime recording_end; // TODO: insert Conditions, StateData }; class Compound : public Experiment { public: virtual ~Compound() { std::for_each(elements.begin(), elements.end(), functor_delete<Experiment* >() ); } virtual void perform() { std::for_each(elements.begin(), elements.end(), functor_perform<Experiment* >() ); } virtual void add(Experiment* e) { std::cout << "Compound::add called!" << std::endl; elements.push_back(e); } bool empty() const { return elements.empty(); } typedef std::vector<Experiment*>::const_iterator const_iterator; const_iterator begin() const { return elements.begin(); } private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { std::cout << "Compound::serialize called!" << std::endl; ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Experiment) & BOOST_SERIALIZATION_NVP(elements); } std::vector<Experiment* > elements; }; class Collection : public Compound { public: virtual ~Collection() {}; // No restricton on add, just inherit the one from Compound private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Compound); } }; class Homogeneous : public Compound { public: virtual ~Homogeneous() {}; // Exploit Homogeneous invariant, that all elements // have the same dynamics const int dynamics_kind() const { if(Compound::empty()) throw ExperimentException("No dynamics type for empty Homogeneous"); Experiment *e = *Compound::begin(); return e->dynamics_kind(); } const int realism_kind() const { if(Compound::empty()) throw ExperimentException("No realism type for empty Homogeneous"); Experiment *e = *Compound::begin(); return e->realism_kind(); } virtual void add(Experiment* e) { std::cout << "Homogeneous::add called!" << std::endl; // Check that e is atomic if(!dynamic_cast<Atomic*>(e)) throw ExperimentException("Cannot build Homogeneous with non-atomic elements!"); // Check if this is the first inserted element if(Compound::empty()) return Compound::add(e); // Do element's type match? Experiment *ep = *Compound::begin(); if(ep->dynamics_kind() == e->dynamics_kind() && ep->realism_kind() == e->realism_kind()) return Compound::add(e); throw ExperimentException("All elements added to a Homogeneous compound should be of the same Experiment type!"); } private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { std::cout << "Homogeneous::serialize called!" << std::endl; ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Compound); } }; class Sequential : public Compound { public: virtual ~Sequential() {}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Compound); } }; class Simultaneous : public Compound { public: virtual ~Simultaneous() {}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Compound); } }; typedef Action<quantity::ElectricPotential, quantity::Time> MyAction; } // end namespace experiment BOOST_IS_ABSTRACT(experiment::Experiment) BOOST_IS_ABSTRACT(experiment::Atomic) BOOST_CLASS_EXPORT_GUID(experiment::Atomic, "experiment::Atomic") BOOST_CLASS_EXPORT_GUID(experiment::MyAction, "experiment::MyAction") BOOST_CLASS_EXPORT_GUID(experiment::Observation, "experiment::Observation") BOOST_CLASS_EXPORT_GUID(experiment::Compound, "experiment::Compound") BOOST_CLASS_EXPORT_GUID(experiment::Homogeneous, "experiment::Homogeneous") BOOST_CLASS_EXPORT_GUID(experiment::Sequential, "experiment::Sequential") BOOST_CLASS_EXPORT_GUID(experiment::Simultaneous, "experiment::Simultaneous") #endif |