28 #ifndef __CALCIUM_COUPLING_POLICY__  
   29 #define __CALCIUM_COUPLING_POLICY__ 
   34 #include "DisplayPair.hxx" 
   35 #include "CouplingPolicy.hxx" 
   36 #include "AdjacentFunctor.hxx" 
   37 #include <boost/lambda/lambda.hpp> 
   38 #include <boost/utility/enable_if.hpp> 
   39 #include <boost/type_traits/is_arithmetic.hpp> 
   40 #include "CalciumTypes.hxx" 
   41 #include "CalciumException.hxx" 
   52   template <
typename DataManipulator, 
 
   58   typedef CalciumTypes::DependencyType       DependencyType;
 
   59   typedef CalciumTypes::DateCalSchem         DateCalSchem;
 
   60   typedef CalciumTypes::InterpolationSchem   InterpolationSchem;
 
   61   typedef CalciumTypes::ExtrapolationSchem   ExtrapolationSchem;
 
   62   typedef CalciumTypes::DisconnectDirective  DisconnectDirective;  
 
   66   DependencyType      _dependencyType;
 
   68   DateCalSchem        _dateCalSchem;
 
   69   InterpolationSchem  _interpolationSchem;
 
   70   ExtrapolationSchem  _extrapolationSchem;
 
   73   DisconnectDirective _disconnectDirective;
 
   78   void           setDependencyType (DependencyType dependencyType);
 
   79   DependencyType getDependencyType () 
const;
 
   81   void   setStorageLevel   (
size_t storageLevel);
 
   82   size_t getStorageLevel   () 
const;
 
   84   void         setDateCalSchem   (DateCalSchem   dateCalSchem);
 
   85   DateCalSchem getDateCalSchem () 
const;
 
   87   void   setAlpha(
double alpha);
 
   88   double getAlpha() 
const ;
 
   90   void   setDeltaT(
double deltaT );
 
   91   double getDeltaT() 
const ;
 
   93   void setInterpolationSchem (InterpolationSchem interpolationSchem);
 
   94   void setExtrapolationSchem (ExtrapolationSchem extrapolationSchem);
 
   95   InterpolationSchem getInterpolationSchem () 
const ;
 
   96   ExtrapolationSchem getExtrapolationSchem () 
const ;
 
  101   typedef double TimeType;
 
  102   typedef long   TagType;
 
  103   typedef std::pair< TimeType , TagType >     DataId;
 
  105   typedef std::vector< DataId >::iterator  iterator;
 
  107   template <
typename T_TIME, 
typename T_TAG >  
 
  110   inline TimeType getTime(
const DataId &dataId)
 const { 
return dataId.first;}
 
  111   inline TagType  getTag (
const DataId &dataId)
 const { 
return dataId.second;}
 
  113   template <
typename DataManipulator, 
 
  126   template < 
typename AssocContainer >
 
  127   bool isDataIdConveniant( AssocContainer & storedDatas, 
 
  128                            const typename AssocContainer::key_type & expectedDataId,
 
  129                            bool & isEqual, 
bool & isBounded, 
 
  130                            typename AssocContainer::iterator & wDataIt1) 
const;
 
  132   TimeType getEffectiveTime(TimeType ti, TimeType tf);
 
  134   void disconnect(
bool provideLastGivenValue);
 
  146 template <
