libSBML C API  5.18.0
SBMLExtension_t Class Reference

Detailed Description

Base class for SBML Level 3 package plug-ins.

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_t 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_t class provides methods for managing common attributes of package extensions (e.g., package name, package version), registration of instantiated SBasePluginCreator_t objects, and initialization/registration of package extensions when the library code for the package is loaded.

How to extend SBMLExtension_t for a package implementation

Each package implementation must contain a class that extends SBMLExtension_t. For example, the class GroupsExtension_t 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.

1. Define the getPackageName() method

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:

const std::string& GroupsExtension_t::getPackageName ()
{
static const std::string pkgName = "groups";
return pkgName;
}

2. Define methods returning package version information

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:

unsigned int GroupsExtension_t::getDefaultLevel()
{
return 3;
}
unsigned int GroupsExtension_t::getDefaultVersion()
{
return 1;
}
unsigned int GroupsExtension_t::getDefaultPackageVersion()
{
return 1;
}

3. Define methods returning the package namespace URIs

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().

const std::string& GroupsExtension_t::getXmlnsL3V1V1 ()
{
static const std::string xmlns = "http://www.sbml.org/sbml/level3/version1/groups/version1";
return xmlns;
}

Define other similar methods to return additional namespace URIs if the package extension implements other package versions or supports other SBML Level/Version combinations.

4. Override basic pure virtual methods

Override the following pure virtual methods on SBMLExtension_t:

  • 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 version
  • virtual SBMLExtension_t* 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:

const std::string& GroupsExtension_t::getName() const
{
return getPackageName();
}
unsigned int GroupsExtension_t::getLevel(const std::string &uri) const
{
if (uri == getXmlnsL3V1V1())
return 3;
else
return 0;
}
unsigned int GroupsExtension_t::getVersion(const std::string &uri) const
{
if (uri == getXmlnsL3V1V1())
return 1;
else
return 0;
}
unsigned int GroupsExtension_t::getPackageVersion(const std::string &uri) const
{
if (uri == getXmlnsL3V1V1())
return 1;
else
return 0;
}
const std::string& GroupsExtension_t::getURI(unsigned int sbmlLevel,
unsigned int sbmlVersion,
unsigned int pkgVersion) const
{
if (sbmlLevel == 3 && sbmlVersion == 1 && pkgVersion == 1)
return getXmlnsL3V1V1();
static std::string empty = "";
return empty;
}
GroupsExtension_t* GroupsExtension_t::clone() const
{
return new GroupsExtension_t(*this);
}

Constructor, copy constructor, and destructor methods also must be overridden if additional data members are defined in the derived class.

5. Create SBMLExtensionNamespaces_t-related definitions

Define typedef and template instantiation code for a package-specific subclass of the SBMLExtensionNamespaces_t template class. The SBMLExtensionNamespaces_t template class is a derived class of SBMLNamespaces_t and can be used as an argument of constructors of SBase_t-derived classes defined in the package extensions.

  1. Define a typedef. For example, the typedef for GroupsExtension_t is implemented in the file GroupsExtension.h as follows:

    // GroupsPkgNamespaces_t is derived from the SBMLNamespaces_t class.
    // It is used when creating a Groups package object of a class
    // derived from SBase_t.
    typedef SBMLExtensionNamespaces_t<GroupsExtension_t> GroupsPkgNamespaces_t;

  2. Define a template instantiation for the typedef. For example, the template instantiation code for GroupsExtension_t is implemented in the file GroupsExtension.cpp as follows:

    template class LIBSBML_EXTERN SBMLExtensionNamespaces_t<GroupsExtension_t>;

Here is example of how the resulting class is used. The definitions above allow a GroupsPkgNamespaces_t object to be used when creating a new Group_t object. The GroupsPkgNamespaces_t is handed to the constructor as an argument, as shown below:

GroupPkgNamespaces gpns(3, 1, 1); // SBML Level, Version, & pkg version.
Group_t g = new Group_t(&gpns); // Creates a Group_t object.

