dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
time-dependency.t.cpp
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_TIME_DEPENDENCY_T_CPP
7 #define DYNAMIC_GRAPH_TIME_DEPENDENCY_T_CPP
8 #include <dynamic-graph/time-dependency.h>
9 
10 #undef VP_TEMPLATE_DEBUG_MODE
11 #define VP_TEMPLATE_DEBUG_MODE 0
12 #include <dynamic-graph/debug.h>
13 
14 #define __TIME_DEPENDENCY_INIT(sig, dep) \
15  leader(*sig), dependencies(), updateFromAllChildren(ALL_READY_DEFAULT), \
16  dependencyType(dep), periodTime(PERIOD_TIME_DEFAULT)
17 
18 namespace dynamicgraph {
19 template <class Time>
20 TimeDependency<Time>::TimeDependency(SignalBase<Time> *sig,
21  const DependencyType dep)
22  : __TIME_DEPENDENCY_INIT(sig, dep) {}
23 
24 template <class Time>
25 TimeDependency<Time>::TimeDependency(SignalBase<Time> *sig,
26  const SignalArray_const<Time> &ar,
27  const DependencyType dep)
28  : __TIME_DEPENDENCY_INIT(sig, dep) {
29  addDependencies(ar);
30 
31  return;
32 }
33 
34 /* ------------------------------------------------------------------------ */
35 template <class Time>
36 void TimeDependency<Time>::addDependencies(const SignalArray_const<Time> &ar) {
37  for (unsigned int i = 0; i < ar.getSize(); ++i) {
38  addDependency(ar[i]);
39  }
40 }
41 
42 template <class Time>
43 void TimeDependency<Time>::addDependency(const SignalBase<Time> &sig) {
44  dependencies.push_front(&sig);
45 }
46 
47 template <class Time>
48 void TimeDependency<Time>::removeDependency(const SignalBase<Time> &sig) {
49  dependencies.remove(&sig);
50 }
51 
52 template <class Time> void TimeDependency<Time>::clearDependency() {
53  dependencies.clear();
54 }
55 
56 template <class Time>
57 bool TimeDependency<Time>::needUpdate(const Time &t1) const {
58  dgTDEBUG(15) << "# In {" << leader.getName() << " : (" << leader.getReady()
59  << ") " << t1 << " >? " << leader.getTime() << std::endl;
60 
61  if (leader.getReady()) {
62  dgTDEBUGOUT(15);
63  return true;
64  }
65  if (lastAskForUpdate) {
66  dgTDEBUGOUT(15);
67  return true;
68  }
69 
70  switch (dependencyType) {
71  case ALWAYS_READY: {
72  dgTDEBUGOUT(15);
73  return true;
74  }
75  case BOOL_DEPENDENT:
76  break;
77  case TIME_DEPENDENT: {
78  if (t1 < leader.getTime() + periodTime) {
79  dgTDEBUGOUT(15);
80  return false;
81  }
82  break;
83  }
84  };
85 
86  bool res = updateFromAllChildren;
87  const typename Dependencies::const_iterator itend = dependencies.end();
88  for (typename Dependencies::const_iterator it = dependencies.begin();
89  it != itend; ++it) {
90  const SignalBase<Time> &sig = **it;
91  dgTDEBUG(15) << "Ask update for " << sig << std::endl;
92 
93  if ((sig.getTime() > leader.getTime()) || (sig.needUpdate(t1))) {
94  if (updateFromAllChildren)
95  continue;
96  else {
97  res = true;
98  break;
99  }
100  } else {
101  if (updateFromAllChildren) {
102  res = false;
103  break;
104  } else
105  continue;
106  }
107  }
108  lastAskForUpdate = res;
109 
110  dgTDEBUGOUT(15);
111  return res;
112 }
113 
114 template <class Time>
115 std::ostream &TimeDependency<Time>::writeGraph(std::ostream &os) const {
116  return os;
117 }
118 
119 template <class Time>
120 std::ostream &
121 TimeDependency<Time>::displayDependencies(std::ostream &os, const int depth,
122  std::string space, std::string next1,
123  std::string next2) const {
124  leader.SignalBase<Time>::displayDependencies(os, depth, space, next1, next2)
125  << " (";
126  switch (dependencyType) {
127  case ALWAYS_READY:
128  os << "A";
129  break;
130  case BOOL_DEPENDENT:
131  os << "ready=" << ((leader.getReady()) ? "TRUE" : "FALSE");
132  break;
133  case TIME_DEPENDENT:
134  os << "t=" << leader.getTime() << " (/" << periodTime << ") ";
135  break;
136  };
137  os << ")"; //<<std::endl;
138  {
139  const typename Dependencies::const_iterator itend = dependencies.end();
140  for (typename Dependencies::const_iterator it = dependencies.begin();
141  it != itend; ++it)
142  if (depth != 0) {
143  os << std::endl;
144  std::string ajout = "|";
145  std::string ajout2 = "|";
146  typename Dependencies::const_iterator it2 = it;
147  ++it2;
148  if (it2 == dependencies.end()) {
149  ajout = "`";
150  ajout2 = " ";
151  }
152  (*it)->displayDependencies(os, depth - 1, space + next2 + " ", ajout,
153  ajout2);
154  } else {
155  os << std::endl << space << " `-- ...";
156  break;
157  }
158  }
159  return os;
160 }
161 
162 } // end of namespace dynamicgraph
163 
164 #undef __TIME_DEPENDENCY_INIT
165 #endif