1 /**
2  * @file:   SpeciesFeatureValue.cpp
3  * @brief:  Implementation of the SpeciesFeatureValue 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) 2009-2013 jointly by the following organizations:
16  *     1. California Institute of Technology, Pasadena, CA, USA
17  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
18  *
19  * Copyright (C) 2006-2008 by the California Institute of Technology,
20  *     Pasadena, CA, USA
21  *
22  * Copyright (C) 2002-2005 jointly by the following organizations:
23  *     1. California Institute of Technology, Pasadena, CA, USA
24  *     2. Japan Science and Technology Agency, Japan
25  *
26  * This library is free software; you can redistribute it and/or modify it
27  * under the terms of the GNU Lesser General Public License as published by
28  * the Free Software Foundation.  A copy of the license agreement is provided
29  * in the file named "LICENSE.txt" included with this software distribution
30  * and also available online as http://sbml.org/software/libsbml/license.html
31  * ------------------------------------------------------------------------ -->
32  */
33 
34 
35 #include <sbml/packages/multi/sbml/SpeciesFeatureValue.h>
36 #include <sbml/packages/multi/validator/MultiSBMLError.h>
37 
38 
39 using namespace std;
40 
41 
42 #ifdef __cplusplus
43 
44 LIBSBML_CPP_NAMESPACE_BEGIN
45 
46 
47 /*
48  * Creates a new SpeciesFeatureValue with the given level, version, and package version.
49  */
SpeciesFeatureValue(unsigned int level,unsigned int version,unsigned int pkgVersion)50 SpeciesFeatureValue::SpeciesFeatureValue (unsigned int level, unsigned int version, unsigned int pkgVersion)
51   : SBase(level, version)
52    ,mValue ("")
53 {
54   // set an SBMLNamespaces derived object of this package
55   setSBMLNamespacesAndOwn(new MultiPkgNamespaces(level, version, pkgVersion));
56 }
57 
58 
59 /*
60  * Creates a new SpeciesFeatureValue with the given MultiPkgNamespaces object.
61  */
SpeciesFeatureValue(MultiPkgNamespaces * multins)62 SpeciesFeatureValue::SpeciesFeatureValue (MultiPkgNamespaces* multins)
63   : SBase(multins)
64    ,mValue ("")
65 {
66   // set the element namespace of this object
67   setElementNamespace(multins->getURI());
68 
69   // load package extensions bound with this object (if any)
70   loadPlugins(multins);
71 }
72 
73 
74 /*
75  * Copy constructor for SpeciesFeatureValue.
76  */
SpeciesFeatureValue(const SpeciesFeatureValue & orig)77 SpeciesFeatureValue::SpeciesFeatureValue (const SpeciesFeatureValue& orig)
78   : SBase(orig)
79   , mValue  ( orig.mValue)
80 {
81 }
82 
83 
84 /*
85  * Assignment for SpeciesFeatureValue.
86  */
87 SpeciesFeatureValue&
operator =(const SpeciesFeatureValue & rhs)88 SpeciesFeatureValue::operator=(const SpeciesFeatureValue& rhs)
89 {
90   if (&rhs != this)
91   {
92     SBase::operator=(rhs);
93     mValue  = rhs.mValue;
94   }
95   return *this;
96 }
97 
98 
99 /*
100  * Clone for SpeciesFeatureValue.
101  */
102 SpeciesFeatureValue*
clone() const103 SpeciesFeatureValue::clone () const
104 {
105   return new SpeciesFeatureValue(*this);
106 }
107 
108 
109 /*
110  * Destructor for SpeciesFeatureValue.
111  */
~SpeciesFeatureValue()112 SpeciesFeatureValue::~SpeciesFeatureValue ()
113 {
114 }
115 
116 
117 /*
118  * Returns the value of the "value" attribute of this SpeciesFeatureValue.
119  */
120 const std::string&
getValue() const121 SpeciesFeatureValue::getValue() const
122 {
123   return mValue;
124 }
125 
126 
127 /*
128  * Returns true/false if value is set.
129  */
130 bool
isSetValue() const131 SpeciesFeatureValue::isSetValue() const
132 {
133   return (mValue.empty() == false);
134 }
135 
136 
137 /*
138  * Sets value and returns value indicating success.
139  */
140 int
setValue(const std::string & value)141 SpeciesFeatureValue::setValue(const std::string& value)
142 {
143   if (!(SyntaxChecker::isValidInternalSId(value)))
144   {
145     return LIBSBML_INVALID_ATTRIBUTE_VALUE;
146   }
147   else
148   {
149     mValue = value;
150     return LIBSBML_OPERATION_SUCCESS;
151   }
152 }
153 
154 
155 /*
156  * Unsets value and returns value indicating success.
157  */
158 int
unsetValue()159 SpeciesFeatureValue::unsetValue()
160 {
161   mValue.erase();
162 
163   if (mValue.empty() == true)
164   {
165     return LIBSBML_OPERATION_SUCCESS;
166   }
167   else
168   {
169     return LIBSBML_OPERATION_FAILED;
170   }
171 }
172 
173 
174 /*
175  * rename attributes that are SIdRefs or instances in math
176  */
177 void
renameSIdRefs(const std::string & oldid,const std::string & newid)178 SpeciesFeatureValue::renameSIdRefs(const std::string& oldid, const std::string& newid)
179 {
180   SBase::renameSIdRefs(oldid, newid);
181   if (isSetValue() == true && mValue == oldid)
182   {
183     setValue(newid);
184   }
185 
186 }
187 
188 
189 /*
190  * Returns the XML element name of this object
191  */
192 const std::string&
getElementName() const193 SpeciesFeatureValue::getElementName () const
194 {
195   static const string name = "speciesFeatureValue";
196   return name;
197 }
198 
199 
200 /*
201  * Returns the libSBML type code for this SBML object.
202  */
203 int
getTypeCode() const204 SpeciesFeatureValue::getTypeCode () const
205 {
206   return SBML_MULTI_SPECIES_FEATURE_VALUE;
207 }
208 
209 
210 /*
211  * check if all the required attributes are set
212  */
213 bool
hasRequiredAttributes() const214 SpeciesFeatureValue::hasRequiredAttributes () const
215 {
216   bool allPresent = true;
217 
218   if (isSetValue() == false)
219     allPresent = false;
220 
221   return allPresent;
222 }
223 
224 
225   /** @cond doxygenLibsbmlInternal */
226 
227 /*
228  * write contained elements
229  */
230 void
writeElements(XMLOutputStream & stream) const231 SpeciesFeatureValue::writeElements (XMLOutputStream& stream) const
232 {
233   SBase::writeElements(stream);
234 
235   SBase::writeExtensionElements(stream);
236 }
237 
238 
239   /** @endcond */
240 
241 
242   /** @cond doxygenLibsbmlInternal */
243 
244 /*
245  * Accepts the given SBMLVisitor.
246  */
247 bool
accept(SBMLVisitor & v) const248 SpeciesFeatureValue::accept (SBMLVisitor& v) const
249 {
250   return v.visit(*this);
251 }
252 
253 
254   /** @endcond */
255 
256 
257   /** @cond doxygenLibsbmlInternal */
258 
259 /*
260  * Sets the parent SBMLDocument.
261  */
262 void
setSBMLDocument(SBMLDocument * d)263 SpeciesFeatureValue::setSBMLDocument (SBMLDocument* d)
264 {
265   SBase::setSBMLDocument(d);
266 }
267 
268 
269   /** @endcond */
270 
271 
272   /** @cond doxygenLibsbmlInternal */
273 
274 /*
275  * Enables/Disables the given package with this element.
276  */
277 void
enablePackageInternal(const std::string & pkgURI,const std::string & pkgPrefix,bool flag)278 SpeciesFeatureValue::enablePackageInternal(const std::string& pkgURI,
279              const std::string& pkgPrefix, bool flag)
280 {
281   SBase::enablePackageInternal(pkgURI, pkgPrefix, flag);
282 }
283 
284 
285   /** @endcond */
286 
287 
288   /** @cond doxygenLibsbmlInternal */
289 
290 /*
291  * Get the list of expected attributes for this element.
292  */
293 void
addExpectedAttributes(ExpectedAttributes & attributes)294 SpeciesFeatureValue::addExpectedAttributes(ExpectedAttributes& attributes)
295 {
296   SBase::addExpectedAttributes(attributes);
297 
298   attributes.add("value");
299 }
300 
301 
302   /** @endcond */
303 
304 
305   /** @cond doxygenLibsbmlInternal */
306 
307 /*
308  * Read values from the given XMLAttributes set into their specific fields.
309  */
310 void
readAttributes(const XMLAttributes & attributes,const ExpectedAttributes & expectedAttributes)311 SpeciesFeatureValue::readAttributes (const XMLAttributes& attributes,
312                              const ExpectedAttributes& expectedAttributes)
313 {
314   const unsigned int sbmlLevel   = getLevel  ();
315   const unsigned int sbmlVersion = getVersion();
316 
317   unsigned int numErrs;
318 
319   /* look to see whether an unknown attribute error was logged
320    * during the read of the listOfSpeciesFeatureValues - which will have
321    * happened immediately prior to this read
322   */
323 
324   ListOfSpeciesFeatureValues * parentListOf =
325       static_cast<ListOfSpeciesFeatureValues*>(getParentSBMLObject());
326 
327   if (getErrorLog() != NULL &&
328       parentListOf->size() < 2)
329   {
330     numErrs = getErrorLog()->getNumErrors();
331     for (int n = numErrs-1; n >= 0; n--)
332     {
333       if (getErrorLog()->getError(n)->getErrorId() == UnknownPackageAttribute)
334       {
335         const std::string details =
336               getErrorLog()->getError(n)->getMessage();
337         getErrorLog()->remove(UnknownPackageAttribute);
338         getErrorLog()->logPackageError("multi", MultiLofSpeFtrVals_AllowedAtts,
339                   getPackageVersion(), sbmlLevel, sbmlVersion, details,
340                   parentListOf->getLine(), parentListOf->getColumn());
341       }
342       else if (getErrorLog()->getError(n)->getErrorId() == UnknownCoreAttribute)
343       {
344         const std::string details =
345                    getErrorLog()->getError(n)->getMessage();
346         getErrorLog()->remove(UnknownCoreAttribute);
347         getErrorLog()->logPackageError("multi", MultiLofSpeFtrVals_AllowedAtts,
348                   getPackageVersion(), sbmlLevel, sbmlVersion, details,
349                   parentListOf->getLine(), parentListOf->getColumn());
350       }
351     }
352   }
353 
354   SBase::readAttributes(attributes, expectedAttributes);
355 
356   // look to see whether an unknown attribute error was logged
357   if (getErrorLog() != NULL)
358   {
359     numErrs = getErrorLog()->getNumErrors();
360     for (int n = numErrs-1; n >= 0; n--)
361     {
362       if (getErrorLog()->getError(n)->getErrorId() == UnknownPackageAttribute)
363       {
364         const std::string details =
365                           getErrorLog()->getError(n)->getMessage();
366         getErrorLog()->remove(UnknownPackageAttribute);
367         getErrorLog()->logPackageError("multi", MultiSpeFtrVal_AllowedMultiAtts,
368                        getPackageVersion(), sbmlLevel, sbmlVersion, details,
369                        getLine(), getColumn());
370       }
371       else if (getErrorLog()->getError(n)->getErrorId() == UnknownCoreAttribute)
372       {
373         const std::string details =
374                           getErrorLog()->getError(n)->getMessage();
375         getErrorLog()->remove(UnknownCoreAttribute);
376         getErrorLog()->logPackageError("multi", MultiSpeFtrVal_AllowedCoreAtts,
377                        getPackageVersion(), sbmlLevel, sbmlVersion, details,
378                        getLine(), getColumn());
379       }
380     }
381   }
382 
383   bool assigned = false;
384 
385   //
386   // value SIdRef   ( use = "required" )
387   //
388   assigned = attributes.readInto("value", mValue);
389 
390   if (assigned == true)
391   {
392     // check string is not empty and correct syntax
393 
394     if (mValue.empty() == true)
395     {
396       logEmptyString(mValue, getLevel(), getVersion(), "<SpeciesFeatureValue>");
397     }
398     else if (SyntaxChecker::isValidSBMLSId(mValue) == false && getErrorLog() != NULL)
399     {
400         std::string details = "The syntax of the attribute value='" + mValue + "' does not conform.";
401         getErrorLog()->logPackageError("multi", MultiInvSIdSyn,
402                    getPackageVersion(), sbmlLevel, sbmlVersion, details,
403                    getLine(), getColumn());
404     }
405   }
406   else
407   {
408     std::string message = "Multi attribute 'value' is missing.";
409     getErrorLog()->logPackageError("multi", MultiSpeFtrVal_AllowedMultiAtts,
410                    getPackageVersion(), sbmlLevel, sbmlVersion, message, getLine(), getColumn());
411   }
412 
413 }
414 
415 
416   /** @endcond */
417 
418 
419   /** @cond doxygenLibsbmlInternal */
420 
421 /*
422  * Write values of XMLAttributes to the output stream.
423  */
424   void
writeAttributes(XMLOutputStream & stream) const425 SpeciesFeatureValue::writeAttributes (XMLOutputStream& stream) const
426 {
427   SBase::writeAttributes(stream);
428 
429   if (isSetValue() == true)
430     stream.writeAttribute("value", getPrefix(), mValue);
431 
432   SBase::writeExtensionAttributes(stream);
433 
434 }
435 
436 
437   /** @endcond */
438 
439 
440 /*
441  * Constructor
442  */
ListOfSpeciesFeatureValues(unsigned int level,unsigned int version,unsigned int pkgVersion)443 ListOfSpeciesFeatureValues::ListOfSpeciesFeatureValues(unsigned int level,
444                              unsigned int version,
445                              unsigned int pkgVersion)
446  : ListOf(level, version)
447 {
448   setSBMLNamespacesAndOwn(new MultiPkgNamespaces(level, version, pkgVersion));
449 }
450 
451 
452 /*
453  * Constructor
454  */
ListOfSpeciesFeatureValues(MultiPkgNamespaces * multins)455 ListOfSpeciesFeatureValues::ListOfSpeciesFeatureValues(MultiPkgNamespaces* multins)
456   : ListOf(multins)
457 {
458   setElementNamespace(multins->getURI());
459 }
460 
461 
462 /*
463  * Returns a deep copy of this ListOfSpeciesFeatureValues
464  */
465 ListOfSpeciesFeatureValues*
clone() const466 ListOfSpeciesFeatureValues::clone () const
467  {
468   return new ListOfSpeciesFeatureValues(*this);
469 }
470 
471 
472 /*
473  * Get a SpeciesFeatureValue from the ListOfSpeciesFeatureValues by index.
474  */
475 SpeciesFeatureValue*
get(unsigned int n)476 ListOfSpeciesFeatureValues::get(unsigned int n)
477 {
478   return static_cast<SpeciesFeatureValue*>(ListOf::get(n));
479 }
480 
481 
482 /*
483  * Get a SpeciesFeatureValue from the ListOfSpeciesFeatureValues by index.
484  */
485 const SpeciesFeatureValue*
get(unsigned int n) const486 ListOfSpeciesFeatureValues::get(unsigned int n) const
487 {
488   return static_cast<const SpeciesFeatureValue*>(ListOf::get(n));
489 }
490 
491 
492 /*
493  * Get a SpeciesFeatureValue from the ListOfSpeciesFeatureValues by id.
494  */
495 SpeciesFeatureValue*
get(const std::string & sid)496 ListOfSpeciesFeatureValues::get(const std::string& sid)
497 {
498   return const_cast<SpeciesFeatureValue*>(
499     static_cast<const ListOfSpeciesFeatureValues&>(*this).get(sid));
500 }
501 
502 
503 /*
504  * Get a SpeciesFeatureValue from the ListOfSpeciesFeatureValues by id.
505  */
506 const SpeciesFeatureValue*
get(const std::string & sid) const507 ListOfSpeciesFeatureValues::get(const std::string& sid) const
508 {
509   vector<SBase*>::const_iterator result;
510 
511   result = find_if( mItems.begin(), mItems.end(), IdEq<SpeciesFeatureValue>(sid) );
512   return (result == mItems.end()) ? 0 : static_cast <SpeciesFeatureValue*> (*result);
513 }
514 
515 
516 /*
517  * Removes the nth SpeciesFeatureValue from this ListOfSpeciesFeatureValues
518  */
519 SpeciesFeatureValue*
remove(unsigned int n)520 ListOfSpeciesFeatureValues::remove(unsigned int n)
521 {
522   return static_cast<SpeciesFeatureValue*>(ListOf::remove(n));
523 }
524 
525 
526 /*
527  * Removes the SpeciesFeatureValue from this ListOfSpeciesFeatureValues with the given identifier
528  */
529 SpeciesFeatureValue*
remove(const std::string & sid)530 ListOfSpeciesFeatureValues::remove(const std::string& sid)
531 {
532   SBase* item = NULL;
533   vector<SBase*>::iterator result;
534 
535   result = find_if( mItems.begin(), mItems.end(), IdEq<SpeciesFeatureValue>(sid) );
536 
537   if (result != mItems.end())
538   {
539     item = *result;
540     mItems.erase(result);
541   }
542 
543   return static_cast <SpeciesFeatureValue*> (item);
544 }
545 
546 
547 /*
548  * Returns the XML element name of this object
549  */
550 const std::string&
getElementName() const551 ListOfSpeciesFeatureValues::getElementName () const
552 {
553   static const string name = "listOfSpeciesFeatureValues";
554   return name;
555 }
556 
557 
558 /*
559  * Returns the libSBML type code for this SBML object.
560  */
561 int
getTypeCode() const562 ListOfSpeciesFeatureValues::getTypeCode () const
563 {
564   return SBML_LIST_OF;
565 }
566 
567 
568 /*
569  * Returns the libSBML type code for the objects in this LIST_OF.
570  */
571 int
getItemTypeCode() const572 ListOfSpeciesFeatureValues::getItemTypeCode () const
573 {
574   return SBML_MULTI_SPECIES_FEATURE_VALUE;
575 }
576 
577 
578   /** @cond doxygenLibsbmlInternal */
579 
580 /*
581  * Creates a new SpeciesFeatureValue in this ListOfSpeciesFeatureValues
582  */
583 SBase*
createObject(XMLInputStream & stream)584 ListOfSpeciesFeatureValues::createObject(XMLInputStream& stream)
585 {
586   const std::string& name   = stream.peek().getName();
587   SBase* object = NULL;
588 
589   if (name == "speciesFeatureValue")
590   {
591     MULTI_CREATE_NS(multins, getSBMLNamespaces());
592     object = new SpeciesFeatureValue(multins);
593     appendAndOwn(object);
594     delete multins;
595   }
596 
597   return object;
598 }
599 
600 
601   /** @endcond */
602 
603 
604   /** @cond doxygenLibsbmlInternal */
605 
606 /*
607  * Write the namespace for the Multi package.
608  */
609 void
writeXMLNS(XMLOutputStream & stream) const610 ListOfSpeciesFeatureValues::writeXMLNS(XMLOutputStream& stream) const
611 {
612   XMLNamespaces xmlns;
613 
614   std::string prefix = getPrefix();
615 
616   if (prefix.empty())
617   {
618     XMLNamespaces* thisxmlns = getNamespaces();
619     if (thisxmlns && thisxmlns->hasURI(MultiExtension::getXmlnsL3V1V1()))
620     {
621       xmlns.add(MultiExtension::getXmlnsL3V1V1(),prefix);
622     }
623   }
624 
625   stream << xmlns;
626 }
627 
628 
629   /** @endcond */
630 
631 
632 LIBSBML_EXTERN
633 SpeciesFeatureValue_t *
SpeciesFeatureValue_create(unsigned int level,unsigned int version,unsigned int pkgVersion)634 SpeciesFeatureValue_create(unsigned int level, unsigned int version,
635                            unsigned int pkgVersion)
636 {
637   return new SpeciesFeatureValue(level, version, pkgVersion);
638 }
639 
640 
641 LIBSBML_EXTERN
642 void
SpeciesFeatureValue_free(SpeciesFeatureValue_t * sfv)643 SpeciesFeatureValue_free(SpeciesFeatureValue_t * sfv)
644 {
645   if (sfv != NULL)
646     delete sfv;
647 }
648 
649 
650 LIBSBML_EXTERN
651 SpeciesFeatureValue_t *
SpeciesFeatureValue_clone(SpeciesFeatureValue_t * sfv)652 SpeciesFeatureValue_clone(SpeciesFeatureValue_t * sfv)
653 {
654   if (sfv != NULL)
655   {
656     return static_cast<SpeciesFeatureValue_t*>(sfv->clone());
657   }
658   else
659   {
660     return NULL;
661   }
662 }
663 
664 
665 LIBSBML_EXTERN
666 char *
SpeciesFeatureValue_getValue(SpeciesFeatureValue_t * sfv)667 SpeciesFeatureValue_getValue(SpeciesFeatureValue_t * sfv)
668 {
669   if (sfv == NULL)
670     return NULL;
671 
672   return sfv->getValue().empty() ? NULL : safe_strdup(sfv->getValue().c_str());
673 }
674 
675 
676 LIBSBML_EXTERN
677 int
SpeciesFeatureValue_isSetValue(SpeciesFeatureValue_t * sfv)678 SpeciesFeatureValue_isSetValue(SpeciesFeatureValue_t * sfv)
679 {
680   return (sfv != NULL) ? static_cast<int>(sfv->isSetValue()) : 0;
681 }
682 
683 
684 LIBSBML_EXTERN
685 int
SpeciesFeatureValue_setValue(SpeciesFeatureValue_t * sfv,const char * value)686 SpeciesFeatureValue_setValue(SpeciesFeatureValue_t * sfv, const char * value)
687 {
688   return (sfv != NULL) ? sfv->setValue(value) : LIBSBML_INVALID_OBJECT;
689 }
690 
691 
692 LIBSBML_EXTERN
693 int
SpeciesFeatureValue_unsetValue(SpeciesFeatureValue_t * sfv)694 SpeciesFeatureValue_unsetValue(SpeciesFeatureValue_t * sfv)
695 {
696   return (sfv != NULL) ? sfv->unsetValue() : LIBSBML_INVALID_OBJECT;
697 }
698 
699 
700 LIBSBML_EXTERN
701 int
SpeciesFeatureValue_hasRequiredAttributes(SpeciesFeatureValue_t * sfv)702 SpeciesFeatureValue_hasRequiredAttributes(SpeciesFeatureValue_t * sfv)
703 {
704   return (sfv != NULL) ? static_cast<int>(sfv->hasRequiredAttributes()) : 0;
705 }
706 
707 
708 LIBSBML_EXTERN
709 SpeciesFeatureValue_t *
ListOfSpeciesFeatureValues_getById(ListOf_t * lo,const char * sid)710 ListOfSpeciesFeatureValues_getById(ListOf_t * lo, const char * sid)
711 {
712   if (lo == NULL)
713     return NULL;
714 
715   return (sid != NULL) ? static_cast <ListOfSpeciesFeatureValues *>(lo)->get(sid) : NULL;
716 }
717 
718 
719 LIBSBML_EXTERN
720 SpeciesFeatureValue_t *
ListOfSpeciesFeatureValues_removeById(ListOf_t * lo,const char * sid)721 ListOfSpeciesFeatureValues_removeById(ListOf_t * lo, const char * sid)
722 {
723   if (lo == NULL)
724     return NULL;
725 
726   return (sid != NULL) ? static_cast <ListOfSpeciesFeatureValues *>(lo)->remove(sid) : NULL;
727 }
728 
729 
730 
731 
732 LIBSBML_CPP_NAMESPACE_END
733 
734 #endif /*__cplusplus */
735 
736 
737