COPASI API  4.16.103
CEvent.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "copasi.h"
16 
17 #include <stdio.h>
18 
20 #include "CModel.h"
21 #include "CEvent.h"
22 
25 #include "utilities/utility.h"
27 #include "report/CKeyFactory.h"
29 #include "function/CExpression.h"
30 #include "MIRIAM/CRDFUtilities.h"
31 
32 // The default constructor is intentionally not implemented.
33 // CEventAssignment::CEventAssignment() {}
34 
35 CEventAssignment::CEventAssignment(const std::string & targetKey,
36  const CCopasiContainer * pParent) :
37  CCopasiContainer(targetKey, pParent, "EventAssignment"),
38  mKey(CCopasiRootContainer::getKeyFactory()->add("EventAssignment", this)),
39  mpModel(static_cast<CModel *>(getObjectAncestor("Model"))),
40  mpTarget(NULL),
41  mpExpression(NULL)
42 {
43  if (mpModel != NULL)
44  {
45  mpModel->setCompileFlag(true);
46  }
47 }
48 
50  const CCopasiContainer * pParent):
51  CCopasiContainer(src, pParent),
52  mKey(CCopasiRootContainer::getKeyFactory()->add("EventAssignment", this)),
53  mpModel(static_cast<CModel *>(getObjectAncestor("Model"))),
54  mpTarget(src.mpTarget),
55  mpExpression(NULL)
56 {
57  if (mpModel != NULL)
58  {
59  mpModel->setCompileFlag(true);
60  }
61 
63 }
64 
66 {
68 
69  if (mpModel != NULL)
70  {
71  mpModel->setCompileFlag(true);
72  }
73 }
74 
76 {
77  if (pParent != getObjectParent() &&
78  mpModel != NULL)
79  {
80  mpModel->setCompileFlag(true);
81  }
82 
83  bool success = CCopasiContainer::setObjectParent(pParent);
84  mpModel = static_cast<CModel *>(getObjectAncestor("Model"));
85 
86  if (mpModel != NULL)
87  {
88  mpModel->setCompileFlag(true);
89  }
90 
91  return success;
92 }
93 
94 bool CEventAssignment::compile(std::vector< CCopasiContainer * > listOfContainer)
95 {
97 
98  bool success = true;
99 
100  mpTarget = NULL;
101 
102  CModelEntity * pEntity =
104 
105  // The entity type must not be an ASSIGNMENT
106  if (pEntity != NULL &&
107  pEntity->getStatus() != CModelEntity::ASSIGNMENT)
108  {
109  // We need use the virtual method getValueObject to retrieve the
110  // target value from the model entity
111  mpTarget = pEntity->getValueObject();
112  }
113  else if (pEntity != NULL &&
114  pEntity->getStatus() == CModelEntity::ASSIGNMENT)
115  {
116  CCopasiMessage(CCopasiMessage::WARNING, "Invalid EventAssignment for '%s': an Assignment Rule already exists", pEntity->getObjectName().c_str());
117  }
118  else if (pEntity == NULL)
119  {
120  CCopasiMessage(CCopasiMessage::WARNING, "Invalid EventAssignment for '%s': object does not exist.", getObjectName().c_str());
121  }
122 
123  if (mpTarget == NULL)
124  {
125  success = false;
126  }
127 
128  if (mpExpression != NULL)
129  {
130  success &= mpExpression->compile(listOfContainer);
132  }
133  else
134  {
135  success = false;
136  }
137 
138  return success;
139 }
140 
141 const std::string & CEventAssignment::getKey() const
142 {
143  return mKey;
144 }
145 
147 {
148  return mpTarget;
149 }
150 
151 bool CEventAssignment::setTargetKey(const std::string & targetKey)
152 {
153  if (targetKey != getTargetKey() &&
154  mpModel != NULL)
155  {
156  mpModel->setCompileFlag(true);
157  }
158 
159  return setObjectName(targetKey);
160 }
161 
162 const std::string & CEventAssignment::getTargetKey() const
163 {
164  return getObjectName();
165 }
166 
167 bool CEventAssignment::setExpression(const std::string & expression)
168 {
169  if (mpExpression == NULL)
170  mpExpression = new CExpression("Expression", this);
171 
172  if (mpExpression->getInfix() != expression &&
173  mpModel != NULL)
174  {
175  mpModel->setCompileFlag(true);
176  }
177 
178  return mpExpression->setInfix(expression);
179 }
180 
182 {
183  if (pExpression == mpExpression) return true;
184 
185  if (pExpression == mpExpression) return true;
186 
187  if (pExpression == NULL) return false;
188 
189  if (mpModel != NULL)
190  {
191  mpModel->setCompileFlag(true);
192  }
193 
194  CExpression * pOld = mpExpression;
195  mpExpression = pExpression;
196 
197  mpExpression->setObjectName("Expression");
198  add(mpExpression, true);
199  std::vector< CCopasiContainer * > listOfContainer;
200  listOfContainer.push_back(mpModel);
201 
202  if (mpExpression->compile(listOfContainer))
203  {
204  pdelete(pOld);
205  return true;
206  }
207 
208  // If compile fails we do not take ownership
209  // and we remove the object from the container
210  remove(mpExpression);
212  mpExpression = pOld;
213  return false;
214 }
215 
217 {
218  if (mpExpression == NULL)
219  return "";
220 
222 
223  return mpExpression->getInfix();
224 }
225 
227 {
228  return mpExpression;
229 }
230 
232 {
233  return mpExpression;
234 }
235 
236 //****************************************************************
237 
238 CEvent::CEvent(const std::string & name,
239  const CCopasiContainer * pParent):
240  CCopasiContainer(name, pParent, "Event"),
241  CAnnotation(),
242  mpModel(static_cast<CModel *>(getObjectAncestor("Model"))),
243  mAssignments("ListOfAssignments", this),
244  mDelayAssignment(true),
245  mFireAtInitialTime(false),
246  mPersistentTrigger(false),
247  mpTriggerExpression(NULL),
248  mpDelayExpression(NULL),
249  mpPriorityExpression(NULL),
250  mType(Assignment)
251 {
253 
254  initObjects();
255 }
256 
257 CEvent::CEvent(const CEvent & src,
258  const CCopasiContainer * pParent):
259  CCopasiContainer(src, pParent),
260  CAnnotation(src),
261  mpModel(static_cast<CModel *>(getObjectAncestor("Model"))),
262  mAssignments(src.mAssignments, this),
263  mDelayAssignment(src.mDelayAssignment),
264  mFireAtInitialTime(src.mFireAtInitialTime),
265  mPersistentTrigger(src.mPersistentTrigger),
266  mpTriggerExpression(src.mpTriggerExpression != NULL ? new CExpression(*src.mpTriggerExpression, this) : NULL),
267  mpDelayExpression(src.mpDelayExpression != NULL ? new CExpression(*src.mpDelayExpression, this) : NULL),
268  mpPriorityExpression(src.mpPriorityExpression != NULL ? new CExpression(*src.mpPriorityExpression, this) : NULL),
269  mType(src.mType)
270 {
272 
273  initObjects();
274 
276 }
277 
279 {
284 }
285 
286 // virtual
287 const std::string & CEvent::getKey() const
288 {
289  return CAnnotation::getKey();
290 }
291 
292 // virtual
293 bool CEvent::mustBeDeleted(const CCopasiObject::DataObjectSet & deletedObjects) const
294 {
295  bool MustBeDeleted = false;
296 
297  CCopasiObject::DataObjectSet ChildObjects;
298 
299  if (mpTriggerExpression != NULL)
300  {
301  ChildObjects.insert(mpTriggerExpression);
302  }
303 
304  if (mpDelayExpression != NULL)
305  {
306  ChildObjects.insert(mpDelayExpression);
307  }
308 
309  if (mpPriorityExpression != NULL)
310  {
311  ChildObjects.insert(mpPriorityExpression);
312  }
313 
314  // We need to add all assignment targets and expressions
317 
318  for (; itAssignment != endAssignment; ++itAssignment)
319  {
320  if ((*itAssignment)->getTargetObject() != NULL)
321  {
322  ChildObjects.insert((*itAssignment)->getTargetObject());
323  }
324 
325  if ((*itAssignment)->getExpressionPtr() != NULL)
326  {
327  ChildObjects.insert((*itAssignment)->getExpressionPtr());
328  }
329  }
330 
331  DataObjectSet::const_iterator it = ChildObjects.begin();
332  DataObjectSet::const_iterator end = ChildObjects.end();
333 
334  for (; it != end; ++it)
335  {
336  if ((*it)->mustBeDeleted(deletedObjects))
337  {
338  MustBeDeleted = true;
339  break;
340  }
341  }
342 
343  return MustBeDeleted;
344 }
345 
346 bool CEvent::compile(std::vector< CCopasiContainer * > listOfContainer)
347 {
348  bool success = true;
349 
350  // Clear the old direct dependencies.
352 
353  // Compile the trigger expression
354  if (mpTriggerExpression != NULL)
355  {
356  success &= mpTriggerExpression->compile(listOfContainer);
358  }
359 
360  // Compile the delay expression
361  if (mpDelayExpression != NULL)
362  {
363  success &= mpDelayExpression->compile(listOfContainer);
365  }
366 
367  // Compile the assignments
370 
371  for (; itAssignment != endAssignment; ++itAssignment)
372  {
373  success &= (*itAssignment)->compile(listOfContainer);
374  addDirectDependency(*itAssignment);
375  }
376 
377  return success;
378 }
379 
381 {
382  if (mpModel != NULL)
383  {
384  mpModel->setCompileFlag(true);
385  }
386 }
387 
388 std::ostream & operator<<(std::ostream &os, const CEvent & d)
389 {
390  os << "CEvent: " << d.getObjectName() << std::endl;
391  os << " SBML id: " << d.mSBMLId << std::endl;
392 
393  os << "----CEvent" << std::endl;
394 
395  return os;
396 }
397 
398 void CEvent::setSBMLId(const std::string& id)
399 {
400  this->mSBMLId = id;
401 }
402 
403 const std::string& CEvent::getSBMLId() const
404 {
405  return this->mSBMLId;
406 }
407 
408 void CEvent::setDelayAssignment(const bool & delayAssignment)
409 {
410  if (mDelayAssignment != delayAssignment &&
411  mpModel != NULL)
412  {
413  mpModel->setCompileFlag(true);
414  }
415 
416  mDelayAssignment = delayAssignment;
417 }
418 
419 const bool & CEvent::getDelayAssignment() const
420 {
421  return mDelayAssignment;
422 }
423 
424 void CEvent::setFireAtInitialTime(const bool & fireAtInitialTime)
425 {
426  mFireAtInitialTime = fireAtInitialTime;
427 }
428 
429 const bool & CEvent::getFireAtInitialTime() const
430 {
431  return mFireAtInitialTime;
432 }
433 
434 void CEvent::setPersistentTrigger(const bool & persistentTrigger)
435 {
436  mPersistentTrigger = persistentTrigger;
437 }
438 
439 const bool & CEvent::getPersistentTrigger() const
440 {
441  return mPersistentTrigger;
442 }
443 
445 {
446  if (pParent != getObjectParent() &&
447  mpModel != NULL)
448  {
449  mpModel->setCompileFlag(true);
450  }
451 
452  bool success = CCopasiContainer::setObjectParent(pParent);
453 
454  mpModel = static_cast<CModel *>(getObjectAncestor("Model"));
455 
456  if (mpModel != NULL)
457  {
458  mpModel->setCompileFlag(true);
459  }
460 
461  return success;
462 }
463 
464 std::string CEvent::getObjectDisplayName(bool regular, bool richtext) const
465 {
466  CModel* tmp = dynamic_cast<CModel*>(this->getObjectAncestor("Model"));
467 
468  if (tmp)
469  return "((" + getObjectName() + "))";
470 
471  return CCopasiObject::getObjectDisplayName(regular, richtext);
472 }
473 
474 bool CEvent::setTriggerExpression(const std::string & expression)
475 {
476  if (mpTriggerExpression == NULL)
477  {
478  mpTriggerExpression = new CExpression("TriggerExpression", this);
480  }
481 
482  if (mpTriggerExpression->getInfix() != expression &&
483  mpModel != NULL)
484  {
485  mpModel->setCompileFlag(true);
486  }
487 
488  return mpTriggerExpression->setInfix(expression);
489 }
490 
492 {
493  if (pExpression == mpTriggerExpression) return true;
494 
495  if (pExpression == NULL) return false;
496 
497  if (mpModel != NULL)
498  {
499  mpModel->setCompileFlag(true);
500  }
501 
503  mpTriggerExpression = pExpression;
504 
505  mpTriggerExpression->setObjectName("TriggerExpression");
506  add(mpTriggerExpression, true);
507  std::vector< CCopasiContainer * > listOfContainer;
508  listOfContainer.push_back(mpModel);
509 
510  if (mpTriggerExpression->compile(listOfContainer))
511  {
512  pdelete(pOld);
513  return true;
514  }
515 
516  // If compile fails we do not take ownership
517  // and we remove the object from the container
518  remove(mpTriggerExpression);
520  mpTriggerExpression = pOld;
521  return false;
522 }
523 
524 std::string CEvent::getTriggerExpression() const
525 {
526  if (mpTriggerExpression == NULL)
527  return "";
528 
530 
531  return mpTriggerExpression->getInfix();
532 }
533 
535 {
536  return mpTriggerExpression;
537 }
538 
540 {
541  return mpTriggerExpression;
542 }
543 
544 bool CEvent::setDelayExpression(const std::string & expression)
545 {
546  if (mpDelayExpression == NULL)
547  mpDelayExpression = new CExpression("DelayExpression", this);
548 
549  if (mpDelayExpression->getInfix() != expression &&
550  mpModel != NULL)
551  {
552  mpModel->setCompileFlag(true);
553  }
554 
555  return mpDelayExpression->setInfix(expression);
556 }
557 
559 {
560  if (pExpression == mpDelayExpression) return true;
561 
562  if (pExpression == NULL) return false;
563 
564  if (mpModel != NULL)
565  {
566  mpModel->setCompileFlag(true);
567  }
568 
570  mpDelayExpression = pExpression;
571 
572  mpDelayExpression->setObjectName("DelayExpression");
573  add(mpDelayExpression, true);
574  std::vector< CCopasiContainer * > listOfContainer;
575  listOfContainer.push_back(mpModel);
576 
577  if (mpDelayExpression->compile(listOfContainer))
578  {
579  pdelete(pOld);
580  return true;
581  }
582 
583  // If compile fails we do not take ownership
584  // and we remove the object from the container
585  remove(mpDelayExpression);
587  mpDelayExpression = pOld;
588  return false;
589 }
590 
591 std::string CEvent::getDelayExpression() const
592 {
593  if (mpDelayExpression == NULL)
594  return "";
595 
597 
598  return mpDelayExpression->getInfix();
599 }
600 
602 {
603  return mpDelayExpression;
604 }
605 
607 {
608  return mpDelayExpression;
609 }
610 
611 bool CEvent::setPriorityExpression(const std::string & expression)
612 {
613  if (mpPriorityExpression == NULL)
614  mpPriorityExpression = new CExpression("PriorityExpression", this);
615 
616  if (mpPriorityExpression->getInfix() != expression &&
617  mpModel != NULL)
618  {
619  mpModel->setCompileFlag(true);
620  }
621 
622  return mpPriorityExpression->setInfix(expression);
623 }
624 
626 {
627  if (pExpression == mpPriorityExpression) return true;
628 
629  if (pExpression == NULL) return false;
630 
631  if (mpModel != NULL)
632  {
633  mpModel->setCompileFlag(true);
634  }
635 
637  mpPriorityExpression = pExpression;
638 
639  mpPriorityExpression->setObjectName("PriorityExpression");
640  add(mpPriorityExpression, true);
641  std::vector< CCopasiContainer * > listOfContainer;
642  listOfContainer.push_back(mpModel);
643 
644  if (mpPriorityExpression->compile(listOfContainer))
645  {
646  pdelete(pOld);
647  return true;
648  }
649 
650  // If compile fails we do not take ownership
651  // and we remove the object from the container
652  remove(mpPriorityExpression);
654  mpPriorityExpression = pOld;
655  return false;
656 }
657 
658 std::string CEvent::getPriorityExpression() const
659 {
660  if (mpPriorityExpression == NULL)
661  return "";
662 
664 
665  return mpPriorityExpression->getInfix();
666 }
667 
669 {
670  return mpPriorityExpression;
671 }
672 
674 {
675  return mpPriorityExpression;
676 }
677 
679 {
680  return mAssignments;
681 }
682 
684 {
685  return mAssignments;
686 }
687 
688 void CEvent::deleteAssignment(const std::string & key)
689 {
690  CEventAssignment * pAssignment =
692 
693  if (pAssignment != NULL)
694  {
695  mAssignments.CCopasiVector< CEventAssignment >::remove(pAssignment);
696  }
697 }
698 
700 {
701  return mType;
702 }
703 
704 void CEvent::setType(const CEvent::Type & type)
705 {
706  mType = type;
707 }
Definition: CEvent.h:152
Header file of class CExpression.
virtual bool setObjectParent(const CCopasiContainer *pParent)
CExpression * mpTriggerExpression
Definition: CEvent.h:440
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
Definition: CEvent.cpp:464
CCopasiContainer * getObjectAncestor(const std::string &type) const
bool remove(const std::string &key)
const CCopasiVectorN< CEventAssignment > & getAssignments() const
Definition: CEvent.cpp:678
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
virtual bool setInfix(const std::string &infix)
Definition: CExpression.cpp:63
const bool & getDelayAssignment() const
Definition: CEvent.cpp:419
void clearDirectDependencies()
#define pdelete(p)
Definition: copasi.h:215
void setPersistentTrigger(const bool &persistentTrigger)
Definition: CEvent.cpp:434
CExpression * mpDelayExpression
Definition: CEvent.h:445
const std::string & getObjectName() const
const std::string & getTargetKey() const
Definition: CEvent.cpp:162
bool setDelayExpressionPtr(CExpression *pExpression)
Definition: CEvent.cpp:558
CCopasiObject * get(const std::string &key)
iterator begin()
std::string mSBMLId
Definition: CEvent.h:462
void setDirectDependencies(const DataObjectSet &directDependencies)
const CExpression * getExpressionPtr() const
Definition: CEvent.cpp:226
bool mPersistentTrigger
Definition: CEvent.h:435
bool setExpression(const std::string &expression)
Definition: CEvent.cpp:167
Type
Definition: CEvent.h:155
void deleteAssignment(const std::string &key)
Definition: CEvent.cpp:688
CModel * mpModel
Definition: CEvent.h:139
const std::string & getObjectType() const
std::string getTriggerExpression() const
Definition: CEvent.cpp:524
bool updateInfix()
virtual bool compile(std::vector< CCopasiContainer * > listOfContainer=CCopasiContainer::EmptyList)
Definition: CExpression.cpp:97
bool setPriorityExpressionPtr(CExpression *pExpression)
Definition: CEvent.cpp:625
void setMiriamAnnotation(const std::string &miriamAnnotation, const std::string &newId, const std::string &oldId)
bool setTargetKey(const std::string &targetKey)
Definition: CEvent.cpp:151
void setIsBoolean(const bool &booleanRequired)
Definition: CExpression.cpp:58
Type mType
Definition: CEvent.h:455
CExpression * mpExpression
Definition: CEvent.h:149
const std::string & getMiriamAnnotation() const
std::ostream & operator<<(std::ostream &os, const CEvent &d)
Definition: CEvent.cpp:388
CModel * mpModel
Definition: CEvent.h:412
CEvent(const std::string &name="NoName", const CCopasiContainer *pParent=NULL)
Definition: CEvent.cpp:238
CExpression * getDelayExpressionPtr()
Definition: CEvent.cpp:606
~CEvent()
Definition: CEvent.cpp:278
const Type & getType() const
Definition: CEvent.cpp:699
std::string mKey
Definition: CAnnotation.h:119
const bool & getPersistentTrigger() const
Definition: CEvent.cpp:439
bool mFireAtInitialTime
Definition: CEvent.h:429
iterator end()
void addDirectDependency(const CCopasiObject *pObject)
CCopasiVectorN< CEventAssignment > mAssignments
Definition: CEvent.h:417
virtual bool setObjectParent(const CCopasiContainer *pParent)
Definition: CEvent.cpp:444
const CCopasiObject * mpTarget
Definition: CEvent.h:144
bool setTriggerExpression(const std::string &expression)
Definition: CEvent.cpp:474
virtual const CCopasiObject * getValueObject() const
bool setExpressionPtr(CExpression *pExpression)
Definition: CEvent.cpp:181
const std::string & getKey() const
Definition: CEvent.cpp:141
void setDelayAssignment(const bool &delayCalculation)
Definition: CEvent.cpp:408
virtual bool mustBeDeleted(const DataObjectSet &deletedObjects) const
Definition: CEvent.cpp:293
bool setPriorityExpression(const std::string &expression)
Definition: CEvent.cpp:611
std::string add(const std::string &prefix, CCopasiObject *pObject)
std::string getDelayExpression() const
Definition: CEvent.cpp:591
void initObjects()
Definition: CEvent.cpp:380
void setCompileFlag(bool flag=true)
Definition: CModel.cpp:607
virtual const DataObjectSet & getDirectDependencies(const DataObjectSet &context=DataObjectSet()) const
const CExpression * getTriggerExpressionPtr() const
Definition: CEvent.cpp:534
static CKeyFactory * getKeyFactory()
bool compile(std::vector< CCopasiContainer * > listOfContainer)
Definition: CEvent.cpp:94
CExpression * mpPriorityExpression
Definition: CEvent.h:450
bool setDelayExpression(const std::string &expression)
Definition: CEvent.cpp:544
void setFireAtInitialTime(const bool &fireAtInitialTime)
Definition: CEvent.cpp:424
std::string mKey
Definition: CEvent.h:134
bool mDelayAssignment
Definition: CEvent.h:423
void setType(const Type &type)
Definition: CEvent.cpp:704
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
CEventAssignment(const std::string &targetKey="", const CCopasiContainer *pParent=NULL)
Definition: CEvent.cpp:35
const std::string & getSBMLId() const
Definition: CEvent.cpp:403
const bool & getFireAtInitialTime() const
Definition: CEvent.cpp:429
Definition: CModel.h:50
Header file of class CEvent.
const CModelEntity::Status & getStatus() const
virtual bool setObjectParent(const CCopasiContainer *pParent)
Definition: CEvent.cpp:75
CExpression * getPriorityExpressionPtr()
Definition: CEvent.cpp:668
const CCopasiObject * getTargetObject() const
Definition: CEvent.cpp:146
bool setObjectName(const std::string &name)
std::set< const CCopasiObject * > DataObjectSet
std::string getPriorityExpression() const
Definition: CEvent.cpp:658
void setSBMLId(const std::string &id)
Definition: CEvent.cpp:398
virtual const std::string & getKey() const
Definition: CEvent.cpp:287
bool compile(std::vector< CCopasiContainer * > listOfContainer)
Definition: CEvent.cpp:346
const std::string & getInfix() const
CCopasiContainer * getObjectParent() const
std::string getExpression() const
Definition: CEvent.cpp:216
bool setTriggerExpressionPtr(CExpression *pExpression)
Definition: CEvent.cpp:491
virtual const std::string & getKey() const