COPASI API  4.16.103
CAnnotatedMatrix.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/utilities/CAnnotatedMatrix.cpp,v $
3 // $Revision: 1.37 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/04/20 12:08:24 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 /*!
24  \file CAnnotatedMatrix.cpp
25  \brief Implementation file of class CCopasiArray and CArrayAnnotation
26  */
27 
28 #include <sstream>
29 #include "CAnnotatedMatrix.h"
30 #include "CCopasiVector.h"
31 
32 #include "report/CKeyFactory.h"
35 
36 CArrayAnnotation::CArrayAnnotation(const std::string & name,
37  const CCopasiContainer * pParent,
38  CCopasiAbstractArray * array,
39  const bool & adopt)
40  : CCopasiContainer(name, pParent, "Array" , CCopasiObject::Array),
41  mpArray(array),
42  mDestructArray(adopt),
43  mDefaultMode(OBJECTS)
44 {
45  assert(mpArray);
46 
47  resize();
48 
49  //addObjectReference("Annotated Matrix", *this, CCopasiObject::ValueDbl);
50 }
51 
53 {
54  if (mDestructArray)
56 }
57 
59 {
60  mpArray = a;
61 }
62 
64 {
65  mModes[d] = m;
67 }
68 
70 {
71  mDefaultMode = m;
72 
73  size_t i;
74 
75  for (i = 0; i < mModes.size(); ++i)
76  setMode(i, m);
77 }
78 
80 {
81  return mModes.size();
82 }
83 
85 {
86  assert(d < mCopasiVectors.size());
87  assert((mModes[d] == VECTOR) || (mModes[d] == VECTOR_ON_THE_FLY));
88 
89  mCopasiVectors[d] = v;
90 
91  if (mModes[d] == VECTOR)
93 }
94 
95 void CArrayAnnotation::setAnnotationCN(size_t d, size_t i, const std::string cn)
96 {
97  assert(d < mAnnotationsCN.size());
98  assert(mModes[d] == OBJECTS);
99 
101  assert(i < mAnnotationsCN[d].size());
102 
103  mAnnotationsCN[d][i] = cn;
104 }
105 
106 void CArrayAnnotation::setAnnotationString(size_t d, size_t i, const std::string s)
107 {
108  assert(d < mAnnotationsString.size());
109  assert(mModes[d] == STRINGS);
110 
112  assert(i < mAnnotationsString[d].size());
113 
114  mAnnotationsString[d][i] = s;
115 }
116 
117 const std::vector<CRegisteredObjectName> & CArrayAnnotation::getAnnotationsCN(size_t d) const
118 {
119  assert(d < mModes.size());
120  assert(mModes[d] != STRINGS);
121  assert(mModes[d] != NUMBERS);
122 
123  if (mModes[d] == VECTOR_ON_THE_FLY)
125 
126  return mAnnotationsCN[d];
127 }
128 
129 const std::vector<std::string> & CArrayAnnotation::getAnnotationsString(size_t d, bool display) const
130 {
131  assert(d < mModes.size());
132 
133  //generate CNs (if necessary)
134  if (mModes[d] == VECTOR_ON_THE_FLY)
136 
137  //generate DisplayNames
138  if ((mModes[d] == VECTOR) || (mModes[d] == VECTOR_ON_THE_FLY) || (mModes[d] == OBJECTS))
139  {
140  size_t i, imax = mAnnotationsCN[d].size();
141  mAnnotationsString[d].resize(imax);
142  const CCopasiDataModel* pDataModel = getObjectDataModel();
143  assert(pDataModel != NULL);
144 
145  for (i = 0; i < imax; ++i)
146  {
147  const CCopasiObject * obj = pDataModel->getDataObject(mAnnotationsCN[d][i]);
148 
149  if (obj)
150  mAnnotationsString[d][i] =
151  display ? obj->getObjectDisplayName() : obj->getObjectName();
152  else
153  mAnnotationsString[d][i] = "???";
154  }
155  }
156 
157  //generate Numbers
158  if (mModes[d] == NUMBERS)
159  createNumbers(d);
160 
161  // if the mode is STRINGS do nothing, mAnnotationsString is supposed to contain
162  // the correct strings
163 
164  return mAnnotationsString[d];
165 }
166 
167 const std::string & CArrayAnnotation::getDimensionDescription(size_t d) const
168 {
169  assert(d < mDimensionDescriptions.size());
170  return mDimensionDescriptions[d];
171 }
172 
173 void CArrayAnnotation::setDimensionDescription(size_t d, const std::string & s)
174 {
175  assert(d < mDimensionDescriptions.size());
176  mDimensionDescriptions[d] = s;
177 }
178 
179 const std::string & CArrayAnnotation::getDescription() const
180 {return mDescription;}
181 
182 void CArrayAnnotation::setDescription(const std::string & s)
183 {mDescription = s;}
184 
186  const CCopasiContainer* v) const
187 {
188  if (!v) return false;
189 
190  if (!(v->isVector() || v->isNameVector())) return false;
191 
192  if (d >= mpArray->dimensionality()) return false;
193 
194  //now we know we have a vector. A CCopasiVector[N/S], hopefully, so that the following cast is valid:
195  const CCopasiVector< CCopasiObject > * pVector =
196  reinterpret_cast<const CCopasiVector< CCopasiObject > * >(v);
197 
198  mAnnotationsCN[d].resize(pVector->size());
199  //if (pVector->size() < mAnnotations[d].size()) return false;
200 
201  size_t i;
202 
203  for (i = 0; i < mAnnotationsCN[d].size(); ++i)
204  {
205  if (!(*pVector)[i])
206  return false;
207  else
208  mAnnotationsCN[d][i] = (*pVector)[i]->getCN();
209  }
210 
211  return true;
212 }
213 
214 //private
215 void CArrayAnnotation::createNumbers(size_t /*d*/) const
216 {
217  //TODO
218 }
219 
220 //private
222 {
223  mAnnotationsCN.resize(d);
224  mAnnotationsString.resize(d);
225  mDimensionDescriptions.resize(d);
226  mCopasiVectors.resize(d);
227  mModes.resize(d, mDefaultMode);
228 }
229 
230 //private
232 {
233  if (mModes[d] != VECTOR)
234  {
235  mAnnotationsCN[d].resize(mpArray->size()[d]);
236  mAnnotationsString[d].resize(mpArray->size()[d]);
237  }
238 }
239 
241 {
242  assert(mpArray);
244 
245  size_t i, imax = mpArray->dimensionality();
246 
247  for (i = 0; i < imax; ++i)
249 }
250 
252 {
253  //generate the index string
254  std::string tmp;
255  std::ostringstream indexString;
256  size_t ii = 0;
257 
258  for (ii = 0; ii < index.size(); ++ii)
259  {
260  indexString << "[" << index[ii] << "]";
261  }
262 
263  //handle zero-dimensional arrays
264  if (index.size() == 0)
265  indexString << "[.]";
266 
267  return this->getObject(indexString.str());
268 }
269 
271 {
273  index.push_back(u);
274  index.push_back(v);
275  return addElementReference(index);
276 }
277 
279 {
281  index.push_back(u);
282  return addElementReference(index);
283 }
284 
285 void CArrayAnnotation::appendElementReferences(std::set< const CCopasiObject * > & objects) const
286 {
287  objectMap::const_iterator it = mObjects.begin();
288  objectMap::const_iterator end = mObjects.end();
289 
290  for (; it != end; ++it)
291  if (dynamic_cast<const CArrayElementReference *>(it->second) != NULL)
292  objects.insert(it->second);
293 
294  return;
295 }
296 
298 {
299  if (cn == "")
300  {
301  return this;
302  }
303 
304  if (cn == "Property=DisplayName")
305  {
306  return CCopasiObject::getObject(cn);
307  }
308 
309  //if there are no indices there could still be other children. This can be handled
310  //by the container base class
311  if (cn.getElementName(0, false) == "") //no indices
312  return this->CCopasiContainer::getObject(cn);
313 
314  //so now we know we have indices. So we check if the array element reference
315  //exists, if not we create it. Then getObject() of the element reference is called
316  //with the remainder TODO
317 
318  /*
319  //first get the array indices. At the moment only numerical indices...
320  C_INT32 ii = 0;
321  CCopasiArray::index_type index;
322  while (cn.getElementName(ii, false) != "")
323  {
324  index.push_back(cn.getElementIndex(ii));
325  ++ii;
326  }
327  */
328 
329  //first get the index string
330  std::string tmp;
331  std::string indexString;
332  C_INT32 ii = 0;
333 
334  while ((tmp = cn.getElementName(ii, false)) != "")
335  {
336  indexString += "[" + tmp + "]";
337  ++ii;
338  }
339 
340  const CCopasiObject* pObject = NULL; //this will contain the element reference
341 
342  //if the reference object already exists, its name will be identical to the index
343  std::pair< objectMap::const_iterator, objectMap::const_iterator > range =
344  mObjects.equal_range(indexString);
345 
346  objectMap::const_iterator it = range.first;
347 
348  while (it != range.second && it->second->getObjectType() != "ElementReference") ++it;
349 
350  if (it == range.second) //not found
351  {
352  //create new element reference
353  pObject = new CArrayElementReference(cn, this);
354  }
355  else
356  {
357  pObject = it->second;
358  }
359 
360  if (pObject)
361  return pObject->getObject(cn.getRemainder());
362  else
363  return NULL;
364 
365  /*
366  if (index.size() != dimensionality()) //wrong number of indices for this array
367  return NULL;
368 
369  size_t i;
370  for (i = 0; i < dimensionality(); ++i)
371  if (index[i] >= size()[i]) //out of range
372  return NULL;
373  */
374 
375  //TODO remove element children after resize
376  //TODO check optimization expression widget...
377 }
378 
379 void CArrayAnnotation::print(std::ostream * ostream) const
380 {*ostream << *this;}
381 
382 #define SPC(level) std::string(level, ' ')
383 
384 void CArrayAnnotation::printRecursively(std::ostream & ostream, size_t level,
386  const std::vector<std::vector<std::string> > & display) const
387 {
388  size_t indent = 2 * (dimensionality() - 1 - level);
389 
390  if (level == 0) //only vector
391  {
392  ostream << SPC(indent) << "Rows: " << getDimensionDescription(level) << "\n";
393 
394  size_t imax = size()[0];
395 
396  for (index[0] = 0; index[0] < imax; ++index[0])
397  ostream << SPC(indent) << display[0][index[0]] << "\t" << (*array())[index] << "\n";
398  }
399  else if (level == 1) //matrix
400  {
401  ostream << SPC(indent) << "Rows: " << getDimensionDescription(level - 1) << "\n";
402  ostream << SPC(indent) << "Columns: " << getDimensionDescription(level) << "\n";
403 
404  size_t imax = size()[0];
405  size_t jmax = size()[1];
406  ostream << SPC(indent);
407 
408  for (index[1] = 0; index[1] < jmax; ++index[1])
409  ostream << "\t" << display[1][index[1]];
410 
411  ostream << "\n";
412 
413  for (index[0] = 0; index[0] < imax; ++index[0])
414  {
415  ostream << SPC(indent) << display[0][index[0]];
416 
417  for (index[1] = 0; index[1] < jmax; ++index[1])
418  ostream << "\t" << (*array())[index];
419 
420  ostream << "\n";
421  }
422  }
423  else //dimensionality > 2
424  {
425  size_t i, imax = size()[level];
426 
427  for (i = 0; i < imax; ++i)
428  {
429  ostream << SPC(indent) << getDimensionDescription(level) << ": " << display[level][i] << "\n";
430  index[level] = i;
431  printRecursively(ostream, level - 1, index, display);
432  }
433  }
434 }
435 
436 #undef SPC
437 
438 std::ostream &operator<<(std::ostream &os, const CArrayAnnotation & o)
439 {
440  if (!o.array()) return os;
441 
442  //cache the display names
443  std::vector<std::vector<std::string> > displaynames;
444  size_t i;
445 
446  for (i = 0; i < o.dimensionality(); ++i)
447  displaynames.push_back(o.getAnnotationsString(i));
448 
449  os << o.getObjectName() << std::endl;
450  os << o.getDescription() << std::endl;
451 
452  CCopasiAbstractArray::index_type arraysize = o.array()->size();
453 
454  if (o.dimensionality() == 0)
455  {
456  //only one scalar value
457  os << (*o.array())[arraysize] << std::endl;
458  }
459  else
460  {
461  o.printRecursively(os, o.dimensionality() - 1, arraysize, displaynames);
462  }
463 
464  return os;
465 }
466 
468 {
469  // either the dimension itself ...
470  size_t dim = dimensionality();
471 
472  if (dim == 0) return false;
473 
474  // ... or the size of each dimension should be greater than zero.
475  size_t idx = 0;
476 
477  for (idx = 0; idx < dim; idx++)
478  {
479  if (!size()[idx])
480  return true;
481  }
482 
483  return false;
484 }
485 
486 std::string CArrayAnnotation::getObjectDisplayName(bool regular, bool richtext) const
487 {
488  std::string part;
489 
490  if (getObjectParent() && getObjectParent()->getObjectType() != "Model")
491  part = getObjectParent()->getObjectDisplayName(regular, richtext) + ".";
492 
493  return part + getObjectName() + "[[]]";
494 }
495 
496 // void CArrayAnnotation::printDebugLoop(std::ostream & out, CCopasiAbstractArray::index_type & index, size_t level) const
497 // {
498 // size_t i, imax = mArray->size()[level];
499 // for (i = 0; i < imax; ++i)
500 // {
501 // index[level] = i;
502 //
503 // if (level == dimensionality() - 1)
504 // {
505 // out << (*mArray)[index] << " ";
506 //}
507 // else
508 // {
509 // printDebugLoop(out, index, level + 1);
510 // out << std::endl;
511 //}
512 //}
513 //}
514 //
515 // void CArrayAnnotation::printDebug(std::ostream & out) const
516 // {
517 // out << mDescription << std::endl;
518 // out << " Dimensionality: " << dimensionality() << std::endl;
519 //
520 // size_t i, j;
521 // for (i = 0; i < dimensionality(); ++i)
522 // {
523 // out << " " << i << ": " << mDimensionDescriptions[i] << std::endl;
524 //
525 // //std::vector<std::string> names = const_cast<CArrayAnnotation*>(this)->getAnnotationsDisplay(i);
526 //
527 // for (j = 0; j < mAnnotations[i].size(); ++j)
528 // out << " " << mAnnotations[i][j] /*<< " :" << names[j]*/ << std::endl;
529 //
530 // out << std::endl;
531 //}
532 //
533 // CCopasiAbstractArray::index_type index; index.resize(dimensionality());
534 // printDebugLoop(out, index, 0);
535 //}
CCopasiDataModel * getObjectDataModel()
std::vector< std::string > mDimensionDescriptions
virtual void print(std::ostream *ostream) const
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
#define pdelete(p)
Definition: copasi.h:215
void createNumbers(size_t d) const
const std::string & getDimensionDescription(size_t d) const
bool isNameVector() const
const std::string & getObjectName() const
CCopasiObjectName getRemainder() const
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
const std::vector< std::string > & getAnnotationsString(size_t d, bool display=true) const
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
virtual size_t size() const
virtual const index_type & size() const =0
void resizeOneDimension(size_t d)
void setAnnotationCN(size_t d, size_t i, const std::string cn)
CCopasiAbstractArray * array()
const std::string & getObjectType() const
std::ostream & operator<<(std::ostream &os, const CArrayAnnotation &o)
CCopasiAbstractArray * mpArray
void setDescription(const std::string &s)
std::vector< size_t > index_type
Definition: CCopasiArray.h:34
void reDimensionalize(size_t d)
#define C_INT32
Definition: copasi.h:90
#define SPC(level)
const std::string & getDescription() const
void setAnnotationString(size_t d, size_t i, const std::string s)
std::string mDescription
std::vector< Mode > mModes
virtual ~CArrayAnnotation()
std::vector< std::vector< CRegisteredObjectName > > mAnnotationsCN
void appendElementReferences(std::set< const CCopasiObject * > &objects) const
std::string getElementName(const size_t &pos, const bool &unescape=true) const
void setCopasiVector(size_t d, const CCopasiContainer *v)
std::vector< std::vector< std::string > > mAnnotationsString
void setMode(size_t d, Mode m)
void setDimensionDescription(size_t d, const std::string &s)
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
Header file of class CArrayAnnotation.
bool createAnnotationsCNFromCopasiVector(size_t d, const CCopasiContainer *v) const
bool isVector() const
const CObjectInterface * addElementReference(CCopasiAbstractArray::index_type index) const
std::vector< const CCopasiContainer * > mCopasiVectors
const std::vector< CRegisteredObjectName > & getAnnotationsCN(size_t d) const
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
void setArray(CCopasiAbstractArray *a)
virtual size_t dimensionality() const =0
CCopasiAbstractArray::index_type size() const
size_t dimensionality() const
CCopasiContainer * getObjectParent() const
void printRecursively(std::ostream &ostream, size_t level, CCopasiAbstractArray::index_type &index, const std::vector< std::vector< std::string > > &display) const