public class CobraFormulaParser extends java.lang.Object implements IFormulaParser, CobraFormulaParserConstants
Support almost the same syntax as defined in the LibSBML L3 parser.
One of the main differences is that ID are allowed to start by a number so that ID like that '8639_AT1' are recognised as ID.
The things not supported for now are the units associated with numbers.
Parsing of the various MathML functions and constants are all
case-sensitive by default: names such as
Cos
and COS
are not parsed as the MathML cosine
operator, <cos>
.
You can change this behaviour by using the setCaseSensitive(boolean)
method.
Token | Operation | Class | Precedence | Associates |
---|---|---|---|---|
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 and boolean 'not' | unary | 6 | right |
*, /, % | multiplication, division, and modulo | binary | 5 | left |
+, - | addition and subtraction | binary | 4 | left |
==, <, >, <=, >=, != | boolean equality, inequality, and comparison | 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 @c * 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 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. |
Modifier and Type | Class and Description |
---|---|
(package private) static class |
CobraFormulaParser.JJCalls |
private static class |
CobraFormulaParser.LookaheadSuccess |
Modifier and Type | Field and Description |
---|---|
private boolean |
ignoreCase |
private CobraFormulaParser.JJCalls[] |
jj_2_rtns |
private int |
jj_endpos |
private java.util.List<int[]> |
jj_expentries |
private int[] |
jj_expentry |
private int |
jj_gc |
private int |
jj_gen |
(package private) SimpleCharStream |
jj_input_stream |
private int |
jj_kind |
private int |
jj_la |
private int[] |
jj_la1 |
private static int[] |
jj_la1_0 |
private Token |
jj_lastpos |
private int[] |
jj_lasttokens |
private CobraFormulaParser.LookaheadSuccess |
jj_ls |
Token |
jj_nt
Next token.
|
private int |
jj_ntk |
private boolean |
jj_rescan |
private Token |
jj_scanpos |
static java.util.Properties |
stringToType |
Token |
token
Current token.
|
CobraFormulaParserTokenManager |
token_source
Generated Token Manager.
|
AND, BOOLEAN_LOGIC, CLOSE_PAR, COMPARISON, DEFAULT, DIGIT, DIVIDE, EOF, EOL, IDCHAR, LEFT_BRACES, LEFT_BRACKET, LETTER, LOG, MINUS, MODULO, NOT, OPEN_PAR, OR, PLUS, POWER, RIGHT_BRACES, RIGHT_BRACKET, SPLITTER, STRING, TIMES, tokenImage, XOR
Constructor and Description |
---|
CobraFormulaParser(CobraFormulaParserTokenManager tm)
Constructor with generated Token Manager.
|
CobraFormulaParser(java.io.InputStream stream)
Constructor with InputStream.
|
CobraFormulaParser(java.io.InputStream stream,
java.lang.String encoding)
Constructor with InputStream and supplied encoding
|
CobraFormulaParser(java.io.Reader stream)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkSize(java.util.ArrayList<ASTNode> arguments,
int i) |
private ASTNode |
createModulo(ASTNode leftChild,
ASTNode rightChild)
Returns a piecewise
ASTNode representing the modulo operation between the left and right child given. |
void |
disable_tracing()
Disable tracing.
|
void |
enable_tracing()
Enable tracing.
|
private ASTNode |
expression() |
ParseException |
generateParseException()
Generate ParseException.
|
private java.lang.Integer |
getInteger(ASTNode node) |
Token |
getNextToken()
Get the next Token.
|
Token |
getToken(int index)
Get the specific Token.
|
private boolean |
jj_2_1(int xla) |
private boolean |
jj_2_2(int xla) |
private boolean |
jj_2_3(int xla) |
private boolean |
jj_3_1() |
private boolean |
jj_3_2() |
private boolean |
jj_3_3() |
private boolean |
jj_3R_10() |
private boolean |
jj_3R_11() |
private boolean |
jj_3R_12() |
private boolean |
jj_3R_13() |
private boolean |
jj_3R_14() |
private boolean |
jj_3R_15() |
private boolean |
jj_3R_16() |
private boolean |
jj_3R_17() |
private boolean |
jj_3R_18() |
private boolean |
jj_3R_19() |
private boolean |
jj_3R_20() |
private boolean |
jj_3R_21() |
private boolean |
jj_3R_22() |
private boolean |
jj_3R_23() |
private boolean |
jj_3R_24() |
private boolean |
jj_3R_25() |
private boolean |
jj_3R_26() |
private boolean |
jj_3R_27() |
private boolean |
jj_3R_28() |
private boolean |
jj_3R_29() |
private boolean |
jj_3R_30() |
private boolean |
jj_3R_31() |
private boolean |
jj_3R_32() |
private boolean |
jj_3R_33() |
private boolean |
jj_3R_34() |
private void |
jj_add_error_token(int kind,
int pos) |
private Token |
jj_consume_token(int kind) |
private static void |
jj_la1_init_0() |
private int |
jj_ntk() |
private void |
jj_rescan_token() |
private void |
jj_save(int index,
int xla) |
private boolean |
jj_scan_token(int kind) |
ASTNode |
parse() |
void |
ReInit(CobraFormulaParserTokenManager tm)
Reinitialise.
|
void |
ReInit(java.io.InputStream stream)
Reinitialise.
|
void |
ReInit(java.io.InputStream stream,
java.lang.String encoding)
Reinitialise.
|
void |
ReInit(java.io.Reader stream)
Reinitialise.
|
void |
setCaseSensitive(boolean caseSensitive)
Sets the case sensitivity of the parser, for the mathml elements.
|
Token |
string() |
private ASTNode |
termLevel2() |
private ASTNode |
termLevel3() |
private ASTNode |
termLevel4() |
private ASTNode |
termLevel5() |
private ASTNode |
termLevel6() |
private ASTNode |
termLevel7() |
private ASTNode |
termLevel8() |
public static java.util.Properties stringToType
private boolean ignoreCase
public CobraFormulaParserTokenManager token_source
SimpleCharStream jj_input_stream
public Token token
public Token jj_nt
private int jj_ntk
private Token jj_scanpos
private Token jj_lastpos
private int jj_la
private int jj_gen
private final int[] jj_la1
private static int[] jj_la1_0
private final CobraFormulaParser.JJCalls[] jj_2_rtns
private boolean jj_rescan
private int jj_gc
private final CobraFormulaParser.LookaheadSuccess jj_ls
private java.util.List<int[]> jj_expentries
private int[] jj_expentry
private int jj_kind
private int[] jj_lasttokens
private int jj_endpos
public CobraFormulaParser(java.io.InputStream stream)
public CobraFormulaParser(java.io.InputStream stream, java.lang.String encoding)
public CobraFormulaParser(java.io.Reader stream)
public CobraFormulaParser(CobraFormulaParserTokenManager tm)
public void setCaseSensitive(boolean caseSensitive)
The default behavior is to be case sensitive, meaning
'cos
' would be recognized as the mathematical cosinus but
'Cos
', 'cOs
' or any other alternatives would be recognized
as a name and read as a 'ci' element. If you pass false
to this method
all the different versions of cos
would be recognized as the mathematical
cosinus.
caseSensitive
- boolean to define if the parser should be case sensitive or not.private void checkSize(java.util.ArrayList<ASTNode> arguments, int i) throws ParseException
ParseException
private java.lang.Integer getInteger(ASTNode node)
private ASTNode createModulo(ASTNode leftChild, ASTNode rightChild)
ASTNode
representing the modulo operation between the left and right child given.
The formula produced for 'a % b' or modulo(a, b) is 'piecewise(floor(a/b), gt(a/b, 0), ceil(a/b))'leftChild
- rightChild
- ASTNode
representing the modulo operation between the left and right child given.public final Token string() throws ParseException
ParseException
public final ASTNode parse() throws ParseException
parse
in interface IFormulaParser
ParseException
private final ASTNode expression() throws ParseException
ParseException
private final ASTNode termLevel2() throws ParseException
ParseException
private final ASTNode termLevel3() throws ParseException
ParseException
private final ASTNode termLevel4() throws ParseException
ParseException
private final ASTNode termLevel5() throws ParseException
ParseException
private final ASTNode termLevel6() throws ParseException
ParseException
private final ASTNode termLevel7() throws ParseException
ParseException
private final ASTNode termLevel8() throws ParseException, java.lang.NumberFormatException
ParseException
java.lang.NumberFormatException
private boolean jj_2_1(int xla)
private boolean jj_2_2(int xla)
private boolean jj_2_3(int xla)
private boolean jj_3R_28()
private boolean jj_3R_22()
private boolean jj_3R_31()
private boolean jj_3R_14()
private boolean jj_3_3()
private boolean jj_3R_34()
private boolean jj_3R_29()
private boolean jj_3R_12()
private boolean jj_3R_18()
private boolean jj_3R_21()
private boolean jj_3R_16()
private boolean jj_3R_15()
private boolean jj_3R_13()
private boolean jj_3R_11()
private boolean jj_3R_33()
private boolean jj_3R_27()
private boolean jj_3R_25()
private boolean jj_3R_10()
private boolean jj_3R_30()
private boolean jj_3_1()
private boolean jj_3_2()
private boolean jj_3R_26()
private boolean jj_3R_24()
private boolean jj_3R_20()
private boolean jj_3R_32()
private boolean jj_3R_17()
private boolean jj_3R_19()
private boolean jj_3R_23()
private static void jj_la1_init_0()
public void ReInit(java.io.InputStream stream)
ReInit
in interface IFormulaParser
public void ReInit(java.io.InputStream stream, java.lang.String encoding)
ReInit
in interface IFormulaParser
public void ReInit(java.io.Reader stream)
ReInit
in interface IFormulaParser
public void ReInit(CobraFormulaParserTokenManager tm)
private Token jj_consume_token(int kind) throws ParseException
ParseException
private boolean jj_scan_token(int kind)
public final Token getNextToken()
public final Token getToken(int index)
private int jj_ntk()
private void jj_add_error_token(int kind, int pos)
public ParseException generateParseException()
public final void enable_tracing()
public final void disable_tracing()
private void jj_rescan_token()
private void jj_save(int index, int xla)