1 /**
2  * @file    RateRule.cpp
3  * @brief   Implementations of RateRule.
4  * @author  Ben Bornstein
5  *
6  *
7  * <!--------------------------------------------------------------------------
8  * This file is part of libSBML.  Please visit http://sbml.org for more
9  * information about SBML, and the latest version of libSBML.
10  *
11  * Copyright (C) 2020 jointly by the following organizations:
12  *     1. California Institute of Technology, Pasadena, CA, USA
13  *     2. University of Heidelberg, Heidelberg, Germany
14  *     3. University College London, London, UK
15  *
16  * Copyright (C) 2019 jointly by the following organizations:
17  *     1. California Institute of Technology, Pasadena, CA, USA
18  *     2. University of Heidelberg, Heidelberg, Germany
19  *
20  * Copyright (C) 2013-2018 jointly by the following organizations:
21  *     1. California Institute of Technology, Pasadena, CA, USA
22  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
23  *     3. University of Heidelberg, Heidelberg, Germany
24  *
25  * Copyright (C) 2009-2013 jointly by the following organizations:
26  *     1. California Institute of Technology, Pasadena, CA, USA
27  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
28  *
29  * Copyright (C) 2006-2008 by the California Institute of Technology,
30  *     Pasadena, CA, USA
31  *
32  * Copyright (C) 2002-2005 jointly by the following organizations:
33  *     1. California Institute of Technology, Pasadena, CA, USA
34  *     2. Japan Science and Technology Agency, Japan
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the GNU Lesser General Public License as published by
38  * the Free Software Foundation.  A copy of the license agreement is provided
39  * in the file named "LICENSE.txt" included with this software distribution
40  * and also available online as http://sbml.org/software/libsbml/license.html
41  * ---------------------------------------------------------------------- -->*/
42 
43 #include <sbml/xml/XMLNode.h>
44 #include <sbml/xml/XMLAttributes.h>
45 #include <sbml/xml/XMLInputStream.h>
46 #include <sbml/xml/XMLOutputStream.h>
47 #include <sbml/xml/XMLNamespaces.h>
48 
49 #include <sbml/math/FormulaFormatter.h>
50 #include <sbml/math/FormulaParser.h>
51 #include <sbml/math/MathML.h>
52 #include <sbml/math/ASTNode.h>
53 
54 #include <sbml/SBO.h>
55 #include <sbml/SBMLTypeCodes.h>
56 #include <sbml/SBMLVisitor.h>
57 #include <sbml/SBMLError.h>
58 #include <sbml/SBMLDocument.h>
59 #include <sbml/Model.h>
60 #include <sbml/RateRule.h>
61 
62 /** @cond doxygenIgnored */
63 using namespace std;
64 /** @endcond */
65 
66 LIBSBML_CPP_NAMESPACE_BEGIN
67 #ifdef __cplusplus
68 
RateRule(unsigned int level,unsigned int version)69 RateRule::RateRule (unsigned int level, unsigned int version) :
70   Rule(SBML_RATE_RULE, level, version)
71 {
72   if (!hasValidLevelVersionNamespaceCombination())
73     throw SBMLConstructorException();
74 }
75 
RateRule(SBMLNamespaces * sbmlns)76 RateRule::RateRule (SBMLNamespaces *sbmlns) :
77   Rule(SBML_RATE_RULE, sbmlns)
78 {
79   if (!hasValidLevelVersionNamespaceCombination())
80   {
81     throw SBMLConstructorException(getElementName(), sbmlns);
82   }
83 
84   loadPlugins(sbmlns);
85 }
86 
87 
88 /*
89  * Destroys this RateRule.
90  */
~RateRule()91 RateRule::~RateRule ()
92 {
93 }
94 
95 /*
96  * @return a (deep) copy of this Rule.
97  */
98 RateRule*
clone() const99 RateRule::clone () const
100 {
101   return new RateRule(*this);
102 }
103 
104 
105 bool
hasRequiredAttributes() const106 RateRule::hasRequiredAttributes() const
107 {
108   bool allPresent = Rule::hasRequiredAttributes();
109 
110   /* required attributes for rateRule: variable (comp/species/name in L1) */
111 
112   if (!isSetVariable())
113     allPresent = false;
114 
115   return allPresent;
116 }
117 
118 
119 
120 
121 /** @cond doxygenLibsbmlInternal */
122 
123 /*
124  * Returns the value of the "attributeName" attribute of this RateRule.
125  */
126 int
getAttribute(const std::string & attributeName,bool & value) const127 RateRule::getAttribute(const std::string& attributeName, bool& value) const
128 {
129   int return_value = Rule::getAttribute(attributeName, value);
130 
131   return return_value;
132 }
133 
134 /** @endcond */
135 
136 
137 
138 /** @cond doxygenLibsbmlInternal */
139 
140 /*
141  * Returns the value of the "attributeName" attribute of this RateRule.
142  */
143 int
getAttribute(const std::string & attributeName,int & value) const144 RateRule::getAttribute(const std::string& attributeName, int& value) const
145 {
146   int return_value = Rule::getAttribute(attributeName, value);
147 
148   return return_value;
149 }
150 
151 /** @endcond */
152 
153 
154 
155 /** @cond doxygenLibsbmlInternal */
156 
157 /*
158  * Returns the value of the "attributeName" attribute of this RateRule.
159  */
160 int
getAttribute(const std::string & attributeName,double & value) const161 RateRule::getAttribute(const std::string& attributeName, double& value) const
162 {
163   int return_value = Rule::getAttribute(attributeName, value);
164 
165   return return_value;
166 }
167 
168 /** @endcond */
169 
170 
171 
172 /** @cond doxygenLibsbmlInternal */
173 
174 /*
175  * Returns the value of the "attributeName" attribute of this RateRule.
176  */
177 int
getAttribute(const std::string & attributeName,unsigned int & value) const178 RateRule::getAttribute(const std::string& attributeName,
179                        unsigned int& value) const
180 {
181   int return_value = Rule::getAttribute(attributeName, value);
182 
183   return return_value;
184 }
185 
186 /** @endcond */
187 
188 
189 
190 /** @cond doxygenLibsbmlInternal */
191 
192 /*
193  * Returns the value of the "attributeName" attribute of this RateRule.
194  */
195 int
getAttribute(const std::string & attributeName,std::string & value) const196 RateRule::getAttribute(const std::string& attributeName,
197                        std::string& value) const
198 {
199   int return_value = LIBSBML_OPERATION_FAILED;
200   if (getLevel() > 1)
201   {
202     return_value = Rule::getAttribute(attributeName, value);
203   }
204 
205   if (return_value == LIBSBML_OPERATION_SUCCESS)
206   {
207     return return_value;
208   }
209 
210   if (attributeName == "variable")
211   {
212     value = getVariable();
213     return_value = LIBSBML_OPERATION_SUCCESS;
214   }
215   else
216   {
217     int l1type = getL1TypeCode();
218     if ((attributeName == "name" && l1type == SBML_PARAMETER_RULE)
219       || (attributeName == "compartment" && l1type == SBML_COMPARTMENT_VOLUME_RULE)
220       || (attributeName == "species" && l1type == SBML_SPECIES_CONCENTRATION_RULE))
221     {
222       value = getVariable();
223       return_value = LIBSBML_OPERATION_SUCCESS;
224     }
225   }
226 
227   return return_value;
228 }
229 
230 /** @endcond */
231 
232 
233 
234 /** @cond doxygenLibsbmlInternal */
235 
236 /*
237  * Returns the value of the "attributeName" attribute of this RateRule.
238  */
239 //int
240 //RateRule::getAttribute(const std::string& attributeName,
241 //                       const char* value) const
242 //{
243 //  int return_value = LIBSBML_OPERATION_FAILED;
244 //  if (getLevel() > 1)
245 //  {
246 //    return_value = Rule::getAttribute(attributeName, value);
247 //  }
248 //
249 //  if (return_value == LIBSBML_OPERATION_SUCCESS)
250 //  {
251 //    return return_value;
252 //  }
253 //
254 //  if (attributeName == "variable")
255 //  {
256 //    value = getVariable().c_str();
257 //    return_value = LIBSBML_OPERATION_SUCCESS;
258 //  }
259 //  else
260 //  {
261 //    int l1type = getL1TypeCode();
262 //    if ((attributeName == "name" && l1type == SBML_PARAMETER_RULE)
263 //      || (attributeName == "compartment" && l1type == SBML_COMPARTMENT_VOLUME_RULE)
264 //      || (attributeName == "species" && l1type == SBML_SPECIES_CONCENTRATION_RULE))
265 //    {
266 //      value = getVariable().c_str();
267 //      return_value = LIBSBML_OPERATION_SUCCESS;
268 //    }
269 //  }
270 //
271 //  return return_value;
272 //}
273 //
274 /** @endcond */
275 
276 
277 
278 /** @cond doxygenLibsbmlInternal */
279 
280 /*
281  * Predicate returning @c true if this RateRule's attribute "attributeName" is
282  * set.
283  */
284 bool
isSetAttribute(const std::string & attributeName) const285 RateRule::isSetAttribute(const std::string& attributeName) const
286 {
287   bool value = false;
288   if (getLevel() > 1)
289   {
290     value = Rule::isSetAttribute(attributeName);
291   }
292 
293   if (attributeName == "variable")
294   {
295     value = isSetVariable();
296   }
297   else
298   {
299     int l1type = getL1TypeCode();
300     if ((attributeName == "name" && l1type == SBML_PARAMETER_RULE)
301       || (attributeName == "compartment" && l1type == SBML_COMPARTMENT_VOLUME_RULE)
302       || (attributeName == "species" && l1type == SBML_SPECIES_CONCENTRATION_RULE))
303     {
304       value = isSetVariable();
305     }
306   }
307 
308   return value;
309 }
310 
311 /** @endcond */
312 
313 
314 
315 /** @cond doxygenLibsbmlInternal */
316 
317 /*
318  * Sets the value of the "attributeName" attribute of this RateRule.
319  */
320 int
setAttribute(const std::string & attributeName,bool value)321 RateRule::setAttribute(const std::string& attributeName, bool value)
322 {
323   int return_value = Rule::setAttribute(attributeName, value);
324 
325   return return_value;
326 }
327 
328 /** @endcond */
329 
330 
331 
332 /** @cond doxygenLibsbmlInternal */
333 
334 /*
335  * Sets the value of the "attributeName" attribute of this RateRule.
336  */
337 int
setAttribute(const std::string & attributeName,int value)338 RateRule::setAttribute(const std::string& attributeName, int value)
339 {
340   int return_value = Rule::setAttribute(attributeName, value);
341 
342   return return_value;
343 }
344 
345 /** @endcond */
346 
347 
348 
349 /** @cond doxygenLibsbmlInternal */
350 
351 /*
352  * Sets the value of the "attributeName" attribute of this RateRule.
353  */
354 int
setAttribute(const std::string & attributeName,double value)355 RateRule::setAttribute(const std::string& attributeName, double value)
356 {
357   int return_value = Rule::setAttribute(attributeName, value);
358 
359   return return_value;
360 }
361 
362 /** @endcond */
363 
364 
365 
366 /** @cond doxygenLibsbmlInternal */
367 
368 /*
369  * Sets the value of the "attributeName" attribute of this RateRule.
370  */
371 int
setAttribute(const std::string & attributeName,unsigned int value)372 RateRule::setAttribute(const std::string& attributeName, unsigned int value)
373 {
374   int return_value = Rule::setAttribute(attributeName, value);
375 
376   return return_value;
377 }
378 
379 /** @endcond */
380 
381 
382 
383 /** @cond doxygenLibsbmlInternal */
384 
385 /*
386  * Sets the value of the "attributeName" attribute of this RateRule.
387  */
388 int
setAttribute(const std::string & attributeName,const std::string & value)389 RateRule::setAttribute(const std::string& attributeName,
390                        const std::string& value)
391 {
392   int return_value = LIBSBML_OPERATION_FAILED;
393   if (getLevel() > 1)
394   {
395     return_value = Rule::setAttribute(attributeName, value);
396   }
397 
398   if (attributeName == "variable")
399   {
400     return_value = setVariable(value);
401   }
402   else
403   {
404     int l1type = getL1TypeCode();
405     if ((attributeName == "name" && l1type == SBML_PARAMETER_RULE)
406       || (attributeName == "compartment" && l1type == SBML_COMPARTMENT_VOLUME_RULE)
407       || (attributeName == "species" && l1type == SBML_SPECIES_CONCENTRATION_RULE))
408     {
409       return_value = setVariable(value);
410     }
411   }
412 
413   return return_value;
414 }
415 
416 /** @endcond */
417 
418 
419 
420 /** @cond doxygenLibsbmlInternal */
421 
422 /*
423  * Sets the value of the "attributeName" attribute of this RateRule.
424  */
425 //int
426 //RateRule::setAttribute(const std::string& attributeName, const char* value)
427 //{
428 //  int return_value = LIBSBML_OPERATION_FAILED;
429 //  if (getLevel() > 1)
430 //  {
431 //    return_value = Rule::setAttribute(attributeName, value);
432 //  }
433 //
434 //  if (attributeName == "variable")
435 //  {
436 //    return_value = setVariable(value);
437 //  }
438 //  else
439 //  {
440 //    int l1type = getL1TypeCode();
441 //    if ((attributeName == "name" && l1type == SBML_PARAMETER_RULE)
442 //      || (attributeName == "compartment" && l1type == SBML_COMPARTMENT_VOLUME_RULE)
443 //      || (attributeName == "species" && l1type == SBML_SPECIES_CONCENTRATION_RULE))
444 //    {
445 //        return_value = setVariable(value);
446 //    }
447 //  }
448 //
449 //  return return_value;
450 //}
451 //
452 /** @endcond */
453 
454 
455 
456 /** @cond doxygenLibsbmlInternal */
457 
458 /*
459  * Unsets the value of the "attributeName" attribute of this RateRule.
460  */
461 int
unsetAttribute(const std::string & attributeName)462 RateRule::unsetAttribute(const std::string& attributeName)
463 {
464   int return_value = LIBSBML_OPERATION_FAILED;
465   if (getLevel() > 1)
466   {
467     return_value = Rule::unsetAttribute(attributeName);
468   }
469 
470   if (attributeName == "variable")
471   {
472     return_value = unsetVariable();
473   }
474   else
475   {
476     int l1type = getL1TypeCode();
477     if ((attributeName == "name" && l1type == SBML_PARAMETER_RULE)
478       || (attributeName == "compartment" && l1type == SBML_COMPARTMENT_VOLUME_RULE)
479       || (attributeName == "species" && l1type == SBML_SPECIES_CONCENTRATION_RULE))
480     {
481       return_value = unsetVariable();
482     }
483   }
484 
485   return return_value;
486 }
487 
488 /** @endcond */
489 
490 
491 
492 
493 
494 
495 
496 /** @cond doxygenLibsbmlInternal */
497 bool
accept(SBMLVisitor & v) const498 RateRule::accept (SBMLVisitor& v) const
499 {
500   return v.visit(*this);
501 }
502 /** @endcond */
503 
504 
505 void
renameSIdRefs(const std::string & oldid,const std::string & newid)506 RateRule::renameSIdRefs(const std::string& oldid, const std::string& newid)
507 {
508   Rule::renameSIdRefs(oldid, newid);
509   if (isSetVariable()) {
510     if (getVariable()==oldid) {
511       setVariable(newid);
512     }
513   }
514 }
515 
516 #endif /* __cplusplus */
517 
518 
519 /** @cond doxygenIgnored */
520 /** @endcond */
521 
522 
523 LIBSBML_EXTERN
524 RateRule_t *
RateRule_create(unsigned int level,unsigned int version)525 RateRule_create(unsigned int level, unsigned int version)
526 {
527   try
528   {
529     RateRule* obj = new RateRule(level,version);
530     return obj;
531   }
532   catch (SBMLConstructorException)
533   {
534     return NULL;
535   }
536 }
537 
538 
539 LIBSBML_EXTERN
540 RateRule_t *
RateRule_createWithNS(SBMLNamespaces_t * sbmlns)541 RateRule_createWithNS (SBMLNamespaces_t* sbmlns)
542 {
543   try
544   {
545     RateRule* obj = new RateRule(sbmlns);
546     return obj;
547   }
548   catch (SBMLConstructorException)
549   {
550     return NULL;
551   }
552 }
553 
554 
555 LIBSBML_EXTERN
556 void
RateRule_free(RateRule_t * rr)557 RateRule_free(RateRule_t * rr)
558 {
559   if (rr != NULL)
560     delete rr;
561 }
562 
563 
564 LIBSBML_EXTERN
565 RateRule_t *
RateRule_clone(RateRule_t * rr)566 RateRule_clone(RateRule_t * rr)
567 {
568   if (rr != NULL)
569   {
570     return static_cast<RateRule_t*>(rr->clone());
571   }
572   else
573   {
574     return NULL;
575   }
576 }
577 
578 
579 LIBSBML_EXTERN
580 const char *
RateRule_getVariable(const RateRule_t * rr)581 RateRule_getVariable(const RateRule_t * rr)
582 {
583 	return (rr != NULL && rr->isSetVariable()) ? rr->getVariable().c_str() : NULL;
584 }
585 
586 
587 LIBSBML_EXTERN
588 const ASTNode_t*
RateRule_getMath(const RateRule_t * rr)589 RateRule_getMath(const RateRule_t * rr)
590 {
591 	if (rr == NULL)
592 		return NULL;
593 
594 	return (ASTNode_t*)(rr->getMath());
595 }
596 
597 
598 LIBSBML_EXTERN
599 const char *
RateRule_getFormula(const RateRule_t * r)600 RateRule_getFormula (const RateRule_t *r)
601 {
602   return (r != NULL && r->isSetFormula()) ? r->getFormula().c_str() : NULL;
603 }
604 
605 
606 LIBSBML_EXTERN
607 int
RateRule_isSetVariable(const RateRule_t * rr)608 RateRule_isSetVariable(const RateRule_t * rr)
609 {
610   return (rr != NULL) ? static_cast<int>(rr->isSetVariable()) : 0;
611 }
612 
613 
614 LIBSBML_EXTERN
615 int
RateRule_isSetMath(const RateRule_t * rr)616 RateRule_isSetMath(const RateRule_t * rr)
617 {
618   return (rr != NULL) ? static_cast<int>(rr->isSetMath()) : 0;
619 }
620 
621 
622 LIBSBML_EXTERN
623 int
RateRule_isSetFormula(const RateRule_t * r)624 RateRule_isSetFormula (const RateRule_t *r)
625 {
626   return (r != NULL) ? static_cast<int>( r->isSetFormula() ) : 0;
627 }
628 
629 
630 LIBSBML_EXTERN
631 int
RateRule_setVariable(RateRule_t * rr,const char * variable)632 RateRule_setVariable(RateRule_t * rr, const char * variable)
633 {
634   if (rr != NULL)
635     return (variable == NULL) ? rr->setVariable("") : rr->setVariable(variable);
636   else
637     return LIBSBML_INVALID_OBJECT;
638 }
639 
640 
641 LIBSBML_EXTERN
642 int
RateRule_setMath(RateRule_t * rr,const ASTNode_t * math)643 RateRule_setMath(RateRule_t * rr, const ASTNode_t* math)
644 {
645 	return (rr != NULL) ? rr->setMath(math) : LIBSBML_INVALID_OBJECT;
646 }
647 
648 
649 LIBSBML_EXTERN
650 int
RateRule_setFormula(RateRule_t * r,const char * formula)651 RateRule_setFormula (RateRule_t *r, const char *formula)
652 {
653   if (r != NULL)
654     return (formula == NULL) ? r->setMath(NULL) : r->setFormula(formula);
655   else
656     return LIBSBML_INVALID_OBJECT;
657 }
658 
659 
660 LIBSBML_EXTERN
661 int
RateRule_unsetVariable(RateRule_t * ar)662 RateRule_unsetVariable(RateRule_t * ar)
663 {
664   return (ar != NULL) ? static_cast<int>(ar->unsetVariable())
665                       : LIBSBML_INVALID_OBJECT;
666 }
667 
668 
669 LIBSBML_EXTERN
670 int
RateRule_hasRequiredAttributes(const RateRule_t * rr)671 RateRule_hasRequiredAttributes(const RateRule_t * rr)
672 {
673   return (rr != NULL) ? static_cast<int>(rr->hasRequiredAttributes()) : 0;
674 }
675 
676 
677 LIBSBML_EXTERN
678 int
RateRule_hasRequiredElements(const RateRule_t * rr)679 RateRule_hasRequiredElements(const RateRule_t * rr)
680 {
681 	return (rr != NULL) ? static_cast<int>(rr->hasRequiredElements()) : 0;
682 }
683 
684 
685 
686 
687 LIBSBML_CPP_NAMESPACE_END
688