time-dependency.t.cpp 4.91 KB
Newer Older
1
2
3
4
 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Copyright Projet JRL-Japan, 2007
 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
5
 * File:      TimeDependency.t.cpp
6
 * Project:   DYNAMIC-GRAPH
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Author:    Nicolas Mansard
 *
 * Version control
 * ===============
 *
 *  $Id$
 *
 * Description
 * ============
 *
 *
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/




#ifndef __TIME_DEPENDANCY_TCPP
#define __TIME_DEPENDANCY_TCPP

26
#include <dynamic-graph/time-dependency.h>
27

28
namespace dynamicgraph {
29
30
31

#undef VP_TEMPLATE_DEBUG_MODE
#define VP_TEMPLATE_DEBUG_MODE 0
32
#include <dynamic-graph/debug.h>
33
34
35
36
37
38
39
40
41
42

#define __TIME_DEPENDANCY_INIT(sig,dep)     \
    leader(*sig)                                \
    ,dependancies()                             \
    ,updateFromAllChildren(ALL_READY_DEFAULT)   \
    ,dependancyType(dep)                        \
    ,periodTime(PERIOD_TIME_DEFAULT)
                         

template< class Time >
43
44
TimeDependency<Time>::
TimeDependency( SignalBase<Time> *sig
45
46
47
48
49
		   ,const DependancyType dep )
  :__TIME_DEPENDANCY_INIT(sig,dep)
{}

template< class Time >
50
51
TimeDependency<Time>::
TimeDependency( SignalBase<Time> * sig
52
53
54
55
56
57
58
59
60
61
62
63
		   ,const SignalArray_const<Time>& ar
		   ,const DependancyType dep )
  :__TIME_DEPENDANCY_INIT(sig,dep)
{
  for( unsigned int i=0;i<ar.getSize();++i ) 
    {addDependancy( ar[i] );  }
  
  return ;
}

/* -------------------------------------------------------------------------- */
template< class Time >
64
void TimeDependency<Time>::
65
66
67
68
69
70
addDependancy( const SignalBase<Time>& sig )
{
  dependancies.push_front(&sig); 
}

template< class Time >
71
void TimeDependency<Time>::
72
73
74
75
76
removeDependancy( const SignalBase<Time>& sig )
{
  dependancies.remove(&sig); 
}
template< class Time >
77
void TimeDependency<Time>::
78
79
80
81
82
83
clearDependancy( void )
{
  dependancies.clear(); 
}

template< class Time >
84
bool TimeDependency<Time>::
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
needUpdate( const Time& t1 ) const
{ 
  dgTDEBUG(15) << "# In {" << leader.getName() << " : ("<< leader.getReady()<<") "
		<< t1 << " >? " << leader.getTime() << std::endl;

  if( leader.getReady() ) { dgTDEBUGOUT(15);return true; }
  if( lastAskForUpdate ) { dgTDEBUGOUT(15);return true; }

  switch( dependancyType )
    {
    case ALWAYS_READY: 
      { dgTDEBUGOUT(15);return true; }
    case BOOL_DEPENDANT:  break;
    case TIME_DEPENDANT: 
      {
	if( t1<leader.getTime()+periodTime ) 
	  { dgTDEBUGOUT(15);return false; }
	break;
      }
    };

  bool res = updateFromAllChildren;
  const typename Dependancies::const_iterator itend=dependancies.end();
  for( typename Dependancies::const_iterator it=dependancies.begin();it!=itend;++it )
    {
      const SignalBase<Time> &sig = **it;
      dgTDEBUG(15)<< "Ask update for "<< sig <<std::endl;

      if( (sig.getTime()>leader.getTime()) || (sig.needUpdate(t1)) )
	{ if(updateFromAllChildren) continue; else {res=true; break;} }
      else { if(updateFromAllChildren) {res=false; break;} else continue; }
    }
  lastAskForUpdate = res;

  dgTDEBUGOUT(15);
  return res;
}


template< class Time >
125
std::ostream& TimeDependency<Time>::
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
writeGraph(  std::ostream & os) const
{
  std::string LeaderLocalName;
  std::string LeaderNodeName;
  leader.ExtractNodeAndLocalNames(LeaderLocalName,LeaderNodeName);
  if (dependancies.size()!=0)
    {
      const typename Dependancies::const_iterator itend=dependancies.end();
      for( typename Dependancies::const_iterator it=dependancies.begin();it!=itend;++it )
	{
	  std::string itLocalName,itNodeName;
	  (*it)->ExtractNodeAndLocalNames(itLocalName,itNodeName);
	  os << "\"" << itNodeName << "\" -> \"" << LeaderNodeName << "\"" << std::endl
	     << " [ headlabel = \"" << LeaderLocalName << "\" , taillabel = \"" << itLocalName << "\" ]" << std::endl;
	}
    }
  return os;
}

template< class Time >
146
std::ostream& TimeDependency<Time>::
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
displayDependancies( std::ostream& os,const int depth,
		     std::string space,
		     std::string next1,std::string next2 ) const
{
  leader.SignalBase<Time>::displayDependancies(os,depth,space,next1,next2)<<" (";
  switch( dependancyType )
    {
    case ALWAYS_READY: os<<"A"; break;
    case BOOL_DEPENDANT: os << "ready=" << ((leader.getReady())?"TRUE":"FALSE"); break;
    case TIME_DEPENDANT:  
      os  <<"t="<<leader.getTime() <<" (/"<<periodTime<<") " ; 
      break;
    };
  os<<")"; //<<std::endl;
    {
      const typename Dependancies::const_iterator itend=dependancies.end();
      for( typename Dependancies::const_iterator it=dependancies.begin();it!=itend;++it )
	if( depth!=0 )
	  {
	    os<<std::endl;
	    std::string ajout = "|";
	    std::string ajout2 = "|";
	    typename Dependancies::const_iterator it2=it; it2++;
	    if( it2==dependancies.end() ) { ajout = "`"; ajout2= " "; }
	    (*it)->displayDependancies( os,depth-1,space+next2+"   ",ajout,ajout2 );
	  }
	else 
	  { os<<std::endl<<space<<"   `-- ..."; break; }
    }
  return os;
}

179
} // namespace dynamicgraph
180
181
182
183

#endif /* #ifndef __TIME_DEPENDANCY_TCPP */