libSBML C++ API
5.20.2
|
This class of objects is defined by libSBML only and has no direct equivalent in terms of SBML components. This class is not prescribed by the SBML specifications, although it is used to implement features defined in SBML.
The SBMLExtension class is a component of the libSBML package extension mechanism. It is an abstract class that is extended by each package extension implementation. The SBMLExtension class provides methods for managing common attributes of package extensions (e.g., package name, package version), registration of instantiated SBasePluginCreator objects, and initialization/registration of package extensions when the library code for the package is loaded.
GroupsExtension
serves this purpose for the SBML Level 3 Groups package extension in libSBML. The following subsections detail the basic steps involved in implementing such an extended class.Define a method named getPackageName()
that returns the name of the package as a string. The following is an example from the implementation of the Groups package extension:
Define a set of methods that return the default SBML Level, SBML Version and version of the package. These methods must be named getDefaultLevel()
, getDefaultVersion()
and getDefaultPackageVersion()
, respectively. The following are examples drawn from the Groups package implementation:
Define methods that return strings representing the XML namespace URI for the package. One method should be defined for each SBML Level/Version combination for which the package can be used. For instance, if a package is only usable in SBML Level 3 Version 1, and the libSBML extension for the package implements version 1 of the package, the necessary method is getXmlnsL3V1V1()
.
Define other similar methods to return additional namespace URIs if the package extension implements other package versions or supports other SBML Level/Version combinations.
Override the following pure virtual methods on SBMLExtension:
virtual const std::string& getName() const =0
. This method returns the nickname of the package (e.g., "layout", "groups").virtual unsigned int getLevel(const std::string& uri) const =0
. This method returns the SBML Level with the given URI of this package.virtual unsigned int getVersion(const std::string& uri) const =0
. This method returns the SBML Version with the given URI of this package.virtual unsigned int getPackageVersion(const std::string &uri) const =0
. This method returns the package version with the given URI of this package.virtual unsigned int getURI(unsigned int sbmlLevel, unsigned int sbmlVersion, unsigned int pkgVersion) const =0
. This method returns the URI (namespace) of the package corresponding to the combination of the given SBML Level, SBML Version, and package versionvirtual SBMLExtension* clone() const = 0
. This method creates and returns a deep copy of this derived object.As an example, the following are the versions of these methods for the Groups package:
Constructor, copy constructor, and destructor methods also must be overridden if additional data members are defined in the derived class.
Define typedef and template instantiation code for a package-specific subclass of the SBMLExtensionNamespaces template class. The SBMLExtensionNamespaces template class is a derived class of SBMLNamespaces and can be used as an argument of constructors of SBase-derived classes defined in the package extensions.
Define a typedef. For example, the typedef for GroupsExtension
is implemented in the file GroupsExtension.h
as follows:
Define a template instantiation for the typedef. For example, the template instantiation code for GroupsExtension is
implemented in the file GroupsExtension.cpp
as follows:
Here is example of how the resulting class is used. The definitions above allow a GroupsPkgNamespaces
object to be used when creating a new Group
object. The GroupsPkgNamespaces
is handed to the constructor as an argument, as shown below:
The GroupsPkgNamespaces
object can also be used when creating an SBMLDocument object with the Groups package. The code fragment below shows an example of this:
Override the pure virtual method getSBMLExtensionNamespaces()
, which returns an SBMLNamespaces derived object. For example, the method is overridden in the class GroupsExtension
as follows:
Define an enum type for representing the type code of the objects defined in the package extension. For example, the enumeration SBMLGroupsTypeCode_t
for the Groups package is defined in GroupsExtension.h
as follows:
In the enumeration above, SBML_GROUPS_GROUP
corresponds to the Group
class (for the <group>
element defined by the SBML Level 3 Groups package) and SBML_GROUPS_MEMBER
corresponds to the Member
class (for the <member>
element defined by the Level 3 Groups package), respectively.
Similarly, SBMLLayoutTypeCode_t for the Layout package is defined in the file LayoutExtension.h
as follows:
These enum values are returned by corresponding getTypeCode()
methods. (E.g., SBML_GROUPS_GROUP
is returned in Group::getTypeCode()
.)
Note that libSBML does not require that type codes are unique across all packages—the same type codes may be used within individual package extensions. LibSBML development must permit this because package implementations are developed by separate groups at different times; coordinating the type codes used is impractical. It does mean that callers must check two things when identifying objects: to distinguish the type codes of different packages, callers much check not only the return value of the method getTypeCode()
method but also that of the method getPackageName()
. Here is an example of doing that:
Readers may have noticed that in the SBMLLayoutTypeCode_t and SBMLGroupsTypeCode_t
enumerations above, unique values are in fact assigned to the enumeration values. This can be convenient when it can be arranged, but it is not required by libSBML.
Override the pure virtual method getStringFromTypeCode()
, which returns a string corresponding to the given type code. Here is an example, again drawn from the implementation of the Groups package:
For example, the method for the Groups extension is implemented as shown below:
Implement a static void init()
method in the derived class. This method serves to encapsulate initialization code that creates an instance of the derived class and registration code that registers the instance with the SBMLExtensionRegistry class.
For example, the init()
method for the Groups package is implemented as follows:
Instantiate a global SBMLExtensionRegister object using the class derived from SBMLExtension (discussed above). Here is an example for the Groups package extension, for the object GroupsExtension
. This could is placed in the GroupsExtension.cpp
:
The init()
method on GroupsExtension
is automatically invoked when the "register" object is instantiated. This results in initialization and registration of the package extension with libSBML.
This has the following consequence. If an application queries for the presence of Layout in an SBML Level 2 document by testing only for the existence of the plugin object, it will always get a positive result; in other words, the presence of a Layout extension object is not an indication of whether a read-in Level 2 document does or does not use SBML Layout. Instead, callers have to query explicitly for the existence of layout information. An example of such a query is the following code:
The special, always-available Level 2 Layout behavior was motivated by a desire to support legacy applications. In SBML Level 3, the Layout package uses the normal SBML Level 3 scheme of requiring declarations on the SBML document element. This means that upon reading a model, libSBML knows right away whether it contains layout information. In SBML Level 2, there is no top-level declaration because layout is stored as annotations in the body of the model. Detecting the presence of layout information when reading a Level 2 model requires parsing the annotations. For efficiency reasons, libSBML normally does not parse annotations automatically when reading a model. However, applications that predated the introduction of Level 3 Layout and the updated version of libSBML never had to do anything special to enable parsing layout; the facilities were always available for every Level 2 model as long as libSBML was compiled with Layout support. To avoid burdening developers of legacy applications with the need to modify their software, libSBML provides backward compatibility by always preloading the Layout package extension when reading Level 2 models. The same applies to the creation of Level 2 models: with the plugin-oriented libSBML, applications normally would have to take deliberate steps to activate package code, instantiate objects, manage namespaces, and so on. LibSBML again loads the Layout package plugin automatically when creating a Level 2 model, thereby making the APIs available to legacy applications without further work on their part.
The mechanisms for triggering this Level 2-specific behavior involves a set of virtual methods on the SBMLExtension class that must be implemented by individual package extensions. These methods are SBMLExtension::addL2Namespaces(), SBMLExtension::removeL2Namespaces(), and SBMLExtension::enableL2NamespaceForDocument().
Public Member Functions | |
virtual void | addL2Namespaces (XMLNamespaces *xmlns) const |
Adds the package's Level 2 namespace(s). More... | |
virtual SBMLExtension * | clone () const =0 |
Creates and returns a deep copy of this SBMLExtension object. More... | |
virtual void | enableL2NamespaceForDocument (SBMLDocument *doc) const |
Called to enable the package on the SBMLDocument object. More... | |
virtual unsigned int | getLevel (const std::string &uri) const =0 |
Returns the SBML Level associated with the given XML namespace URI. More... | |
virtual const std::string & | getName () const =0 |
Returns the nickname of this package. More... | |
int | getNumOfSBasePlugins () const |
Returns the number of SBasePluginCreatorBase objects stored in this object. More... | |
unsigned int | getNumOfSupportedPackageURI () const |
Returns the number of supported package namespace URIs. More... | |
virtual unsigned int | getPackageVersion (const std::string &uri) const =0 |
Returns the package version associated with the given XML namespace URI. More... | |
virtual SBMLNamespaces * | getSBMLExtensionNamespaces (const std::string &uri) const =0 |
Returns a specialized SBMLNamespaces object corresponding to a given namespace URI. More... | |
virtual const char * | getStringFromTypeCode (int typeCode) const =0 |
Returns a string representation of a type code. More... | |
const std::string & | getSupportedPackageURI (unsigned int n) const |
Returns the nth XML namespace URI. More... | |
virtual const std::string & | getURI (unsigned int sbmlLevel, unsigned int sbmlVersion, unsigned int pkgVersion) const =0 |
Returns the XML namespace URI for a given Level and Version. More... | |
virtual unsigned int | getVersion (const std::string &uri) const =0 |
Returns the SBML Version associated with the given XML namespace URI. More... | |
bool | isEnabled () const |
Returns true if this package is enabled. More... | |
virtual bool | isInUse (SBMLDocument *doc) const |
Indicates whether this extension is being used by the given SBMLDocument. More... | |
bool | isSupported (const std::string &uri) const |
Returns true if the given XML namespace URI is supported by this package extension. More... | |
SBMLExtension & | operator= (const SBMLExtension &rhs) |
Assignment operator for SBMLExtension. More... | |
virtual void | removeL2Namespaces (XMLNamespaces *xmlns) const |
Removes the package's Level 2 namespace(s). More... | |
SBMLExtension () | |
Constructor; creates a new SBMLExtension object. More... | |
SBMLExtension (const SBMLExtension &orig) | |
Copy constructor. More... | |
bool | setEnabled (bool isEnabled) |
Enable or disable this package. More... | |
virtual | ~SBMLExtension () |
Destroy this SBMLExtension object. More... | |
SBMLExtension::SBMLExtension | ( | ) |
Constructor; creates a new SBMLExtension object.
SBMLExtension::SBMLExtension | ( | const SBMLExtension & | orig | ) |
Copy constructor.
This creates a copy of an SBMLExtension object.
orig | the SBMLExtension object to copy. |
|
virtual |
Destroy this SBMLExtension object.
|
virtual |
Adds the package's Level 2 namespace(s).
This virtual method should be overridden by all package extensions that want to serialize to an SBML Level 2 annotation. In Level 2, the XML namespace declaration for the package is not placed on the top-level SBML document object but rather inside individual annotations. addL2Namespaces() is invoked automatically for Level 2 documents when an SBMLExtensionNamespace object is created; removeL2Namespaces() is automatically invoked by SBMLDocument to prevent the namespace(s) from being put on the top-level SBML Level 2 element (because Level 2 doesn't support namespaces there); and enableL2NamespaceForDocument() is called automatically when any SBML document (of any Level/Version) is read in.
xmlns | an XMLNamespaces object that will be used for the annotation. Implementation should override this method with something that adds the package's namespace(s) to the set of namespaces in xmlns . For instance, here is the code from the Layout package extension: if (!xmlns->containsUri( LayoutExtension::getXmlnsL2()))
xmlns->add(LayoutExtension::getXmlnsL2(), "layout");
static const std::string & getXmlnsL2() Returns the XML namespace URI of the SBML Level 2 version of the package implemented by this libSBML ... Definition: LayoutExtension.cpp:109 |
Reimplemented in RenderExtension, and LayoutExtension.
|
pure virtual |
Creates and returns a deep copy of this SBMLExtension object.
Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
|
virtual |
Called to enable the package on the SBMLDocument object.
This virtual method should be overridden by all package extensions that want to serialize to an SBML Level 2 annotation. In Level 2, the XML namespace declaration for the package is not placed on the top-level SBML document object but rather inside individual annotations. addL2Namespaces() is invoked automatically for Level 2 documents when an SBMLExtensionNamespace object is created; removeL2Namespaces() is automatically invoked by SBMLDocument to prevent the namespace(s) from being put on the top-level SBML Level 2 element (because Level 2 doesn't support namespaces there); and enableL2NamespaceForDocument() is called automatically when any SBML document (of any Level/Version) is read in.
doc | the SBMLDocument object for the model. Implementations should override this method with something that enables the package based on the package's namespace(s). For example, here is the code from the Layout package extension: if (doc->getLevel() == 2)
|
Reimplemented in RenderExtension, and LayoutExtension.
|
pure virtual |
Returns the SBML Level associated with the given XML namespace URI.
uri | the string of URI that represents a version of the package. |
Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
|
pure virtual |
Returns the nickname of this package.
This returns the short-form name of an SBML Level 3 package implemented by a given SBMLExtension-derived class. Examples of such names are "layout", "fbc", etc.
Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
int SBMLExtension::getNumOfSBasePlugins | ( | ) | const |
Returns the number of SBasePluginCreatorBase objects stored in this object.
unsigned int SBMLExtension::getNumOfSupportedPackageURI | ( | ) | const |
Returns the number of supported package namespace URIs.
|
pure virtual |
Returns the package version associated with the given XML namespace URI.
uri | the string of URI that represents a version of this package. |
Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
|
pure virtual |
Returns a specialized SBMLNamespaces object corresponding to a given namespace URI.
LibSBML package extensions each define a subclass of SBMLExtensionNamespaces. This object has the form
SBMLExtensionNamespaces<class SBMLExtensionType>
For example, this kind of object for the Layout package is
SBMLExtensionNamespaces<LayoutExtension>
The present method returns the appropriate object corresponding to the given XML namespace URI in argument uri
.
uri | the namespace URI that represents one of versions of the package implemented in this extension. |
NULL
if the given uri
is not defined in the corresponding package.Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
|
pure virtual |
Returns a string representation of a type code.
This method takes a numerical type code typeCode
for a component object implemented by this package extension, and returns a string representing that type code.
typeCode | the type code to turn into a string. |
typeCode
.Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
const std::string & SBMLExtension::getSupportedPackageURI | ( | unsigned int | n | ) | const |
Returns the nth XML namespace URI.
n | the index number of the namespace URI being sought. |
|
pure virtual |
Returns the XML namespace URI for a given Level and Version.
sbmlLevel | the SBML Level. |
sbmlVersion | the SBML Version. |
pkgVersion | the version of the package. |
Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
|
pure virtual |
Returns the SBML Version associated with the given XML namespace URI.
uri | the string of URI that represents a version of the package. |
Implemented in SpatialExtension, RenderExtension, QualExtension, MultiExtension, LayoutExtension, GroupsExtension, FbcExtension, DistribExtension, and CompExtension.
bool SBMLExtension::isEnabled | ( | ) | const |
Returns true
if this package is enabled.
true
if this package is enabled, false
otherwise.
|
virtual |
Indicates whether this extension is being used by the given SBMLDocument.
The default implementation returns true
. This means that when a document had this extension enabled, it will not be possible to convert it to SBML Level 2 as we cannot make sure that the extension can be converted.
doc | the SBML document to test. |
Reimplemented in RenderExtension, and LayoutExtension.
bool SBMLExtension::isSupported | ( | const std::string & | uri | ) | const |
Returns true
if the given XML namespace URI is supported by this package extension.
true
if the given XML namespace URI (equivalent to a package version) is supported by this package extension, false
otherwise. SBMLExtension & SBMLExtension::operator= | ( | const SBMLExtension & | rhs | ) |
Assignment operator for SBMLExtension.
rhs | the object whose values are used as the basis of the assignment. |
|
virtual |
Removes the package's Level 2 namespace(s).
This virtual method should be overridden by all package extensions that want to serialize to an SBML Level 2 annotation. In Level 2, the XML namespace declaration for the package is not placed on the top-level SBML document object but rather inside individual annotations. addL2Namespaces() is invoked automatically for Level 2 documents when an SBMLExtensionNamespace object is created; removeL2Namespaces() is automatically invoked by SBMLDocument to prevent the namespace(s) from being put on the top-level SBML Level 2 element (because Level 2 doesn't support namespaces there); and enableL2NamespaceForDocument() is called automatically when any SBML document (of any Level/Version) is read in.
xmlns | an XMLNamespaces object that will be used for the annotation. Implementations should override this method with something that removes the package's namespace(s) from the set of namespaces in xmlns . For instance, here is the code from the Layout package extension: for (int n = 0; n < xmlns->getNumNamespaces(); n++)
{
if (xmlns->getURI(n) == LayoutExtension::getXmlnsL2())
xmlns->remove(n);
}
|
Reimplemented in RenderExtension, and LayoutExtension.
bool SBMLExtension::setEnabled | ( | bool | isEnabled | ) |
Enable or disable this package.
isEnabled | flag indicating whether to enable (if true ) or disable (false ) this package extension. |
true
if this call succeeded; false
otherwise.