libSBML C API
5.18.0
|
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.
Abstract Syntax Trees (ASTs) are a simple kind of data structure used in libSBML for storing mathematical expressions. The ASTNode_t is the cornerstone of libSBML's AST representation. An AST "node" represents the most basic, indivisible part of a mathematical formula and come in many types. For instance, there are node types to represent numbers (with subtypes to distinguish integer, real, and rational numbers), names (e.g., constants or variables), simple mathematical operators, logical or relational operators and functions. LibSBML ASTs provide a canonical, in-memory representation for all mathematical formulas regardless of their original format (which might be MathML or might be text strings).
"1 + 2"
is represented as an AST with one plus node having two integer children nodes for the numbers 1
and 2
. The figure also shows the corresponding MathML representation:Infix | AST | MathML |
---|---|---|
1 + 2
|
<math xmlns="http://www.w3.org/1998/Math/MathML"> <apply> <plus/> <cn type="integer"> 1 </cn> <cn type="integer"> 2 </cn> </apply> </math>
|
The following are other noteworthy points about the AST representation in libSBML:
double
data type. This is done so that when an SBML model is read in and then written out again, the amount of change introduced by libSBML to the SBML during the round-trip activity is minimized.For many applications, the details of ASTs are irrelevant because libSBML provides text-string based translation functions such as SBML_formulaToL3String() and SBML_parseL3Formula(). If you find the complexity of using the AST representation of expressions too high for your purposes, perhaps the string-based functions will be more suitable.
AST_CONSTANT_E | AST_FUNCTION_CSC | AST_LOGICAL_AND |
AST_CONSTANT_FALSE | AST_FUNCTION_CSCH | AST_LOGICAL_IMPLIES2 |
AST_CONSTANT_PI | AST_FUNCTION_DELAY | AST_LOGICAL_NOT |
AST_CONSTANT_TRUE | AST_FUNCTION_EXP | AST_LOGICAL_OR |
AST_DIVIDE | AST_FUNCTION_FACTORIAL | AST_LOGICAL_XOR |
AST_FUNCTION | AST_FUNCTION_FLOOR | AST_MINUS |
AST_FUNCTION_ABS | AST_FUNCTION_LN | AST_NAME |
AST_FUNCTION_ARCCOS | AST_FUNCTION_LOG | AST_NAME_AVOGADRO1 |
AST_FUNCTION_ARCCOSH | AST_FUNCTION_MAX2 | AST_NAME_TIME |
AST_FUNCTION_ARCCOT | AST_FUNCTION_MIN2 | AST_ORIGINATES_IN_PACKAGE2 |
AST_FUNCTION_ARCCOTH | AST_FUNCTION_PIECEWISE | AST_PLUS |
AST_FUNCTION_ARCCSC | AST_FUNCTION_POWER | AST_POWER |
AST_FUNCTION_ARCCSCH | AST_FUNCTION_QUOTIENT2 | AST_RATIONAL |
AST_FUNCTION_ARCSEC | AST_FUNCTION_RATE_OF2 | AST_REAL |
AST_FUNCTION_ARCSECH | AST_FUNCTION_REM2 | AST_REAL_E |
AST_FUNCTION_ARCSIN | AST_FUNCTION_ROOT | AST_RELATIONAL_EQ |
AST_FUNCTION_ARCSINH | AST_FUNCTION_SEC | AST_RELATIONAL_GEQ |
AST_FUNCTION_ARCTAN | AST_FUNCTION_SECH | AST_RELATIONAL_GT |
AST_FUNCTION_ARCTANH | AST_FUNCTION_SIN | AST_RELATIONAL_LEQ |
AST_FUNCTION_CEILING | AST_FUNCTION_SINH | AST_RELATIONAL_LT |
AST_FUNCTION_COS | AST_FUNCTION_TAN | AST_RELATIONAL_NEQ |
AST_FUNCTION_COSH | AST_FUNCTION_TANH | AST_TIMES |
AST_FUNCTION_COT | AST_INTEGER | AST_UNKNOWN |
AST_FUNCTION_COTH | AST_LAMBDA | |
1 (Level 3 only) | ||
2 (Level 3 Version 2+ only) |
The types have the following meanings:
"+"
), then the node's type will be AST_PLUS, AST_MINUS, AST_TIMES, AST_DIVIDE, or AST_POWER, as appropriate.AST_FUNCTION_
X, AST_LOGICAL_
X, or AST_RELATIONAL_
X, as appropriate. (Examples: AST_FUNCTION_LOG, AST_RELATIONAL_LEQ.)"ExponentialE"
, "Pi"
, "True"
or "False"
), then the node's type will be AST_CONSTANT_E, AST_CONSTANT_PI, AST_CONSTANT_TRUE, or AST_CONSTANT_FALSE.time
, the value of the node will be AST_NAME_TIME. (Note, however, that the MathML csymbol delay
is translated into a node of type AST_FUNCTION_DELAY. The difference is due to the fact that time
is a single variable, whereas delay
is actually a function taking arguments.)avogadro
, the value of the node will be AST_NAME_AVOGADRO.rateOf
, the value of the node will be AST_FUNCTION_RATE_OF.The text-string form of mathematical formulas produced by SBML_formulaToString() and read by SBML_parseFormula() and SBML_parseL3Formula() are in a simple C-inspired infix notation. A formula in this text-string form can be handed to a program that understands SBML mathematical expressions, or used as part of a translation system. The libSBML distribution comes with an example program in the "examples"
subdirectory called translateMath
that implements an interactive command-line demonstration of translating infix formulas into MathML and vice-versa.
The formula strings may contain operators, function calls, symbols, and white space characters. The allowable white space characters are tab and space. The following are illustrative examples of formulas expressed in the syntax:
0.10 * k4^2
(vm * s1)/(km + s1)
The following table shows the precedence rules in this syntax. In the Class column, operand implies the construct is an operand, prefix implies the operation is applied to the following arguments, unary implies there is one argument, and binary implies there are two arguments. The values in the Precedence column show how the order of different types of operation are determined. For example, the expression a * b + c is evaluated as (a * b) + c because the *
operator has higher precedence. The Associates column shows how the order of similar precedence operations is determined; for example, a - b + c is evaluated as (a - b) + c because the +
and -
operators are left-associative. The precedence and associativity rules are taken from the C programming language, except for the symbol ^
, which is used in C for a different purpose. (Exponentiation can be invoked using either ^
or the function power
.)
Token | Operation | Class | Precedence | Associates |
---|---|---|---|---|
name | symbol reference | operand | 6 | n/a |
( expression) | expression grouping | operand | 6 | n/a |
f( ...) | function call | prefix | 6 | left |
- | negation | unary | 5 | right |
^ | power | binary | 4 | left |
* | multiplication | binary | 3 | left |
/ | divison | binary | 3 | left |
+ | addition | binary | 2 | left |
- | subtraction | binary | 2 | left |
, | argument delimiter | binary | 1 | left |
A program parsing a formula in an SBML model should assume that names appearing in the formula are the identifiers of Species_t, Parameter_t, Compartment_t, FunctionDefinition_t, Reaction_t (in SBML Levels 2 and 3), or SpeciesReference_t (in SBML Level 3 only) objects defined in a model. When a function call is involved, the syntax consists of a function identifier, followed by optional white space, followed by an opening parenthesis, followed by a sequence of zero or more arguments separated by commas (with each comma optionally preceded and/or followed by zero or more white space characters), followed by a closing parenthesis. There is an almost one-to-one mapping between the list of predefined functions available, and those defined in MathML. All of the MathML functions are recognized; this set is larger than the functions defined in SBML Level 1. In the subset of functions that overlap between MathML and SBML Level 1, there exist a few differences. The following table summarizes the differences between the predefined functions in SBML Level 1 and the MathML equivalents in SBML Levels 2 and 3:
Text string formula functions | MathML equivalents in SBML Levels 2 and 3 |
---|---|
acos | arccos |
asin | arcsin |
atan | arctan |
ceil | ceiling |
log | ln |
log10(x) | log(x) or log(10, x) |
pow(x, y) | power(x, y) |
sqr(x) | power(x, 2) |
sqrt(x) | root(x) or root(2, x) |
Public Member Functions | |
int | ASTNode_addChild (ASTNode_t *node, ASTNode_t *disownedChild) |
Adds a node as a child of another node. More... | |
int | ASTNode_addSemanticsAnnotation (ASTNode_t *node, XMLNode_t *disownedAnnotation) |
Adds a given XML node structure as a MathML semantic annotation of a given ASTNode_t structure. More... | |
int | ASTNode_canonicalize (ASTNode_t *node) |
Converts a given node to a canonical form and returns 1 if successful, 0 otherwise. More... | |
ASTNode_t * | ASTNode_create (void) |
Creates a new ASTNode_t structure and returns a pointer to it. More... | |
ASTNode_t * | ASTNode_createFromToken (Token_t *token) |
Creates a new ASTNode_t structure from the given Token_t data and returns a pointer to it. More... | |
ASTNode_t * | ASTNode_createWithType (ASTNodeType_t type) |
Creates a new ASTNode_t structure and sets its type. More... | |
ASTNode_t * | ASTNode_deepCopy (const ASTNode_t *node) |
Creates a recursive copy of a node and all its children. More... | |
void | ASTNode_fillListOfNodes (const ASTNode_t *node, ASTNodePredicate predicate, List_t *lst) |
Returns a list of nodes rooted at a given node and satisfying a given predicate. More... | |
void | ASTNode_free (ASTNode_t *node) |
Frees the given ASTNode_t structure, including any child nodes. More... | |
int | ASTNode_freeName (ASTNode_t *node) |
Frees the name field of a given node and sets it to null. More... | |
char | ASTNode_getCharacter (const ASTNode_t *node) |
Gets the value of a node as a single character. More... | |
ASTNode_t * | ASTNode_getChild (const ASTNode_t *node, unsigned int n) |
Gets a child of a node according to its index number. More... | |
char * | ASTNode_getClass (const ASTNode_t *node) |
Returns the MathML "class" attribute of a given node. More... | |
XMLAttributes_t * | ASTNode_getDefinitionURL (ASTNode_t *node) |
Returns the MathML "definitionURL" attribute value of the given node. More... | |
char * | ASTNode_getDefinitionURLString (ASTNode_t *node) |
Returns the MathML "definitionURL" attribute value of the given node as a string. More... | |
long | ASTNode_getDenominator (const ASTNode_t *node) |
Gets the numerator value of a node representing a rational number. More... | |
long | ASTNode_getExponent (const ASTNode_t *node) |
Get the exponent value of a node. More... | |
char * | ASTNode_getId (const ASTNode_t *node) |
Returns the MathML "id" attribute of a given node. More... | |
long | ASTNode_getInteger (const ASTNode_t *node) |
Gets the value of a node as an integer. More... | |
ASTNode_t * | ASTNode_getLeftChild (const ASTNode_t *node) |
Returns the left-most child of a given node. More... | |
List_t * | ASTNode_getListOfNodes (const ASTNode_t *node, ASTNodePredicate predicate) |
Returns a list of nodes rooted at a given node and satisfying a given predicate. More... | |
double | ASTNode_getMantissa (const ASTNode_t *node) |
Get the mantissa value of a node. More... | |
const char * | ASTNode_getName (const ASTNode_t *node) |
Gets the value of a node as a string. More... | |
unsigned int | ASTNode_getNumChildren (const ASTNode_t *node) |
Returns the number of children of a given node. More... | |
long | ASTNode_getNumerator (const ASTNode_t *node) |
Gets the numerator value of a node representing a rational number. More... | |
unsigned int | ASTNode_getNumSemanticsAnnotations (ASTNode_t *node) |
Returns the number of MathML semantic annotations inside the given node. More... | |
SBase_t * | ASTNode_getParentSBMLObject (ASTNode_t *node) |
Returns the parent SBML structure containing the given node. More... | |
int | ASTNode_getPrecedence (const ASTNode_t *node) |
Gets the precedence of a node in the infix math syntax of SBML Level 1. More... | |
double | ASTNode_getReal (const ASTNode_t *node) |
Get the real-numbered value of a node. More... | |
ASTNode_t * | ASTNode_getRightChild (const ASTNode_t *node) |
Returns the right-most child of a given node. More... | |
XMLNode_t * | ASTNode_getSemanticsAnnotation (ASTNode_t *node, unsigned int n) |
Returns the nth MathML semantic annotation attached to the given node. More... | |
char * | ASTNode_getStyle (const ASTNode_t *node) |
Returns the MathML "style" attribute of a given node. More... | |
ASTNodeType_t | ASTNode_getType (const ASTNode_t *node) |
Returns the type of the given node. More... | |
char * | ASTNode_getUnits (const ASTNode_t *node) |
Returns the SBML "units" attribute of a given node. More... | |
void * | ASTNode_getUserData (const ASTNode_t *node) |
Returns the user data associated with this node. More... | |
double | ASTNode_getValue (const ASTNode_t *node) |
Returns the numerical value of this ASTNode_t. More... | |
int | ASTNode_hasCorrectNumberArguments (ASTNode_t *node) |
Returns true if the given node has the correct number of children for its type. More... | |
int | ASTNode_hasTypeAndNumChildren (const ASTNode_t *node, ASTNodeType_t type, unsigned int numchildren) |
Returns true if the given node is of a specific type and has a specific number of children. More... | |
int | ASTNode_hasUnits (const ASTNode_t *node) |
Returns true if the given node or any of its children have the SBML "units" attribute set. More... | |
int | ASTNode_insertChild (ASTNode_t *node, unsigned int n, ASTNode_t *disownedChild) |
Insert a new child node at a given point in the list of children of a node. More... | |
int | ASTNode_isAvogadro (const ASTNode_t *node) |
Returns true if the given node represents the special symbol avogadro . More... | |
int | ASTNode_isBoolean (const ASTNode_t *node) |
Returns true if this node is some type of Boolean value or operator. More... | |
int | ASTNode_isConstant (const ASTNode_t *node) |
Returns true if the given node represents a MathML constant. More... | |
int | ASTNode_isConstantNumber (const ASTNode_t *node) |
Returns true if the given node represents a MathML constant. More... | |
int | ASTNode_isFunction (const ASTNode_t *node) |
Returns true if the given node represents a function. More... | |
int | ASTNode_isInfinity (const ASTNode_t *node) |
Returns true if the given node stands for infinity. More... | |
int | ASTNode_isInteger (const ASTNode_t *node) |
Returns true if the given node contains an integer value. More... | |
int | ASTNode_isLambda (const ASTNode_t *node) |
Returns true if the given node is a MathML lambda function. More... | |
int | ASTNode_isLog10 (const ASTNode_t *node) |
Returns true if the given node represents the log base-10 function. More... | |
int | ASTNode_isLogical (const ASTNode_t *node) |
Returns true if the given node is a logical operator. More... | |
int | ASTNode_isName (const ASTNode_t *node) |
Returns true if the given node is a named entity. More... | |
int | ASTNode_isNaN (const ASTNode_t *node) |
Returns true if the given node represents not-a-number. More... | |
int | ASTNode_isNegInfinity (const ASTNode_t *node) |
Returns true if the given node represents negative infinity. More... | |
int | ASTNode_isNumber (const ASTNode_t *node) |
Returns true if the given node contains a number. More... | |
int | ASTNode_isOperator (const ASTNode_t *node) |
Returns true if the given node is a mathematical operator. More... | |
int | ASTNode_isPiecewise (const ASTNode_t *node) |
Returns true if the given node represents the MathML <piecewise> operator. More... | |
int | ASTNode_isRational (const ASTNode_t *node) |
Returns true if the given node represents a rational number. More... | |
int | ASTNode_isReal (const ASTNode_t *node) |
Returns true if the given node represents a real number. More... | |
int | ASTNode_isRelational (const ASTNode_t *node) |
Returns true if the given node represents a MathML relational operator. More... | |
int | ASTNode_isSetClass (const ASTNode_t *node) |
Returns true if the given node's MathML "class" attribute is set. More... | |
int | ASTNode_isSetId (const ASTNode_t *node) |
Returns true if the given node's MathML "id" attribute is set. More... | |
int | ASTNode_isSetParentSBMLObject (ASTNode_t *node) |
Returns true if the given node's parent SBML object is set. More... | |
int | ASTNode_isSetStyle (const ASTNode_t *node) |
Returns true if the given node's MathML "style" attribute is set. More... | |
int | ASTNode_isSetUnits (const ASTNode_t *node) |
Returns true if this node's SBML "units" attribute is set. More... | |
int | ASTNode_isSetUserData (const ASTNode_t *node) |
Returns true if the given node's user data object is set. More... | |
int | ASTNode_isSqrt (const ASTNode_t *node) |
Returns true if the given node is the MathML square-root operator. More... | |
int | ASTNode_isUMinus (const ASTNode_t *node) |
Returns true if the given node represents a unary minus. More... | |
int | ASTNode_isUnknown (const ASTNode_t *node) |
Returns true if the type of the node is unknown. More... | |
int | ASTNode_isUPlus (const ASTNode_t *node) |
Returns true if the given node is a unary plus. More... | |
int | ASTNode_isWellFormedASTNode (ASTNode_t *node) |
Returns true if the given node is well-formed. More... | |
int | ASTNode_prependChild (ASTNode_t *node, ASTNode_t *disownedChild) |
Adds a node as a child of another node. More... | |
void | ASTNode_reduceToBinary (ASTNode_t *node) |
Reduces the given node to a binary true. More... | |
int | ASTNode_removeChild (ASTNode_t *node, unsigned int n) |
Removes the nth child of a given node. More... | |
int | ASTNode_replaceAndDeleteChild (ASTNode_t *node, unsigned int n, ASTNode_t *disownedChild) |
Replaces and deletes the nth child of a given node. More... | |
void | ASTNode_replaceArgument (ASTNode_t *node, const char *bvar, ASTNode_t *arg) |
Replaces occurrences of a given name with a new ASTNode_t structure. More... | |
int | ASTNode_replaceChild (ASTNode_t *node, unsigned int n, ASTNode_t *disownedChild) |
Replaces the nth child of a given node. More... | |
int | ASTNode_returnsBoolean (const ASTNode_t *node) |
Returns true if the given node is something that returns a Boolean value. More... | |
int | ASTNode_returnsBooleanForModel (const ASTNode_t *node, const Model_t *model) |
Returns true if the given node is something that returns a Boolean value. More... | |
int | ASTNode_setCharacter (ASTNode_t *node, char value) |
Sets the value of a given node to a character. More... | |
int | ASTNode_setClass (ASTNode_t *node, const char *className) |
Sets the MathML "class" of the given node. More... | |
int | ASTNode_setDefinitionURL (ASTNode_t *node, XMLAttributes_t *defnURL) |
Sets the MathML "definitionURL" attribute of the given node. More... | |
int | ASTNode_setDefinitionURLString (ASTNode_t *node, const char *defnURL) |
Sets the MathML "definitionURL" attribute of the given node. More... | |
int | ASTNode_setId (ASTNode_t *node, const char *id) |
Sets the MathML "id" attribute of the given node. More... | |
int | ASTNode_setInteger (ASTNode_t *node, long value) |
Sets the given node to a integer and sets it type to AST_INTEGER. More... | |
int | ASTNode_setName (ASTNode_t *node, const char *name) |
Sets the node to represent a named entity. More... | |
int | ASTNode_setRational (ASTNode_t *node, long numerator, long denominator) |
Sets the value of a given node to a rational number and sets its type to AST_RATIONAL. More... | |
int | ASTNode_setReal (ASTNode_t *node, double value) |
Sets the value of a given node to a real (double ) and sets its type to AST_REAL. More... | |
int | ASTNode_setRealWithExponent (ASTNode_t *node, double mantissa, long exponent) |
Sets the value of a given node to a real (double ) in two parts, a mantissa and an exponent. More... | |
int | ASTNode_setStyle (ASTNode_t *node, const char *style) |
Sets the MathML "style" of the given node. More... | |
int | ASTNode_setType (ASTNode_t *node, ASTNodeType_t type) |
Explicitly sets the type of the given ASTNode_t structure. More... | |
int | ASTNode_setUnits (ASTNode_t *node, const char *units) |
Sets the units of the given node. More... | |
int | ASTNode_setUserData (ASTNode_t *node, void *userData) |
Sets the user data of the given node. More... | |
int | ASTNode_swapChildren (ASTNode_t *node, ASTNode_t *that) |
Swaps the children of two nodes. More... | |
int | ASTNode_unsetClass (ASTNode_t *node) |
Unsets the MathML "class" attribute of the given node. More... | |
int | ASTNode_unsetId (ASTNode_t *node) |
Unsets the MathML "id" attribute of the given node. More... | |
int | ASTNode_unsetStyle (ASTNode_t *node) |
Unsets the MathML "style" attribute of the given node. More... | |
int | ASTNode_unsetUnits (ASTNode_t *node) |
Unsets the units associated with the given node. More... | |
int | ASTNode_unsetUserData (ASTNode_t *node) |
Unsets the user data of the given node. More... | |
ASTNode_t * | readMathMLFromString (const char *xml) |
Reads the MathML from the given XML string, constructs a corresponding abstract syntax tree, and returns a pointer to the root of the tree. More... | |
ASTNode_t * | readMathMLFromStringWithNamespaces (const char *xml, XMLNamespaces_t *xmlns) |
Reads the MathML from the given XML string, constructs a corresponding abstract syntax tree, and returns a pointer to the root of the tree. More... | |
char * | SBML_formulaToL3String (const ASTNode_t *tree) |
Converts an AST to a string representation of a formula using a syntax derived from SBML Level 1, but extended to include elements from SBML Level 2 and SBML Level 3. More... | |
char * | SBML_formulaToL3StringWithSettings (const ASTNode_t *tree, const L3ParserSettings_t *settings) |
Converts an AST to a string representation of a formula using a syntax basically derived from SBML Level 1, with behavior modifiable with custom settings. More... | |
char * | SBML_formulaToString (const ASTNode_t *tree) |
Converts an AST to a string representation of a formula using a syntax basically derived from SBML Level 1. More... | |
L3ParserSettings_t * | SBML_getDefaultL3ParserSettings () |
char * | SBML_getLastParseL3Error () |
Returns the last error reported by the "L3" mathematical formula parser. More... | |
ASTNode_t * | SBML_parseFormula (const char *formula) |
ASTNode_t * | SBML_parseL3Formula (const char *formula) |
Parses a text string as a mathematical formula and returns an AST representation of it. More... | |
ASTNode_t * | SBML_parseL3FormulaWithModel (const char *formula, const Model_t *model) |
Parses a text string as a mathematical formula using a Model_t to resolve symbols, and returns an AST representation of the result. More... | |
ASTNode_t * | SBML_parseL3FormulaWithSettings (const char *formula, const L3ParserSettings_t *settings) |
Parses a text string as a mathematical formula using specific parser settings and returns an AST representation of the result. More... | |
char * | writeMathMLToString (const ASTNode_t *node) |
Writes the given ASTNode_t (and its children) to a string as MathML, and returns the string. More... | |
char * | writeMathMLToString (const ASTNode *node) |
char * | writeMathMLWithNamespaceToString (const ASTNode_t *node, SBMLNamespaces_t *sbmlns) |
Writes the given AST node (and its children) to a string as MathML, and returns the string. More... | |
Adds a node as a child of another node.
Child nodes are added in order from "left-to-right".
node | the node which will get the new child node |
disownedChild | the ASTNode_t instance to add |
Adds a given XML node structure as a MathML semantic annotation of a given ASTNode_t structure.
The<semantics>
element is a MathML 2.0 construct
that can be used to associate additional information with a MathML
construct. The construct can be used to decorate a MathML expressions with
a sequence of one or more <annotation>
or
<annotation-xml>
elements. Each such element contains a
pair of items; the first is a symbol that acts as an attribute or key, and
the second is the value associated with the attribute or key. Please refer
to the MathML 2.0 documentation, particularly the Section
5.2, Semantic Annotations for more information about these constructs.
node | the node to modify |
disownedAnnotation | the annotation to add |
int ASTNode_canonicalize | ( | ASTNode_t * | node | ) |
Converts a given node to a canonical form and returns 1
if successful, 0
otherwise.
The rules determining the canonical form conversion are as follows:
"ExponentialE"
, "Pi"
, "True"
or "False"
the node type is converted to the corresponding AST_CONSTANT_
X type. AST_FUNCTION_
X or AST_LOGICAL_
X type.SBML Level 1 function names are searched first; thus, for example, canonicalizing log
will result in a node type of AST_FUNCTION_LN. (See the SBML Level 1 Version 2 Specification, Appendix C.)
Sometimes, canonicalization of a node results in a structural conversion of the node as a result of adding a child. For example, a node with the SBML Level 1 function name sqr
and a single child node (the argument) will be transformed to a node of type AST_FUNCTION_POWER with two children. The first child will remain unchanged, but the second child will be an ASTNode_t of type AST_INTEGER and a value of 2. The function names that result in structural changes are: log10
, sqr
, and sqrt
.
node | the node to be converted. |
1
if successful, 0
otherwise. ASTNode_t * ASTNode_create | ( | void | ) |
Creates a new ASTNode_t structure and returns a pointer to it.
The returned node will have a type of AST_UNKNOWN
. The caller should be set the node type to something else as soon as possible using ASTNode_setType().
Creates a new ASTNode_t structure from the given Token_t data and returns a pointer to it.
The returned ASTNode_t structure will contain the same data as the Token_t structure. The Token_t structure is used to store a token returned by FormulaTokenizer_nextToken(). It contains a union whose members can store different types of tokens, such as numbers and symbols.
token | the Token_t structure to use |
ASTNode_t * ASTNode_createWithType | ( | ASTNodeType_t | type | ) |
Creates a new ASTNode_t structure and sets its type.
type | the type of node to create |
void ASTNode_fillListOfNodes | ( | const ASTNode_t * | node, |
ASTNodePredicate | predicate, | ||
List_t * | lst | ||
) |
Returns a list of nodes rooted at a given node and satisfying a given predicate.
This method is identical to ASTNode_getListOfNodes(), except that instead of creating a new List_t structure, it uses the one passed in as argument lst
. This function performs a depth-first search of the tree rooted at the given ASTNode_t structure, and adds to lst
the nodes for which the given function predicate(node)
returns true (i.e., nonzero).
The predicate is passed in as a pointer to a function. The function definition must have the type ASTNodePredicate , which is defined as
int (*ASTNodePredicate) (const ASTNode_t *node);
where a return value of non-zero represents true and zero represents false.
node | the node at which the search is to be started |
predicate | the predicate to use |
lst | the list to use |
void ASTNode_free | ( | ASTNode_t * | node | ) |
Frees the given ASTNode_t structure, including any child nodes.
node | the node to be freed. |
int ASTNode_freeName | ( | ASTNode_t * | node | ) |
Frees the name field of a given node and sets it to null.
This operation is only applicable to ASTNode_t structures corresponding to operators, numbers, or AST_UNKNOWN. This method will have no effect on other types of nodes.
node | the node whose name field should be freed. |
char ASTNode_getCharacter | ( | const ASTNode_t * | node | ) |
Gets the value of a node as a single character.
This function should be called only when ASTNode_getType() returns AST_PLUS, AST_MINUS, AST_TIMES, AST_DIVIDE or AST_POWER for the given node
.
node | the node whose value is to be returned. |
node
as a single character, or the value CHAR_MAX
if is
null. Gets a child of a node according to its index number.
node | the node whose child should be obtained. |
n | the index of the desired child node. |
n >
ASTNode_getNumChildre() - 1
).char * ASTNode_getClass | ( | const ASTNode_t * | node | ) |
Returns the MathML "class" attribute of a given node.
node | the node whose class should be returned |
is
a null pointer. XMLAttributes_t * ASTNode_getDefinitionURL | ( | ASTNode_t * | node | ) |
Returns the MathML "definitionURL" attribute value of the given node.
node | the node to query |
node
does not have a value for the attribute.char * ASTNode_getDefinitionURLString | ( | ASTNode_t * | node | ) |
Returns the MathML "definitionURL" attribute value of the given node as a string.
node | the node to query |
node
does not have a value for the attribute.long ASTNode_getDenominator | ( | const ASTNode_t * | node | ) |
Gets the numerator value of a node representing a rational number.
This function should be called only when ASTNode_getType() returns AST_RATIONAL for the given node
.
node | the node whose value is to be returned. |
node
, or the value LONG_MAX
if is
null.long ASTNode_getExponent | ( | const ASTNode_t * | node | ) |
Get the exponent value of a node.
This function should be called only when ASTNode_getType() returns AST_REAL_E or AST_REAL for the given node
.
node | the node whose value is to be returned. |
node
ASTNode_t structure, or NaN if is
null. char * ASTNode_getId | ( | const ASTNode_t * | node | ) |
Returns the MathML "id" attribute of a given node.
node | the node whose identifier should be returned |
is
a null pointer. long ASTNode_getInteger | ( | const ASTNode_t * | node | ) |
Gets the value of a node as an integer.
This function should be called only when ASTNode_getType() returns AST_INTEGER for the given node
.
node | the node whose value is to be returned. |
long
) integer, or the value LONG_MAX
if is
null. Returns the left-most child of a given node.
This is equivalent to ASTNode_getChild(node, 0)
.
node | the node whose child is to be returned. |
List_t * ASTNode_getListOfNodes | ( | const ASTNode_t * | node, |
ASTNodePredicate | predicate | ||
) |
Returns a list of nodes rooted at a given node and satisfying a given predicate.
This function performs a depth-first search of the tree rooted at the given ASTNode_t structure, and returns a List_t structure of nodes for which the given function predicate(node)
returns true (i.e., non-zero).
The predicate is passed in as a pointer to a function. The function definition must have the type ASTNodePredicate, which is defined as
int (*ASTNodePredicate) (const ASTNode_t *node);
where a return value of nonzero represents true and zero represents false.
node | the node at which the search is to be started |
predicate | the predicate to use |
double ASTNode_getMantissa | ( | const ASTNode_t * | node | ) |
Get the mantissa value of a node.
This function should be called only when ASTNode_getType() returns AST_REAL_E or AST_REAL for the given node
. If ASTNode_getType() returns AST_REAL for node
, this method behaves identically to ASTNode_getReal().
node | the node whose value is to be returned. |
node
, or NaN if is
null. const char * ASTNode_getName | ( | const ASTNode_t * | node | ) |
Gets the value of a node as a string.
This function may be called on nodes that (1) are not operators, i.e., nodes for which ASTNode_isOperator() returns false (0
), and (2) are not numbers, i.e., for which ASTNode_isNumber() also returns false (0
).
node | the node whose value is to be returned. |
node
as a string, or a null pointer if is
null. unsigned int ASTNode_getNumChildren | ( | const ASTNode_t * | node | ) |
Returns the number of children of a given node.
node | the ASTNode_t structure whose children are to be counted. |
node
, or 0
is it has no children. long ASTNode_getNumerator | ( | const ASTNode_t * | node | ) |
Gets the numerator value of a node representing a rational number.
This function should be called only when ASTNode_getType() returns AST_RATIONAL for the given node
.
node | the node whose value is to be returned. |
node
, or the value LONG_MAX
if is
null.unsigned int ASTNode_getNumSemanticsAnnotations | ( | ASTNode_t * | node | ) |
Returns the number of MathML semantic annotations inside the given node.
The<semantics>
element is a MathML 2.0 construct
that can be used to associate additional information with a MathML
construct. The construct can be used to decorate a MathML expressions with
a sequence of one or more <annotation>
or
<annotation-xml>
elements. Each such element contains a
pair of items; the first is a symbol that acts as an attribute or key, and
the second is the value associated with the attribute or key. Please refer
to the MathML 2.0 documentation, particularly the Section
5.2, Semantic Annotations for more information about these constructs.
node | the node to query |
Returns the parent SBML structure containing the given node.
node | the node to query |
int ASTNode_getPrecedence | ( | const ASTNode_t * | node | ) |
Gets the precedence of a node in the infix math syntax of SBML Level 1.
0.10 * k4^2
(vm * s1)/(km + s1)
Note that this facility is provided as a convenience by libSBML—the MathML standard does not actually define a "string-form" equivalent to MathML expression trees, so the choice of formula syntax is somewhat arbitrary. The approach taken by libSBML is to use the syntax defined by SBML Level 1 (which in fact used a text-string representation of formulas and not MathML). This formula syntax is based mostly on C programming syntax, and may contain operators, function calls, symbols, and white space characters. The following table provides the precedence rules for the different entities that may appear in formula strings.
Token | Operation | Class | Precedence | Associates |
---|---|---|---|---|
name | symbol reference | operand | 6 | n/a |
( expression) | expression grouping | operand | 6 | n/a |
f( ...) | function call | prefix | 6 | left |
- | negation | unary | 5 | right |
^ | power | binary | 4 | left |
* | multiplication | binary | 3 | left |
/ | divison | binary | 3 | left |
+ | addition | binary | 2 | left |
- | subtraction | binary | 2 | left |
, | argument delimiter | binary | 1 | left |
In the table above, operand implies the construct is an operand, prefix implies the operation is applied to the following arguments, unary implies there is one argument, and binary implies there are two arguments. The values in the Precedence column show how the order of different types of operation are determined. For example, the expression a * b + c
is evaluated as (a * b) + c
because the *
operator has higher precedence. The Associates column shows how the order of similar precedence operations is determined; for example, a - b + c
is evaluated as (a - b) + c
because the +
and -
operators are left-associative.
The function call syntax consists of a function name, followed by optional white space, followed by an opening parenthesis token, followed by a sequence of zero or more arguments separated by commas (with each comma optionally preceded and/or followed by zero or more white space characters, followed by a closing parenthesis token. The function name must be chosen from one of the pre-defined functions in SBML or a user-defined function in the model. The following table lists the names of certain common mathematical functions; this table corresponds to Table 6 in the SBML Level 1 Version 2 specification:
Name | Args | Formula or meaning | Argument Constraints | Result constraints |
---|---|---|---|---|
abs | x | absolute value of x | ||
acos | x | arc cosine of x in radians | -1.0 ≤ x ≤ 1.0 | 0 ≤ acos(x) ≤ π |
asin | x | arc sine of x in radians | -1.0 ≤ x ≤ 1.0 | 0 ≤ asin(x) ≤ π |
atan | x | arc tangent of x in radians | 0 ≤ atan(x) ≤ π | |
ceil | x | smallest number not less than x whose value is an exact integer | ||
cos | x | cosine of x | ||
exp | x | e x, where e is the base of the natural logarithm | ||
floor | x | largest number not greater than x whose value is an exact integer | ||
log | x | natural logarithm of x | x > 0 | |
log10 | x | base 10 logarithm of x | x > 0 | |
pow | x, y | x y | ||
sqr | x | x2 | ||
sqrt | x | √x | x > 0 | sqrt(x) ≥ 0 |
sin | x | sine of x | ||
tan | x | tangent of x | x ≠ n*π/2, for odd integer n |
log(x)
represents the natural logarithm, whereas in MathML, the natural logarithm is <ln/>
. Application writers are urged to be careful when translating between text forms and MathML forms, especially if they provide a direct text-string input facility to users of their software systems.node | the node whose precedence is to be calculated. |
node
(as defined in the SBML Level 1 specification). double ASTNode_getReal | ( | const ASTNode_t * | node | ) |
Get the real-numbered value of a node.
This function should be called only when ASTNode_isReal() returns non-zero for node
. This function performs the necessary arithmetic if the node type is AST_REAL_E (mantissa * 10 exponent) or AST_RATIONAL (numerator / denominator).
node | the node whose value is to be returned. |
node
as a real (double), or NaN if is
null. Returns the right-most child of a given node.
If ASTNode_getNumChildren(node) > 1
, then this is equivalent to:
ASTNode_getChild(node, ASTNode_getNumChildren(node) - 1);
node | the node whose child node is to be obtained. |
node
, or a null pointer if node
has no right child.Returns the nth MathML semantic annotation attached to the given node.
The<semantics>
element is a MathML 2.0 construct
that can be used to associate additional information with a MathML
construct. The construct can be used to decorate a MathML expressions with
a sequence of one or more <annotation>
or
<annotation-xml>
elements. Each such element contains a
pair of items; the first is a symbol that acts as an attribute or key, and
the second is the value associated with the attribute or key. Please refer
to the MathML 2.0 documentation, particularly the Section
5.2, Semantic Annotations for more information about these constructs.
node | the node to query |
n | the index of the semantic annotation to fetch |
node
, or a null pointer if the node has no nth annotation (which would mean that n > ASTNode_getNumSemanticsAnnotations(node) - 1
).char * ASTNode_getStyle | ( | const ASTNode_t * | node | ) |
Returns the MathML "style" attribute of a given node.
node | the node |
is
a null pointer. ASTNodeType_t ASTNode_getType | ( | const ASTNode_t * | node | ) |
Returns the type of the given node.
node | the node |
char * ASTNode_getUnits | ( | const ASTNode_t * | node | ) |
Returns the SBML "units" attribute of a given node.
SBML Level 3 Version 1 introduced the ability to include an attributesbml:units
on MathML cn
elements
appearing in SBML mathematical formulas. The value of this attribute can
be used to indicate the unit of measurement to be associated with the
number in the content of the cn
element. The value of this
attribute must be the identifier of a unit of measurement defined by SBML
or the enclosing Model. Here, the sbml
portion is an XML
namespace prefix that must be associated with the SBML namespace for SBML
Level 3. The following example illustrates how this attribute can be
used to define a number with value 10
and unit of measurement
second
:
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:sbml="http://www.sbml.org/sbml/level3/version1/core"> <cn type="integer" sbml:units="second"> 10 </cn> </math>
node | the node whose units are to be returned. |
is
a null pointer.sbml:units
attribute for MathML expressions is only defined in SBML Level 3. It may not be used in Levels 1–2 of SBML.void * ASTNode_getUserData | ( | const ASTNode_t * | node | ) |
Returns the user data associated with this node.
node | the node to query |
double ASTNode_getValue | ( | const ASTNode_t * | node | ) |
Returns the numerical value of this ASTNode_t.
node | the ASTNode_t whose value is to be returned. |
NaN
if this is not a type of node that has a numerical value.1
for nodes of type AST_CONSTANT_TRUE and 0
for nodes of type AST_CONSTANT_FALSE. It does not evaluate the node in any way so, for example, it will not return the value of a named ASTNode_t or attempt to evaluate a function. This includes a node representing time
i.e. nodes of type AST_NAME_TIME. int ASTNode_hasCorrectNumberArguments | ( | ASTNode_t * | node | ) |
Returns true if the given node has the correct number of children for its type.
For example, an ASTNode_t structure with type AST_PLUS expects 2 child nodes.
node | the node to query |
1
if node
has the appropriate number of children for its type, 0
otherwise.int ASTNode_hasTypeAndNumChildren | ( | const ASTNode_t * | node, |
ASTNodeType_t | type, | ||
unsigned int | numchildren | ||
) |
Returns true if the given node is of a specific type and has a specific number of children.
This function is designed for use in cases such as when callers want to determine if the node is a unary not
or unary minus
, or a times
node with no children, etc.
node | the node to query |
type | the type that the node should have |
numchildren | the number of children that the node should have. |
1
if node
is has the specified type and number of children, 0
otherwise. int ASTNode_hasUnits | ( | const ASTNode_t * | node | ) |
Returns true if the given node or any of its children have the SBML "units" attribute set.
SBML Level 3 Version 1 introduced the ability to include an attributesbml:units
on MathML cn
elements
appearing in SBML mathematical formulas. The value of this attribute can
be used to indicate the unit of measurement to be associated with the
number in the content of the cn
element. The value of this
attribute must be the identifier of a unit of measurement defined by SBML
or the enclosing Model. Here, the sbml
portion is an XML
namespace prefix that must be associated with the SBML namespace for SBML
Level 3. The following example illustrates how this attribute can be
used to define a number with value 10
and unit of measurement
second
:
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:sbml="http://www.sbml.org/sbml/level3/version1/core"> <cn type="integer" sbml:units="second"> 10 </cn> </math>
node | the node to query |
1
if the attribute is set, 0
otherwise.sbml:units
attribute is only available in SBML Level 3. It may not be used in Levels 1–2 of SBML.Insert a new child node at a given point in the list of children of a node.
node | the ASTNode_t structure to modify. |
n | unsigned int the index of the location where the disownedChild is to be added. |
disownedChild | ASTNode_t structure to insert as the nth child. |
int ASTNode_isAvogadro | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents the special symbol avogadro
.
node | the node to query |
1
if this stands for avogadro
, 0
otherwise.int ASTNode_isBoolean | ( | const ASTNode_t * | node | ) |
Returns true if this node is some type of Boolean value or operator.
node | the node in question |
1
(true) if node
is a Boolean (a logical operator, a relational operator, or the constants true
or false
), 0
otherwise. int ASTNode_isConstant | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a MathML constant.
Examples of constants in this context are Pi
, true
, etc.
node | the node |
1
if node
is a MathML constant, 0
otherwise. int ASTNode_isConstantNumber | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a MathML constant.
Examples of constants in this context are Pi
, true
, etc.
node | the node |
1
if node
is a MathML constant, 0
otherwise. int ASTNode_isFunction | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a function.
node | the node |
1
if node
is a function in SBML, whether predefined (in SBML Level 1), defined by MathML (SBML Levels 2–3) or user-defined. int ASTNode_isInfinity | ( | const ASTNode_t * | node | ) |
Returns true if the given node stands for infinity.
node | the node to query |
1
if node
is the special IEEE 754 value for infinity, 0
otherwise. int ASTNode_isInteger | ( | const ASTNode_t * | node | ) |
Returns true if the given node contains an integer value.
node | the node to query |
1
if node
is of type AST_INTEGER, 0
otherwise. int ASTNode_isLambda | ( | const ASTNode_t * | node | ) |
Returns true if the given node is a MathML lambda function.
node | the node to query |
1
if node
is of type AST_LAMBDA, 0
otherwise. int ASTNode_isLog10 | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents the log base-10 function.
More precisely, this function tests if the given node's
type is AST_FUNCTION_LOG with two children, the first of which is an AST_INTEGER equal to 10
.
1
if node
represents a log10() function, 0
otherwise.int ASTNode_isLogical | ( | const ASTNode_t * | node | ) |
Returns true if the given node is a logical operator.
node | the node to query |
1
if node
is a MathML logical operator (and
, or
, not
, xor
), 0otherwise
. int ASTNode_isName | ( | const ASTNode_t * | node | ) |
Returns true if the given node is a named entity.
More precisely, this returns a true value if node
is a user-defined variable name or the special symbols time
or avogadro
.
node | the node to query |
1
if node
is a named variable, 0
otherwise. int ASTNode_isNaN | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents not-a-number.
node | the node to query |
1
if node
is the special IEEE 754 value NaN ("not a
number"), 0
otherwise. int ASTNode_isNegInfinity | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents negative infinity.
node | the node to query |
1
if node
is the special IEEE 754 value negative infinity, 0
otherwise.int ASTNode_isNumber | ( | const ASTNode_t * | node | ) |
Returns true if the given node contains a number.
This is functionally equivalent to:
ASTNode_isInteger(node) || ASTNode_isReal(node).
node | the node to query |
1
if node
is a number, 0
otherwise. int ASTNode_isOperator | ( | const ASTNode_t * | node | ) |
Returns true if the given node is a mathematical operator.
The possible mathematical operators are +
, -
, *
, /
and ^
(power).
node | the node to query |
1
if node
is an operator, 0
otherwise. int ASTNode_isPiecewise | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents the MathML <piecewise>
operator.
node | the node to query |
1
if node
is the MathML piecewise function, 0
otherwise. int ASTNode_isRational | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a rational number.
node | the node to query |
1
if node
is of type AST_RATIONAL, 0
otherwise. int ASTNode_isReal | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a real number.
More precisely, this node must be of one of the following types: AST_REAL, AST_REAL_E or AST_RATIONAL.
node | the node to query |
1
if the value of node
can represent a real number, 0
otherwise. int ASTNode_isRelational | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a MathML relational operator.
node | the node to query |
1
if node
is a MathML relational operator, meaning ==
, >=
, >
, <
, and !=
. int ASTNode_isSetClass | ( | const ASTNode_t * | node | ) |
Returns true if the given node's MathML "class" attribute is set.
node | the node to query |
1
if the attribute is set, 0
otherwise. int ASTNode_isSetId | ( | const ASTNode_t * | node | ) |
Returns true if the given node's MathML "id" attribute is set.
node | the node to query |
1
if it is set, 0
otherwise. int ASTNode_isSetParentSBMLObject | ( | ASTNode_t * | node | ) |
Returns true if the given node's parent SBML object is set.
node | the node to query |
1
if the parent SBML object is set, 0
otherwise. int ASTNode_isSetStyle | ( | const ASTNode_t * | node | ) |
Returns true if the given node's MathML "style" attribute is set.
node | the node to query |
1
if the attribute is set, 0
otherwise. int ASTNode_isSetUnits | ( | const ASTNode_t * | node | ) |
Returns true if this node's SBML "units" attribute is set.
SBML Level 3 Version 1 introduced the ability to include an attributesbml:units
on MathML cn
elements
appearing in SBML mathematical formulas. The value of this attribute can
be used to indicate the unit of measurement to be associated with the
number in the content of the cn
element. The value of this
attribute must be the identifier of a unit of measurement defined by SBML
or the enclosing Model. Here, the sbml
portion is an XML
namespace prefix that must be associated with the SBML namespace for SBML
Level 3. The following example illustrates how this attribute can be
used to define a number with value 10
and unit of measurement
second
:
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:sbml="http://www.sbml.org/sbml/level3/version1/core"> <cn type="integer" sbml:units="second"> 10 </cn> </math>
node | the node to query |
1
if the attribute is set, 0
otherwise.sbml:units
attribute is only available in SBML Level 3. It may not be used in Levels 1–2 of SBML. int ASTNode_isSetUserData | ( | const ASTNode_t * | node | ) |
Returns true if the given node's user data object is set.
node | the node to query |
1
if the user data object is set, 0
otherwise.int ASTNode_isSqrt | ( | const ASTNode_t * | node | ) |
Returns true if the given node is the MathML square-root operator.
More precisely, the node type must be AST_FUNCTION_ROOT with two children, the first of which is an AST_INTEGER node having value equal to 2.
node | the node to query |
1
if node
represents a sqrt() function, 0
otherwise. int ASTNode_isUMinus | ( | const ASTNode_t * | node | ) |
Returns true if the given node represents a unary minus.
A node is defined as a unary minus node if it is of type AST_MINUS and has exactly one child.
For numbers, unary minus nodes can be "collapsed" by negating the number. In fact, SBML_parseFormula() does this during its parsing process, and SBML_parseL3Formula() has a configuration option that allows this behavior to be turned on or off. However, unary minus nodes for symbols (AST_NAME) cannot be "collapsed", so this predicate function is still necessary.
node | the node to query |
1
if node
is a unary minus, 0
otherwise.int ASTNode_isUnknown | ( | const ASTNode_t * | node | ) |
Returns true if the type of the node is unknown.
"Unknown" nodes have the type AST_UNKNOWN. Nodes with unknown types will not appear in an ASTNode_t tree returned by libSBML based upon valid SBML input; the only situation in which a node with type AST_UNKNOWN may appear is immediately after having create a new, untyped node using the ASTNode_t constructor. Callers creating nodes should endeavor to set the type to a valid node type as soon as possible after creating new nodes.
node | the node to query |
1
if node
is of type AST_UNKNOWN
, 0
otherwise. int ASTNode_isUPlus | ( | const ASTNode_t * | node | ) |
Returns true if the given node is a unary plus.
A node is defined as a unary minus node if it is of type AST_MINUS and has exactly one child.
node | the node to query |
1
if node
is a unary plus, 0
otherwise. int ASTNode_isWellFormedASTNode | ( | ASTNode_t * | node | ) |
Returns true if the given node is well-formed.
node | the node to query |
1
if node
is well-formed, 0
otherwise.Adds a node as a child of another node.
This method adds child nodes from right to left.
node | the node that will receive the given child node. |
disownedChild | the ASTNode_t instance to add. |
void ASTNode_reduceToBinary | ( | ASTNode_t * | node | ) |
Reduces the given node to a binary true.
Example: if node
is and(x, y, z)
, then the formula of the reduced node is and(and(x, y), z)
. The operation replaces the formula stored in the current ASTNode_t structure.
node | the node to modify |
int ASTNode_removeChild | ( | ASTNode_t * | node, |
unsigned int | n | ||
) |
Removes the nth child of a given node.
node | the node whose child element is to be removed. |
n | unsigned int the index of the child to remove. |
Replaces and deletes the nth child of a given node.
node | the ASTNode_t node to modify |
n | unsigned int the index of the child to replace |
disownedChild | ASTNode_t structure to replace the nth child |
Replaces occurrences of a given name with a new ASTNode_t structure.
For example, if the formula in node
is x + y
, then the <bvar>
is x
and arg
is an ASTNode_t structure representing the real value 3
. This function substitutes 3
for x
within the node
ASTNode_t structure.
node | the node to modify |
bvar | the MathML <bvar> to use |
arg | the replacement node or structure |
Replaces the nth child of a given node.
node | the ASTNode_t node to modify |
n | unsigned int the index of the child to replace |
disownedChild | ASTNode_t structure to replace the nth child |
int ASTNode_returnsBoolean | ( | const ASTNode_t * | node | ) |
Returns true if the given node is something that returns a Boolean value.
This function looks at the whole ASTNode_t structure rather than just the top level of node
. Thus, it will consider return values from MathML piecewise
statements. In addition, if the ASTNode_t structure in node
uses a function call, this function will examine the return value of the function. Note that this is only possible in cases the ASTNode_t structure can trace its parent Model_t structure; that is, the ASTNode_t structure must represent the <math>
element of some SBML object that has already been added to an instance of an SBMLDocument_t structure.
node | the node to query |
1
if node
returns a boolean, 0
otherwise.Returns true if the given node is something that returns a Boolean value.
This function looks at the whole ASTNode_t structure rather than just the top level of node
. Thus, it will consider return values from MathML piecewise
statements. In addition, if the ASTNode_t structure in node
uses a function call, this function will examine the return value of the function using the definition of the function found in the given Model_t structure given by model
(rather than the model that might be traced from node
itself). This function is similar to ASTNode_returnsBoolean(), but is useful in situations where the ASTNode_t structure has not been hooked into a model yet.
node | the node to query |
model | the model to use as the basis for finding the definition of the function |
1
if node
returns a boolean, 0
otherwise.int ASTNode_setCharacter | ( | ASTNode_t * | node, |
char | value | ||
) |
Sets the value of a given node to a character.
If character is one of +
, -
, *
, /
or ^
, the node type will be set accordingly. For all other characters, the node type will be set to AST_UNKNOWN.
node | the node to set |
value | the character value for the node. |
int ASTNode_setClass | ( | ASTNode_t * | node, |
const char * | className | ||
) |
Sets the MathML "class" of the given node.
node | the node to set |
className | the new value for the "class" attribute |
int ASTNode_setDefinitionURL | ( | ASTNode_t * | node, |
XMLAttributes_t * | defnURL | ||
) |
Sets the MathML "definitionURL" attribute of the given node.
node | the node to modify |
defnURL | the value to which the attribute should be set |
int ASTNode_setDefinitionURLString | ( | ASTNode_t * | node, |
const char * | defnURL | ||
) |
Sets the MathML "definitionURL" attribute of the given node.
node | the node to modify |
defnURL | a string to which the attribute should be set |
int ASTNode_setId | ( | ASTNode_t * | node, |
const char * | id | ||
) |
Sets the MathML "id" attribute of the given node.
node | the node to set |
id | the identifier to use |
int ASTNode_setInteger | ( | ASTNode_t * | node, |
long | value | ||
) |
Sets the given node to a integer and sets it type to AST_INTEGER.
node | the node to set |
value | the value to set it to |
int ASTNode_setName | ( | ASTNode_t * | node, |
const char * | name | ||
) |
Sets the node to represent a named entity.
As a side-effect, this ASTNode_t object's type will be reset to AST_NAME if (and only if) the node
was previously an operator (i.e., ASTNode_isOperator() returns true), number (i.e., ASTNode_isNumber() returns true), or unknown. This allows names to be set for AST_FUNCTION nodes and the like.
node | the node to set |
name | the name value for the node |
int ASTNode_setRational | ( | ASTNode_t * | node, |
long | numerator, | ||
long | denominator | ||
) |
Sets the value of a given node to a rational number and sets its type to AST_RATIONAL.
node | the node to set |
numerator | the numerator value to use |
denominator | the denominator value to use |
int ASTNode_setReal | ( | ASTNode_t * | node, |
double | value | ||
) |
Sets the value of a given node to a real (double
) and sets its type to AST_REAL.
This is functionally equivalent to:
ASTNode_setRealWithExponent(node, value, 0);
node | the node to set |
value | the value to set the node to |
int ASTNode_setRealWithExponent | ( | ASTNode_t * | node, |
double | mantissa, | ||
long | exponent | ||
) |
Sets the value of a given node to a real (double
) in two parts, a mantissa and an exponent.
As a side-effect, the node's
type will be set to AST_REAL.
node | the node to set |
mantissa | the mantissa of this node's real-numbered value |
exponent | the exponent of this node's real-numbered value |
int ASTNode_setStyle | ( | ASTNode_t * | node, |
const char * | style | ||
) |
Sets the MathML "style" of the given node.
node | the node to set |
style | the new value for the "style" attribute |
int ASTNode_setType | ( | ASTNode_t * | node, |
ASTNodeType_t | type | ||
) |
Explicitly sets the type of the given ASTNode_t structure.
node | the node to set |
type | the new type |
int ASTNode_setUnits | ( | ASTNode_t * | node, |
const char * | units | ||
) |
Sets the units of the given node.
The units will be set only if the ASTNode_t object in node
represents a MathML <cn>
element, i.e., represents a number. Callers may use ASTNode_isNumber() to inquire whether the node is of that type.
sbml:units
on MathML cn
elements
appearing in SBML mathematical formulas. The value of this attribute can
be used to indicate the unit of measurement to be associated with the
number in the content of the cn
element. The value of this
attribute must be the identifier of a unit of measurement defined by SBML
or the enclosing Model. Here, the sbml
portion is an XML
namespace prefix that must be associated with the SBML namespace for SBML
Level 3. The following example illustrates how this attribute can be
used to define a number with value 10
and unit of measurement
second
:
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:sbml="http://www.sbml.org/sbml/level3/version1/core"> <cn type="integer" sbml:units="second"> 10 </cn> </math>
node | the node to modify |
units | the units to set it to. |
sbml:units
attribute is only available in SBML Level 3. It may not be used in Levels 1–2 of SBML. int ASTNode_setUserData | ( | ASTNode_t * | node, |
void * | userData | ||
) |
Sets the user data of the given node.
The user data can be used by the application developer to attach custom information to the node. In case of a deep copy, this attribute will passed as it is. The attribute will be never interpreted by this class.
node | the node to modify |
userData | the new user data |
Swaps the children of two nodes.
node | the node to modify |
that | the other node whose children should be used to replace those of node |
int ASTNode_unsetClass | ( | ASTNode_t * | node | ) |
Unsets the MathML "class" attribute of the given node.
node | the node to modify |
int ASTNode_unsetId | ( | ASTNode_t * | node | ) |
Unsets the MathML "id" attribute of the given node.
node | the node to modify |
int ASTNode_unsetStyle | ( | ASTNode_t * | node | ) |
Unsets the MathML "style" attribute of the given node.
node | the node to modify |
int ASTNode_unsetUnits | ( | ASTNode_t * | node | ) |
Unsets the units associated with the given node.
node | the node to modify |
int ASTNode_unsetUserData | ( | ASTNode_t * | node | ) |
Unsets the user data of the given node.
The user data can be used by the application developer to attach custom information to the node. In case of a deep copy, this attribute will passed as it is. The attribute will be never interpreted by this class.
node | the node to modify |
ASTNode_t * readMathMLFromString | ( | const char * | xml | ) |
Reads the MathML from the given XML string, constructs a corresponding abstract syntax tree, and returns a pointer to the root of the tree.
xml | a string containing a full MathML expression |
NULL
is returned if the given string is NULL
or invalid. ASTNode_t * readMathMLFromStringWithNamespaces | ( | const char * | xml, |
XMLNamespaces_t * | xmlns | ||
) |
Reads the MathML from the given XML string, constructs a corresponding abstract syntax tree, and returns a pointer to the root of the tree.
xml | a string containing a full MathML expression |
xmlns | an XMLNamespaces_t structure containing namespaces that are considered active during the read. (For example, an SBML Level 3 package namespace.) |
NULL
is returned if the given string is NULL
or invalid. char * SBML_formulaToL3String | ( | const ASTNode_t * | tree | ) |
Converts an AST to a string representation of a formula using a syntax derived from SBML Level 1, but extended to include elements from SBML Level 2 and SBML Level 3.
The following lists the main differences in the formula syntax supported by the Level 3 ("L3") versions of the formula parsers and formatters, compared to what is supported by the Level 1-oriented SBML_parseFormula() and SBML_formulaToString():
SId
in the SBML specifications). The whitespace between number and unit is optional.&&
(and), ||
(or), !
(not), and !=
(not equals) may be used.%
and will produce a <piecewise>
function in the corresponding MathML output by default, or can produce the MathML function rem
, depending on the L3ParserSettings_t object (see L3ParserSettings_setParseModuloL3v2() ).arc
as a prefix or simply a
; in other words, both arccsc
and acsc
are interpreted as the operator arccosecant as defined in MathML 2.0. (Many functions in the simpler SBML Level 1 oriented parser implemented by SBML_parseFormula() are defined this way as well, but not all.)(integer/integer)Spaces are not allowed in this construct; in other words, "
(3 / 4)
" (with whitespace between the numbers and the operator) will be parsed into the MathML <divide>
construct rather than a rational number. You can, however, assign units to a rational number as a whole; here is an example: "(3/4) ml
". (In the case of division rather than a rational number, units are not interpreted in this way.)The function log
with a single argument ("log(x)
") can be parsed as log10(x)
, ln(x)
, or treated as an error, as desired.
Unary minus signs can be collapsed or preserved; that is, sequential pairs of unary minuses (e.g., "- -3
") can be removed from the input entirely and single unary minuses can be incorporated into the number node, or all minuses can be preserved in the AST node structure.
Parsing of units embedded in the input string can be turned on and off.
The string avogadro
can be parsed as a MathML csymbol or as an identifier.
The string % can be parsed either as a piecewise function or as the 'rem' function: a % b
will either become
piecewise(a - b*ceil(a/b), xor((a < 0), (b < 0)), a - b*floor(a/b))
or
rem(a, b)
.
The latter is simpler, but the rem
MathML is only allowed as of SBML Level 3 Version 2.
A Model_t object may optionally be provided to the parser using the variant function call SBML_parseL3FormulaWithModel() or stored in a L3ParserSettings_t object passed to the variant function SBML_parseL3FormulaWithSettings(). When a Model_t object is provided, identifiers (values of type SId
) from that model are used in preference to pre-defined MathML definitions for both symbols and functions. More precisely:
In the case of symbols: the Model_t entities whose identifiers will shadow identical symbols in the mathematical formula are: Species_t, Compartment_t, Parameter_t, Reaction_t, and SpeciesReference_t. For instance, if the parser is given a Model_t containing a Species_t with the identifier "pi
", and the formula to be parsed is "3*pi
", the MathML produced will contain the construct <ci> pi </ci>
instead of the construct <pi/>
.
SId
values of user-defined functions present in the model will be used preferentially over pre-defined MathML functions. For example, if the passed-in Model_t contains a FunctionDefinition_t object with the identifier "sin
", that function will be used instead of the predefined MathML function <sin/>
. These configuration settings cannot be changed directly using the basic parser and formatter functions, but can be changed on a per-call basis by using the alternative functions SBML_parseL3FormulaWithSettings() and SBML_formulaToL3StringWithSettings().
Neither SBML nor the MathML standard define a "string-form" equivalent to MathML expressions. The approach taken by libSBML is to start with the formula syntax defined by SBML Level 1 (which in fact used a custom text-string representation of formulas, and not MathML), and expand it to include the functionality described above. This formula syntax is based mostly on C programming syntax, and may contain operators, function calls, symbols, and white space characters. The following table provides the precedence rules for the different entities that may appear in formula strings.
Token | Operation | Class | Preced. | Assoc. |
---|---|---|---|---|
name | symbol reference | operand | 8 | n/a |
( expression) | expression grouping | operand | 8 | n/a |
f( ...) | function call | prefix | 8 | left |
^ | power | binary | 7 | left |
-, ! | negation, Boolean 'not' | unary | 6 | right |
*, /, % | multip., div., modulo | binary | 5 | left |
+, - | addition and subtraction | binary | 4 | left |
==, <, >, <=, >=, != | Boolean comparisons | binary | 3 | left |
&&, || | Boolean 'and' and 'or' | binary | 2 | left |
, | argument delimiter | binary | 1 | left |
In the table above, operand implies the construct is an operand, prefix implies the operation is applied to the following arguments, unary implies there is one argument, and binary implies there are two arguments. The values in the Precedence column show how the order of different types of operation are determined. For example, the expression a + b * c
is evaluated as a + (b * c)
because the *
operator has higher precedence. The Associates column shows how the order of similar precedence operations is determined; for example, a && b || c
is evaluated as (a && b) || c
because the &&
and ||
operators are left-associative and have the same precedence.
The function call syntax consists of a function name, followed by optional white space, followed by an opening parenthesis token, followed by a sequence of zero or more arguments separated by commas (with each comma optionally preceded and/or followed by zero or more white space characters), followed by a closing parenthesis token. The function name must be chosen from one of the pre-defined functions in SBML or a user-defined function in the model. The following table lists the names of certain common mathematical functions; this table corresponds to Table 6 in the SBML Level 1 Version 2 specification with additions based on the functions added in SBML Level 2 and Level 3:
Name | Argument(s) | Formula or meaning | Argument Constraints | Result constraints |
---|---|---|---|---|
abs |
x | Absolute value of x. | ||
acos , arccos |
x | Arccosine of x in radians. | –1.0 ≤ x ≤ 1.0 | 0 ≤ acos(x) ≤ π |
acosh , arccosh |
x | Hyperbolic arccosine of x in radians. | ||
acot , arccot |
x | Arccotangent of x in radians. | ||
acoth , arccoth |
x | Hyperbolic arccotangent of x in radians. | ||
acsc , arccsc |
x | Arccosecant of x in radians. | ||
acsch , arccsch |
x | Hyperbolic arccosecant of x in radians. | ||
asec , arcsec |
x | Arcsecant of x in radians. | ||
asech , arcsech |
x | Hyperbolic arcsecant of x in radians. | ||
asin , arcsin |
x | Arcsine of x in radians. | –1.0 ≤ x ≤ 1.0 | 0 ≤ asin(x) ≤ π |
atan , arctan |
x | Arctangent of x in radians. | 0 ≤ atan(x) ≤ π | |
atanh , arctanh |
x | Hyperbolic arctangent of x in radians. | ||
ceil , ceiling |
x | Smallest number not less than x whose value is an exact integer. | ||
cos |
x | Cosine of x | ||
cosh |
x | Hyperbolic cosine of x. | ||
cot |
x | Cotangent of x. | ||
coth |
x | Hyperbolic cotangent of x. | ||
csc |
x | Cosecant of x. | ||
csch |
x | Hyperbolic cosecant of x. | ||
delay |
x, y | The value of x at y time units in the past. | ||
factorial |
n | The factorial of n. Factorials are defined by n! = n*(n–1)* ... * 1. | n must be an integer. | |
exp |
x | e x, where e is the base of the natural logarithm. | ||
floor |
x | The largest number not greater than x whose value is an exact integer. | ||
ln |
x | Natural logarithm of x. | x > 0 | |
log |
x | By default, the base 10 logarithm of x, but can be set to be the natural logarithm of x, or to be an illegal construct. | x > 0 | |
log |
x, y | The base x logarithm of y. | y > 0 | |
log10 |
x | Base 10 logarithm of x. | x > 0 | |
piecewise |
x1, y1, [x2, y2,] [...] [z] | A piecewise function: if (y1), x1. Otherwise, if (y2), x2, etc. Otherwise, z. | y1, y2, y3 [etc] must be Boolean | |
pow , power |
x, y | x y. | ||
root |
b, x | The root base b of x. | ||
sec |
x | Secant of x. | ||
sech |
x | Hyperbolic secant of x. | ||
sqr |
x | x2. | ||
sqrt |
x | √x. | x > 0 | sqrt(x) ≥ 0 |
sin |
x | Sine of x. | ||
sinh |
x | Hyperbolic sine of x. | ||
tan |
x | Tangent of x. | x ≠ n*π/2, for odd integer n | |
tanh |
x | Hyperbolic tangent of x. | ||
and |
x, y, z... | Boolean and(x, y, z...): returns true if all of its arguments are true. Note that and is an n-ary function, taking 0 or more arguments, and that and() returns true . |
All arguments must be Boolean | |
not |
x | Boolean not(x) | x must be Boolean | |
or |
x, y, z... | Boolean or(x, y, z...): returns true if at least one of its arguments is true. Note that or is an n-ary function, taking 0 or more arguments, and that or() returns false . |
All arguments must be Boolean | |
xor |
x, y, z... | Boolean xor(x, y, z...): returns true if an odd number of its arguments is true. Note that xor is an n-ary function, taking 0 or more arguments, and that xor() returns false . |
All arguments must be Boolean | |
eq |
x, y, z... | Boolean eq(x, y, z...): returns true if all arguments are equal. Note that eq is an n-ary function, but must take 2 or more arguments. |
||
geq |
x, y, z... | Boolean geq(x, y, z...): returns true if each argument is greater than or equal to the argument following it. Note that geq is an n-ary function, but must take 2 or more arguments. |
||
gt |
x, y, z... | Boolean gt(x, y, z...): returns true if each argument is greater than the argument following it. Note that gt is an n-ary function, but must take 2 or more arguments. |
||
leq |
x, y, z... | Boolean leq(x, y, z...): returns true if each argument is less than or equal to the argument following it. Note that leq is an n-ary function, but must take 2 or more arguments. |
||
lt |
x, y, z... | Boolean lt(x, y, z...): returns true if each argument is less than the argument following it. Note that lt is an n-ary function, but must take 2 or more arguments. |
||
neq |
x, y | Boolean x != y: returns true unless x and y are equal. |
||
plus |
x, y, z... | x + y + z + ...: The sum of the arguments of the function. Note that plus is an n-ary function taking 0 or more arguments, and that plus() returns 0 . |
||
times |
x, y, z... | x * y * z * ...: The product of the arguments of the function. Note that times is an n-ary function taking 0 or more arguments, and that times() returns 1 . |
||
minus |
x, y | x – y. | ||
divide |
x, y | x / y. |
Parsing of the various MathML functions and constants are all case-insensitive by default: function names such as cos
, Cos
and COS
are all parsed as the MathML cosine operator, <cos>
. However, when a Model_t object is used in conjunction with either SBML_parseL3FormulaWithModel() or SBML_parseL3FormulaWithSettings(), any identifiers found in that model will be parsed in a case-sensitive way. For example, if a model contains a Species_t having the identifier Pi
, the parser will parse "Pi
" in the input as "<ci> Pi </ci>
" but will continue to parse the symbols "pi
" and "PI
" as "<pi>
".
As mentioned above, the manner in which the "L3" versions of the formula parser and formatter interpret the function "log
" can be changed. To do so, callers should use the function SBML_parseL3FormulaWithSettings() and pass it an appropriate L3ParserSettings_t object. By default, unlike the SBML Level 1 parser implemented by SBML_parseFormula(), the string "log
" is interpreted as the base 10 logarithm, and not as the natural logarithm. However, you can change the interpretation to be base-10 log, natural log, or as an error; since the name "log" by itself is ambiguous, you require that the parser uses log10
or ln
instead, which are more clear. Please refer to SBML_parseL3FormulaWithSettings().
In addition, the following symbols will be translated to their MathML equivalents, if no symbol with the same SId
identifier string exists in the Model_t object provided:
Name | Meaning | MathML |
---|---|---|
true |
Boolean value true |
<true/> |
false |
Boolean value false |
<false/> |
pi |
Mathematical constant pi | <pi/> |
avogadro |
Value of Avogadro's constant stipulated by SBML | <csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/avogadro"> avogadro </csymbol/> |
time |
Simulation time as defined in SBML | <csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/time"> time </csymbol/> |
inf , infinity |
Mathematical constant "infinity" | <infinity/> |
nan , notanumber |
Mathematical concept "not a number" | <notanumber/> |
Again, as mentioned above, whether the string "avogadro
" is parsed as an AST node of type AST_NAME_AVOGADRO or AST_NAME is configurable; use the version of the parser function called SBML_parseL3FormulaWithSettings(). This Avogadro-related functionality is provided because SBML Level 2 models may not use AST_NAME_AVOGADRO AST nodes.
tree | the AST to be converted. |
char * SBML_formulaToL3StringWithSettings | ( | const ASTNode_t * | tree, |
const L3ParserSettings_t * | settings | ||
) |
Converts an AST to a string representation of a formula using a syntax basically derived from SBML Level 1, with behavior modifiable with custom settings.
This function behaves identically to SBML_formulaToL3String(), but its behavior can be modified by two settings in the
settings | object, namely: |
All other settings will not affect the behavior of this function: the 'parseLog' setting is ignored, and "log10(x)", "ln(x)", and "log(x, y)" are always produced. Nothing in the Model_t object is used, and whether Avogadro is a csymbol or not is immaterial to the produced infix.
tree | the AST to be converted. |
settings | the L3ParserSettings_t object used to modify behavior. |
char * SBML_formulaToString | ( | const ASTNode_t * | tree | ) |
Converts an AST to a string representation of a formula using a syntax basically derived from SBML Level 1.
0.10 * k4^2
(vm * s1)/(km + s1)
Note that this facility is provided as a convenience by libSBML—the MathML standard does not actually define a "string-form" equivalent to MathML expression trees, so the choice of formula syntax is somewhat arbitrary. The approach taken by libSBML is to use the syntax defined by SBML Level 1 (which in fact used a text-string representation of formulas and not MathML). This formula syntax is based mostly on C programming syntax, and may contain operators, function calls, symbols, and white space characters. The following table provides the precedence rules for the different entities that may appear in formula strings.
Token | Operation | Class | Precedence | Associates |
---|---|---|---|---|
name | symbol reference | operand | 6 | n/a |
( expression) | expression grouping | operand | 6 | n/a |
f( ...) | function call | prefix | 6 | left |
- | negation | unary | 5 | right |
^ | power | binary | 4 | left |
* | multiplication | binary | 3 | left |
/ | divison | binary | 3 | left |
+ | addition | binary | 2 | left |
- | subtraction | binary | 2 | left |
, | argument delimiter | binary | 1 | left |
In the table above, operand implies the construct is an operand, prefix implies the operation is applied to the following arguments, unary implies there is one argument, and binary implies there are two arguments. The values in the Precedence column show how the order of different types of operation are determined. For example, the expression a * b + c
is evaluated as (a * b) + c
because the *
operator has higher precedence. The Associates column shows how the order of similar precedence operations is determined; for example, a - b + c
is evaluated as (a - b) + c
because the +
and -
operators are left-associative.
The function call syntax consists of a function name, followed by optional white space, followed by an opening parenthesis token, followed by a sequence of zero or more arguments separated by commas (with each comma optionally preceded and/or followed by zero or more white space characters, followed by a closing parenthesis token. The function name must be chosen from one of the pre-defined functions in SBML or a user-defined function in the model. The following table lists the names of certain common mathematical functions; this table corresponds to Table 6 in the SBML Level 1 Version 2 specification:
Name | Args | Formula or meaning | Argument Constraints | Result constraints |
---|---|---|---|---|
abs | x | absolute value of x | ||
acos | x | arc cosine of x in radians | -1.0 ≤ x ≤ 1.0 | 0 ≤ acos(x) ≤ π |
asin | x | arc sine of x in radians | -1.0 ≤ x ≤ 1.0 | 0 ≤ asin(x) ≤ π |
atan | x | arc tangent of x in radians | 0 ≤ atan(x) ≤ π | |
ceil | x | smallest number not less than x whose value is an exact integer | ||
cos | x | cosine of x | ||
exp | x | e x, where e is the base of the natural logarithm | ||
floor | x | largest number not greater than x whose value is an exact integer | ||
log | x | natural logarithm of x | x > 0 | |
log10 | x | base 10 logarithm of x | x > 0 | |
pow | x, y | x y | ||
sqr | x | x2 | ||
sqrt | x | √x | x > 0 | sqrt(x) ≥ 0 |
sin | x | sine of x | ||
tan | x | tangent of x | x ≠ n*π/2, for odd integer n |
log(x)
represents the natural logarithm, whereas in MathML, the natural logarithm is <ln/>
. Application writers are urged to be careful when translating between text forms and MathML forms, especially if they provide a direct text-string input facility to users of their software systems.tree | the AST to be converted. |
L3ParserSettings_t * SBML_getDefaultL3ParserSettings | ( | ) |
char * SBML_getLastParseL3Error | ( | ) |
Returns the last error reported by the "L3" mathematical formula parser.
If the functions SBML_parseL3Formula(), SBML_parseL3FormulaWithSettings(), or SBML_parseL3FormulaWithModel() return NULL
, an error is set internally. This function allows callers to retrieve information about the error.
ASTNode_t * SBML_parseFormula | ( | const char * | formula | ) |
ASTNode_t * SBML_parseL3Formula | ( | const char * | formula | ) |
Parses a text string as a mathematical formula and returns an AST representation of it.
The following lists the main differences in the formula syntax supported by the Level 3 ("L3") versions of the formula parsers and formatters, compared to what is supported by the Level 1-oriented SBML_parseFormula() and SBML_formulaToString():
SId
in the SBML specifications). The whitespace between number and unit is optional.&&
(and), ||
(or), !
(not), and !=
(not equals) may be used.%
and will produce a <piecewise>
function in the corresponding MathML output by default, or can produce the MathML function rem
, depending on the L3ParserSettings_t object (see L3ParserSettings_setParseModuloL3v2() ).arc
as a prefix or simply a
; in other words, both arccsc
and acsc
are interpreted as the operator arccosecant as defined in MathML 2.0. (Many functions in the simpler SBML Level 1 oriented parser implemented by SBML_parseFormula() are defined this way as well, but not all.)(integer/integer)Spaces are not allowed in this construct; in other words, "
(3 / 4)
" (with whitespace between the numbers and the operator) will be parsed into the MathML <divide>
construct rather than a rational number. You can, however, assign units to a rational number as a whole; here is an example: "(3/4) ml
". (In the case of division rather than a rational number, units are not interpreted in this way.)The function log
with a single argument ("log(x)
") can be parsed as log10(x)
, ln(x)
, or treated as an error, as desired.
Unary minus signs can be collapsed or preserved; that is, sequential pairs of unary minuses (e.g., "- -3
") can be removed from the input entirely and single unary minuses can be incorporated into the number node, or all minuses can be preserved in the AST node structure.
Parsing of units embedded in the input string can be turned on and off.
The string avogadro
can be parsed as a MathML csymbol or as an identifier.
The string % can be parsed either as a piecewise function or as the 'rem' function: a % b
will either become
piecewise(a - b*ceil(a/b), xor((a < 0), (b < 0)), a - b*floor(a/b))
or
rem(a, b)
.
The latter is simpler, but the rem
MathML is only allowed as of SBML Level 3 Version 2.
A Model_t object may optionally be provided to the parser using the variant function call SBML_parseL3FormulaWithModel() or stored in a L3ParserSettings_t object passed to the variant function SBML_parseL3FormulaWithSettings(). When a Model_t object is provided, identifiers (values of type SId
) from that model are used in preference to pre-defined MathML definitions for both symbols and functions. More precisely:
In the case of symbols: the Model_t entities whose identifiers will shadow identical symbols in the mathematical formula are: Species_t, Compartment_t, Parameter_t, Reaction_t, and SpeciesReference_t. For instance, if the parser is given a Model_t containing a Species_t with the identifier "pi
", and the formula to be parsed is "3*pi
", the MathML produced will contain the construct <ci> pi </ci>
instead of the construct <pi/>
.
SId
values of user-defined functions present in the model will be used preferentially over pre-defined MathML functions. For example, if the passed-in Model_t contains a FunctionDefinition_t object with the identifier "sin
", that function will be used instead of the predefined MathML function <sin/>
. These configuration settings cannot be changed directly using the basic parser and formatter functions, but can be changed on a per-call basis by using the alternative functions SBML_parseL3FormulaWithSettings() and SBML_formulaToL3StringWithSettings().
Neither SBML nor the MathML standard define a "string-form" equivalent to MathML expressions. The approach taken by libSBML is to start with the formula syntax defined by SBML Level 1 (which in fact used a custom text-string representation of formulas, and not MathML), and expand it to include the functionality described above. This formula syntax is based mostly on C programming syntax, and may contain operators, function calls, symbols, and white space characters. The following table provides the precedence rules for the different entities that may appear in formula strings.
Token | Operation | Class | Preced. | Assoc. |
---|---|---|---|---|
name | symbol reference | operand | 8 | n/a |
( expression) | expression grouping | operand | 8 | n/a |
f( ...) | function call | prefix | 8 | left |
^ | power | binary | 7 | left |
-, ! | negation, Boolean 'not' | unary | 6 | right |
*, /, % | multip., div., modulo | binary | 5 | left |
+, - | addition and subtraction | binary | 4 | left |
==, <, >, <=, >=, != | Boolean comparisons | binary | 3 | left |
&&, || | Boolean 'and' and 'or' | binary | 2 | left |
, | argument delimiter | binary | 1 | left |
In the table above, operand implies the construct is an operand, prefix implies the operation is applied to the following arguments, unary implies there is one argument, and binary implies there are two arguments. The values in the Precedence column show how the order of different types of operation are determined. For example, the expression a + b * c
is evaluated as a + (b * c)
because the *
operator has higher precedence. The Associates column shows how the order of similar precedence operations is determined; for example, a && b || c
is evaluated as (a && b) || c
because the &&
and ||
operators are left-associative and have the same precedence.
The function call syntax consists of a function name, followed by optional white space, followed by an opening parenthesis token, followed by a sequence of zero or more arguments separated by commas (with each comma optionally preceded and/or followed by zero or more white space characters), followed by a closing parenthesis token. The function name must be chosen from one of the pre-defined functions in SBML or a user-defined function in the model. The following table lists the names of certain common mathematical functions; this table corresponds to Table 6 in the SBML Level 1 Version 2 specification with additions based on the functions added in SBML Level 2 and Level 3:
Name | Argument(s) | Formula or meaning | Argument Constraints | Result constraints |
---|---|---|---|---|
abs |
x | Absolute value of x. | ||
acos , arccos |
x | Arccosine of x in radians. | –1.0 ≤ x ≤ 1.0 | 0 ≤ acos(x) ≤ π |
acosh , arccosh |
x | Hyperbolic arccosine of x in radians. | ||
acot , arccot |
x | Arccotangent of x in radians. | ||
acoth , arccoth |
x | Hyperbolic arccotangent of x in radians. | ||
acsc , arccsc |
x | Arccosecant of x in radians. | ||
acsch , arccsch |
x | Hyperbolic arccosecant of x in radians. | ||
asec , arcsec |
x | Arcsecant of x in radians. | ||
asech , arcsech |
x | Hyperbolic arcsecant of x in radians. | ||
asin , arcsin |
x | Arcsine of x in radians. | –1.0 ≤ x ≤ 1.0 | 0 ≤ asin(x) ≤ π |
atan , arctan |
x | Arctangent of x in radians. | 0 ≤ atan(x) ≤ π | |
atanh , arctanh |
x | Hyperbolic arctangent of x in radians. | ||
ceil , ceiling |
x | Smallest number not less than x whose value is an exact integer. | ||
cos |
x | Cosine of x | ||
cosh |
x | Hyperbolic cosine of x. | ||
cot |
x | Cotangent of x. | ||
coth |
x | Hyperbolic cotangent of x. | ||
csc |
x | Cosecant of x. | ||
csch |
x | Hyperbolic cosecant of x. | ||
delay |
x, y | The value of x at y time units in the past. | ||
factorial |
n | The factorial of n. Factorials are defined by n! = n*(n–1)* ... * 1. | n must be an integer. | |
exp |
x | e x, where e is the base of the natural logarithm. | ||
floor |
x | The largest number not greater than x whose value is an exact integer. | ||
ln |
x | Natural logarithm of x. | x > 0 | |
log |
x | By default, the base 10 logarithm of x, but can be set to be the natural logarithm of x, or to be an illegal construct. | x > 0 | |
log |
x, y | The base x logarithm of y. | y > 0 | |
log10 |
x | Base 10 logarithm of x. | x > 0 | |
piecewise |
x1, y1, [x2, y2,] [...] [z] | A piecewise function: if (y1), x1. Otherwise, if (y2), x2, etc. Otherwise, z. | y1, y2, y3 [etc] must be Boolean | |
pow , power |
x, y | x y. | ||
root |
b, x | The root base b of x. | ||
sec |
x | Secant of x. | ||
sech |
x | Hyperbolic secant of x. | ||
sqr |
x | x2. | ||
sqrt |
x | √x. | x > 0 | sqrt(x) ≥ 0 |
sin |
x | Sine of x. | ||
sinh |
x | Hyperbolic sine of x. | ||
tan |
x | Tangent of x. | x ≠ n*π/2, for odd integer n | |
tanh |
x | Hyperbolic tangent of x. | ||
and |
x, y, z... | Boolean and(x, y, z...): returns true if all of its arguments are true. Note that and is an n-ary function, taking 0 or more arguments, and that and() returns true . |
All arguments must be Boolean | |
not |
x | Boolean not(x) | x must be Boolean | |
or |
x, y, z... | Boolean or(x, y, z...): returns true if at least one of its arguments is true. Note that or is an n-ary function, taking 0 or more arguments, and that or() returns false . |
All arguments must be Boolean | |
xor |
x, y, z... | Boolean xor(x, y, z...): returns true if an odd number of its arguments is true. Note that xor is an n-ary function, taking 0 or more arguments, and that xor() returns false . |
All arguments must be Boolean | |
eq |
x, y, z... | Boolean eq(x, y, z...): returns true if all arguments are equal. Note that eq is an n-ary function, but must take 2 or more arguments. |
||
geq |
x, y, z... | Boolean geq(x, y, z...): returns true if each argument is greater than or equal to the argument following it. Note that geq is an n-ary function, but must take 2 or more arguments. |
||
gt |
x, y, z... | Boolean gt(x, y, z...): returns true if each argument is greater than the argument following it. Note that gt is an n-ary function, but must take 2 or more arguments. |
||
leq |
x, y, z... | Boolean leq(x, y, z...): returns true if each argument is less than or equal to the argument following it. Note that leq is an n-ary function, but must take 2 or more arguments. |
||
lt |
x, y, z... | Boolean lt(x, y, z...): returns true if each argument is less than the argument following it. Note that lt is an n-ary function, but must take 2 or more arguments. |
||
neq |
x, y | Boolean x != y: returns true unless x and y are equal. |
||
plus |
x, y, z... | x + y + z + ...: The sum of the arguments of the function. Note that plus is an n-ary function taking 0 or more arguments, and that plus() returns 0 . |
||
times |
x, y, z... | x * y * z * ...: The product of the arguments of the function. Note that times is an n-ary function taking 0 or more arguments, and that times() returns 1 . |
||
minus |
x, y | x – y. | ||
divide |
x, y | x / y. |
Parsing of the various MathML functions and constants are all case-insensitive by default: function names such as cos
, Cos
and COS
are all parsed as the MathML cosine operator, <cos>
. However, when a Model_t object is used in conjunction with either SBML_parseL3FormulaWithModel() or SBML_parseL3FormulaWithSettings(), any identifiers found in that model will be parsed in a case-sensitive way. For example, if a model contains a Species_t having the identifier Pi
, the parser will parse "Pi
" in the input as "<ci> Pi </ci>
" but will continue to parse the symbols "pi
" and "PI
" as "<pi>
".
As mentioned above, the manner in which the "L3" versions of the formula parser and formatter interpret the function "log
" can be changed. To do so, callers should use the function SBML_parseL3FormulaWithSettings() and pass it an appropriate L3ParserSettings_t object. By default, unlike the SBML Level 1 parser implemented by SBML_parseFormula(), the string "log
" is interpreted as the base 10 logarithm, and not as the natural logarithm. However, you can change the interpretation to be base-10 log, natural log, or as an error; since the name "log" by itself is ambiguous, you require that the parser uses log10
or ln
instead, which are more clear. Please refer to SBML_parseL3FormulaWithSettings().
In addition, the following symbols will be translated to their MathML equivalents, if no symbol with the same SId
identifier string exists in the Model_t object provided:
Name | Meaning | MathML |
---|---|---|
true |
Boolean value true |
<true/> |
false |
Boolean value false |
<false/> |
pi |
Mathematical constant pi | <pi/> |
avogadro |
Value of Avogadro's constant stipulated by SBML | <csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/avogadro"> avogadro </csymbol/> |
time |
Simulation time as defined in SBML | <csymbol encoding="text" definitionURL="http://www.sbml.org/sbml/symbols/time"> time </csymbol/> |
inf , infinity |
Mathematical constant "infinity" | <infinity/> |
nan , notanumber |
Mathematical concept "not a number" | <notanumber/> |
Again, as mentioned above, whether the string "avogadro
" is parsed as an AST node of type AST_NAME_AVOGADRO or AST_NAME is configurable; use the version of the parser function called SBML_parseL3FormulaWithSettings(). This Avogadro-related functionality is provided because SBML Level 2 models may not use AST_NAME_AVOGADRO AST nodes.
formula | the text-string formula expression to be parsed. |
NULL
if an error occurred while parsing the formula. When NULL
is returned, an error is recorded internally; information about the error can be retrieved using SBML_getLastParseL3Error().Parses a text string as a mathematical formula using a Model_t to resolve symbols, and returns an AST representation of the result.
This is identical to SBML_parseL3Formula(), except that this function uses the given model in the argument model
to check against identifiers that appear in the formula
. For more information about the parser, please see the definition of L3ParserSettings_t and the function SBML_parseL3Formula().
formula | the mathematical formula expression to be parsed. |
model | the Model_t object to use for checking identifiers. |
NULL
if an error occurred while parsing the formula. When NULL
is returned, an error is recorded internally; information about the error can be retrieved using SBML_getLastParseL3Error().ASTNode_t * SBML_parseL3FormulaWithSettings | ( | const char * | formula, |
const L3ParserSettings_t * | settings | ||
) |
Parses a text string as a mathematical formula using specific parser settings and returns an AST representation of the result.
This is identical to SBML_parseL3Formula(), except that this function uses the parser settings given in the argument settings
. The settings override the default parsing behavior. The following parsing behaviors can be configured:
SId
) from the model in preference to pre-defined MathML symbols More precisely, the Model_t entities whose identifiers will shadow identical symbols in the mathematical formula are: Species_t, Compartment_t, Parameter_t, Reaction_t, and SpeciesReference_t. For instance, if the parser is given a Model_t containing a Species_t with the identifier "pi
", and the formula to be parsed is "3*pi
", the MathML produced by the parser will contain the construct <ci> pi </ci>
instead of the construct <pi/>
. Another example, if the passed-in Model_t contains a FunctionDefinition_t with the identifier "sin
", that function will be used instead of the predefined MathML function <sin/>
. log
with a single argument ("log(x)
") can be parsed as log10(x)
, ln(x)
, or treated as an error, as desired. - -3
") from the input and incorporate single unary minuses into the number node, or (2) preserve all minuses in the AST node structure, turning them into ASTNode_t objects of type AST_MINUS. number id
" can be interpreted as a numerical value number
followed by units of measurement indicated by id
, or it can be treated as a syntax error. (In Level 3, MathML <cn>
elements can have an attribute named units
placed in the SBML namespace, which can be used to indicate the units to be associated with the number. The text-string infix formula parser allows units to be placed after raw numbers; they are interpreted as unit identifiers for units defined by the SBML specification or in the containing Model_t object.) avogadro
can be parsed either as a MathML csymbol or as a identifier. More specifically, "avogadro
" can be treated as an ASTNode_t of type AST_NAME_AVOGADRO or of type AST_NAME. For more details about the parser, please see the definition of L3ParserSettings_t and SBML_parseL3FormulaWithSettings().
formula | the mathematical formula expression to be parsed. |
settings | the settings to be used for this parser invocation. |
NULL
if an error occurred while parsing the formula. When NULL
is returned, an error is recorded internally; information about the error can be retrieved using SBML_getLastParseL3Error().char * writeMathMLToString | ( | const ASTNode_t * | node | ) |
Writes the given ASTNode_t (and its children) to a string as MathML, and returns the string.
node | the root of an AST to write out to the stream. |
NULL
is returned if the given argument is NULL
. char * writeMathMLToString | ( | const ASTNode * | node | ) |
char * writeMathMLWithNamespaceToString | ( | const ASTNode_t * | node, |
SBMLNamespaces_t * | sbmlns | ||
) |
Writes the given AST node (and its children) to a string as MathML, and returns the string.
node | the root of an AST to write out to the stream. |
sbmlns | the SBML namespace to be used |
NULL
is returned if the given argument is NULL
.