COPASI API  4.16.103
ListOfModelParameterSets.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 - 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 #include "copasi.h"
7 
8 #include "CExpat.h"
9 #include "CCopasiXMLParser.h"
10 #include "CCopasiXMLInterface.h"
11 
12 #include "model/CModel.h"
14 
15 #define START_ELEMENT -1
16 #define UNKNOWN_ELEMENT -2
17 
18 // Model Parameter Sets
20  SCopasiXMLParserCommon & common):
22  mActiveSet(),
23  mpModelParameterSetElement(NULL)
24 {}
25 
26 // virtual
28 {
29  pdelete(mpModelParameterSetElement);
30 }
31 
32 // virtual
34  const XML_Char **papszAttrs)
35 {
36  mpCurrentHandler = NULL;
37  mCurrentElement = mLastKnownElement;
38 
39  while (mpCurrentHandler == NULL)
40  {
41  mCurrentElement++; /* We should always be on the next element */
42 
43  switch (mCurrentElement)
44  {
45  case ListOfModelParameterSets:
46 
47  if (strcmp(pszName, "ListOfModelParameterSets"))
49  pszName, "ListOfModelParameterSets", mParser.getCurrentLineNumber());
50 
51  mLastKnownElement = ListOfModelParameterSets;
52 
54  mActiveSet = mParser.getAttributeValue("activeSet", papszAttrs, "");
55 
56  return;
57 
58  case ModelParameterSet:
59 
60  if (!strcmp(pszName, "ModelParameterSet"))
61  {
62  /* If we do not have a ModelParameterSet element handler we create one. */
63  if (!mpModelParameterSetElement)
64  mpModelParameterSetElement = new ModelParameterSetElement(mParser, mCommon);
65 
66  mpCurrentHandler = mpModelParameterSetElement;
67  }
68 
69  break;
70 
71  default:
72  mCurrentElement = UNKNOWN_ELEMENT;
73  mpCurrentHandler = &mParser.mUnknownElement;
74  break;
75  }
76  }
77 
78  mParser.pushElementHandler(mpCurrentHandler);
79 
80  if (mpCurrentHandler != &mParser.mUnknownElement)
81  {
82  mLastKnownElement = mCurrentElement;
83  }
84 
85  mParser.onStartElement(pszName, papszAttrs);
86 }
87 
88 // virtual
90 {
91  switch (mCurrentElement)
92  {
93  case ListOfModelParameterSets:
94 
95  if (strcmp(pszName, "ListOfModelParameterSets"))
97  pszName, "ListOfModelParameterSets", mParser.getCurrentLineNumber());
98 
99  mParser.popElementHandler();
100  mCurrentElement = START_ELEMENT;
101  mLastKnownElement = mCurrentElement;
102 
103  /* Tell the parent element we are done. */
104  mParser.onEndElement(pszName);
105  return;
106 
107  break;
108 
109  case ModelParameterSet:
110 
111  if (strcmp(pszName, "ModelParameterSet"))
113  pszName, "ModelParameterSet", mParser.getCurrentLineNumber());
114 
115  {
116  const CModelParameterSet * pModelParameterSet = dynamic_cast< CModelParameterSet * >(mCommon.KeyMap.get(mActiveSet));
117 
118  if (pModelParameterSet != NULL)
119  {
120  size_t Size = CCopasiMessage::size();
121 
122  mCommon.pModel->getModelParameterSet().assignSetContent(*pModelParameterSet, false);
123  delete pModelParameterSet;
124  mActiveSet = "";
125 
126  // Remove error messages created by setExpression as this may fail
127  // due to incomplete model specification at this time.
128  while (CCopasiMessage::size() > Size)
130  }
131  }
132 
133  mCurrentElement = ListOfModelParameterSets;
134  break;
135 
136  case UNKNOWN_ELEMENT:
137  mCurrentElement = mLastKnownElement;
138  break;
139 
140  default:
142  pszName, "???", mParser.getCurrentLineNumber());
143  break;
144  }
145 
146  mLastKnownElement = mCurrentElement;
147 
148  return;
149 }
150 
152  SCopasiXMLParserCommon & common):
154  mKey(),
155  mpModelParameterGroupElement(NULL),
156  mpModelParameterElement(NULL)
157 {}
158 
160 {
161  pdelete(mpModelParameterGroupElement);
162  pdelete(mpModelParameterElement);
163 }
164 
166  const XML_Char **papszAttrs)
167 {
168  const char * Name;
169 
170  mpCurrentHandler = NULL;
171  mCurrentElement = mLastKnownElement;
172 
173  while (mpCurrentHandler == NULL)
174  {
175  mCurrentElement++; /* We should always be on the next element */
176 
177  switch (mCurrentElement)
178  {
179  case ModelParameterSet:
180 
181  if (strcmp(pszName, "ModelParameterSet"))
183  pszName, "ModelParameterSet", mParser.getCurrentLineNumber());
184 
185  mLastKnownElement = ModelParameterSet;
186 
187  // Element specific code.
188  mKey = mParser.getAttributeValue("key", papszAttrs);
189  Name = mParser.getAttributeValue("name", papszAttrs);
190 
191  {
192  CModelParameterSet * pModelParameterSet = new CModelParameterSet(Name);
193  int index = 1;
194 
195  // We need to resolve naming conflicts.
196  while (!mCommon.pModel->getModelParameterSets().add(pModelParameterSet, true))
197  {
198  std::ostringstream NewName;
199  NewName << Name << "[" << index++ << "]";
200  pModelParameterSet->setObjectName(NewName.str());
201  }
202 
203  mCommon.ModelParameterGroupStack.push(pModelParameterSet);
204  mCommon.KeyMap.addFix(mKey, pModelParameterSet);
205  }
206  return;
207 
208  case MiriamAnnotation:
209 
210  if (!strcmp(pszName, "MiriamAnnotation"))
211  {
212  mpCurrentHandler = &mParser.mMiriamAnnotationElement;
213  mLastKnownElement = mCurrentElement;
214  }
215 
216  break;
217 
218  case Comment:
219 
220  if (!strcmp(pszName, "Comment"))
221  {
222  mpCurrentHandler = &mParser.mCommentElement;
223  mLastKnownElement = mCurrentElement;
224  }
225 
226  break;
227 
228  case ListOfUnsupportedAnnotations:
229 
230  if (!strcmp(pszName, "ListOfUnsupportedAnnotations"))
231  mpCurrentHandler = &mParser.mListOfUnsupportedAnnotationsElement;
232 
233  break;
234 
235  case Content:
236 
237  if (!strcmp(pszName, "ModelParameterGroup"))
238  {
239  /* If we do not have an etc element handler we create one. */
240  if (!mpModelParameterGroupElement)
241  mpModelParameterGroupElement = new ModelParameterGroupElement(mParser, mCommon);
242 
243  mpCurrentHandler = mpModelParameterGroupElement;
244  }
245  else if (!strcmp(pszName, "ModelParameter"))
246  {
247  /* If we do not have an etc element handler we create one. */
248  if (!mpModelParameterElement)
249  mpModelParameterElement = new ModelParameterElement(mParser, mCommon);
250 
251  mpCurrentHandler = mpModelParameterElement;
252  }
253 
254  break;
255 
256  default:
257  mCurrentElement = UNKNOWN_ELEMENT;
258  mpCurrentHandler = &mParser.mUnknownElement;
259  break;
260  }
261  }
262 
263  mParser.pushElementHandler(mpCurrentHandler);
264 
265  if (mpCurrentHandler != &mParser.mUnknownElement)
266  {
267  mLastKnownElement = mCurrentElement;
268  }
269 
270  mParser.onStartElement(pszName, papszAttrs);
271 
272  return;
273 }
274 
276 {
277  switch (mCurrentElement)
278  {
279  case ModelParameterSet:
280 
281  if (strcmp(pszName, "ModelParameterSet"))
283  pszName, "ModelParameterSet", mParser.getCurrentLineNumber());
284 
286 
287  mParser.popElementHandler();
288  mLastKnownElement = START_ELEMENT;
289 
290  /* Tell the parent element we are done. */
291  mParser.onEndElement(pszName);
292  break;
293 
294  case MiriamAnnotation:
295 
296  if (strcmp(pszName, "MiriamAnnotation"))
298  pszName, "MiriamAnnotation", mParser.getCurrentLineNumber());
299 
300  {
301  CModelParameterSet * pModelParameterSet = static_cast< CModelParameterSet * >(mCommon.ModelParameterGroupStack.top());
302  pModelParameterSet->setMiriamAnnotation(mCommon.CharacterData, pModelParameterSet->getKey(), mKey);
303  }
304 
305  mCommon.CharacterData = "";
306  break;
307 
308  case Comment:
309 
310  if (strcmp(pszName, "Comment"))
312  pszName, "Comment", mParser.getCurrentLineNumber());
313 
314  static_cast< CModelParameterSet * >(mCommon.ModelParameterGroupStack.top())->setNotes(mCommon.CharacterData);
315  mCommon.CharacterData = "";
316  break;
317 
318  case ListOfUnsupportedAnnotations:
319 
320  if (strcmp(pszName, "ListOfUnsupportedAnnotations"))
322  pszName, "ListOfUnsupportedAnnotations", mParser.getCurrentLineNumber());
323 
324  static_cast< CModelParameterSet * >(mCommon.ModelParameterGroupStack.top())->getUnsupportedAnnotations() =
325  mParser.mListOfUnsupportedAnnotationsElement.getUnsupportedAnnotations();
326 
327  break;
328 
329  case Content:
330 
331  if (strcmp(pszName, "ModelParameterGroup") &&
332  strcmp(pszName, "ModelParameter"))
333  {
335  pszName, "ModelParameterGroup or ModelParameter", mParser.getCurrentLineNumber());
336  }
337 
338  if (mCommon.pCurrentModelParameter != NULL)
339  {
342  }
343 
344  // Content may be repeated therefore we set to the previous element which is ListOfUnkownAnnotations.
345  mCurrentElement = mLastKnownElement = ModelParameterSet;
346  break;
347 
348  case UNKNOWN_ELEMENT:
349  mCurrentElement = mLastKnownElement;
350  break;
351 
352  default:
354  pszName, "???", mParser.getCurrentLineNumber());
355  break;
356  }
357 
358  return;
359 }
360 
362  SCopasiXMLParserCommon & common):
364  mpModelParameterGroupElement(NULL),
365  mpModelParameterElement(NULL)
366 {}
367 
369 {
370  pdelete(mpModelParameterGroupElement);
371  pdelete(mpModelParameterElement);
372 }
373 
375  const XML_Char **papszAttrs)
376 {
377  const char * CN;
378  const char * pType;
380 
381  mpCurrentHandler = NULL;
382  mCurrentElement = mLastKnownElement;
383 
384  while (mpCurrentHandler == NULL)
385  {
386  mCurrentElement++; /* We should always be on the next element */
387 
388  switch (mCurrentElement)
389  {
390  case ModelParameterGroup:
391 
392  if (strcmp(pszName, "ModelParameterGroup"))
394  pszName, "ModelParameterGroup", mParser.getCurrentLineNumber());
395 
396  mLastKnownElement = ModelParameterGroup;
397 
398  // Element specific code.
399  CN = mParser.getAttributeValue("cn", papszAttrs);
400  pType = mParser.getAttributeValue("type", papszAttrs);
402 
403  {
404  CModelParameterGroup * pModelParameterGroup = new CModelParameterGroup(mCommon.ModelParameterGroupStack.top(), Type);
405  pModelParameterGroup->setCN(std::string(CN));
406  mCommon.ModelParameterGroupStack.push(pModelParameterGroup);
407  }
408  return;
409 
410  case Content:
411 
412  if (!strcmp(pszName, "ModelParameterGroup"))
413  {
414  /* If we do not have an etc element handler we create one. */
415  if (!mpModelParameterGroupElement)
416  mpModelParameterGroupElement = new ModelParameterGroupElement(mParser, mCommon);
417 
418  mpCurrentHandler = mpModelParameterGroupElement;
419  }
420  else if (!strcmp(pszName, "ModelParameter"))
421  {
422  /* If we do not have an etc element handler we create one. */
423  if (!mpModelParameterElement)
424  mpModelParameterElement = new ModelParameterElement(mParser, mCommon);
425 
426  mpCurrentHandler = mpModelParameterElement;
427  }
428 
429  break;
430 
431  default:
432  mCurrentElement = UNKNOWN_ELEMENT;
433  mpCurrentHandler = &mParser.mUnknownElement;
434  break;
435  }
436  }
437 
438  mParser.pushElementHandler(mpCurrentHandler);
439 
440  if (mpCurrentHandler != &mParser.mUnknownElement)
441  {
442  mLastKnownElement = mCurrentElement;
443  }
444 
445  mParser.onStartElement(pszName, papszAttrs);
446 
447  return;
448 }
449 
451 {
452  switch (mCurrentElement)
453  {
454  case ModelParameterGroup:
455 
456  if (strcmp(pszName, "ModelParameterGroup"))
458  pszName, "ModelParameterGroup", mParser.getCurrentLineNumber());
459 
462 
463  mParser.popElementHandler();
464  mLastKnownElement = START_ELEMENT;
465 
466  /* Tell the parent element we are done. */
467  mParser.onEndElement(pszName);
468  break;
469 
470  case Content:
471 
472  if (strcmp(pszName, "ModelParameterGroup") &&
473  strcmp(pszName, "ModelParameter"))
474  {
476  pszName, "ModelParameterGroup or ModelParameter", mParser.getCurrentLineNumber());
477  }
478 
479  if (mCommon.pCurrentModelParameter != NULL)
480  {
483  }
484 
485  // Content may be repeated therefore we set to the previous element which is ModelParameterGroup.
486  mCurrentElement = mLastKnownElement = ModelParameterGroup;
487  break;
488 
489  case UNKNOWN_ELEMENT:
490  mCurrentElement = mLastKnownElement;
491  break;
492 
493  default:
495  pszName, "???", mParser.getCurrentLineNumber());
496  break;
497  }
498 
499  return;
500 }
501 
503  SCopasiXMLParserCommon & common):
505 {}
506 
508 {}
509 
511  const XML_Char **papszAttrs)
512 {
513  const char * CN;
514  const char * pValue;
515  C_FLOAT64 Value = std::numeric_limits< C_FLOAT64 >::quiet_NaN();
516  const char * pType;
518  const char * pSimulationType;
519  CModelEntity::Status SimulationType;
520 
521  mpCurrentHandler = NULL;
522  mCurrentElement = mLastKnownElement;
523 
524  while (mpCurrentHandler == NULL)
525  {
526  mCurrentElement++; /* We should always be on the next element */
527 
528  switch (mCurrentElement)
529  {
530  case ModelParameter:
531 
532  if (strcmp(pszName, "ModelParameter"))
534  pszName, "ModelParameter", mParser.getCurrentLineNumber());
535 
536  mLastKnownElement = ModelParameter;
537 
538  // Element specific code.
539  CN = mParser.getAttributeValue("cn", papszAttrs);
540  pValue = mParser.getAttributeValue("value", papszAttrs);
541  pType = mParser.getAttributeValue("type", papszAttrs);
542  pSimulationType = mParser.getAttributeValue("simulationType", papszAttrs);
543 
544  if (pValue != NULL)
545  {
546  Value = CCopasiXMLInterface::DBL(pValue);
547  }
548 
550 
551  switch (Type)
552  {
555  break;
556 
559  break;
560 
563  break;
564 
565  default:
567  break;
568  }
569 
570  mCommon.pCurrentModelParameter->setCN(std::string(CN));
571 
572  SimulationType = toEnum(pSimulationType, CModelEntity::XMLStatus, CModelEntity::FIXED);
574 
576  return;
577 
578  case InitialExpression:
579 
580  if (!strcmp(pszName, "InitialExpression"))
581  mpCurrentHandler = &mParser.mCharacterDataElement;
582 
583  break;
584 
585  default:
586  mCurrentElement = UNKNOWN_ELEMENT;
587  mpCurrentHandler = &mParser.mUnknownElement;
588  break;
589  }
590  }
591 
592  mParser.pushElementHandler(mpCurrentHandler);
593 
594  if (mpCurrentHandler != &mParser.mUnknownElement)
595  {
596  mLastKnownElement = mCurrentElement;
597  }
598 
599  mParser.onStartElement(pszName, papszAttrs);
600 
601  return;
602 }
603 
604 void CCopasiXMLParser::ModelParameterElement::end(const XML_Char *pszName)
605 {
606  switch (mCurrentElement)
607  {
608  case ModelParameter:
609 
610  if (strcmp(pszName, "ModelParameter"))
612  pszName, "ModelParameter", mParser.getCurrentLineNumber());
613 
614  mParser.popElementHandler();
615  mLastKnownElement = START_ELEMENT;
616 
617  /* Tell the parent element we are done. */
618  mParser.onEndElement(pszName);
619  break;
620 
621  case InitialExpression:
622 
623  if (strcmp(pszName, "InitialExpression"))
625  pszName, "InitialExpression", mParser.getCurrentLineNumber());
626 
627  {
628  size_t Size = CCopasiMessage::size();
629 
631 
632  // Remove error messages created by setExpression as this may fail
633  // due to incomplete model specification at this time.
634  while (CCopasiMessage::size() > Size)
636  }
637 
638  mCurrentElement = ModelParameter;
639 
640  break;
641 
642  case UNKNOWN_ELEMENT:
643  mCurrentElement = mLastKnownElement;
644  break;
645 
646  default:
648  pszName, "???", mParser.getCurrentLineNumber());
649  break;
650  }
651 
652  return;
653 }
ListOfModelParameterSetsElement(CCopasiXMLParser &parser, SCopasiXMLParserCommon &common)
SCopasiXMLParserCommon mCommon
const CModelParameterSet & getModelParameterSet() const
Definition: CModel.cpp:1072
#define pdelete(p)
Definition: copasi.h:215
CCopasiObject * get(const std::string &key)
virtual void end(const XML_Char *pszName)
#define MCXML
static const char * TypeNames[]
XML_Parser mParser
Definition: CExpat.h:48
ModelParameterGroupElement(CCopasiXMLParser &parser, SCopasiXMLParserCommon &common)
void setMiriamAnnotation(const std::string &miriamAnnotation, const std::string &newId, const std::string &oldId)
virtual void start(const XML_Char *pszName, const XML_Char **papszAttrs)
#define UNKNOWN_ELEMENT
void setInitialExpression(const std::string &initialExpression)
bool addFix(const std::string &key, CCopasiObject *pObject)
virtual void setValue(const double &value, const Framework &framework)
virtual bool add(const CType &src)
virtual const std::string & getKey() const
#define START_ELEMENT
static size_t size()
const CCopasiVectorN< CModelParameterSet > & getModelParameterSets() const
Definition: CModel.cpp:1066
virtual void start(const XML_Char *pszName, const XML_Char **papszAttrs)
static const char * XMLStatus[]
Definition: CModelValue.h:72
CType toEnum(const char *attribute, const char **enumNames, const CType &enumDefault)
Definition: utility.h:107
std::stack< CModelParameterGroup * > ModelParameterGroupStack
#define C_FLOAT64
Definition: copasi.h:92
ModelParameterSetElement(CCopasiXMLParser &parser, SCopasiXMLParserCommon &common)
static CCopasiMessage getLastMessage()
virtual void setCN(const CCopasiObjectName &cn)
virtual void start(const XML_Char *pszName, const XML_Char **papszAttrs)
virtual void clear()
virtual void start(const XML_Char *pszName, const XML_Char **papszAttrs)
bool setSimulationType(const CModelEntity::Status &simulationType)
bool setObjectName(const std::string &name)
void assignSetContent(const CModelParameterSet &src, const bool &createMissing)
CModelParameter * pCurrentModelParameter
ModelParameterElement(CCopasiXMLParser &parser, SCopasiXMLParserCommon &common)
virtual void end(const XML_Char *pszName)