COPASI API  4.16.103
CModelParameterGroup.cpp
Go to the documentation of this file.
1 // Copyright (C) 2011 - 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 #include <map>
7 #include <iostream>
8 
9 #include "CModelParameterGroup.h"
10 
12  CModelParameter(pParent, type),
13  mModelParameters()
14 {}
15 
17  CModelParameterGroup * pParent,
18  const bool & createMissing):
19  CModelParameter(src, pParent),
20  mModelParameters()
21 {
22  assignGroupContent(src, createMissing);
23 }
24 
25 // virtual
27 {
28  clear();
29 }
30 
32  const bool & createMissing)
33 {
34  clear();
35 
36  const_iterator itSrc = src.begin();
37  const_iterator endSrc = src.end();
38 
39  for (; itSrc != endSrc; ++itSrc)
40  {
41  if (createMissing ||
42  (*itSrc)->getCompareResult() != Missing)
43  {
44  copy(**itSrc, createMissing);
45  }
46  }
47 }
48 
49 /**
50  * Copy the existing parameter and add it to the group
51  * @param const CModelParameter & src
52  * @return CModelParameter * pCopy
53  */
55  const bool & createMissing)
56 {
57  CModelParameter * pCopy = NULL;
58 
59  switch (src.getType())
60  {
61  case Compartment:
62  pCopy = new CModelParameterCompartment(*static_cast< const CModelParameterCompartment * >(&src), this);
63  break;
64 
65  case Species:
66  pCopy = new CModelParameterSpecies(*static_cast< const CModelParameterSpecies * >(&src), this);
67  break;
68 
69  case ReactionParameter:
70  pCopy = new CModelParameterReactionParameter(*static_cast< const CModelParameterReactionParameter * >(&src), this);
71  break;
72 
73  case Model:
74  case ModelValue:
75  pCopy = new CModelParameter(src, this);
76  break;
77 
78  case Reaction:
79  case Group:
80  pCopy = new CModelParameterGroup(*static_cast< const CModelParameterGroup * >(&src), this, createMissing);
81  break;
82 
83  default:
84  break;
85  }
86 
87  if (pCopy)
88  {
89  mModelParameters.push_back(pCopy);
90  }
91 
92  return pCopy;
93 }
94 
96 {
97  CModelParameter * pModelParameter = NULL;
98 
99  switch (type)
100  {
101  case Compartment:
102  pModelParameter = new CModelParameterCompartment(this);
103  break;
104 
105  case Species:
106  pModelParameter = new CModelParameterSpecies(this);
107  break;
108 
109  case ReactionParameter:
110  pModelParameter = new CModelParameterReactionParameter(this, type);
111  break;
112 
113  case Model:
114  case ModelValue:
115  pModelParameter = new CModelParameter(this, type);
116  break;
117 
118  case Reaction:
119  case Group:
120  pModelParameter = new CModelParameterGroup(this, type);
121  break;
122 
123  default:
124  return pModelParameter;
125  break;
126  }
127 
128  add(pModelParameter);
129 
130  return pModelParameter;
131 }
132 
134 {
135  mModelParameters.push_back(pModelParameter);
136 }
137 
139 {
140  iterator it = begin();
141  iterator End = end();
142 
143  for (; it != End; ++it)
144  {
145  if (*it == pModelParameter)
146  {
147  mModelParameters.erase(it);
148  break;
149  }
150  }
151 }
152 
153 void CModelParameterGroup::remove(const size_t & index)
154 {
155  if (index < mModelParameters.size())
156  {
157  iterator it = mModelParameters.begin() + index;
158  pdelete(*it);
159 
160  mModelParameters.erase(it);
161  }
162 }
163 
165 {
166  return mModelParameters.size();
167 }
168 
170 {
171  return mModelParameters.begin();
172 }
173 
175 {
176  return mModelParameters.end();
177 }
178 
180 {
181  return mModelParameters.begin();
182 }
183 
185 {
186  return mModelParameters.end();
187 }
188 
190 {
192 
193  iterator it = begin();
194  iterator End = end();
195 
196  for (; it != End; ++it)
197  {
198  (*it)->compile();
199  }
200 }
201 
203 {
204  // Clear existing model parameters.
205  iterator it = begin();
206  iterator End = end();
207 
208  for (; it != End; ++it)
209  {
210  (*it)->setParent(NULL);
211  pdelete(*it);
212  }
213 
214  mModelParameters.clear();
215 }
216 
217 // virtual
219  const CModelParameter::Framework & framework,
220  const bool & createMissing)
221 {
222  // We can only work on reactions, groups or sets.
223  assert(other.getType() == Reaction ||
224  other.getType() == Group ||
225  other.getType() == Set);
226 
227  if (mCompareResult == Missing ||
229  {
230  return mCompareResult;
231  }
232 
234 
235  const CModelParameterGroup * pOther = dynamic_cast< const CModelParameterGroup * >(&other);
236 
237  const_iterator itConst = pOther->begin();
238  const_iterator endConst = pOther->end();
239 
240  std::map< CCopasiObjectName, CModelParameter * > Map;
241 
242  for (; itConst != endConst; ++itConst)
243  {
244  Map[(*itConst)->getCN()] = *itConst;
245  }
246 
247  iterator itThis = begin();
248  iterator endThis = end();
249 
250  for (; itThis != endThis; ++itThis)
251  {
252  std::map< CCopasiObjectName, CModelParameter * >::const_iterator itOther = Map.find((*itThis)->getCN());
253 
254  if (itOther == Map.end())
255  {
256  (*itThis)->setCompareResult(Obsolete);
258 
259  continue;
260  }
261 
262  if ((*itThis)->diff(*itOther->second, framework, createMissing) != Identical)
263  {
265  }
266 
267  Map.erase(itOther->first);
268  }
269 
270  if (createMissing)
271  {
272  std::map< CCopasiObjectName, CModelParameter * >::const_iterator itMissing = Map.begin();
273  std::map< CCopasiObjectName, CModelParameter * >::const_iterator endMissing = Map.end();
274 
275  for (; itMissing != endMissing; ++itMissing)
276  {
277  CModelParameter * pMissing = copy(*itMissing->second, createMissing);
278 
279  if (pMissing != NULL)
280  {
281  pMissing->setCompareResult(Missing);
282  }
283 
285  }
286  }
287  else if (Map.size() > 0)
288  {
289  std::map< CCopasiObjectName, CModelParameter * >::const_iterator itMissing = Map.begin();
290  std::map< CCopasiObjectName, CModelParameter * >::const_iterator endMissing = Map.end();
291 
292  for (; itMissing != endMissing; ++itMissing)
293  {
294  if (itMissing->second->getCompareResult() != CModelParameter::Missing)
295  {
297  break;
298  }
299  }
300  }
301 
302  return mCompareResult;
303 }
304 
305 // virtual
307 {
308  bool success = true;
309 
310  iterator it = begin();
311  iterator End = end();
312 
313  for (; it != End; ++it)
314  {
315  success &= (*it)->updateModel();
316  }
317 
318  return success;
319 }
320 
321 // virtual
322 bool CModelParameterGroup::refreshFromModel(const bool & modifyExistence)
323 {
324  bool success = true;
325 
326  if (modifyExistence)
327  {
329  {
330  delete this;
331 
332  return true;
333  }
334 
336  {
338  }
339  }
340 
341  iterator it = begin();
342  iterator End = end();
343 
344  std::vector< CModelParameter * > ToBeDeleted;
345 
346  for (; it != End; ++it)
347  {
348  if (modifyExistence &&
349  (*it)->getCompareResult() == CModelParameter::Obsolete)
350  {
351  ToBeDeleted.push_back(*it);
352  }
353  else
354  {
355  success &= (*it)->refreshFromModel(modifyExistence);
356  }
357  }
358 
359  std::vector< CModelParameter * >::iterator itDelete = ToBeDeleted.begin();
360  std::vector< CModelParameter * >::iterator endDelete = ToBeDeleted.end();
361 
362  for (; itDelete != endDelete; ++itDelete)
363  {
364  success &= (*itDelete)->refreshFromModel(modifyExistence);
365  }
366 
367  return success;
368 }
369 
371 {
372  CModelParameter * pModelParameter = NULL;
373 
374  const_iterator it = begin();
375  const_iterator End = end();
376 
377  for (; it != End && pModelParameter == NULL; ++it)
378  {
379  if (cn == (*it)->getCN())
380  {
381  pModelParameter = *it;
382  }
383  else if ((*it)->getType() == CModelParameter::Reaction ||
384  (*it)->getType() == CModelParameter::Group ||
385  (*it)->getType() == CModelParameter::Set)
386  {
387  pModelParameter = static_cast< const CModelParameterGroup * >(*it)->getModelParameter(cn);
388  }
389  }
390 
391  return pModelParameter;
392 }
393 
395  const CModelParameter::Type & type) const
396 {
397  CModelParameter * pModelParameter = NULL;
398 
399  const_iterator it = begin();
400  const_iterator End = end();
401 
402  for (; it != End && pModelParameter == NULL; ++it)
403  {
404  if (type == (*it)->getType() &&
405  name == (*it)->getName())
406  {
407  pModelParameter = *it;
408  }
409  else if ((*it)->getType() == CModelParameter::Reaction ||
410  (*it)->getType() == CModelParameter::Group ||
411  (*it)->getType() == CModelParameter::Set)
412  {
413  pModelParameter = static_cast< const CModelParameterGroup * >(*it)->getModelParameter(name, type);
414  }
415  }
416 
417  return pModelParameter;
418 }
419 
420 // virtual
422 {
423  return size();
424 }
425 
426 // virtual
427 const CModelParameter * CModelParameterGroup::getChild(const size_t & index) const
428 {
429  if (index < size())
430  {
431  return mModelParameters[index];
432  }
433 
434  return NULL;
435 }
CModelParameterGroup(CModelParameterGroup *pParent, const CModelParameter::Type &type=CModelParameter::Group)
#define pdelete(p)
Definition: copasi.h:215
virtual const CModelParameter * getChild(const size_t &index) const
std::vector< CModelParameter * >::iterator iterator
CModelParameter * getModelParameter(const std::string &cn) const
void setCompareResult(const CompareResult &compareResult)
std::vector< CModelParameter * > mModelParameters
virtual void compile()
virtual size_t getNumChildren() const
void assignGroupContent(const CModelParameterGroup &src, const bool &createMissing)
CModelParameter * copy(const CModelParameter &src, const bool &createMissing)
CompareResult mCompareResult
void remove(CModelParameter *pModelParameter)
std::vector< CModelParameter * >::const_iterator const_iterator
virtual const CompareResult & diff(const CModelParameter &other, const CModelParameter::Framework &framework, const bool &createMissing=false)
virtual bool refreshFromModel(const bool &modifyExistence)
const Type & getType() const
CModelParameter * add(const CModelParameter::Type &type)