1 /**
2  * @file SedListOfVariables.cpp
3  * @brief Implementation of the SedListOfVariables 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/SedListOfVariables.h>
35 #include <sbml/xml/XMLInputStream.h>
36 
37 #include <sedml/SedVariable.h>
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 SedListOfVariables using the given SED-ML Level and @ p version
53  * values.
54  */
SedListOfVariables(unsigned int level,unsigned int version)55 SedListOfVariables::SedListOfVariables(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 SedListOfVariables using the given SedNamespaces object @p
65  * sedmlns.
66  */
SedListOfVariables(SedNamespaces * sedmlns)67 SedListOfVariables::SedListOfVariables(SedNamespaces *sedmlns)
68   : SedListOf(sedmlns)
69 {
70   setElementNamespace(sedmlns->getURI());
71 }
72 
73 
74 /*
75  * Copy constructor for SedListOfVariables.
76  */
SedListOfVariables(const SedListOfVariables & orig)77 SedListOfVariables::SedListOfVariables(const SedListOfVariables& orig)
78   : SedListOf( orig )
79 {
80 }
81 
82 
83 /*
84  * Assignment operator for SedListOfVariables.
85  */
86 SedListOfVariables&
operator =(const SedListOfVariables & rhs)87 SedListOfVariables::operator=(const SedListOfVariables& 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 SedListOfVariables object.
100  */
101 SedListOfVariables*
clone() const102 SedListOfVariables::clone() const
103 {
104   return new SedListOfVariables(*this);
105 }
106 
107 
108 /*
109  * Destructor for SedListOfVariables.
110  */
~SedListOfVariables()111 SedListOfVariables::~SedListOfVariables()
112 {
113 }
114 
115 
116 /*
117  * Get a SedVariable from the SedListOfVariables.
118  */
119 SedVariable*
get(unsigned int n)120 SedListOfVariables::get(unsigned int n)
121 {
122   return static_cast<SedVariable*>(SedListOf::get(n));
123 }
124 
125 
126 /*
127  * Get a SedVariable from the SedListOfVariables.
128  */
129 const SedVariable*
get(unsigned int n) const130 SedListOfVariables::get(unsigned int n) const
131 {
132   return static_cast<const SedVariable*>(SedListOf::get(n));
133 }
134 
135 
136 /*
137  * Get a SedVariable from the SedListOfVariables based on its identifier.
138  */
139 SedVariable*
get(const std::string & sid)140 SedListOfVariables::get(const std::string& sid)
141 {
142   return const_cast<SedVariable*>(static_cast<const
143     SedListOfVariables&>(*this).get(sid));
144 }
145 
146 
147 /*
148  * Get a SedVariable from the SedListOfVariables based on its identifier.
149  */
150 const SedVariable*
get(const std::string & sid) const151 SedListOfVariables::get(const std::string& sid) const
152 {
153   vector<SedBase*>::const_iterator result;
154   result = find_if(mItems.begin(), mItems.end(), SedIdEq<SedVariable>(sid));
155   return (result == mItems.end()) ? 0 : static_cast <const SedVariable*>
156     (*result);
157 }
158 
159 
160 /*
161  * Removes the nth SedVariable from this SedListOfVariables and returns a
162  * pointer to it.
163  */
164 SedVariable*
remove(unsigned int n)165 SedListOfVariables::remove(unsigned int n)
166 {
167   return static_cast<SedVariable*>(SedListOf::remove(n));
168 }
169 
170 
171 /*
172  * Removes the SedVariable from this SedListOfVariables based on its identifier
173  * and returns a pointer to it.
174  */
175 SedVariable*
remove(const std::string & sid)176 SedListOfVariables::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<SedVariable>(sid));
182 
183   if (result != mItems.end())
184   {
185     item = *result;
186     mItems.erase(result);
187   }
188 
189   return static_cast <SedVariable*> (item);
190 }
191 
192 
193 /*
194  * Adds a copy of the given SedVariable to this SedListOfVariables.
195  */
196 int
addVariable(const SedVariable * sv)197 SedListOfVariables::addVariable(const SedVariable* sv)
198 {
199   if (sv == NULL)
200   {
201     return LIBSEDML_OPERATION_FAILED;
202   }
203   else if (sv->hasRequiredAttributes() == false)
204   {
205     return LIBSEDML_INVALID_OBJECT;
206   }
207   else if (getLevel() != sv->getLevel())
208   {
209     return LIBSEDML_LEVEL_MISMATCH;
210   }
211   else if (getVersion() != sv->getVersion())
212   {
213     return LIBSEDML_VERSION_MISMATCH;
214   }
215   else if (matchesRequiredSedNamespacesForAddition(static_cast<const
216     SedBase*>(sv)) == false)
217   {
218     return LIBSEDML_NAMESPACES_MISMATCH;
219   }
220   else
221   {
222     return append(sv);
223   }
224 }
225 
226 
227 /*
228  * Get the number of SedVariable objects in this SedListOfVariables.
229  */
230 unsigned int
getNumVariables() const231 SedListOfVariables::getNumVariables() const
232 {
233   return size();
234 }
235 
236 
237 /*
238  * Creates a new SedVariable object, adds it to this SedListOfVariables object
239  * and returns the SedVariable object created.
240  */
241 SedVariable*
createVariable()242 SedListOfVariables::createVariable()
243 {
244   SedVariable* sv = NULL;
245 
246   try
247   {
248     sv = new SedVariable(getSedNamespaces());
249   }
250   catch (...)
251   {
252   }
253 
254   if (sv != NULL)
255   {
256     appendAndOwn(sv);
257   }
258 
259   return sv;
260 }
261 
262 
263 /** @cond doxygenLibSEDMLInternal */
264 
265 /*
266  * checks concrete types
267  */
268 bool
isValidTypeForList(SedBase * item)269 SedListOfVariables::isValidTypeForList(SedBase* item)
270 {
271   unsigned int tc = item->getTypeCode();
272 
273   return (tc == SEDML_VARIABLE);
274 }
275 
276 /** @endcond */
277 
278 
279 /*
280  * Used by SedListOfVariables::get() to lookup a SedVariable based on its
281  * TaskReference.
282  */
283 struct SedIdEqTR
284 {
285   const string& id;
286 
SedIdEqTRSedIdEqTR287   SedIdEqTR (const string& id) : id(id) { }
operator ()SedIdEqTR288   bool operator() (SedBase* sb)
289   {
290   return (static_cast<SedVariable*>(sb)->getTaskReference() == id);
291   }
292 };
293 
294 
295 /*
296  * Get a SedVariable from the SedListOfVariables based on the TaskReference to
297  * which it refers.
298  */
299 const SedVariable*
getByTaskReference(const std::string & sid) const300 SedListOfVariables::getByTaskReference(const std::string& sid) const
301 {
302   vector<SedBase*>::const_iterator result;
303   result = find_if(mItems.begin(), mItems.end(), SedIdEqTR(sid));
304   return (result == mItems.end()) ? 0 : static_cast <const SedVariable*>
305     (*result);
306 }
307 
308 
309 /*
310  * Get a SedVariable from the SedListOfVariables based on the TaskReference to
311  * which it refers.
312  */
313 SedVariable*
getByTaskReference(const std::string & sid)314 SedListOfVariables::getByTaskReference(const std::string& sid)
315 {
316   return const_cast<SedVariable*>(static_cast<const
317     SedListOfVariables&>(*this).getByTaskReference(sid));
318 }
319 
320 
321 /*
322  * Used by SedListOfVariables::get() to lookup a SedVariable based on its
323  * ModelReference.
324  */
325 struct SedIdEqMR
326 {
327   const string& id;
328 
SedIdEqMRSedIdEqMR329   SedIdEqMR (const string& id) : id(id) { }
operator ()SedIdEqMR330   bool operator() (SedBase* sb)
331   {
332   return (static_cast<SedVariable*>(sb)->getModelReference() == id);
333   }
334 };
335 
336 
337 /*
338  * Get a SedVariable from the SedListOfVariables based on the ModelReference to
339  * which it refers.
340  */
341 const SedVariable*
getByModelReference(const std::string & sid) const342 SedListOfVariables::getByModelReference(const std::string& sid) const
343 {
344   vector<SedBase*>::const_iterator result;
345   result = find_if(mItems.begin(), mItems.end(), SedIdEqMR(sid));
346   return (result == mItems.end()) ? 0 : static_cast <const SedVariable*>
347     (*result);
348 }
349 
350 
351 /*
352  * Get a SedVariable from the SedListOfVariables based on the ModelReference to
353  * which it refers.
354  */
355 SedVariable*
getByModelReference(const std::string & sid)356 SedListOfVariables::getByModelReference(const std::string& sid)
357 {
358   return const_cast<SedVariable*>(static_cast<const
359     SedListOfVariables&>(*this).getByModelReference(sid));
360 }
361 
362 
363 /*
364  * Returns the XML element name of this SedListOfVariables object.
365  */
366 const std::string&
getElementName() const367 SedListOfVariables::getElementName() const
368 {
369   static const string name = "listOfVariables";
370   return name;
371 }
372 
373 
374 /*
375  * Returns the libSEDML type code for this SedListOfVariables object.
376  */
377 int
getTypeCode() const378 SedListOfVariables::getTypeCode() const
379 {
380   return SEDML_LIST_OF;
381 }
382 
383 
384 /*
385  * Returns the libSEDML type code for the SED-ML objects contained in this
386  * SedListOfVariables object.
387  */
388 int
getItemTypeCode() const389 SedListOfVariables::getItemTypeCode() const
390 {
391   return SEDML_VARIABLE;
392 }
393 
394 
395 
396 /** @cond doxygenLibSEDMLInternal */
397 
398 /*
399  * Creates a new SedVariable in this SedListOfVariables
400  */
401 SedBase*
createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLInputStream & stream)402 SedListOfVariables::createObject(LIBSBML_CPP_NAMESPACE_QUALIFIER
403   XMLInputStream& stream)
404 {
405   const std::string& name = stream.peek().getName();
406   SedBase* object = NULL;
407 
408   if (name == "variable")
409   {
410     object = new SedVariable(getSedNamespaces());
411     appendAndOwn(object);
412   }
413 
414   return object;
415 }
416 
417 /** @endcond */
418 
419 
420 
421 
422 #endif /* __cplusplus */
423 
424 
425 /*
426  * Get a SedVariable_t from the SedListOf_t.
427  */
428 LIBSEDML_EXTERN
429 SedVariable_t*
SedListOfVariables_getVariable(SedListOf_t * slo,unsigned int n)430 SedListOfVariables_getVariable(SedListOf_t* slo, unsigned int n)
431 {
432   if (slo == NULL)
433   {
434     return NULL;
435   }
436 
437   return static_cast <SedListOfVariables*>(slo)->get(n);
438 }
439 
440 
441 /*
442  * Get a SedVariable_t from the SedListOf_t based on its identifier.
443  */
444 LIBSEDML_EXTERN
445 SedVariable_t*
SedListOfVariables_getById(SedListOf_t * slo,const char * sid)446 SedListOfVariables_getById(SedListOf_t* slo, const char *sid)
447 {
448   if (slo == NULL)
449   {
450     return NULL;
451   }
452 
453   return (sid != NULL) ? static_cast <SedListOfVariables*>(slo)->get(sid) :
454     NULL;
455 }
456 
457 
458 /*
459  * Removes the nth SedVariable_t from this SedListOf_t and returns a pointer to
460  * it.
461  */
462 LIBSEDML_EXTERN
463 SedVariable_t*
SedListOfVariables_remove(SedListOf_t * slo,unsigned int n)464 SedListOfVariables_remove(SedListOf_t* slo, unsigned int n)
465 {
466   if (slo == NULL)
467   {
468     return NULL;
469   }
470 
471   return static_cast <SedListOfVariables*>(slo)->remove(n);
472 }
473 
474 
475 /*
476  * Removes the SedVariable_t from this SedListOf_t based on its identifier and
477  * returns a pointer to it.
478  */
479 LIBSEDML_EXTERN
480 SedVariable_t*
SedListOfVariables_removeById(SedListOf_t * slo,const char * sid)481 SedListOfVariables_removeById(SedListOf_t* slo, const char* sid)
482 {
483   if (slo == NULL)
484   {
485     return NULL;
486   }
487 
488   return (sid != NULL) ? static_cast <SedListOfVariables*>(slo)->remove(sid) :
489     NULL;
490 }
491 
492 
493 
494 
495 LIBSEDML_CPP_NAMESPACE_END
496 
497 
498