The GroupsPkgNamespaces_t object can also be used when creating an SBMLDocument_t object with the Groups package. The code fragment below shows an example of this:

GroupsPkgNamespaces_t gpns(3, 1, 1);
doc = new SBMLDocument_t(&gnps);

6. Override the method getSBMLExtensionNamespaces()

Override the pure virtual method getSBMLExtensionNamespaces(), which returns an SBMLNamespaces_t derived object. For example, the method is overridden in the class GroupsExtension_t as follows:

GroupsExtension_t::getSBMLExtensionNamespaces(const std::string &uri) const
{
GroupsPkgNamespaces_t* pkgns = NULL;
if ( uri == getXmlnsL3V1V1())
{
pkgns = new GroupsPkgNamespaces_t(3, 1, 1);
}
return pkgns;
}

7. Define an enumeration for the package object type codes

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_t class (for the <group> element defined by the SBML Level 3 Groups package) and SBML_GROUPS_MEMBER corresponds to the Member_t class (for the <member> element defined by the Level 3 Groups package), respectively.

Similarly, SBMLLayoutTypeCode_t for the Layout_t 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_t::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:

void example (const SBase_t *sb)
{
const std::string pkgName = sb->getPackageName();
if (pkgName == "core") {
switch (sb->getTypeCode()) {
case SBML_MODEL:
....
break;
....
}
}
else if (pkgName == "layout") {
switch (sb->getTypeCode()) {
....
break;
....
}
}
else if (pkgName == "groups") {
switch (sb->getTypeCode()) {
....
break;
....
}
}
...
}

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.

8. Override the method getStringFromTypeCode()

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:

virtual const char* SBMLExtension_t::(int typeCode) const;

For example, the method for the Groups extension is implemented as shown below:

static const char* SBML_GROUPS_TYPECODE_STRINGS[] =
{
"Group_t"
, "Member_t"
};
const char* GroupsExtension_t::getStringFromTypeCode(int typeCode) const
{
int min = SBML_GROUPS_GROUP;
int max = SBML_GROUPS_MEMBER;
if (typeCode < min || typeCode > max)
{
return "(Unknown SBML Groups Type)";
}
return SBML_GROUPS_TYPECODE_STRINGS[typeCode - min];
}

9. Implement an init() method

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_t class.

For example, the init() method for the Groups package is implemented as follows:

void GroupsExtension_t::init()
{
// 1. Check if the Groups package has already been registered.
if ( SBMLExtensionRegistry_t::getInstance().isRegistered(getPackageName()) )
{
// do nothing;
return;
}
// 2. Create an SBMLExtension_t derived object.
// 3. Create SBasePluginCreator_t-derived objects. The derived classes
// can be instantiated by using the following template class:
//
// template<class SBasePluginType> class SBasePluginCreator_t
//
// The constructor of the creator class takes two arguments:
//
// 1) SBaseExtensionPoint_t: extension point to which the plugin connects
// 2) std::vector<std::string>: a vector that contains a list of URI
// (package versions) supported by the plugin object.
//
// For example, two plugin objects are required as part of the Groups
// implementation: one plugged into SBMLDocument_t and one into Model_t.
// For the former, since the specification for the SBML Groups package
// mandates that the 'required' flag is always 'false', the existing
// SBMLDocumentPluginNotRequired_t class can be used as-is as part of
// the implementation. For Model_t, since the lists of supported
// package versions (currently only SBML L3V1 Groups V1) are equal
// in the both plugin objects, the same vector can be handed to each
// constructor.
std::vector<std::string> pkgURIs;
pkgURIs.push_back(getXmlnsL3V1V1());
SBaseExtensionPoint_t docExtPoint("core", SBML_DOCUMENT);
SBaseExtensionPoint_t modelExtPoint("core", SBML_MODEL);
SBasePluginCreator_t<GroupsModelPlugin_t, GroupsExtension_t> modelPluginCreator(modelExtPoint, pkgURIs);
// 4. Add the above objects to the SBMLExtension_t-derived object.
gext.addSBasePluginCreator(&docPluginCreator);
gext.addSBasePluginCreator(&modelPluginCreator);
// 5. Register the SBMLExtension_t-derived object with the extension
// registry, SBMLExtensionRegistry_t.
int result = SBMLExtensionRegistry_t::getInstance().addExtension(&gext);
{
std::cerr << "[Error] GroupsExtension_t::init() failed." << std::endl;
}
}

