00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef SAMPLE_COMPOSITION_HPP
00033 #define SAMPLE_COMPOSITION_HPP
00034
00035 #include <rtt/Property.hpp>
00036 #include <rtt/PropertyBag.hpp>
00037 #include <rtt/TemplateTypeInfo.hpp>
00038 #include <rtt/Types.hpp>
00039 #include <rtt/Logger.hpp>
00040 #include <rtt/DataSources.hpp>
00041 #include <ostream>
00042 #include <sstream>
00043 #include <vector>
00044
00045 #include "../../sample/sample.h"
00046 #include "../../sample/weightedsample.h"
00047 namespace BFL
00048 {
00049 using namespace RTT;
00050 using namespace RTT::detail;
00051 using namespace MatrixWrapper;
00052 class PropertyIntrospection;
00053
00054
00055
00056
00061 template<class T>
00062 void decomposeProperty(const Sample<T>& sample, PropertyBag& targetbag)
00063 {
00064 std::string tname = detail::DataSourceTypeInfo<T>::getType();
00065 targetbag.setType("Sample");
00066
00067
00068 assert( targetbag.empty() );
00069
00070 bool result =true;
00071
00072
00073
00074
00075 Property<PropertyBag>* el_bag = new Property<PropertyBag>("SampleValue", "Sample Value");
00076 Property<T> el("SampleValue" , "Sample value ",sample.ValueGet()) ;
00077 if( el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
00078 {
00079
00080 targetbag.add( el_bag );
00081 }
00082 else
00083 {
00084
00085
00086 targetbag.add( new Property<T>("SampleValue" ,"Sample Value",sample.ValueGet() ));
00087 }
00088 };
00089
00094 template<class T>
00095 bool composeProperty(const PropertyBag& bag, Sample<T>& sample)
00096 {
00097
00098 std::string tname = detail::DataSourceTypeInfo<T>::getType();
00099 if ( bag.getType() == std::string("Sample") ) {
00100
00101 Property<PropertyBag>* el_bag = bag.getProperty<PropertyBag>("SampleValue");
00102
00103 if(el_bag==NULL){
00104
00105 PropertyBase* element = bag.getItem( 0 );
00106
00107 Property<T> my_property_t (element->getName(),element->getDescription());
00108 if(my_property_t.getType()!=element->getType())
00109 {
00110 log(Error)<< "Type of "<< element->getName() << " does not match type of Sample"<< "OR "<<"Could not read Sample Value "<<endlog();
00111 return false;
00112 }
00113 else{
00114 my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00115 sample.ValueSet( my_property_t.get());
00116 }
00117 }
00118 else{
00119
00120 const std::string el_bagType = el_bag->getType();
00121 Property<T > el_p(el_bag->getName(),el_bag->getDescription());
00122 if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
00123 log(Error)<<"Could not compose SampleValue "<<endlog();
00124 return false;
00125 }
00126 if(el_p.ready()){
00127 sample.ValueSet( el_p.get());
00128 }else{
00129 log(Error)<<"Property of SampleValue was not ready for use"<<endlog();
00130 return false;
00131 }
00132 }
00133 }
00134 else {
00135 Logger::log() << Logger::Error << "Composing Property< Sample<T> > :"
00136 << " type mismatch, got type '"<< bag.getType()
00137 << "', expected type "<<tname<<"."<<Logger::endl;
00138 return false;
00139 }
00140 return true;
00141 };
00142
00143 template <typename T>
00144 struct SampleTypeInfo
00145 : public TemplateTypeInfo<Sample<T>, true>
00146 {
00147 SampleTypeInfo<T>(std::string name)
00148 : TemplateTypeInfo<Sample<T>, true >(name)
00149 {
00150 };
00151
00152 bool decomposeTypeImpl(const Sample<T>& sample, PropertyBag& targetbag) const
00153 {
00154 decomposeProperty<T>( sample, targetbag );
00155 return true;
00156 };
00157
00158 bool composeTypeImpl(const PropertyBag& bag, Sample<T>& result) const
00159 {
00160 return composeProperty<T>( bag, result );
00161 }
00162
00163 };
00164
00165 template<typename T>
00166 struct Sample_ctor
00167 : public std::unary_function<T, const Sample<T>&>
00168 {
00169 typedef const Sample<T>& (Signature)( T );
00170 mutable boost::shared_ptr< Sample<T> > ptr;
00171 Sample_ctor()
00172 : ptr( new Sample<T>() ) {}
00173 const Sample<T>& operator()( T value ) const
00174 {
00175 ptr->ValueSet( value );
00176 return *(ptr);
00177 }
00178 };
00179
00180
00181
00182
00187 template<class T>
00188 void decomposeProperty(const WeightedSample<T>& weightedSample, PropertyBag& targetbag)
00189 {
00190 std::string tname = detail::DataSourceTypeInfo<T>::getType();
00191 targetbag.setType("WeightedSample");
00192
00193
00194 assert( targetbag.empty() );
00195
00196 bool result =true;
00197
00198
00199
00200
00201
00202 Property<PropertyBag>* el_bag = new Property<PropertyBag>("WeightedSampleValue", "WeightedSample Value");
00203 Property<T> el("WeightedSampleValue" , "WeightedSample value ",weightedSample.ValueGet()) ;
00204 if( el.getTypeInfo()->decomposeType(el.getDataSource(),el_bag->value()) )
00205 {
00206
00207 targetbag.add( el_bag );
00208 }
00209 else
00210 {
00211
00212
00213 targetbag.add( new Property<T>("WeightedSampleValue" ,"WeightedSample Value",weightedSample.ValueGet() ));
00214 }
00215
00216 targetbag.add( new Property<double>("WeightedSampleWeight" ,"WeightedSample Weight",weightedSample.WeightGet() ));
00217 };
00218
00223 template<class T>
00224 bool composeProperty(const PropertyBag& bag, WeightedSample<T>& weightedSample)
00225 {
00226
00227 std::string tname = detail::DataSourceTypeInfo<T>::getType();
00228 if ( bag.getType() == std::string("WeightedSample") ) {
00229
00230 Property<PropertyBag>* el_bag = bag.getProperty<PropertyBag>("WeightedSampleValue");
00231
00232 if(el_bag==NULL){
00233
00234 PropertyBase* element = bag.getItem( 0 );
00235
00236 Property<T> my_property_t (element->getName(),element->getDescription());
00237 if(my_property_t.getType()!=element->getType())
00238 {
00239 log(Error)<< "Type of "<< element->getName() << " does not match type of WeightedSample"<< "OR "<<"Could not read WeightedSample Value "<<endlog();
00240 return false;
00241 }
00242 else{
00243 my_property_t.getTypeInfo()->composeType(element->getDataSource(),my_property_t.getDataSource());
00244 weightedSample.ValueSet( my_property_t.get());
00245 }
00246 }
00247 else{
00248
00249 const std::string el_bagType = el_bag->getType();
00250 Property<T > el_p(el_bag->getName(),el_bag->getDescription());
00251 if(!(el_p.getDataSource()->composeType(el_bag->getDataSource()))){
00252 log(Error)<<"Could not compose WeightedSampleValue "<<endlog();
00253 return false;
00254 }
00255 if(el_p.ready()){
00256 weightedSample.ValueSet( el_p.get());
00257 }else{
00258 log(Error)<<"Property of WeightedSampleValue was not ready for use"<<endlog();
00259 return false;
00260 }
00261 }
00262
00263 Property<double>* weightProp = bag.getProperty<double>("WeightedSampleWeight");
00264
00265 if(!weightProp)
00266 {
00267 log(Error)<< "Error reading weight of WeightedSample"<<endlog();
00268 return false;
00269 }
00270 else{
00271 weightedSample.WeightSet( weightProp->get());
00272 }
00273 }
00274 else {
00275 Logger::log() << Logger::Error << "Composing Property< WeightedSample<T> > :"
00276 << " type mismatch, got type '"<< bag.getType()
00277 << "', expected type "<<tname<<"."<<Logger::endl;
00278 return false;
00279 }
00280 return true;
00281 };
00282
00283 template <typename T>
00284 struct WeightedSampleTypeInfo
00285 : public TemplateTypeInfo<WeightedSample<T>, true>
00286 {
00287 WeightedSampleTypeInfo<T>(std::string name)
00288 : TemplateTypeInfo<WeightedSample<T>, true >(name)
00289 {
00290 };
00291
00292 bool decomposeTypeImpl(const WeightedSample<T>& weightedSample, PropertyBag& targetbag) const
00293 {
00294 decomposeProperty<T>( weightedSample, targetbag );
00295 return true;
00296 };
00297
00298 bool composeTypeImpl(const PropertyBag& bag, WeightedSample<T>& result) const
00299 {
00300 return composeProperty<T>( bag, result );
00301 }
00302
00303 };
00304
00305 template<typename T>
00306 struct WeightedSample_ctor
00307 : public std::binary_function<T ,double , const WeightedSample<T>&>
00308 {
00309 typedef const WeightedSample<T>& (Signature)( T, double );
00310 mutable boost::shared_ptr< WeightedSample<T> > ptr;
00311 WeightedSample_ctor()
00312 : ptr( new WeightedSample<T>() ) {}
00313 const WeightedSample<T>& operator()( T value , double weight) const
00314 {
00315 ptr->ValueSet( value );
00316 ptr->WeightSet( weight );
00317 return *(ptr);
00318 }
00319 };
00320
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 };
00340 #endif
00341