1 /**
2  * @file SedListOfSetValues.cpp
3  * @brief Implementation of the SedListOfSetValues class.
4  * @author DEVISER
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSEDML. Please visit http://sed-ml.org for more
8  * information about SED-ML. The latest version of libSEDML can be found on
9  * github: https://github.com/fbergmann/libSEDML/
10  *
11 
12  * Copyright (c) 2013-2019, Frank T. Bergmann
13  * All rights reserved.
14  *
15 
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are met:
18  *
19 
20  * 1. Redistributions of source code must retain the above copyright notice,
21  * this
22  * list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * This library is free software; you can redistribute it and/or modify it
28  * under the terms of the GNU Lesser General Public License as published by the
29  * Free Software Foundation. A copy of the license agreement is provided in the
30  * file named "LICENSE.txt" included with this software distribution and also
31  * available online as http://sbml.org/software/libsbml/license.html
32  * ------------------------------------------------------------------------ -->
33  */
34 #include <sedml/SedListOfSetValues.h>
35 #include <sbml/xml/XMLInputStream.h>
36 #include <sbml/math/MathML.h>
37 
38 
39 using namespace std;
40 
41 
42 
43 LIBSEDML_CPP_NAMESPACE_BEGIN
44 
45 
46 
47 
48 #ifdef __cplusplus
49 
50 
51 /*
52  * Creates a new SedListOfSetValues using the given SED-ML Level and @ p version
53  * values.
54  */
SedListOfSetValues(unsigned int level,unsigned int version)55 SedListOfSetValues::SedListOfSetValues(unsigned int level,
56                                        unsigned int version)
57   : SedListOf(level, version)
58 {
59   setSedNamespacesAndOwn(new SedNamespaces(level, version));
60 }
61 
62 
63 /*
64  * Creates a new SedListOfSetValues using the given SedNamespaces object @p
65  * sedmlns.
66  */
SedListOfSetValues(SedNamespaces * sedmlns)67 SedListOfSetValues::SedListOfSetValues(SedNamespaces *sedmlns)
68   : SedListOf(sedmlns)
69 {
70   setElementNamespace(sedmlns->getURI());
71 }
72 
73 
74 /*
75  * Copy constructor for SedListOfSetValues.
76  */
SedListOfSetValues(const SedListOfSetValues & orig)77 SedListOfSetValues::SedListOfSetValues(const SedListOfSetValues& orig)
78   : SedListOf( orig )
79 {
80 }
81 
82 
83 /*
84  * Assignment operator for SedListOfSetValues.
85  */
86 SedListOfSetValues&
operator =(const SedListOfSetValues & rhs)87 SedListOfSetValues::operator=(const SedListOfSetValues& rhs)
88 {
89   if (&rhs != this)
90   {
91     SedListOf::operator=(rhs);
92   }
93 
94   return *this;
95 }
96 
97 
98 /*
99  * Creates and returns a deep copy of this SedListOfSetValues object.
100  */
101 SedListOfSetValues*
clone() const102 SedListOfSetValues::clone() const
103 {
104   return new SedListOfSetValues(*this);
105 }
106 
107 
108 /*
109  * Destructor for SedListOfSetValues.
110  */
~SedListOfSetValues()111 SedListOfSetValues::~SedListOfSetValues()
112 {
113 }
114 
115 
116 /*
117  * Get a SedSetValue from the SedListOfSetValues.
118  */
119 SedSetValue*
get(unsigned int n)120 SedListOfSetValues::get(unsigned int n)
121 {
122   return static_cast<SedSetValue*>(SedListOf::get(n));
123 }
124 
125 
126 /*
127  * Get a SedSetValue from the SedListOfSetValues.
128  */
129 const SedSetValue*
get(unsigned int n) const130 SedListOfSetValues::get(unsigned int n) const
131 {
132   return static_cast<const SedSetValue*>(SedListOf::get(n));
133 }
134 
135 
136 /*
137  * Get a SedSetValue from the SedListOfSetValues based on its identifier.
138  */
139 SedSetValue*
get(const std::string & sid)140 SedListOfSetValues::get(const std::string& sid)
141 {
142   return const_cast<SedSetValue*>(static_cast<const
143     SedListOfSetValues&>(*this).get(sid));
144 }
145 
146 
147 /*
148  * Get a SedSetValue from the SedListOfSetValues based on its identifier.
149  */
150 const SedSetValue*
get(const std::string & sid) const151 SedListOfSetValues::get(const std::string& sid) const
152 {
153   vector<SedBase*>::const_iterator result;
154   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedSetValue>(sid));
155   return (result == mItems.end()) ? 0 : static_cast <const SedSetValue*>
156     (*result);
157 }
158 
159 
160 /*
161  * Removes the nth SedSetValue from this SedListOfSetValues and returns a
162  * pointer to it.
163  */
164 SedSetValue*
remove(unsigned int n)165 SedListOfSetValues::remove(unsigned int n)
166 {
167   return static_cast<SedSetValue*>(SedListOf::remove(n));
168 }
169 
170 
171 /*
172  * Removes the SedSetValue from this SedListOfSetValues based on its identifier
173  * and returns a pointer to it.
174  */
175 SedSetValue*
remove(const std::string & sid)176 SedListOfSetValues::remove(const std::string& sid)
177 {
178   SedBase* item = NULL;
179   vector<SedBase*>::iterator result;
180 
181   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedSetValue>(sid));
182 
183   if (result != mItems.end())
184   {
185     item = *result;
186     mItems.erase(result);
187   }
188 
189   return static_cast <SedSetValue*> (item);
190 }
191 
192 
193 /*
194  * Adds a copy of the given SedSetValue to this SedListOfSetValues.
195  */
196 int
addTaskChange(const SedSetValue * ssv)197 SedListOfSetValues::addTaskChange(const SedSetValue* ssv)
198 {
199   if (ssv == NULL)
200   {
201     return LIBSEDML_OPERATION_FAILED;
202   }
203   else if (ssv->hasRequiredAttributes() == false)
204   {
205     return LIBSEDML_INVALID_OBJECT;
206   }
207   else if (ssv->hasRequiredElements() == false)
208   {
209     return LIBSEDML_INVALID_OBJECT;
210   }
211   else if (getLevel() != ssv->getLevel())
212   {
213     return LIBSEDML_LEVEL_MISMATCH;
214   }
215   else if (getVersion() != ssv->getVersion())
216   {
217     return LIBSEDML_VERSION_MISMATCH;
218   }
219   else if (matchesRequiredSedNamespacesForAddition(static_cast<const
220     SedBase*>(ssv)) == false)
221   {
222     return LIBSEDML_NAMESPACES_MISMATCH;
223   }
224   else
225   {
226     return append(ssv);
227   }
228 }
229 
230 
231 /*
232  * Get the number of SedSetValue objects in this SedListOfSetValues.
233  */
234 unsigned int
getNumTaskChanges() const235 SedListOfSetValues::getNumTaskChanges() const
236 {
237   return size();
238 }
239 
240 
241 /*
242  * Creates a new SedSetValue object, adds it to this SedListOfSetValues object
243  * and returns the SedSetValue object created.
244  */
245 SedSetValue*
createSetValue()246 SedListOfSetValues::createSetValue()
247 {
248   SedSetValue* ssv = NULL;
249 
250   try
251   {
252     ssv = new SedSetValue(getSedNamespaces());
253   }
254   catch (...)
255   {
256   }
257 
258   if (ssv != NULL)
259   {
260     appendAndOwn(ssv);
261   }
262 
263   return ssv;
264 }
265 
266 
267 /*
268  * Used by SedListOfSetValues::get() to lookup a SedSetValue based on its
269  * ModelReference.
270  */
271 struct SedIdEqMR
272 {
273   const string& id;
274 
SedIdEqMRSedIdEqMR275   SedIdEqMR (const string& id) : id(id) { }
operator ()SedIdEqMR276   bool operator() (SedBase* sb)
277   {
278   return (static_cast<SedSetValue*>(sb)->getModelReference() == id);
279   }
280 };
281 
282 
283 /*
284  * Get a SedSetValue from the SedListOfSetValues based on the ModelReference to
285  * which it refers.
286  */
287 const SedSetValue*
getByModelReference(const std::string & sid) const288 SedListOfSetValues::getByModelReference(const std::string& sid) const
289 {
290   vector<SedBase*>::const_iterator result;
291   result = find_if(mItems.begin(), mItems.end(), SedIdEqMR(sid));
292   return (result == mItems.end()) ? 0 : static_cast <const SedSetValue*>
293     (*result);
294 }
295 
296 
297 /*
298  * Get a SedSetValue from the SedListOfSetValues based on the ModelReference to
299  * which it refers.
300  */
301 SedSetValue*
getByModelReference(const std::string & sid)302 SedListOfSetValues::getByModelReference(const std::string& sid)
303 {
304   return const_cast<SedSetValue*>(static_cast<const
305     SedListOfSetValues&>(*this).getByModelReference(sid));
306 }
307 
308 
309 /*
310  * Used by SedListOfSetValues::get() to lookup a SedSetValue based on its
311  * Range.
312  */
313 struct SedIdEqR
314 {
315   const string& id;
316 
SedIdEqRSedIdEqR317   SedIdEqR (const string& id) : id(id) { }
operator ()SedIdEqR318   bool operator() (SedBase* sb)
319   {
320   return (static_cast<SedSetValue*>(sb)->getRange() == id);
321   }
322 };
323 
324 
325 /*
326  * Get a SedSetValue from the SedListOfSetValues based on the Range to which it
327  * refers.
328  */
329 const SedSetValue*
getByRange(const std::string & sid) const330 SedListOfSetValues::getByRange(const std::string& sid) const
331 {
332   vector<SedBase*>::const_iterator result;
333   result = find_if(mItems.begin(), mItems.end(), SedIdEqR(sid));
334   return (result == mItems.end()) ? 0 : static_cast <const SedSetValue*>
335     (*result);
336 }
337 
338 
339 /*
340  * Get a SedSetValue from the SedListOfSetValues based on the Range to which it
341  * refers.
342  */
343 SedSetValue*
getByRange(const std::string & sid)344 SedListOfSetValues::getByRange(const std::string& sid)
345 {
346   return const_cast<SedSetValue*>(static_cast<const
347     SedListOfSetValues&>(*this).getByRange(sid));
348 }
349 
350 
351 /*
352  * Returns the XML element name of this SedListOfSetValues object.
353  */
354 const std::string&
getElementName() const355 SedListOfSetValues::getElementName() const
356 {
357   static const string name = "listOfChanges";
358   return name;
359 }
360 
361 
362 /*
363  * Returns the libSEDML type code for this SedListOfSetValues object.
364  */
365 int
getTypeCode() const366 SedListOfSetValues::getTypeCode() const
367 {
368   return SEDML_LIST_OF;
369 }
370 
371 
372 /*
373  * Returns the libSEDML type code for the SED-ML objects contained in this
374  * SedListOfSetValues object.
375  */
376 int
getItemTypeCode() const377 SedListOfSetValues::getItemTypeCode() const
378 {
379   return SEDML_TASK_SETVALUE;
380 }
381 
382 
383 
384 /** @cond doxygenLibSEDMLInternal */
385 
386 /*
387  * Creates a new SedSetValue in this SedListOfSetValues
388  */
389 SedBase*
createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream & stream)390 SedListOfSetValues::createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER
391   XMLInputStream& stream)
392 {
393   const std::string& name = stream.peek().getName();
394   SedBase* object = NULL;
395 
396   if (name == "setValue")
397   {
398     object = new SedSetValue(getSedNamespaces());
399     appendAndOwn(object);
400   }
401 
402   return object;
403 }
404 
405 /** @endcond */
406 
407 
408 
409 
410 #endif /* __cplusplus */
411 
412 
413 /*
414  * Get a SedSetValue_t from the SedListOf_t.
415  */
416 LIBSEDML_EXTERN
417 SedSetValue_t*
SedListOfSetValues_getTaskChange(SedListOf_t * slo,unsigned int n)418 SedListOfSetValues_getTaskChange(SedListOf_t* slo, unsigned int n)
419 {
420   if (slo == NULL)
421   {
422     return NULL;
423   }
424 
425   return static_cast <SedListOfSetValues*>(slo)->get(n);
426 }
427 
428 
429 /*
430  * Get a SedSetValue_t from the SedListOf_t based on its identifier.
431  */
432 LIBSEDML_EXTERN
433 SedSetValue_t*
SedListOfSetValues_getById(SedListOf_t * slo,const char * sid)434 SedListOfSetValues_getById(SedListOf_t* slo, const char *sid)
435 {
436   if (slo == NULL)
437   {
438     return NULL;
439   }
440 
441   return (sid != NULL) ? static_cast <SedListOfSetValues*>(slo)->get(sid) :
442     NULL;
443 }
444 
445 
446 /*
447  * Removes the nth SedSetValue_t from this SedListOf_t and returns a pointer to
448  * it.
449  */
450 LIBSEDML_EXTERN
451 SedSetValue_t*
SedListOfSetValues_remove(SedListOf_t * slo,unsigned int n)452 SedListOfSetValues_remove(SedListOf_t* slo, unsigned int n)
453 {
454   if (slo == NULL)
455   {
456     return NULL;
457   }
458 
459   return static_cast <SedListOfSetValues*>(slo)->remove(n);
460 }
461 
462 
463 /*
464  * Removes the SedSetValue_t from this SedListOf_t based on its identifier and
465  * returns a pointer to it.
466  */
467 LIBSEDML_EXTERN
468 SedSetValue_t*
SedListOfSetValues_removeById(SedListOf_t * slo,const char * sid)469 SedListOfSetValues_removeById(SedListOf_t* slo, const char* sid)
470 {
471   if (slo == NULL)
472   {
473     return NULL;
474   }
475 
476   return (sid != NULL) ? static_cast <SedListOfSetValues*>(slo)->remove(sid) :
477     NULL;
478 }
479 
480 
481 
482 
483 LIBSEDML_CPP_NAMESPACE_END
484 
485 
486