10. Instantiate a SBMLExtensionRegister_t object

Instantiate a global SBMLExtensionRegister_t object using the class derived from SBMLExtension_t (discussed above). Here is an example for the Groups package extension, for the object GroupsExtension_t. This could is placed in the GroupsExtension.cpp:

static SBMLExtensionRegister_t<GroupsExtension_t> groupsExtensionRegister;

The init() method on GroupsExtension_t is automatically invoked when the "register" object is instantiated. This results in initialization and registration of the package extension with libSBML.

Special handling for SBML Level 2

Due to the historical background of the SBML Layout package, libSBML implements special behavior for that package: it always creates a Layout plugin object for any SBML Level 2 document it reads in, regardless of whether that document actually uses Layout constructs. This is unlike the case for SBML Level 3 documents that use Layout; for them, libSBML will not create a plugin object unless the document actually declares the use of the Layout package (via the usual Level 3 namespace declaration for Level 3 packages).

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_t class that must be implemented by individual package extensions. These methods are SBMLExtension_t::addL2Namespaces(), SBMLExtension_t::removeL2Namespaces(), and SBMLExtension_t::enableL2NamespaceForDocument().

Examples:
spec_example1.c.

Public Member Functions

int SBMLExtension_addSBasePluginCreator (SBMLExtension_t *ext, SBasePluginCreatorBase_t *sbaseExt)
 Adds the given SBasePluginCreatorBase_t structure to this package extension. More...
 
SBMLExtension_tSBMLExtension_clone (SBMLExtension_t *ext)
 Creates a deep copy of the given SBMLExtension_t structure. More...
 
int SBMLExtension_free (SBMLExtension_t *ext)
 Frees the given SBMLExtension_t structure. More...
 
unsigned int SBMLExtension_getLevel (SBMLExtension_t *ext, const char *uri)
 Returns the SBML level associated with the given URI of this package. More...
 
const char * SBMLExtension_getName (SBMLExtension_t *ext)
 Returns the name of the package extension. More...
 
int SBMLExtension_getNumOfSBasePlugins (SBMLExtension_t *ext)
 Returns the number of SBasePlugin_t structures stored in the structure. More...
 
int SBMLExtension_getNumOfSupportedPackageURI (SBMLExtension_t *ext)
 Returns the number of supported package namespaces (package versions) for this package extension. More...
 
unsigned int SBMLExtension_getPackageVersion (SBMLExtension_t *ext, const char *uri)
 Returns the package version associated with the given URI of this package. More...
 
SBasePluginCreatorBase_tSBMLExtension_getSBasePluginCreator (SBMLExtension_t *ext, SBaseExtensionPoint_t *extPoint)
 Returns an SBasePluginCreatorBase_t structure of this package extension bound to the given extension point. More...
 
SBasePluginCreatorBase_tSBMLExtension_getSBasePluginCreatorByIndex (SBMLExtension_t *ext, unsigned int index)
 Returns an SBasePluginCreatorBase_t structure of this package extension with the given index. More...
 
SBMLNamespaces_tSBMLExtension_getSBMLExtensionNamespaces (SBMLExtension_t *ext, const char *uri)
 Returns an SBMLNamespaces_t structure corresponding to the given uri. More...
 
const char * SBMLExtension_getStringFromTypeCode (SBMLExtension_t *ext, int typeCode)
 This method takes a type code of this package and returns a string representing the code. More...
 