typename T_TIME, 
typename T_TAG > 
 
  148   typedef std::vector < DataId >        DataIdVect;
 
  152                           ):std::vector< std::pair< T_TIME,T_TAG> >() {
 
  154     switch (policy._dependencyType) {
 
  155     case CalciumTypes::TIME_DEPENDENCY:
 
  156       this->push_back(DataId(dataId.first,0));
 
  158     case CalciumTypes::ITERATION_DEPENDENCY:
 
  159       this->push_back(DataId(0,dataId.second));
 
  162       throw(
CalciumException(CalciumTypes::CPIT,LOC(
"The dependency type must be set by setDependencyType before calling DataIdContainer contructor")));
 
  169 template <
typename DataManipulator, 
class EnableIf >
 
  172   template < 
typename Iterator, 
typename DataId > 
 
  173   void inline apply(
typename iterator_t<Iterator>::value_type & data,
 
  174                     const DataId & dataId,
 
  175                     const Iterator  & it1)
 const {
 
  176     typedef typename iterator_t<Iterator>::value_type value_type;
 
  178     std::cout << 
"-------- Calcium Generic BoundedDataIdProcessor.apply() called " << std::endl;
 
  185 template <
typename DataManipulator >
 
  188   typename boost::enable_if< boost::is_float< typename DataManipulator::InnerType> >::type > {
 
  193     _couplingPolicy(couplingPolicy) {};
 
  196   template < 
typename MapIterator > 
 
  197   void inline apply (
typename iterator_t<MapIterator>::value_type & data,
 
  198                      const DataId & dataId, 
const MapIterator & it1)
 const {
 
  200     typedef typename iterator_t<MapIterator>::value_type value_type;
 
  201     typedef typename DataManipulator::InnerType InnerType;
 
  202     typedef typename DataManipulator::Type Type;
 
  204     MapIterator it2=it1; ++it2;
 
  205     size_t   dataSize1 = DataManipulator::size(it1->second);
 
  207     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Taille de donnée dataId1 : " << dataSize1 << std::endl;
 
  212     size_t   dataSize2 = DataManipulator::size(it2->second);
 
  214     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Taille de donnée dataId2 : " << dataSize2 << std::endl;
 
  217     size_t   dataSize  = std::min< size_t >( dataSize1, dataSize2 );
 
  218     DataId   dataId2 = it2->first;
 
  219     DataId   dataId1 = it1->first;
 
  220     TimeType t2      = dataId2.first;
 
  221     TimeType t1      = dataId1.first;
 
  223     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Valeur de t1 : " << t1 << std::endl;
 
  224     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Valeur de t2 : " << t2 << std::endl;
 
  226     TimeType t       = dataId.first;
 
  228     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Valeur de t : " << t << std::endl;
 
  230     TimeType timeDiff  = t2-t1;
 
  232     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Valeur de timeDiff : " << timeDiff << std::endl;
 
  234     TimeType coeff   = (t2-t)/timeDiff;
 
  236     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Valeur de coeff : " << coeff << std::endl;
 
  239     InnerType 
const * 
const InIt1 = DataManipulator::getPointer(it1->second);
 
  241     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Données à t1 : " << std::endl;
 
  242     std::copy(InIt1,InIt1+dataSize1,std::ostream_iterator<InnerType>(std::cout,
" "));
 
  243     std::cout << std::endl;
 
  245     InnerType 
const * 
const InIt2 = DataManipulator::getPointer(it2->second);
 
  247     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Données à t2 : " << std::endl;
 
  248     std::copy(InIt2,InIt2+dataSize2,std::ostream_iterator<InnerType>(std::cout,
" "));
 
  249     std::cout << std::endl;
 
  251     Type              dataOut = DataManipulator::create(dataSize);
 
  252     InnerType * 
const OutIt   = DataManipulator::getPointer(dataOut);
 
  255     std::cerr << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : interpolationSchem : " << _couplingPolicy._interpolationSchem << std::endl;
 
  256     std::cerr << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : alpha : " << _couplingPolicy._alpha << std::endl;
 
  257     std::cerr << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : datecalschem : " << _couplingPolicy._dateCalSchem << std::endl;
 
  258     std::cerr << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : storageLevel : " << _couplingPolicy._storageLevel << std::endl;
 
  260     if ( timeDiff == 0.0 ||  _couplingPolicy._interpolationSchem == CalciumTypes::L0_SCHEM ) {
 
  261       std::copy(InIt1,InIt1+dataSize,OutIt);
 
  264       boost::lambda::placeholder1_type _1;
 
  265       boost::lambda::placeholder2_type _2;
 
  268       std::transform(InIt1,InIt1+dataSize,InIt2,OutIt,
 
  269                      ( _1 - _2 ) * coeff + _2 );
 
  276     std::cout << 
"-------- CalciumCouplingPolicy::BoundedDataIdProcessor : Données calculées à t : " << std::endl;
 
  277     std::copy(OutIt,OutIt+dataSize,std::ostream_iterator<InnerType>(std::cout,
" "));
 
  278     std::cout << std::endl;
 
  291 template < 
typename AssocContainer >
 
  292 bool CalciumCouplingPolicy::isDataIdConveniant( AssocContainer & storedDatas, 
const typename AssocContainer::key_type & expectedDataId,
 
  293                                                 bool & isEqual, 
bool & isBounded, 
typename AssocContainer::iterator & wDataIt1)
 const {
 
  296   typedef typename AssocContainer::key_type key_type;
 
  298   if ( _dependencyType == CalciumTypes::TIME_DEPENDENCY )
 
  301     std::cout << 
"-------- time expected : " << expectedDataId.first << std::endl;
 
  302     std::cout << 
"-------- time expected corrected : " << expectedDataId.first*(1.0-_deltaT) << std::endl;
 
  304     af.setMaxValue(key_type(expectedDataId.first*(1.0-_deltaT),0));
 
  323   typename AssocContainer::iterator prev    = storedDatas.begin();
 
  324   typename AssocContainer::iterator current = prev;
 
  325   while ( (current != storedDatas.end()) && !af(current->first)  ) 
 
  328     std::cerr << 
"------- stored time : " << current->first << std::endl;
 
  334   isEqual = af.isEqual();
 
  338   if  ( _dependencyType == CalciumTypes::TIME_DEPENDENCY)  isBounded = af.isBounded();
 
  340   if ( isEqual ) wDataIt1 = current;
 
  342     if (isBounded) wDataIt1 = prev;
 
  344       wDataIt1 = storedDatas.end();
 
  347   std::cout << 
"-------- isDataIdConvenient : isEqual : " << isEqual << 
" , isBounded " << isBounded << std::endl;
 
  350   return isEqual || isBounded;
 
  354 template < 
typename DataManipulator > 
 
  360     _couplingPolicy(couplingPolicy) {};
 
  362   template < 
typename Container,
typename TimeType,
typename TagType >
 
  363   void apply(Container & storedDatas, TimeType time, TagType tag, 
bool before)
 const  
  365       typedef typename Container::iterator   iterator;
 
  366       typedef typename Container::reverse_iterator   riterator;
 
  368       if(_couplingPolicy._dependencyType == CalciumTypes::TIME_DEPENDENCY)
 
  372               iterator it=storedDatas.begin();
 
  373               while(it != storedDatas.end() && it->first.first <= time)
 
  375                   DataManipulator::delete_data(it->second);
 
  376                   storedDatas.erase(it);
 
  377                   it=storedDatas.begin();
 
  382               riterator it=storedDatas.rbegin();
 
  383               while(it != storedDatas.rend() && it->first.first >= time)
 
  385                   DataManipulator::delete_data(it->second);
 
  386                   storedDatas.erase(it->first);
 
  387                   it=storedDatas.rbegin();
 
  395               iterator it=storedDatas.begin();
 
  396               while(it != storedDatas.end() && it->first.second <= tag)
 
  398                   DataManipulator::delete_data(it->second);
 
  399                   storedDatas.erase(it);
 
  400                   it=storedDatas.begin();
 
  405               riterator it=storedDatas.rbegin();
 
  406               while(it != storedDatas.rend() && it->first.second >= tag)
 
  408                   DataManipulator::delete_data(it->second);
 
  409                   storedDatas.erase(it->first);
 
  410                   it=storedDatas.rbegin();
 
  424 template < 
typename DataManipulator > 
 
  430     _couplingPolicy(couplingPolicy) {};
 
  432   template < 
typename Container >
 
  433   void apply(Container & storedDatas, 
 
  434              typename Container::iterator & wDataIt1 )
 const {
 
  436     typedef typename Container::key_type   key_type;
 
  437     typedef typename Container::value_type value_type;
 
  438     typedef typename Container::iterator iterator;
 
  441     std::cout << 
"-------- CalciumCouplingPolicy::eraseDataId, storedDatasSize : " << storedDatas.size() << std::endl;
 
  444     if ( _couplingPolicy._storageLevel == CalciumTypes::UNLIMITED_STORAGE_LEVEL ) 
return;
 
  446     size_t storedDatasSize = storedDatas.size();
 
  447     long   s = storedDatasSize - _couplingPolicy._storageLevel;
 
  449       size_t dist=distance(storedDatas.begin(),wDataIt1);
 
  450       for (
int i=0; i<s; ++i) {
 
  452               DataManipulator::delete_data((*storedDatas.begin()).second);
 
  453               storedDatas.erase(storedDatas.begin());
 
  458                                             << _couplingPolicy._storageLevel << 
 
  459                                             " has just removed the data to send")));
 
  463     std::cout << 
"-------- CalciumCouplingPolicy::eraseDataId, new storedDatasSize : " << storedDatas.size() << std::endl;
 
  478 template < 
typename DataManipulator > 
 
  484     _couplingPolicy(couplingPolicy) {};
 
  486   template < 
typename Container, 
typename DataId >
 
  487   bool apply(Container & storedDatas,
 
  488              const DataId & expectedDataId,
 
  489              typename Container::iterator & wDataIt1 )
 const {
 
  491     typedef typename Container::key_type   key_type;
 
  492     typedef typename Container::value_type value_type;
 
  493     typedef typename Container::iterator   iterator;
 
  497     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor MARK1 ("<< _couplingPolicy._disconnectDirective<<
") --------" << std::endl;
 
  499     if ( (_couplingPolicy._disconnectDirective) == (CalciumTypes::UNDEFINED_DIRECTIVE) ) 
return false;
 
  502     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor MARK2 --------" << std::endl;
 
  506     if ( _couplingPolicy._disconnectDirective == CalciumTypes::CP_ARRET )
 
  508                                            << 
" interrupts all further data reading")));
 
  510     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor MARK3 --------" << std::endl;
 
  516     if ( storedDatas.empty() ) 
 
  518                                           << 
" is active but no data is available.")));
 
  525     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor MARK4  " << expectedDataId <<
" --------" << std::endl;
 
  529     iterator it1 = storedDatas.lower_bound(expectedDataId);
 
  531     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor MARK5  " << std::endl;
 
  532     for (iterator it=storedDatas.begin();it!=storedDatas.end();++it)
 
  533       std::cout <<
" "<<(*it).first ;
 
  534     std::cout <<std::endl;
 
  538     if (it1 == storedDatas.end())
 
  540                                           << 
" is active but the requested dataId is less or equal to the last one received.")));
 
  543     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor MARK6 " << std::endl;
 
  546     wDataIt1 = storedDatas.end();
 
  549     std::cout << 
"-------- CalciumCouplingPolicy::DisconnectProcessor, CP_CONT : " << (*wDataIt1).first << std::endl;