COPASI API  4.16.103
CModelValue.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /*!
16  \file CModelValue.cpp
17  \brief Implementation file of class CModelEntity and CModelValue
18  */
19 
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 #include <limits>
24 
25 #include "copasi.h"
26 
27 #include "CModel.h"
28 #include "CModelValue.h"
29 
31 #include "function/CExpression.h"
33 #include "report/CKeyFactory.h"
34 #include "utilities/utility.h"
36 
37 //static
38 const std::string CModelEntity::StatusName[] =
39 {
40  "fixed",
41  "assignment",
42  "reactions",
43  "ode",
44  "time",
45  ""
46 };
47 
48 //static
49 const char * CModelEntity::XMLStatus[] =
50 {
51  "fixed",
52  "assignment",
53  "reactions",
54  "ode",
55  "time",
56  NULL
57 };
58 
59 // the "variable" keyword is used for compatibility reasons. It actually means "this metab is part
60 // of the reaction network, copasi needs to figure out if it is independent, dependent (moieties) or unused."
61 
62 CModelEntity::CModelEntity(const std::string & name,
63  const CCopasiContainer * pParent,
64  const std::string & type,
65  const unsigned C_INT32 & flag):
66  CCopasiContainer(name, pParent, type, (flag | CCopasiObject::Container | CCopasiObject::ValueDbl | CCopasiObject::ModelEntity)),
67  CAnnotation(),
68  mpValue(NULL),
69  mpIValue(NULL),
70  mRate(0.0),
71  mpExpression(NULL),
72  mpInitialExpression(NULL),
73  mStatus(FIXED),
74  mUsed(false),
75  mpModel(NULL)
76 {
78 
79  initObjects();
80 
81  *mpIValue = 1.0;
82  *mpValue = std::numeric_limits<C_FLOAT64>::quiet_NaN();
83 
85 }
86 
88  const CCopasiContainer * pParent):
89  CCopasiContainer(src, pParent),
90  CAnnotation(src),
91  mpValue(NULL),
92  mpIValue(NULL),
93  mRate(src.mRate),
94  mpExpression(src.mpExpression ? new CExpression(*src.mpExpression, this) : NULL),
95  mpInitialExpression(src.mpInitialExpression ? new CExpression(*src.mpInitialExpression, this) : NULL),
96  mStatus(FIXED),
97  mUsed(false),
98  mpModel(NULL)
99 {
101 
102  initObjects();
103 
104  setStatus(src.mStatus);
105 
106  *mpValue = *src.mpValue;
107  *mpIValue = *src.mpIValue;
108 
110 }
111 
113 {
114  if (mpModel)
116 
117  // After the above call we definitely own the data and
118  // therefore must destroy them.
119 
120  pdelete(mpValue);
121  pdelete(mpIValue);
122  // since the expressions now have the model entity as parent, they should
123  // automatically be destroyed be the destructor of CCopasiContainer
124  //pdelete(mpExpression);
125  //pdelete(mpInitialExpression);
126 
128 }
129 
130 // virtual
131 const std::string & CModelEntity::getKey() const
132 {
133  return CAnnotation::getKey();
134 }
135 
136 const C_FLOAT64 & CModelEntity::getValue() const {return *mpValue;}
137 
139 {return *mpIValue;}
140 
142 
144 {
145  bool success = true;
146 
147  std::set< const CCopasiObject * > NoDependencies;
148  std::vector< CCopasiContainer * > listOfContainer;
149  listOfContainer.push_back(mpModel);
150  CCopasiDataModel* pDataModel = NULL;
151 
152  switch (mStatus)
153  {
154  case ASSIGNMENT:
155  success &= mpExpression->compile(listOfContainer);
157 
159  pDataModel = getObjectDataModel();
160  assert(pDataModel != NULL);
162  mpInitialExpression->setObjectName("InitialExpression");
163  add(mpInitialExpression, true);
164  break;
165 
166  case ODE:
168 
169  success &= mpExpression->compile(listOfContainer);
171  break;
172 
173  default:
174  break;
175  }
176 
177  // Here we handle initial expressions for all types.
178  if (mpInitialExpression != NULL &&
179  mpInitialExpression->getInfix() != "")
180  {
181  success &= mpInitialExpression->compile(listOfContainer);
183 
184  // If we have a valid initial expression, we update the initial value.
185  // In case the expression is constant this suffices other are updated lated again.
188  }
189  else
190  {
191  mpIValueReference->setDirectDependencies(NoDependencies);
192  }
193 
194  return success;
195 }
196 
198 {
199  switch (mStatus)
200  {
201  case ASSIGNMENT:
203  break;
204 
205  case ODE:
207  break;
208 
209  default:
210  break;
211  }
212 }
213 
215 {
216  if (mpInitialExpression != NULL &&
217  mpInitialExpression->getInfix() != "")
219 }
220 
221 bool CModelEntity::setExpression(const std::string & expression)
222 {
223  if (isFixed()) return false;
224 
225  if (mpExpression == NULL)
226  mpExpression = new CExpression("Expression", this);
227 
228  if (mpModel)
229  mpModel->setCompileFlag(true);
230 
231  if (!mpExpression->setInfix(expression)) return false;
232 
233  return compile();
234 }
235 
236 std::string CModelEntity::getExpression() const
237 {
238  if (isFixed() || mpExpression == NULL)
239  return "";
240 
242  return mpExpression->getInfix();
243 }
244 
246 {
247  return mpExpression;
248 }
249 
251 {
252  return mpExpression;
253 }
254 
256 {
257  if (isFixed()) return false;
258 
259  if (pExpression == mpExpression) return true;
260 
261  if (pExpression == NULL) return false;
262 
263  if (mpModel != NULL)
264  {
265  mpModel->setCompileFlag(true);
266  }
267 
268  CExpression * pOld = mpExpression;
269  mpExpression = pExpression;
270 
271  mpExpression->setObjectName("Expression");
272  add(mpExpression, true);
273 
274  if (compile())
275  {
276  pdelete(pOld);
277  return true;
278  }
279 
280  // If compile fails we do not take ownership
281  // and we remove the object from the container
282  remove(mpExpression);
284  mpExpression = pOld;
285 
286  return false;
287 }
288 
290 {
291  return mpInitialExpression;
292 }
293 
295 {
296  return mpInitialExpression;
297 }
298 
300 {
301  if (mStatus == ASSIGNMENT) return false;
302 
303  if (pExpression == mpInitialExpression) return true;
304 
305  if (pExpression == NULL) return false;
306 
307  if (mpModel != NULL)
308  {
309  mpModel->setCompileFlag(true);
310  }
311 
313  mpInitialExpression = pExpression;
314 
315  mpInitialExpression->setObjectName("InitialExpression");
316  add(mpInitialExpression, true);
317 
318  if (compile())
319  {
320  pdelete(pOld);
321  return true;
322  }
323 
324  // If compile fails we do not take ownership
325  // and we remove the object from the container
326  remove(mpInitialExpression);
328  mpInitialExpression = pOld;
329 
330  return false;
331 }
332 
333 bool CModelEntity::setInitialExpression(const std::string & expression)
334 {
335  if (mStatus == ASSIGNMENT) return false;
336 
337  if (mpModel)
338  mpModel->setCompileFlag(true);
339 
340  if (mpInitialExpression == NULL)
341  {
342  mpInitialExpression = new CExpression("InitialExpression", this);
343  }
344 
345  if (!mpInitialExpression->setInfix(expression)) return false;
346 
347  return compile();
348 }
349 
351 {
352  if (mStatus == ASSIGNMENT || mpInitialExpression == NULL)
353  return "";
354 
356  return mpInitialExpression->getInfix();
357 }
358 
359 /**
360  * Return rate of production of this entity
361  */
363 {
364  return mRate;
365 }
366 
368 {return mpIValueReference;}
369 
371 {return mpValueReference;}
372 
374 {return mpRateReference;}
375 
376 //***********
377 
379 {
380  *mpValue = value;
381 }
382 
383 void CModelEntity::setInitialValue(const C_FLOAT64 & initialValue)
384 {
385  *mpIValue = initialValue;
386 }
387 
389 {
390  mRate = rate;
391 }
392 
393 // ******************
394 
396 {
397  if (mStatus != status)
398  {
399  if (mpModel != NULL)
400  mpModel->setCompileFlag(true);
401 
402  // An assignment may not have an initial expression.
403  // However, internally we always create one, which need
404  // to be deleted.
405  if (mStatus == ASSIGNMENT)
407 
408  mStatus = status;
409  this->setValuePtr(mpValue);
410 
411  if (mpModel != NULL)
412  mpModel->setCompileFlag(true);
413 
414  std::set< const CCopasiObject * > NoDependencies;
415 
416  setDirectDependencies(NoDependencies);
417  clearRefresh();
418 
419  mpIValueReference->setDirectDependencies(NoDependencies);
420 
421  mpValueReference->setDirectDependencies(NoDependencies);
423 
424  mpRateReference->setDirectDependencies(NoDependencies);
426  CCopasiDataModel* pDataModel = NULL;
427 
428  switch (mStatus)
429  {
430  case ASSIGNMENT:
431 
432  if (mpExpression == NULL)
433  mpExpression = new CExpression("Expression", this);
434 
436  pDataModel = getObjectDataModel();
438  mpInitialExpression->setObjectName("InitialExpression");
439  add(mpInitialExpression, true);
440 
443 
444  mRate = std::numeric_limits<C_FLOAT64>::quiet_NaN();
445 
446  mUsed = true;
447  break;
448 
449  case ODE:
450 
451  if (mpExpression == NULL)
452  mpExpression = new CExpression("Expression", this);
453 
456 
457  mUsed = true;
458  break;
459 
460  case REACTIONS:
462 
463  mUsed = true;
464  break;
465 
466  case TIME:
468 
469  mUsed = true;
470  break;
471 
472  case FIXED:
474 
475  mRate = 0.0;
476 
477  mUsed = false;
478  break;
479  }
480  }
481 }
482 
483 // virtual
485 {
486  return mpValueReference;
487 }
488 
489 // virtual
491 {
492  return const_cast<C_FLOAT64 *>(mpValue);
493 }
494 
496 {
497  C_FLOAT64 Dummy;
498 
501  Dummy,
504 
506  static_cast<CCopasiObjectReference<C_FLOAT64> *>(addObjectReference("InitialValue",
507  Dummy,
511 
514 
516 
517  mpModel = static_cast<CModel *>(getObjectAncestor("Model"));
518 
519  if (mpModel)
520  {
521  mpModel->getStateTemplate().add(this);
522  }
523  else
524  {
525  // This creates the needed values.
526  setInitialValuePtr(NULL);
527  setValuePtr(NULL);
528  }
529 }
530 
532 {
533  mpIValue = pInitialValue;
534 
535  if (!mpIValue) mpIValue = new C_FLOAT64;
536 
538 }
539 
541 {
542  mpValue = pValue;
543 
544  if (!mpValue) mpValue = new C_FLOAT64;
545 
547 }
548 
550 {
552  CModel * pNewModel = static_cast<CModel *>(getObjectAncestor("Model"));
553 
554  if (mpModel == pNewModel) return true;
555 
556  C_FLOAT64 InitialValue = *mpIValue;
557  C_FLOAT64 Value = *mpValue;
558 
559  if (mpModel)
560  {
561  // This allocates new objects for mpIValue and mpValueData
563  }
564 
565  // We can safely remove the currently allocated objects as they
566  // are not part of an CStateTemplate
567  pdelete(mpIValue);
568  pdelete(mpValue);
569 
570  if (pNewModel)
571  {
572  pNewModel->getStateTemplate().add(this);
573  }
574  else
575  {
576  mpValue = new C_FLOAT64;
577  mpIValue = new C_FLOAT64;
578  }
579 
580  mpModel = pNewModel;
581  *mpIValue = InitialValue;
582  *mpValue = Value;
583 
584  return true;
585 }
586 
587 std::set< const CCopasiObject * > CModelEntity::getDeletedObjects() const
588 {
589  std::set< const CCopasiObject * > Deleted;
590 
591  Deleted.insert(this);
592  Deleted.insert(mpIValueReference);
593  Deleted.insert(mpValueReference);
594  Deleted.insert(mpRateReference);
595 
596  return Deleted;
597 }
598 
599 // virtual
601 {
602  bool MustBeDeleted = false;
603 
604  DataObjectSet ChildObjects = getDeletedObjects();
605 
606  DataObjectSet::const_iterator it = ChildObjects.begin();
607  DataObjectSet::const_iterator end = ChildObjects.end();
608 
609  for (; it != end; ++it)
610  {
611  if (*it == this)
612  {
613  if ((*it)->CCopasiObject::mustBeDeleted(deletedObjects))
614  {
615  MustBeDeleted = true;
616  break;
617  }
618 
619  continue;
620  }
621 
622  if ((*it)->mustBeDeleted(deletedObjects))
623  {
624  MustBeDeleted = true;
625  break;
626  }
627  }
628 
629  return MustBeDeleted;
630 }
631 
632 void CModelEntity::setSBMLId(const std::string& id)
633 {
634  this->mSBMLId = id;
635 }
636 
637 const std::string& CModelEntity::getSBMLId() const
638 {
639  return this->mSBMLId;
640 }
641 
642 void CModelEntity::setUsed(const bool & used)
643 {mUsed = used;}
644 
645 const bool & CModelEntity::isUsed() const
646 {return mUsed;}
647 
648 //********************************************************************+
649 
650 CModelValue::CModelValue(const std::string & name,
651  const CCopasiContainer * pParent):
652  CModelEntity(name, pParent, "ModelValue")
653 {
654  initObjects();
655 
657 }
658 
660  const CCopasiContainer * pParent):
661  CModelEntity(src, pParent)
662 {
663  initObjects();
665 }
666 
668 {
670 }
671 
673 {}
674 
675 std::ostream & operator<<(std::ostream &os, const CModelValue & d)
676 {
677  os << " ++++CModelValue: " << d.getObjectName() << std::endl;
678  os << " mValue " << *d.mpValue << " mIValue " << *d.mpIValue << std::endl;
679  os << " mRate " << d.mRate << " mStatus " << d.getStatus() << std::endl;
680  os << " ----CModelValue " << std::endl;
681 
682  return os;
683 }
const CExpression * getExpressionPtr() const
Header file of class CExpression.
CCopasiDataModel * getObjectDataModel()
virtual bool setObjectParent(const CCopasiContainer *pParent)
CCopasiContainer * getObjectAncestor(const std::string &type) const
void remove(CModelEntity *entity)
Definition: CState.cpp:80
virtual bool setInfix(const std::string &infix)
Definition: CExpression.cpp:63
void setRefresh(CType *pType, void(CType::*method)(void))
#define pdelete(p)
Definition: copasi.h:215
Header file of class CModelEntity and CModelValue.
CCopasiObjectReference< C_FLOAT64 > * mpIValueReference
Definition: CModelValue.h:356
CExpression * mpInitialExpression
Definition: CModelValue.h:341
static const std::string StatusName[]
Definition: CModelValue.h:67
const C_FLOAT64 & getRate() const
const std::string & getSBMLId() const
const std::string & getObjectName() const
void add(CModelEntity *entity)
Definition: CState.cpp:63
C_FLOAT64 mRate
Definition: CModelValue.h:331
C_FLOAT64 * mpValue
Definition: CModelValue.h:321
CCopasiObject * getInitialValueReference() const
void setDirectDependencies(const DataObjectSet &directDependencies)
CCopasiObjectReference< C_FLOAT64 > * mpRateReference
Definition: CModelValue.h:358
std::ostream & operator<<(std::ostream &os, const CModelValue &d)
virtual void setValue(const C_FLOAT64 &value)
const std::string & getObjectType() const
bool setInitialExpression(const std::string &expression)
std::string getExpression() const
bool updateInfix()
virtual bool compile(std::vector< CCopasiContainer * > listOfContainer=CCopasiContainer::EmptyList)
Definition: CExpression.cpp:97
void setMiriamAnnotation(const std::string &miriamAnnotation, const std::string &newId, const std::string &oldId)
#define C_INT32
Definition: copasi.h:90
static CExpression * createInitialExpression(const CExpression &expression, const CCopasiDataModel *pDataModel)
const std::string & getMiriamAnnotation() const
C_FLOAT64 * mpIValue
Definition: CModelValue.h:326
void setUpdateMethod(CType *pType, void(CType::*method)(const C_FLOAT64 &))
void initObjects()
void initObjects()
std::string mKey
Definition: CAnnotation.h:119
virtual bool compile()
bool isFixed() const
Definition: CModelValue.h:124
const C_FLOAT64 & getInitialValue() const
#define DESTRUCTOR_TRACE
Definition: copasi.h:206
void addDirectDependency(const CCopasiObject *pObject)
void setRate(const C_FLOAT64 &rate)
virtual const C_FLOAT64 & calcValue()
void setValuePtr(C_FLOAT64 *pValue)
virtual const std::string & getKey() const
virtual void setStatus(const CModelEntity::Status &status)
virtual const CCopasiObject * getValueObject() const
std::string getInitialExpression() const
void setUsed(const bool &used)
virtual void calculate()
std::string mSBMLId
Definition: CModelValue.h:47
bool isUsable() const
virtual void * getValuePointer() const
CCopasiObjectReference< C_FLOAT64 > * mpValueReference
Definition: CModelValue.h:357
std::string add(const std::string &prefix, CCopasiObject *pObject)
const bool & isUsed() const
static const char * XMLStatus[]
Definition: CModelValue.h:72
virtual std::set< const CCopasiObject * > getDeletedObjects() const
virtual void setReference(referenceType &reference)
void setCompileFlag(bool flag=true)
Definition: CModel.cpp:607
long int flag
Definition: f2c.h:52
virtual const DataObjectSet & getDirectDependencies(const DataObjectSet &context=DataObjectSet()) const
virtual bool setObjectParent(const CCopasiContainer *pParent)
const C_FLOAT64 & getValue() const
static CKeyFactory * getKeyFactory()
Status mStatus
Definition: CModelValue.h:347
#define C_FLOAT64
Definition: copasi.h:92
bool setInitialExpressionPtr(CExpression *pExpression)
const CStateTemplate & getStateTemplate() const
Definition: CModel.cpp:1172
virtual bool mustBeDeleted(const DataObjectSet &deletedObjects) const
CExpression * mpExpression
Definition: CModelValue.h:336
void setInitialValuePtr(C_FLOAT64 *pInitialValue)
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
bool setExpressionPtr(CExpression *pExpression)
CModelEntity(const std::string &name="NoName", const CCopasiContainer *pParent=NULL, const std::string &type="ModelEntity", const unsigned C_INT32 &flag=CCopasiObject::Container|CCopasiObject::ValueDbl)
Definition: CModelValue.cpp:62
virtual void setInitialValue(const C_FLOAT64 &initialValue)
Definition: CModel.h:50
const CModelEntity::Status & getStatus() const
bool setExpression(const std::string &expression)
bool setObjectName(const std::string &name)
CModelValue(const std::string &name="NoName", const CCopasiContainer *pParent=NULL)
CCopasiObject * getRateReference() const
std::set< const CCopasiObject * > DataObjectSet
CModel * mpModel
Definition: CModelValue.h:359
void setSBMLId(const std::string &id)
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
const std::string & getInfix() const
const CExpression * getInitialExpressionPtr() const
virtual void refreshInitialValue()
virtual const std::string & getKey() const
#define CONSTRUCTOR_TRACE
Definition: copasi.h:202
CCopasiObject * getValueReference() const