ocra-recipes
Doxygen documentation for the ocra-recipes repository
Variable.cpp
Go to the documentation of this file.
1 #include "ocra/optim/Variable.h"
3 
4 #include <sstream>
5 #include <stdexcept>
6 #include <algorithm>
7 #include <iterator>
8 #include <iostream>
9 
10 
11 namespace
12 {
13  struct get_address
14  {
15  const double* operator()(const double& element) const { return &element; }
16  };
17 
18  struct get_value
19  {
20  double operator()(const double* element) const { return *element; }
21  };
22 }
23 
24 namespace ocra
25 {
26  VariableParenthood::VariableParenthood() throw()
27  : startIndexInParentMap(0)
28  {
29  }
30 
31 
32  // ------------------------------------------------------------
33  // --- VARIABLE -----------------------------------------------
34  // ------------------------------------------------------------
35 
36  Variable::Variable(const std::string& name)
37  : memoryMap_()
38  , name_(name)
39  , timeDerivative_(0x0, false)
40  , timePrimitive_(0x0, false)
41  , value_()
42  , updateValue_(true)
43  {
44  }
45 
47  {
48  if(timePrimitive_.var_ && timePrimitive_.owns_)
49  {
50  delete timePrimitive_.var_;
51  timePrimitive_ = VariablePtr(0x0, false);
52  }
53  else if(timePrimitive_.var_)
54  timePrimitive_.var_->timeDerivative_ = VariablePtr(0x0, false);
55 
56  if(timeDerivative_.var_ && timeDerivative_.owns_)
57  {
58  delete timeDerivative_.var_;
59  timeDerivative_ = VariablePtr(0x0, false);
60  }
61  else if(timeDerivative_.var_)
62  timeDerivative_.var_->timePrimitive_ = VariablePtr(0x0, false);
63  }
64 
65  double Variable::operator[](size_t i) const
66  {
67  return *memoryMap_[i];
68  }
69 
70  double Variable::at(size_t i) const
71  {
72  if (i >= memoryMap_.size())
73  {
74  std::stringstream err;
75  err << "[ocra::Variable::at] index " << i << " out of bound in variable " << name_ << "; size is " << memoryMap_.size();
76  throw std::runtime_error(err.str());
77  }
78  return *memoryMap_[i];
79  }
80 
81  int Variable::getSize() const
82  {
83  ocra_assert(static_cast<int>(memoryMap_.size()) >= 0 && "This is really bad, we have such a huge memory block that an int can't hold its size");
84  return static_cast<int>(memoryMap_.size());
85  }
86 
87  Variable::operator const VectorXd& () const
88  {
89  if(updateValue_)
90  updateValue();
91  return value_;
92  }
93 
94  const VectorXd& Variable::getValue() const
95  {
96  return *this;
97  }
98 
99  void Variable::setValue(const VectorXd& value)
100  {
101  do_setValue(value);
102  updateValue_ = true;
103  propagate<EVT_CHANGE_VALUE>();
104  }
105 
107  {
108  if(!timeDerivative_)
109  createTimeDerivative(name_ + "_dot");
110  return *timeDerivative_.var_;
111  }
112 
114  {
115  if(!timePrimitive_)
116  createTimePrimitive("int_" + name_);
117  return *timePrimitive_.var_;
118  }
119 
121  {
122  return timeDerivative_;
123  }
124 
126  {
127  return timePrimitive_;
128  }
129 
130  void Variable::createTimeDerivative(const std::string& name)
131  {
132  if(timeDerivative_)
133  {
134  std::stringstream err;
135  err << "[ocra::Variable::createTimeDerivative] This variable already has a derivative, named " << timeDerivative_.var_->name_;
136  throw std::runtime_error(err.str());
137  }
138 
139  timeDerivative_.var_ = do_createTimeDerivative(name);
140  timeDerivative_.owns_ = true;
141  ocra_assert(timeDerivative_ && "The derived class violated its contract: do_createTimeDerivative must return non null pointer!");
142 
143  timeDerivative_.var_->timePrimitive_.var_ = this;
144  timeDerivative_.var_->timePrimitive_.owns_ = false;
145  }
146 
147  void Variable::createTimePrimitive(const std::string& name)
148  {
149  if(timePrimitive_)
150  {
151  std::stringstream err;
152  err << "[ocra::Variable::createTimePrimitive] This variable already has a primitive, named " << timePrimitive_.var_->name_;
153  throw std::runtime_error(err.str());
154  }
155 
156  timePrimitive_.var_ = do_createTimePrimitive(name);
157  timePrimitive_.owns_ = true;
158  ocra_assert(timePrimitive_ && "The derived class violated its contract: do_createTimePrimitive must return non null pointer!");
159 
160  timePrimitive_.var_->timeDerivative_.var_ = this;
161  timePrimitive_.var_->timeDerivative_.owns_ = false;
162  }
163 
164  const std::string& Variable::getName() const
165  {
166  return name_;
167  }
168 
170  {
171  return false;
172  }
173 
174 
175  void Variable::getRelativeMappingOf(const Variable& subVariable, std::vector<int>& mapping) const
176  {
177  // we need to remove the const for performing the 'trick' to obtain the permutation
178  // yet we must undo all modifications done on subVariable before the end of the function
179  // having a const subVariable is the intended behavior!
180  Variable& subVar = const_cast<Variable&>(subVariable);
181  Variable& thisVar = const_cast<Variable&>(*this);
182 
183  // we create storage to save the current values for further restoration
184  std::vector<double> subVar_backup, thisVar_backup;
185  std::transform(subVar.memoryMap_.begin(), subVar.memoryMap_.end(), std::back_inserter(subVar_backup), get_value());
186  std::transform(thisVar.memoryMap_.begin(), thisVar.memoryMap_.end(), std::back_inserter(thisVar_backup), get_value());
187 
188  try {
189  double** subVarMemoryMap = const_cast<double**>(&subVar.memoryMap_[0]);
190  double** thisVarMemoryMap = const_cast<double**>(&thisVar.memoryMap_[0]);
191 
192  for(int i = 0; i < subVar.getSize(); ++i)
193  *subVarMemoryMap[i] = -1;
194 
195  for(int i = 0; i < thisVar.getSize(); ++i)
196  *thisVarMemoryMap[i] = static_cast<double>(i);
197 
198  mapping.clear();
199  mapping.reserve(subVar.getSize());
200  for(int i = 0; i < subVar.getSize(); ++i)
201  {
202  int d = static_cast<int>(subVar[i]);
203  if(d < 0)
204  {
205  mapping.clear();
206  break;
207  }
208  mapping.push_back(d);
209  }
210  }
211  catch(...) {}// whatever happens, we have to restore and thus always execute the end of the method! We said 'const'...
212 
213  double** subVarMemoryMap = const_cast<double**>(&subVar.memoryMap_[0]);
214  double** thisVarMemoryMap = const_cast<double**>(&thisVar.memoryMap_[0]);
215 
216  for(int i = 0; i < subVar.getSize(); ++i)
217  *subVarMemoryMap[i] = subVar_backup[i];
218 
219  for(int i = 0; i < thisVar.getSize(); ++i)
220  *thisVarMemoryMap[i] = thisVar_backup[i];
221  }
222 
224  {
225  return do_getNumberOfChildren();
226  }
227 
229  {
230  connect<EVT_CHANGE_VALUE>(parent.getParent(), &CompositeVariable::onChildValueChanged);
231  connect<EVT_RESIZE>(parent.getParent(), &CompositeVariable::onChildValueChanged);
232  connect<EVT_CHANGE_DEPENDENCIES>(parent.getParent(), &CompositeVariable::onChildValueChanged);
233 
234  const parenthood_t* prevChild = parent.getPreviousChild();
235  if(!prevChild)
236  return;
237 
238  parent.getInfo().startIndexInParentMap =
239  prevChild->getInfo().startIndexInParentMap + prevChild->getChild().memoryMap_.size();
240  }
241 
243  {
244  disconnect<EVT_CHANGE_VALUE>(parent.getParent(), &CompositeVariable::onChildValueChanged);
245  disconnect<EVT_RESIZE>(parent.getParent(), &CompositeVariable::onChildValueChanged);
246  disconnect<EVT_CHANGE_DEPENDENCIES>(parent.getParent(), &CompositeVariable::onChildValueChanged);
247 
248  parent.getInfo().startIndexInParentMap = 0;
249  }
250 
251  void Variable::printNode(int depth, std::ostream& os) const
252  {
253  static const int shiftPerDepthLevel = 3;
254  const int numSpaces = depth * shiftPerDepthLevel;
255 
256  for(int i = 0; i < numSpaces; ++i)
257  os << " ";
258 
259  os << name_ << "(" << memoryMap_.size() << ")" << std::endl;
260  }
261 
262  void Variable::insertInMemoryMap(const parenthood_t& child, size_t whereInChild, std::vector<const double*>::const_iterator start, std::vector<const double*>::const_iterator end)
263  {
264  size_t indexInMap = child.getInfo().startIndexInParentMap + whereInChild;
265  memoryMap_.insert(memoryMap_.begin() + indexInMap, start, end);
266 
267  parenthood_t* nextChild = child.getNextChild();
268  while(nextChild)
269  {
270  nextChild->getInfo().startIndexInParentMap += std::distance(start, end);
271  nextChild = nextChild->getNextChild();
272  }
273 
274  for(size_t i = 0; i < getNumParenthoods(); ++i)
275  getParenthood(i).getParent().insertInMemoryMap(getParenthood(i), indexInMap, start, end);
276  }
277 
279  const parenthood_t& child, size_t whereInChild, std::vector<const double*>::const_iterator start, std::vector<const double*>::const_iterator end)
280  {
281  obj.insertInMemoryMap(child, whereInChild, start, end);
282  }
283 
284  void Variable::removeFromMemoryMap(const parenthood_t& child, size_t whereInChild, size_t numElements)
285  {
286  size_t indexInMap = child.getInfo().startIndexInParentMap + whereInChild;
287  memoryMap_.erase(memoryMap_.begin() + indexInMap, memoryMap_.begin() + indexInMap + numElements);
288 
289  parenthood_t* nextChild = child.getNextChild();
290  while(nextChild)
291  {
292  nextChild->getInfo().startIndexInParentMap -= numElements;
293  nextChild = nextChild->getNextChild();
294  }
295 
296  for(size_t i = 0; i < getNumParenthoods(); ++i)
297  getParenthood(i).getParent().removeFromMemoryMap(getParenthood(i), indexInMap, numElements);
298  }
299 
300  void Variable::callRemoveFromMemoryMap(Variable& obj, const parenthood_t& child, size_t whereInChild, size_t numElements)
301  {
302  obj.removeFromMemoryMap(child, whereInChild, numElements);
303  }
304 
305  std::vector<const double*>& Variable::getMemoryMap(Variable& obj)
306  {
307  return obj.memoryMap_;
308  }
309 
310  void Variable::updateValue() const
311  {
312  if(!memoryMap_.size())
313  throw std::runtime_error("[ocra::Variable::updateValue] cannot update value when the size of the variable is 0.");
314 
315  value_.resize( static_cast<int>(memoryMap_.size()) );
316  for (int i = 0; i < static_cast<int>(memoryMap_.size()); ++i)
317  value_[i] = *memoryMap_[i];
318  updateValue_ = false;
319  }
320 
321  void Variable::onChildValueChanged(int timestamp)
322  {
323  updateValue_ = true;
324  }
325 
326 
327  // ------------------------------------------------------------
328  // --- BASE VARIABLE ------------------------------------------
329  // ------------------------------------------------------------
330 
331  BaseVariable::BaseVariable(const std::string& name, size_t size)
332  : Variable(name)
333  , memory_(size)
334  {
335  std::vector<const double*>& memoryMap = getMemoryMap(*this);
336  std::transform(memory_.begin(), memory_.end(), std::back_inserter(memoryMap), get_address());
337  }
338 
340  {
341  return true;
342  }
343 
344  void BaseVariable::resize(size_t newSize)
345  {
346  std::vector<const double*>& memoryMap = getMemoryMap(*this);
347 
348  for(iterator it = parents_begin(); it != parents_end(); ++it)
349  callRemoveFromMemoryMap((*it)->getParent(), **it, 0, memoryMap.size());
350 
351  memory_.resize(newSize);
352  memoryMap.clear();
353  std::transform(memory_.begin(), memory_.end(), std::back_inserter(memoryMap), get_address());
354 
355  for(iterator it = parents_begin(); it != parents_end(); ++it)
356  callInsertInMemoryMap((*it)->getParent(), **it, 0, memoryMap.begin(), memoryMap.end());
357 
358  propagate<EVT_RESIZE>();
359  }
360 
361  void BaseVariable::do_setValue(const VectorXd& value)
362  {
363  if (value.size() != memory_.size())
364  throw std::runtime_error("[ocra::BaseVariable::setValue] Sizes don't match");
365 
366  for(int i = 0; i < value.size(); ++i)
367  memory_[i] = value[i];
368  }
369 
371  {
372  return static_cast<BaseVariable&>(Variable::getTimeDerivative());
373  }
374 
376  {
377  return static_cast<BaseVariable&>(Variable::getTimePrimitive());
378  }
379 
380  int BaseVariable::isAncestorOf(const Variable& /*unused*/) const
381  {
382  return 0;
383  }
384 
385  void BaseVariable::printSubTree(int depth, std::ostream& os) const
386  {
387  printTree_impl(depth, os);
388  }
389 
391  {
392  return new BaseVariable(name, getSize()); // throws bad_alloc if new fails hence we know we won't return 0x0
393  }
394 
396  {
397  return new BaseVariable(name, getSize()); // throws bad_alloc if new fails hence we know we won't return 0x0
398  }
399 
401  {
402  return 0;
403  }
404 
405 
406  // ------------------------------------------------------------
407  // --- COMPOSITE VARIABLE -------------------------------------
408  // ------------------------------------------------------------
409 
410  CompositeVariable::CompositeVariable(const std::string& name)
411  : Variable(name)
412  {
413  }
414 
415  CompositeVariable::CompositeVariable(const std::string& name, Variable& var)
416  : Variable(name)
417  {
418  add(var);
419  }
420 
421  CompositeVariable::CompositeVariable(const std::string& name, Variable& var1, Variable& var2)
422  : Variable(name)
423  {
424  add(var1);
425  add(var2);
426  }
427 
428  CompositeVariable::CompositeVariable(const std::string& name, const std::vector<Variable*>& vars)
429  : Variable(name)
430  {
431  for(size_t i = 0; i < vars.size(); ++i)
432  add(*vars[i]);
433  }
434 
436  {
437  while(children_begin() != children_end())
438  remove((*children_begin())->getChild());
439  }
440 
441  void CompositeVariable::do_setValue(const VectorXd& value)
442  {
443  if (value.size() != getSize())
444  throw std::runtime_error("[ocra::BaseVariable::setValue] Sizes don't match");
445 
446  int start = 0;
447  for(iterator it = children_begin(); it != children_end(); ++it)
448  {
449  const int size = (*it)->getChild().getSize();
450  (*it)->getChild().setValue(value.segment(start, size));
451  start += size;
452  }
453  }
454 
456  {
457  return static_cast<CompositeVariable&>(Variable::getTimeDerivative());
458  }
459 
461  {
462  return static_cast<CompositeVariable&>(Variable::getTimePrimitive());
463  }
464 
466  {
467  return getChildhood(i).getChild();
468  }
469 
471  {
472  return getChildhood(i).getChild();
473  }
474 
476  {
477  if(v.getNumberOfChildren() == 0)
478  {
479  if(!v.isDescendantOf(*this))
480  add(v);
481  }
482  else
483  {
484  CompositeVariable* compo_v = static_cast<CompositeVariable*>(&v);
485  for(CompositeVariable::iterator it = compo_v->children_begin(); it != compo_v->children_end(); ++it)
486  addByMerge((*it)->getChild());
487  }
488 
489  return *this;
490  }
491 
493  {
494  const std::vector<const double*>& childMemoryMap = getMemoryMap(child.getChild());
495  callInsertInMemoryMap(*this, child, 0, childMemoryMap.begin(), childMemoryMap.end());
496  propagate<EVT_CHANGE_DEPENDENCIES>();
497  if (child.getChild().getSize()>0)
498  propagate<EVT_RESIZE>();
499  }
500 
502  {
503  callRemoveFromMemoryMap(*this, child, 0, child.getChild().getSize());
504  propagate<EVT_CHANGE_DEPENDENCIES>();
505  if (child.getChild().getSize()>0)
506  propagate<EVT_RESIZE>();
507  }
508 
510  {
511  return isAncestorOf_impl(var);
512  }
513 
514  void CompositeVariable::printSubTree(int depth, std::ostream& os) const
515  {
516  printTree_impl(depth, os);
517  }
518 
520  {
521  CompositeVariable* d = new CompositeVariable(name);
522  bool exceptionCaught = false;
523  std::runtime_error toRethrow(""); // we want to re-throw the first exception we encounter...
524 
525  for(iterator it = children_begin(); it != children_end(); ++it)
526  {
527  try {
528  Variable& dChild = (*it)->getChild().getTimeDerivative();
529  d->attach(dChild);
530  }
531  catch(std::runtime_error& e) {
532  if(!exceptionCaught)
533  toRethrow = e;
534  exceptionCaught = true;
535  }
536  }
537 
538  // if any exception caught, we'll have some subtrees created, but that's not enough
539  // to consider that the time derivative of the current node is complete so we delete it and throw.
540  // Note that deleting the derivative will automatically detach it from the derivatives of the children,
541  // so the only side-effect is the creation of the derivative of the children.
542  if(exceptionCaught)
543  {
544  delete d;
545  throw toRethrow;
546  }
547 
548  return d;
549  }
550 
552  {
553  CompositeVariable* p = new CompositeVariable(name);
554  bool exceptionCaught = false;
555  std::runtime_error toRethrow(""); // we want to re-throw the first exception we encounter...
556 
557  for(iterator it = children_begin(); it != children_end(); ++it)
558  {
559  try {
560  Variable& pChild = (*it)->getChild().getTimePrimitive();
561  p->attach(pChild);
562  }
563  catch(std::runtime_error& e) {
564  if(!exceptionCaught)
565  toRethrow = e;
566  exceptionCaught = true;
567  }
568  }
569 
570  // see CompositeVariable::do_createTimeDerivative for details...
571  if(exceptionCaught)
572  {
573  delete p;
574  throw toRethrow;
575  }
576 
577  return p;
578  }
579 
581  {
582  CompositeVariable* ptr = this;
583  Variable* childPtr = &child;
584 
585  while(ptr->hasTimePrimitive() && childPtr->hasTimePrimitive())
586  {
587  ptr = &ptr->getTimePrimitive();
588  childPtr = &childPtr->getTimePrimitive();
589  }
590  if(ptr->hasTimePrimitive()) // it means that childPtr has not... which we do not tolerate!
591  throw std::runtime_error("[ocra::CompositeVariable::do_add] The child variable must have at least the same max order of integration as the parent");
592 
593  while(ptr && childPtr)
594  {
595  ptr = ptr->hasTimeDerivative() ? static_cast<CompositeVariable*>(&ptr->getTimeDerivative()) : 0x0;
596  childPtr = childPtr->hasTimeDerivative() ? &childPtr->getTimeDerivative() : 0x0;
597  }
598  if(ptr) // it means childPtr is 0, which we refuse!
599  throw std::runtime_error("[ocra::CompositeVariable::do_add] The child variable must have at least the same max order of differentiation as the parent");
600 
601  ptr = this;
602  childPtr = &child;
603 
604  while(ptr->hasTimePrimitive() && childPtr->hasTimePrimitive())
605  {
606  ptr = &ptr->getTimePrimitive();
607  childPtr = &childPtr->getTimePrimitive();
608  }
609 
610  while(ptr && childPtr)
611  {
612  ptr->attach(*childPtr);
613  ptr = ptr->hasTimeDerivative() ? static_cast<CompositeVariable*>(&ptr->getTimeDerivative()) : 0x0;
614  childPtr = childPtr->hasTimeDerivative() ? &childPtr->getTimeDerivative() : 0x0;
615  }
616 
617  return *this;
618  }
619 
621  {
622  CompositeVariable* ptr = this;
623  Variable* childPtr = &child;
624 
625  while(ptr->hasTimePrimitive() && childPtr->hasTimePrimitive())
626  {
627  ptr = &ptr->getTimePrimitive();
628  childPtr = &childPtr->getTimePrimitive();
629  }
630 
631  while(ptr && childPtr)
632  {
633  ptr->detach(*childPtr);
634  ptr = ptr->hasTimeDerivative() ? static_cast<CompositeVariable*>(&ptr->getTimeDerivative()) : 0x0;
635  childPtr = childPtr->hasTimeDerivative() ? &childPtr->getTimeDerivative() : 0x0;
636  }
637 
638  return *this;
639  }
640 
642  {
643  return getNumChildhoods();
644  }
645 }
646 
647 // cmake:sourcegroup=Variable
Variable * do_createTimePrimitive(const std::string &name)
Definition: Variable.cpp:551
size_t do_getNumberOfChildren() const
Always return 0.
Definition: Variable.cpp:400
int isDescendantOf(const CompositeDerived &node) const
Returns the number of levels that separates the component from a potential parent.
Definition: Composite.h:485
virtual Variable & getTimeDerivative()
Get the time derivative/primitive of the variable.
Definition: Variable.cpp:106
void detach(ComponentDerived &child)
Erases a Parenthood bond between a Composite and a Component.
Definition: Composite.h:712
virtual const std::string & getName() const
Definition: Variable.cpp:164
CompositeVariable(const std::string &name)
Definition: Variable.cpp:410
void do_setValue(const VectorXd &value)
This method will attempt to assign a given value to the memory map.
Definition: Variable.cpp:441
const VectorXd & getValue() const
Definition: Variable.cpp:94
void onAttachedChild(const parenthood_t &child)
Definition: Variable.cpp:492
static std::vector< const double * > & getMemoryMap(Variable &obj)
Definition: Variable.cpp:305
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
void onDetachedParent(const parenthood_t &parent)
Definition: Variable.cpp:242
const_iterator parents_begin() const
Iterator range on the set of parents.
Definition: Composite.h:472
CompositeVariable & getTimePrimitive()
Definition: Variable.cpp:460
void printTree_impl(int depth, std::ostream &os) const
Definition: Composite.h:797
CompositeDerived & getParent() const
Access to parenthood info, parent, child and their ranks, see constructor postconditions and class de...
Definition: Composite.h:221
size_t getNumberOfChildren() const
Returns 0 in BaseVariable and the number of childhoods otherwise.
Definition: Variable.cpp:223
Component< Variable, CompositeVariable, VariableParenthood >::parenthood_t parenthood_t
Definition: Variable.h:116
void do_setValue(const VectorXd &value)
This method will attempt to assign a given value to the memory map.
Definition: Variable.cpp:361
void getRelativeMappingOf(const Variable &subVariable, std::vector< int > &mapping) const
Returns the indexes of a subvariable in the variable.
Definition: Variable.cpp:175
Variable & operator()(size_t i)
Definition: Variable.cpp:465
void onAttachedParent(const parenthood_t &parent)
Definition: Variable.cpp:228
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
size_t getNumParenthoods() const
Basic access to the parents.
Definition: Composite.h:466
static void callRemoveFromMemoryMap(Variable &obj, const parenthood_t &child, size_t whereInChild, size_t numElements)
Definition: Variable.cpp:300
CompositeVariable & getTimeDerivative()
Get the time derivative/primitive of the variable.
Definition: Variable.cpp:455
void attach(ComponentDerived &child)
Attaches a Component to a Composite by simply creating a Parenthood bond between them.
Definition: Composite.h:706
bool hasTimeDerivative() const
Definition: Variable.cpp:120
void createTimeDerivative(const std::string &name)
Creates a time derivative/primitive with a given name.
Definition: Variable.cpp:130
double at(size_t i) const
Definition: Variable.cpp:70
void printSubTree(int depth, std::ostream &os) const
Overload in ComponentDerived and CompositeDerived to simply call printTree_impl().
Definition: Variable.cpp:385
int getSize() const
Definition: Variable.cpp:81
virtual ~Variable()=0
The variable is automatically detached from parents and children at destruction.
Definition: Variable.cpp:46
double operator[](size_t i) const
Definition: Variable.cpp:65
Variable * do_createTimePrimitive(const std::string &name)
Definition: Variable.cpp:395
virtual void do_setValue(const VectorXd &value)=0
This method will attempt to assign a given value to the memory map.
static void callInsertInMemoryMap(Variable &obj, const parenthood_t &child, size_t whereInChild, std::vector< const double * >::const_iterator start, std::vector< const double * >::const_iterator end)
Proxy to the implementation of memory maps update.
Definition: Variable.cpp:278
virtual Variable * do_createTimeDerivative(const std::string &name)=0
These methods must be overloaded in children classes to perform instantiation and return pointers to ...
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
BaseVariable(const std::string &name, size_t size)
Builds a leaf variable, i.e. a variable that is not composed of other variables.
Definition: Variable.cpp:331
void printNode(int depth, std::ostream &os) const
Prints the name of the variables; preprends 3*depth blank spaces.
Definition: Variable.cpp:251
void printSubTree(int depth, std::ostream &os) const
Overload in ComponentDerived and CompositeDerived to simply call printTree_impl().
Definition: Variable.cpp:514
int isAncestorOf(const Variable &var) const
Returns the number of levels that separate the component from a potential child.
Definition: Variable.cpp:509
void createTimePrimitive(const std::string &name)
Definition: Variable.cpp:147
size_t getNumChildhoods() const
Basic access to the children.
Definition: Composite.h:657
void resize(size_t newSize)
Definition: Variable.cpp:344
virtual Variable & getTimePrimitive()
Definition: Variable.cpp:113
bool isBaseVariable() const
Definition: Variable.cpp:339
virtual Variable * do_createTimePrimitive(const std::string &name)=0
CompositeVariable & addByMerge(Variable &v)
Definition: Variable.cpp:475
BaseVariable & getTimePrimitive()
Definition: Variable.cpp:375
CompositeVariable & add(Variable &child)
Attach/detach the child to/from this node.
Definition: Variable.cpp:580
BaseVariable & getTimeDerivative()
Get the time derivative/primitive of the variable.
Definition: Variable.cpp:370
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
void clear()
Detaches all children.
Definition: Variable.cpp:435
ComponentDerived & getChild() const
Definition: Composite.h:224
Variable * do_createTimeDerivative(const std::string &name)
Definition: Variable.cpp:519
void onDetachedChild(const parenthood_t &child)
Definition: Variable.cpp:501
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
Declaration file of the Variable class.
virtual bool isBaseVariable() const
Definition: Variable.cpp:169
double distance(double a, double b)
Definition: QuadProg++.cpp:714
Variable * do_createTimeDerivative(const std::string &name)
Definition: Variable.cpp:390
CompositeVariable & remove(Variable &child)
Definition: Variable.cpp:620
void printTree_impl(int depth, std::ostream &os) const
Definition: Composite.h:725
virtual size_t do_getNumberOfChildren() const =0
Returns 0 in {Wrap|Base}Variable and the number of childhoods otherwise.
Implements a basic variable.
Definition: Variable.h:304
bool hasTimePrimitive() const
Definition: Variable.cpp:125
const_iterator_ children_begin() const
Iterator range on the children.
Definition: Composite.h:663
size_t do_getNumberOfChildren() const
Always return getNumChildhoods.
Definition: Variable.cpp:641
int isAncestorOf(const Variable &var) const
Returns the number of levels that separate the component from a potential child.
Definition: Variable.cpp:380