1 /**
2 * @file FbcAssociation.cpp
3 * @brief Implementation of the FbcAssociation class
4 * @author SBMLTeam
5 *
6 * <!--------------------------------------------------------------------------
7 * This file is part of libSBML. Please visit http://sbml.org for more
8 * information about SBML, and the latest version of libSBML.
9 *
10 * Copyright (C) 2020 jointly by the following organizations:
11 * 1. California Institute of Technology, Pasadena, CA, USA
12 * 2. University of Heidelberg, Heidelberg, Germany
13 * 3. University College London, London, UK
14 *
15 * Copyright (C) 2019 jointly by the following organizations:
16 * 1. California Institute of Technology, Pasadena, CA, USA
17 * 2. University of Heidelberg, Heidelberg, Germany
18 *
19 * Copyright (C) 2013-2018 jointly by the following organizations:
20 * 1. California Institute of Technology, Pasadena, CA, USA
21 * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22 * 3. University of Heidelberg, Heidelberg, Germany
23 *
24 * Copyright (C) 2009-2013 jointly by the following organizations:
25 * 1. California Institute of Technology, Pasadena, CA, USA
26 * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27 *
28 * Copyright (C) 2006-2008 by the California Institute of Technology,
29 * Pasadena, CA, USA
30 *
31 * Copyright (C) 2002-2005 jointly by the following organizations:
32 * 1. California Institute of Technology, Pasadena, CA, USA
33 * 2. Japan Science and Technology Agency, Japan
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU Lesser General Public License as published by
37 * the Free Software Foundation. A copy of the license agreement is provided
38 * in the file named "LICENSE.txt" included with this software distribution
39 * and also available online as http://sbml.org/software/libsbml/license.html
40 * ------------------------------------------------------------------------ -->
41 */
42
43
44 #include <sbml/packages/fbc/sbml/FbcAssociation.h>
45 #include <sbml/packages/fbc/extension/FbcModelPlugin.h>
46 #include <sbml/packages/fbc/validator/FbcSBMLError.h>
47 #include <sbml/util/ElementFilter.h>
48
49 #include <sbml/packages/fbc/sbml/FbcAnd.h>
50 #include <sbml/packages/fbc/sbml/FbcOr.h>
51 #include <sbml/packages/fbc/sbml/GeneProductRef.h>
52
53 #include <sbml/math/FormulaParser.h>
54 #include <sbml/util/util.h>
55
56 using namespace std;
57
58
59 #ifdef __cplusplus
60
61 LIBSBML_CPP_NAMESPACE_BEGIN
62
63
64 /*
65 * Creates a new FbcAssociation with the given level, version, and package version.
66 */
FbcAssociation(unsigned int level,unsigned int version,unsigned int pkgVersion)67 FbcAssociation::FbcAssociation (unsigned int level, unsigned int version, unsigned int pkgVersion)
68 : SBase(level, version)
69 , mElementName("fbcAssociation")
70 {
71 // set an SBMLNamespaces derived object of this package
72 setSBMLNamespacesAndOwn(new FbcPkgNamespaces(level, version, pkgVersion));
73 }
74
75
76 /*
77 * Creates a new FbcAssociation with the given FbcPkgNamespaces object.
78 */
FbcAssociation(FbcPkgNamespaces * fbcns)79 FbcAssociation::FbcAssociation (FbcPkgNamespaces* fbcns)
80 : SBase(fbcns)
81 , mElementName("fbcAssociation")
82 {
83 // set the element namespace of this object
84 setElementNamespace(fbcns->getURI());
85
86 // load package extensions bound with this object (if any)
87 loadPlugins(fbcns);
88 }
89
90
91 /*
92 * Copy constructor for FbcAssociation.
93 */
FbcAssociation(const FbcAssociation & orig)94 FbcAssociation::FbcAssociation (const FbcAssociation& orig)
95 : SBase(orig)
96 {
97 mElementName = orig.mElementName;
98 }
99
100
101 /*
102 * Assignment for FbcAssociation.
103 */
104 FbcAssociation&
operator =(const FbcAssociation & rhs)105 FbcAssociation::operator=(const FbcAssociation& rhs)
106 {
107 if (&rhs != this)
108 {
109 SBase::operator=(rhs);
110 mElementName = rhs.mElementName;
111 }
112 return *this;
113 }
114
115
116 /*
117 * Clone for FbcAssociation.
118 */
119 FbcAssociation*
clone() const120 FbcAssociation::clone () const
121 {
122 return new FbcAssociation(*this);
123 }
124
125
126 /*
127 * Destructor for FbcAssociation.
128 */
~FbcAssociation()129 FbcAssociation::~FbcAssociation ()
130 {
131 }
132
133
134 /*
135 * Return @c true if of type FbcAnd.
136 */
137 bool
isFbcAnd() const138 FbcAssociation::isFbcAnd() const
139 {
140 return dynamic_cast<const FbcAnd*>(this) != NULL;
141 }
142
143
144 /*
145 * Return @c true if of type FbcOr.
146 */
147 bool
isFbcOr() const148 FbcAssociation::isFbcOr() const
149 {
150 return dynamic_cast<const FbcOr*>(this) != NULL;
151 }
152
153
154 /*
155 * Return @c true if of type GeneProductRef.
156 */
157 bool
isGeneProductRef() const158 FbcAssociation::isGeneProductRef() const
159 {
160 return dynamic_cast<const GeneProductRef*>(this) != NULL;
161 }
162
163
164 /*
165 * Returns the XML element name of this object
166 */
167 const std::string&
getElementName() const168 FbcAssociation::getElementName () const
169 {
170 return mElementName;
171 }
172
173
174 /** @cond doxygenLibsbmlInternal */
175 /*
176 * Sets the element name for this object
177 */
178 void
setElementName(const std::string & name)179 FbcAssociation::setElementName(const std::string& name)
180 {
181 mElementName = name;
182 }
183 /** @endcond */
184
185
186 /*
187 * Returns the libSBML type code for this SBML object.
188 */
189 int
getTypeCode() const190 FbcAssociation::getTypeCode () const
191 {
192 return SBML_FBC_ASSOCIATION;
193 }
194
195
196 /*
197 * check if all the required attributes are set
198 */
199 bool
hasRequiredAttributes() const200 FbcAssociation::hasRequiredAttributes () const
201 {
202 bool allPresent = true;
203
204 return allPresent;
205 }
206
207
208 /** @cond doxygenLibsbmlInternal */
209
210 /*
211 * write contained elements
212 */
213 void
writeElements(XMLOutputStream & stream) const214 FbcAssociation::writeElements (XMLOutputStream& stream) const
215 {
216 SBase::writeElements(stream);
217 SBase::writeExtensionElements(stream);
218 }
219
220
221 /** @endcond */
222
223
224 /** @cond doxygenLibsbmlInternal */
225
226 /*
227 * Accepts the given SBMLVisitor.
228 */
229 bool
accept(SBMLVisitor & v) const230 FbcAssociation::accept (SBMLVisitor& v) const
231 {
232 return v.visit(*this);
233 }
234
235
236 /** @endcond */
237
238
239 FbcAssociation* toAssociation(const ASTNode* node, FbcModelPlugin* plugin, bool usingId, bool addMissingGP);
240
addChildren(FbcAssociation * association,const ASTNode * node,const ASTNode * current,FbcModelPlugin * plugin,bool usingId,bool addMissingGP)241 void addChildren(FbcAssociation* association, const ASTNode* node,
242 const ASTNode *current, FbcModelPlugin* plugin,
243 bool usingId, bool addMissingGP)
244 {
245
246 if (node->getType() == AST_TIMES || node->getType() == AST_PLUS)
247 {
248 for (unsigned int i = 0; i < node->getNumChildren(); ++i)
249 {
250 ASTNode* astChild = node->getChild(i);
251 if (astChild->getType() == current->getType())
252 {
253 addChildren(association, astChild, node, plugin, usingId, addMissingGP);
254 continue;
255 }
256
257 FbcAssociation* child = toAssociation(astChild, plugin, usingId, addMissingGP);
258 if (child == NULL)
259 continue;
260
261 FbcAnd* andAssociation = dynamic_cast<FbcAnd*>(association);
262 if (andAssociation != NULL)
263 {
264 andAssociation->addAssociation(child);
265 }
266 else
267 {
268 FbcOr* orAssociation = dynamic_cast<FbcOr*>(association);
269 if (orAssociation != NULL)
270 orAssociation->addAssociation(child);
271 }
272 delete child;
273 }
274 }
275 else{
276 FbcAssociation* child = toAssociation(node, plugin, usingId, addMissingGP);
277 if (child == NULL)
278 return;
279
280 FbcAnd* andAssociation = dynamic_cast<FbcAnd*>(association);
281 if (andAssociation != NULL)
282 {
283 andAssociation->addAssociation(child);
284 }
285 else
286 {
287 FbcOr* orAssociation = dynamic_cast<FbcOr*>(association);
288 if (orAssociation != NULL)
289 orAssociation->addAssociation(child);
290 }
291
292 delete child;
293
294 }
295 }
296
297
toAssociation(const ASTNode * node,FbcModelPlugin * plugin,bool usingId,bool addMissingGP)298 FbcAssociation* toAssociation(const ASTNode* node, FbcModelPlugin* plugin,
299 bool usingId, bool addMissingGP)
300 {
301 if (node == NULL)
302 return NULL;
303
304 if (node->getType() == AST_NAME)
305 {
306 std::string name = node->getName();
307 if (!usingId)
308 {
309 replaceAllSubStrings(name, "__MINUS__", "-");
310 replaceAllSubStrings(name, "__COLON__", ":");
311 replaceAllSubStrings(name, "__DOT__", ".");
312 replaceAllSubStrings(name, "__ONE__", "1");
313 replaceAllSubStrings(name, "__TWO__", "2");
314 replaceAllSubStrings(name, "__THREE__", "3");
315 replaceAllSubStrings(name, "__FOUR__", "4");
316 replaceAllSubStrings(name, "__FIVE__", "5");
317 replaceAllSubStrings(name, "__SIX__", "6");
318 replaceAllSubStrings(name, "__SEVEN__", "7");
319 replaceAllSubStrings(name, "__EIGHT__", "8");
320 replaceAllSubStrings(name, "__NINE__", "9");
321 replaceAllSubStrings(name, "__ZERO__", "0");
322 }
323
324 GeneProduct* prod = NULL;
325 if (usingId)
326 {
327 prod = plugin->getGeneProduct(name);
328 }
329 else
330 {
331 prod = plugin->getGeneProductByLabel(node->getName());
332 if (prod == NULL)
333 prod = plugin->getGeneProductByLabel(name);
334 }
335 string id;
336 if (prod == NULL)
337 {
338 if (usingId)
339 {
340 id = name;
341 }
342 else
343 {
344 string base("gp_");
345 base += node->getName();
346 id = base;
347 int count = 0;
348 while (plugin->getGeneProduct(id))
349 {
350 stringstream str; str << base << "_" << ++count;
351 id = str.str();
352 }
353 }
354 if (addMissingGP)
355 {
356 prod = plugin->createGeneProduct();
357 if (usingId)
358 {
359 prod->setId(name);
360 prod->setLabel(name);
361 }
362 else
363 {
364 prod->setId(id);
365 prod->setLabel(name);
366 }
367 }
368 }
369 else
370 {
371 id = prod->getId();
372 }
373
374 GeneProductRef* a = new GeneProductRef();
375 a->setGeneProduct(id);
376 return a;
377 }
378 else if (node->getType() == AST_PLUS)
379 {
380 FbcOr* a = new FbcOr();
381 addChildren(a, node, node, plugin, usingId, addMissingGP);
382 return a;
383 }
384 else if (node->getType() == AST_TIMES)
385 {
386 FbcAnd* a = new FbcAnd();
387 addChildren(a, node, node, plugin, usingId, addMissingGP);
388 return a;
389 }
390 return NULL;
391 }
392
393
394
395
396 FbcAssociation*
parseFbcInfixAssociation(const std::string & association,FbcModelPlugin * plugin,bool usingId,bool addMissingGP)397 FbcAssociation::parseFbcInfixAssociation(const std::string& association, FbcModelPlugin* plugin,
398 bool usingId, bool addMissingGP)
399 {
400 std::string tweaked(association);
401 replaceAllSubStrings(tweaked, " and ", " * ");
402 replaceAllSubStrings(tweaked, " AND ", " * ");
403 replaceAllSubStrings(tweaked, " or ", " + ");
404 replaceAllSubStrings(tweaked, " OR ", " + ");
405 if (!usingId)
406 {
407 replaceAllSubStrings(tweaked, "-", "__MINUS__");
408 replaceAllSubStrings(tweaked, ":", "__COLON__");
409 replaceAllSubStrings(tweaked, ".", "__DOT__");
410 replaceAllSubStrings(tweaked, "1", "__ONE__");
411 replaceAllSubStrings(tweaked, "2", "__TWO__");
412 replaceAllSubStrings(tweaked, "3", "__THREE__");
413 replaceAllSubStrings(tweaked, "4", "__FOUR__");
414 replaceAllSubStrings(tweaked, "5", "__FIVE__");
415 replaceAllSubStrings(tweaked, "6", "__SIX__");
416 replaceAllSubStrings(tweaked, "7", "__SEVEN__");
417 replaceAllSubStrings(tweaked, "8", "__EIGHT__");
418 replaceAllSubStrings(tweaked, "9", "__NINE__");
419 replaceAllSubStrings(tweaked, "0", "__ZERO__");
420 }
421
422 ASTNode* node = SBML_parseFormula(tweaked.c_str());
423
424 if (node == NULL)
425 return NULL;
426
427 FbcAssociation* result = toAssociation(node, plugin, usingId, addMissingGP);
428
429 delete node;
430
431 return result;
432
433
434 }
435
436 std::string
toInfix(bool usingId) const437 FbcAssociation::toInfix(bool usingId) const
438 {
439 return "";
440 }
441
442
443 /** @cond doxygenLibsbmlInternal */
444
445 /*
446 * Sets the parent SBMLDocument.
447 */
448 void
setSBMLDocument(SBMLDocument * d)449 FbcAssociation::setSBMLDocument (SBMLDocument* d)
450 {
451 SBase::setSBMLDocument(d);
452 }
453
454
455 /** @endcond */
456
457
458 /** @cond doxygenLibsbmlInternal */
459
460 /*
461 * Enables/Disables the given package with this element.
462 */
463 void
enablePackageInternal(const std::string & pkgURI,const std::string & pkgPrefix,bool flag)464 FbcAssociation::enablePackageInternal(const std::string& pkgURI,
465 const std::string& pkgPrefix, bool flag)
466 {
467 SBase::enablePackageInternal(pkgURI, pkgPrefix, flag);
468 }
469
470
471 /** @endcond */
472
473 /** @cond doxygenLibsbmlInternal */
474
475 /*
476 * Returns the value of the "attributeName" attribute of this Association.
477 */
478 int
getAttribute(const std::string & attributeName,bool & value) const479 FbcAssociation::getAttribute(const std::string& attributeName, bool& value) const
480 {
481 int return_value = SBase::getAttribute(attributeName, value);
482
483 return return_value;
484 }
485
486 /** @endcond */
487
488
489
490 /** @cond doxygenLibsbmlInternal */
491
492 /*
493 * Returns the value of the "attributeName" attribute of this Association.
494 */
495 int
getAttribute(const std::string & attributeName,int & value) const496 FbcAssociation::getAttribute(const std::string& attributeName, int& value) const
497 {
498 int return_value = SBase::getAttribute(attributeName, value);
499
500 return return_value;
501 }
502
503 /** @endcond */
504
505
506
507 /** @cond doxygenLibsbmlInternal */
508
509 /*
510 * Returns the value of the "attributeName" attribute of this Association.
511 */
512 int
getAttribute(const std::string & attributeName,double & value) const513 FbcAssociation::getAttribute(const std::string& attributeName,
514 double& value) const
515 {
516 int return_value = SBase::getAttribute(attributeName, value);
517
518 return return_value;
519 }
520
521 /** @endcond */
522
523
524
525 /** @cond doxygenLibsbmlInternal */
526
527 /*
528 * Returns the value of the "attributeName" attribute of this Association.
529 */
530 int
getAttribute(const std::string & attributeName,unsigned int & value) const531 FbcAssociation::getAttribute(const std::string& attributeName,
532 unsigned int& value) const
533 {
534 int return_value = SBase::getAttribute(attributeName, value);
535
536 return return_value;
537 }
538
539 /** @endcond */
540
541
542
543 /** @cond doxygenLibsbmlInternal */
544
545 /*
546 * Returns the value of the "attributeName" attribute of this Association.
547 */
548 int
getAttribute(const std::string & attributeName,std::string & value) const549 FbcAssociation::getAttribute(const std::string& attributeName,
550 std::string& value) const
551 {
552 int return_value = SBase::getAttribute(attributeName, value);
553
554 return return_value;
555 }
556
557 /** @endcond */
558
559
560
561
562 /** @cond doxygenLibsbmlInternal */
563
564 /*
565 * Predicate returning @c true if this Association's attribute "attributeName"
566 * is set.
567 */
568 bool
isSetAttribute(const std::string & attributeName) const569 FbcAssociation::isSetAttribute(const std::string& attributeName) const
570 {
571 bool value = SBase::isSetAttribute(attributeName);
572
573 return value;
574 }
575
576 /** @endcond */
577
578
579
580 /** @cond doxygenLibsbmlInternal */
581
582 /*
583 * Sets the value of the "attributeName" attribute of this Association.
584 */
585 int
setAttribute(const std::string & attributeName,bool value)586 FbcAssociation::setAttribute(const std::string& attributeName, bool value)
587 {
588 int return_value = SBase::setAttribute(attributeName, value);
589
590 return return_value;
591 }
592
593 /** @endcond */
594
595
596
597 /** @cond doxygenLibsbmlInternal */
598
599 /*
600 * Sets the value of the "attributeName" attribute of this Association.
601 */
602 int
setAttribute(const std::string & attributeName,int value)603 FbcAssociation::setAttribute(const std::string& attributeName, int value)
604 {
605 int return_value = SBase::setAttribute(attributeName, value);
606
607 return return_value;
608 }
609
610 /** @endcond */
611
612
613
614 /** @cond doxygenLibsbmlInternal */
615
616 /*
617 * Sets the value of the "attributeName" attribute of this Association.
618 */
619 int
setAttribute(const std::string & attributeName,double value)620 FbcAssociation::setAttribute(const std::string& attributeName, double value)
621 {
622 int return_value = SBase::setAttribute(attributeName, value);
623
624 return return_value;
625 }
626
627 /** @endcond */
628
629
630
631 /** @cond doxygenLibsbmlInternal */
632
633 /*
634 * Sets the value of the "attributeName" attribute of this Association.
635 */
636 int
setAttribute(const std::string & attributeName,unsigned int value)637 FbcAssociation::setAttribute(const std::string& attributeName,
638 unsigned int value)
639 {
640 int return_value = SBase::setAttribute(attributeName, value);
641
642 return return_value;
643 }
644
645 /** @endcond */
646
647
648
649 /** @cond doxygenLibsbmlInternal */
650
651 /*
652 * Sets the value of the "attributeName" attribute of this Association.
653 */
654 int
setAttribute(const std::string & attributeName,const std::string & value)655 FbcAssociation::setAttribute(const std::string& attributeName,
656 const std::string& value)
657 {
658 int return_value = SBase::setAttribute(attributeName, value);
659
660 return return_value;
661 }
662
663 /** @endcond */
664
665
666
667
668 /** @cond doxygenLibsbmlInternal */
669
670 /*
671 * Unsets the value of the "attributeName" attribute of this Association.
672 */
673 int
unsetAttribute(const std::string & attributeName)674 FbcAssociation::unsetAttribute(const std::string& attributeName)
675 {
676 int value = SBase::unsetAttribute(attributeName);
677
678 return value;
679 }
680
681 /** @endcond */
682
683
684
685 /** @cond doxygenLibsbmlInternal */
686
687 /*
688 * Get the list of expected attributes for this element.
689 */
690 void
addExpectedAttributes(ExpectedAttributes & attributes)691 FbcAssociation::addExpectedAttributes(ExpectedAttributes& attributes)
692 {
693 SBase::addExpectedAttributes(attributes);
694
695 }
696
697
698 /** @endcond */
699
700
701 /** @cond doxygenLibsbmlInternal */
702
703 /*
704 * Read values from the given XMLAttributes set into their specific fields.
705 */
706 void
readAttributes(const XMLAttributes & attributes,const ExpectedAttributes & expectedAttributes)707 FbcAssociation::readAttributes (const XMLAttributes& attributes,
708 const ExpectedAttributes& expectedAttributes)
709 {
710 const unsigned int sbmlLevel = getLevel ();
711 const unsigned int sbmlVersion = getVersion();
712
713 unsigned int numErrs;
714
715 /* look to see whether an unknown attribute error was logged
716 * during the read of the ListOfFbcAssociations - which will have
717 * happened immediately prior to this read
718 */
719
720 ListOfFbcAssociations* listOf = dynamic_cast<ListOfFbcAssociations*>(getParentSBMLObject());
721 unsigned int listOfSize = listOf != NULL ? listOf->size() : 0;
722
723 if (getErrorLog() != NULL && listOfSize < 2)
724 {
725 numErrs = getErrorLog()->getNumErrors();
726 for (int n = (int)numErrs-1; n >= 0; n--)
727 {
728 if (getErrorLog()->getError((unsigned int)n)->getErrorId() == UnknownPackageAttribute)
729 {
730 const std::string details =
731 getErrorLog()->getError((unsigned int)n)->getMessage();
732 getErrorLog()->remove(UnknownPackageAttribute);
733 getErrorLog()->logPackageError("fbc", FbcUnknown,
734 getPackageVersion(), sbmlLevel, sbmlVersion, details, getLine(), getColumn());
735 }
736 else if (getErrorLog()->getError((unsigned int)n)->getErrorId() == UnknownCoreAttribute)
737 {
738 const std::string details =
739 getErrorLog()->getError((unsigned int)n)->getMessage();
740 getErrorLog()->remove(UnknownCoreAttribute);
741 getErrorLog()->logPackageError("fbc", FbcUnknown,
742 getPackageVersion(), sbmlLevel, sbmlVersion, details, getLine(), getColumn());
743 }
744 }
745 }
746
747 SBase::readAttributes(attributes, expectedAttributes);
748
749 // look to see whether an unknown attribute error was logged
750 if (getErrorLog() != NULL)
751 {
752 numErrs = getErrorLog()->getNumErrors();
753 unsigned int pkgErr = FbcGeneProdRefAllowedAttribs;
754 unsigned int coreErr = FbcGeneProdRefAllowedCoreAttribs;
755 if (this->isFbcAnd())
756 {
757 coreErr = FbcAndAllowedCoreAttributes;
758 }
759 else if (this->isFbcOr())
760 {
761 coreErr = FbcOrAllowedCoreAttributes;
762 }
763 for (int n = (int)numErrs-1; n >= 0; n--)
764 {
765 if (getErrorLog()->getError((unsigned int)n)->getErrorId() == UnknownPackageAttribute)
766 {
767 const std::string details =
768 getErrorLog()->getError((unsigned int)n)->getMessage();
769 getErrorLog()->remove(UnknownPackageAttribute);
770 getErrorLog()->logPackageError("fbc", pkgErr,
771 getPackageVersion(), sbmlLevel, sbmlVersion, details, getLine(), getColumn());
772 }
773 else if (getErrorLog()->getError((unsigned int)n)->getErrorId() == UnknownCoreAttribute)
774 {
775 const std::string details =
776 getErrorLog()->getError((unsigned int)n)->getMessage();
777 getErrorLog()->remove(UnknownCoreAttribute);
778 getErrorLog()->logPackageError("fbc", coreErr,
779 getPackageVersion(), sbmlLevel, sbmlVersion, details, getLine(), getColumn());
780 }
781 }
782 }
783
784 }
785
786
787 /** @endcond */
788
789
790 /** @cond doxygenLibsbmlInternal */
791
792 /*
793 * Write values of XMLAttributes to the output stream.
794 */
795 void
writeAttributes(XMLOutputStream & stream) const796 FbcAssociation::writeAttributes (XMLOutputStream& stream) const
797 {
798 SBase::writeAttributes(stream);
799
800 }
801
802
803 /** @endcond */
804
805
806 /*
807 * Constructor
808 */
ListOfFbcAssociations(unsigned int level,unsigned int version,unsigned int pkgVersion)809 ListOfFbcAssociations::ListOfFbcAssociations(unsigned int level,
810 unsigned int version,
811 unsigned int pkgVersion)
812 : ListOf(level, version)
813 {
814 setSBMLNamespacesAndOwn(new FbcPkgNamespaces(level, version, pkgVersion));
815 }
816
817
818 /*
819 * Constructor
820 */
ListOfFbcAssociations(FbcPkgNamespaces * fbcns)821 ListOfFbcAssociations::ListOfFbcAssociations(FbcPkgNamespaces* fbcns)
822 : ListOf(fbcns)
823 {
824 setElementNamespace(fbcns->getURI());
825 }
826
827
828 /*
829 * Returns a deep copy of this ListOfFbcAssociations
830 */
831 ListOfFbcAssociations*
clone() const832 ListOfFbcAssociations::clone () const
833 {
834 return new ListOfFbcAssociations(*this);
835 }
836
837
838 /*
839 * Get a FbcAssociation from the ListOfFbcAssociations by index.
840 */
841 FbcAssociation*
get(unsigned int n)842 ListOfFbcAssociations::get(unsigned int n)
843 {
844 return static_cast<FbcAssociation*>(ListOf::get(n));
845 }
846
847
848 /*
849 * Get a FbcAssociation from the ListOfFbcAssociations by index.
850 */
851 const FbcAssociation*
get(unsigned int n) const852 ListOfFbcAssociations::get(unsigned int n) const
853 {
854 return static_cast<const FbcAssociation*>(ListOf::get(n));
855 }
856
857
858 /*
859 * Get a FbcAssociation from the ListOfFbcAssociations by id.
860 */
861 FbcAssociation*
get(const std::string & sid)862 ListOfFbcAssociations::get(const std::string& sid)
863 {
864 return const_cast<FbcAssociation*>(
865 static_cast<const ListOfFbcAssociations&>(*this).get(sid));
866 }
867
868
869 /*
870 * Get a FbcAssociation from the ListOfFbcAssociations by id.
871 */
872 const FbcAssociation*
get(const std::string & sid) const873 ListOfFbcAssociations::get(const std::string& sid) const
874 {
875 vector<SBase*>::const_iterator result;
876
877 result = find_if( mItems.begin(), mItems.end(), IdEq<FbcAssociation>(sid) );
878 return (result == mItems.end()) ? 0 : static_cast <FbcAssociation*> (*result);
879 }
880
881
882 /*
883 * Adds a copy the given FbcAssociation to this ListOfFbcAssociations.
884 *
885 * @param fa the FbcAssociation object to add.
886 *
887 * @copydetails doc_returns_success_code
888 * @li LIBSBML_OPERATION_SUCCESS
889 * @li LIBSBML_INVALID_ATTRIBUTE_VALUE
890 */
891 int
addFbcAssociation(const FbcAssociation * fa)892 ListOfFbcAssociations::addFbcAssociation(const FbcAssociation* fa)
893 {
894 if (fa == NULL)
895 {
896 return LIBSBML_OPERATION_FAILED;
897 }
898 else if (fa->hasRequiredAttributes() == false)
899 {
900 return LIBSBML_INVALID_OBJECT;
901 }
902 else if (getLevel() != fa->getLevel())
903 {
904 return LIBSBML_LEVEL_MISMATCH;
905 }
906 else if (getVersion() != fa->getVersion())
907 {
908 return LIBSBML_VERSION_MISMATCH;
909 }
910 else if (matchesRequiredSBMLNamespacesForAddition(static_cast<const SBase *>(fa)) == false)
911 {
912 return LIBSBML_NAMESPACES_MISMATCH;
913 }
914 else
915 {
916 return append(fa);
917 }
918 }
919
920
921 /*
922 * Get the number of FbcAssociation objects in this ListOfFbcAssociations.
923 *
924 * @return the number of FbcAssociation objects in this ListOfFbcAssociations
925 */
926 unsigned int
getNumFbcAssociations() const927 ListOfFbcAssociations::getNumFbcAssociations() const
928 {
929 return size();
930 }
931
932
933 /*
934 * Creates a new FbcAnd object, adds it to this ListOfFbcAssociations
935 * and returns the FbcAnd object created.
936 *
937 * @return a new FbcAnd object instance
938 *
939 * @see addAnd(const FbcAssociation* fa)
940 */
941 FbcAnd*
createAnd()942 ListOfFbcAssociations::createAnd()
943 {
944 FbcAnd* fa = NULL;
945
946 try
947 {
948 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
949 fa = new FbcAnd(fbcns);
950 delete fbcns;
951 }
952 catch (...)
953 {
954 /* here we do not create a default object as the level/version must
955 * match the parent object
956 *
957 * do nothing
958 */
959 }
960
961 if(fa != NULL)
962 {
963 appendAndOwn(fa);
964 }
965
966 return fa;
967 }
968
969
970 /*
971 * Creates a new FbcOr object, adds it to this ListOfFbcAssociations
972 * and returns the FbcOr object created.
973 *
974 * @return a new FbcOr object instance
975 *
976 * @see addOr(const FbcAssociation* fa)
977 */
978 FbcOr*
createOr()979 ListOfFbcAssociations::createOr()
980 {
981 FbcOr* fo = NULL;
982
983 try
984 {
985 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
986 fo = new FbcOr(fbcns);
987 delete fbcns;
988 }
989 catch (...)
990 {
991 /* here we do not create a default object as the level/version must
992 * match the parent object
993 *
994 * do nothing
995 */
996 }
997
998 if(fo != NULL)
999 {
1000 appendAndOwn(fo);
1001 }
1002
1003 return fo;
1004 }
1005
1006
1007 /*
1008 * Creates a new GeneProductRef object, adds it to this ListOfFbcAssociations
1009 * and returns the GeneProductRef object created.
1010 *
1011 * @return a new GeneProductRef object instance
1012 *
1013 * @see addGeneProductRef(const FbcAssociation* fa)
1014 */
1015 GeneProductRef*
createGeneProductRef()1016 ListOfFbcAssociations::createGeneProductRef()
1017 {
1018 GeneProductRef* gpr = NULL;
1019
1020 try
1021 {
1022 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
1023 gpr = new GeneProductRef(fbcns);
1024 delete fbcns;
1025 }
1026 catch (...)
1027 {
1028 /* here we do not create a default object as the level/version must
1029 * match the parent object
1030 *
1031 * do nothing
1032 */
1033 }
1034
1035 if(gpr != NULL)
1036 {
1037 appendAndOwn(gpr);
1038 }
1039
1040 return gpr;
1041 }
1042
1043 /*
1044 * Removes the nth FbcAssociation from this ListOfFbcAssociations
1045 */
1046 FbcAssociation*
remove(unsigned int n)1047 ListOfFbcAssociations::remove(unsigned int n)
1048 {
1049 return static_cast<FbcAssociation*>(ListOf::remove(n));
1050 }
1051
1052
1053 /*
1054 * Removes the FbcAssociation from this ListOfFbcAssociations with the given identifier
1055 */
1056 FbcAssociation*
remove(const std::string & sid)1057 ListOfFbcAssociations::remove(const std::string& sid)
1058 {
1059 SBase* item = NULL;
1060 vector<SBase*>::iterator result;
1061
1062 result = find_if( mItems.begin(), mItems.end(), IdEq<FbcAssociation>(sid) );
1063
1064 if (result != mItems.end())
1065 {
1066 item = *result;
1067 mItems.erase(result);
1068 }
1069
1070 return static_cast <FbcAssociation*> (item);
1071 }
1072
1073
1074 /*
1075 * Returns the XML element name of this object
1076 */
1077 const std::string&
getElementName() const1078 ListOfFbcAssociations::getElementName () const
1079 {
1080 static const string name = "listOfFbcAssociations";
1081 return name;
1082 }
1083
1084
1085 /*
1086 * Returns the libSBML type code for this SBML object.
1087 */
1088 int
getTypeCode() const1089 ListOfFbcAssociations::getTypeCode () const
1090 {
1091 return SBML_LIST_OF;
1092 }
1093
1094
1095 /*
1096 * Returns the libSBML type code for the objects in this LIST_OF.
1097 */
1098 int
getItemTypeCode() const1099 ListOfFbcAssociations::getItemTypeCode () const
1100 {
1101 return SBML_FBC_ASSOCIATION;
1102 }
1103
1104
1105 /** @cond doxygenLibsbmlInternal */
1106
1107 /*
1108 * Creates a new FbcAssociation in this ListOfFbcAssociations
1109 */
1110 SBase*
createObject(XMLInputStream & stream)1111 ListOfFbcAssociations::createObject(XMLInputStream& stream)
1112 {
1113 const std::string& name = stream.peek().getName();
1114 SBase* object = NULL;
1115
1116 if (name == "fbcAssociation")
1117 {
1118 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
1119 object = new FbcAssociation(fbcns);
1120 appendAndOwn(object);
1121 delete fbcns;
1122 }
1123
1124 if (name == "and")
1125 {
1126 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
1127 object = new FbcAnd(fbcns);
1128 appendAndOwn(object);
1129 delete fbcns;
1130 }
1131
1132 if (name == "or")
1133 {
1134 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
1135 object = new FbcOr(fbcns);
1136 appendAndOwn(object);
1137 delete fbcns;
1138 }
1139
1140 if (name == "geneProductRef")
1141 {
1142 FBC_CREATE_NS_WITH_VERSION(fbcns, getSBMLNamespaces(), getPackageVersion());
1143 object = new GeneProductRef(fbcns);
1144 appendAndOwn(object);
1145 delete fbcns;
1146 }
1147
1148 return object;
1149 }
1150
1151
1152 /** @endcond */
1153
1154
1155 /** @cond doxygenLibsbmlInternal */
1156
1157 /*
1158 * Write the namespace for the Fbc package.
1159 */
1160 void
writeXMLNS(XMLOutputStream & stream) const1161 ListOfFbcAssociations::writeXMLNS(XMLOutputStream& stream) const
1162 {
1163 XMLNamespaces xmlns;
1164
1165 std::string prefix = getPrefix();
1166
1167 if (prefix.empty())
1168 {
1169 XMLNamespaces* thisxmlns = getNamespaces();
1170 if (thisxmlns && thisxmlns->hasURI(FbcExtension::getXmlnsL3V1V1()))
1171 {
1172 xmlns.add(FbcExtension::getXmlnsL3V1V1(),prefix);
1173 }
1174 }
1175
1176 stream << xmlns;
1177 }
1178
1179
1180 /** @endcond */
1181
1182 bool
isValidTypeForList(SBase * item)1183 ListOfFbcAssociations::isValidTypeForList(SBase * item)
1184 {
1185 int code = item->getTypeCode();
1186 return code == getItemTypeCode() || code == SBML_FBC_AND || code == SBML_FBC_OR || code == SBML_FBC_GENEPRODUCTREF ;
1187 }
1188
1189
1190 #endif /* __cplusplus */
1191
1192 LIBSBML_EXTERN
1193 FbcAssociation_t *
FbcAssociation_create(unsigned int level,unsigned int version,unsigned int pkgVersion)1194 FbcAssociation_create(unsigned int level, unsigned int version,
1195 unsigned int pkgVersion)
1196 {
1197 return new FbcAssociation(level, version, pkgVersion);
1198 }
1199
1200
1201 LIBSBML_EXTERN
1202 void
FbcAssociation_free(FbcAssociation_t * fa)1203 FbcAssociation_free(FbcAssociation_t * fa)
1204 {
1205 if (fa != NULL)
1206 delete fa;
1207 }
1208
1209
1210 LIBSBML_EXTERN
1211 FbcAssociation_t *
FbcAssociation_clone(FbcAssociation_t * fa)1212 FbcAssociation_clone(FbcAssociation_t * fa)
1213 {
1214 if (fa != NULL)
1215 {
1216 return static_cast<FbcAssociation_t*>(fa->clone());
1217 }
1218 else
1219 {
1220 return NULL;
1221 }
1222 }
1223
1224
1225 LIBSBML_EXTERN
1226 int
FbcAssociation_hasRequiredAttributes(const FbcAssociation_t * fa)1227 FbcAssociation_hasRequiredAttributes(const FbcAssociation_t * fa)
1228 {
1229 return (fa != NULL) ? static_cast<int>(fa->hasRequiredAttributes()) : 0;
1230 }
1231
1232
1233 LIBSBML_EXTERN
1234 FbcAssociation_t *
ListOfFbcAssociations_getById(ListOf_t * lo,const char * sid)1235 ListOfFbcAssociations_getById(ListOf_t * lo, const char * sid)
1236 {
1237 if (lo == NULL)
1238 return NULL;
1239
1240 return (sid != NULL) ? static_cast <ListOfFbcAssociations *>(lo)->get(sid) : NULL;
1241 }
1242
1243
1244 LIBSBML_EXTERN
1245 FbcAssociation_t *
ListOfFbcAssociations_removeById(ListOf_t * lo,const char * sid)1246 ListOfFbcAssociations_removeById(ListOf_t * lo, const char * sid)
1247 {
1248 if (lo == NULL)
1249 return NULL;
1250
1251 return (sid != NULL) ? static_cast <ListOfFbcAssociations *>(lo)->remove(sid) : NULL;
1252 }
1253
1254
1255 LIBSBML_EXTERN
1256 char *
FbcAssociation_toInfix(const FbcAssociation_t * fa)1257 FbcAssociation_toInfix(const FbcAssociation_t * fa)
1258 {
1259 return (fa != NULL) ? safe_strdup(fa->toInfix().c_str()) : NULL;
1260 }
1261
1262
1263 FbcAssociation_t*
FbcAssociation_parseFbcInfixAssociation(const char * infix,SBasePlugin_t * plugin)1264 FbcAssociation_parseFbcInfixAssociation(const char * infix, SBasePlugin_t* plugin)
1265 {
1266 if (infix == NULL || plugin == NULL)
1267 {
1268 return NULL;
1269 }
1270
1271 return FbcAssociation::parseFbcInfixAssociation(infix, static_cast<FbcModelPlugin*>(plugin));
1272 }
1273
1274
1275 LIBSBML_CPP_NAMESPACE_END
1276
1277
1278
1279