const char * SBMLExtension_getSupportedPackageURI (SBMLExtension_t *ext, unsigned int index)
 Returns the package URI (package version) for the given index. More...
 
const char * SBMLExtension_getURI (SBMLExtension_t *ext, unsigned int sbmlLevel, unsigned int sbmlVersion, unsigned int pkgVersion)
 Returns the uri corresponding to the given SBML level, SBML version, and package version for this extension. More...
 
unsigned int SBMLExtension_getVersion (SBMLExtension_t *ext, const char *uri)
 Returns the SBML version associated with the given URI of this package. More...
 
int SBMLExtension_isEnabled (SBMLExtension_t *ext)
 Check if this package is enabled or disabled. More...
 
int SBMLExtension_isSupported (SBMLExtension_t *ext, const char *uri)
 Returns a flag indicating whether the given URI (package version) is supported by this package extension. More...
 
int SBMLExtension_setEnabled (SBMLExtension_t *ext, int isEnabled)
 Enable/disable this package. More...
 

Member Function Documentation

int SBMLExtension_addSBasePluginCreator ( SBMLExtension_t ext,
SBasePluginCreatorBase_t sbaseExt 
)

Adds the given SBasePluginCreatorBase_t structure to this package extension.

Parameters
extthe SBMLExtension_t structure to be freed.
sbaseExtthe SBasePluginCreatorBase_t structure bound to some SBML element and creates a corresponding SBasePlugin_t structure of this package extension.
Returns
integer value indicating success/failure of the function. The value is drawn from the enumeration OperationReturnValues_t. The possible values returned by this function are:
SBMLExtension_t * SBMLExtension_clone ( SBMLExtension_t ext)

Creates a deep copy of the given SBMLExtension_t structure.

Parameters
extthe SBMLExtension_t structure to be copied.
Returns
a (deep) copy of the given SBMLExtension_t structure.
int SBMLExtension_free ( SBMLExtension_t ext)

Frees the given SBMLExtension_t structure.

Parameters
extthe SBMLExtension_t structure to be freed.
Returns
integer value indicating success/failure of the function. The value is drawn from the enumeration OperationReturnValues_t. The possible values returned by this function are:
unsigned int SBMLExtension_getLevel ( SBMLExtension_t ext,
const char *  uri 
)

Returns the SBML level associated with the given URI of this package.

Parameters
extthe SBMLExtension_t structure.
urithe string of URI that represents a versions of the package.
Returns
the SBML level associated with the given URI of this package.
const char * SBMLExtension_getName ( SBMLExtension_t ext)

Returns the name of the package extension.

(e.g. "layout", "multi").

Parameters
extthe SBMLExtension_t structure.
Returns
the name of the package extension. (e.g. "layout", "multi").
int SBMLExtension_getNumOfSBasePlugins ( SBMLExtension_t ext)

Returns the number of SBasePlugin_t structures stored in the structure.

Parameters
extthe SBMLExtension_t structure.
Returns
the number of SBasePlugin_t structures stored in the structure, or LIBSBML_INVALID_OBJECT.
int SBMLExtension_getNumOfSupportedPackageURI ( SBMLExtension_t ext)

Returns the number of supported package namespaces (package versions) for this package extension.

Parameters
extthe SBMLExtension_t structure.
Returns
the number of supported package namespaces (package versions) for this package extension or LIBSBML_INVALID_OBJECT.
unsigned int SBMLExtension_getPackageVersion ( SBMLExtension_t ext,
const char *  uri 
)

Returns the package version associated with the given URI of this package.

Parameters
extthe SBMLExtension_t structure.
urithe string of URI that represents a versions of the package.
Returns
the package version associated with the given URI of this package.
SBasePluginCreatorBase_t * SBMLExtension_getSBasePluginCreator ( SBMLExtension_t ext,
SBaseExtensionPoint_t extPoint 
)

Returns an SBasePluginCreatorBase_t structure of this package extension bound to the given extension point.

Parameters
extthe SBMLExtension_t structure.
extPointthe SBaseExtensionPoint_t to which the returned SBasePluginCreatorBase_t structure bound.
Returns
an SBasePluginCreatorBase_t structure of this package extension bound to the given extension point, or NULL for invalid extension of extension point.
SBasePluginCreatorBase_t * SBMLExtension_getSBasePluginCreatorByIndex ( SBMLExtension_t ext,
unsigned int  index 
)

Returns an SBasePluginCreatorBase_t structure of this package extension with the given index.

Parameters
extthe SBMLExtension_t structure.
indexthe index of the returned SBasePluginCreatorBase_t structure for this package extension.
Returns
an SBasePluginCreatorBase_t structure of this package extension with the given index, or NULL for an invalid extension structure.
SBMLNamespaces_t * SBMLExtension_getSBMLExtensionNamespaces ( SBMLExtension_t ext,
const char *  uri 
)

Returns an SBMLNamespaces_t structure corresponding to the given uri.

NULL will be returned if the given uri is not defined in the corresponding package.

Parameters
extthe SBMLExtension_t structure.
urithe string of URI that represents one of versions of the package.
Returns
an SBMLNamespaces_t structure corresponding to the uri. NULL will be returned if the given uri is not defined in the corresponding package or an invalid extension structure was provided.
const char * SBMLExtension_getStringFromTypeCode ( SBMLExtension_t ext,
int  typeCode 
)

This method takes a type code of this package and returns a string representing the code.

Parameters
extthe SBMLExtension_t structure.
typeCodethe typeCode supported by the package.
Returns
the string representing the given typecode, or NULL in case an invalid extension was provided.
const char * SBMLExtension_getSupportedPackageURI ( SBMLExtension_t ext,
unsigned int  index 
)

Returns the package URI (package version) for the given index.

Parameters
extthe SBMLExtension_t structure.
indexthe index of the supported package uri to return.
Returns
the package URI (package version) for the given index or NULL.
const char * SBMLExtension_getURI ( SBMLExtension_t ext,
unsigned int  sbmlLevel,
unsigned int  sbmlVersion,
unsigned int  pkgVersion 
)

Returns the uri corresponding to the given SBML level, SBML version, and package version for this extension.

Parameters
extthe SBMLExtension_t structure.
sbmlLevelthe level of SBML.
sbmlVersionthe version of SBML.
pkgVersionthe version of the package.
Returns
a string of the package URI
Examples:
spec_example1.c.
unsigned int SBMLExtension_getVersion ( SBMLExtension_t ext,
const char *  uri 
)

Returns the SBML version associated with the given URI of this package.

Parameters
extthe SBMLExtension_t structure.
urithe string of URI that represents a versions of the package.
Returns
the SBML version associated with the given URI of this package.
int SBMLExtension_isEnabled ( SBMLExtension_t ext)

Check if this package is enabled or disabled.

Parameters
extthe SBMLExtension_t structure.
Returns
1 (true) if the package is enabled, otherwise 0 (false) is returned. If the extension is invalid, LIBSBML_INVALID_OBJECT will be returned.
int SBMLExtension_isSupported ( SBMLExtension_t ext,
const char *  uri 
)

Returns a flag indicating whether the given URI (package version) is supported by this package extension.

Parameters
extthe SBMLExtension_t structure.
urithe package uri.
Returns
1 (true) if the given URI (package version) is supported by this package extension, otherwise 0 (false) is returned.
int SBMLExtension_setEnabled ( SBMLExtension_t ext,
int  isEnabled 
)

Enable/disable this package.

Parameters
extthe SBMLExtension_t structure.
isEnabledthe value to set : 1 (true; enabled) or 0 (false; disabled).
Returns
1 (true) if this function call succeeded, otherwise 0 (false) is returned. If the extension is invalid, LIBSBML_INVALID_OBJECT will be returned.