Package adams.parser
Class SpreadSheetFormula
-
- All Implemented Interfaces:
Destroyable
,GlobalInfoSupporter
,LoggingLevelHandler
,LoggingSupporter
,OptionHandler
,SizeOfHandler
,GrammarSupplier
,Serializable
public class SpreadSheetFormula extends AbstractSymbolEvaluator<Object>
Evaluates mathematical expressions.
The following grammar is used:
expr_list ::= '=' expr_list expr_part | expr_part ;
expr_part ::= expr ;
expr ::= ( expr )
# data types
| number
| string
| boolean
| date
| cell
# constants
| true
| false
| pi
| e
| now()
| today()
# negating numeric value
| -expr
# comparisons
| expr < expr
| expr <= expr
| expr > expr
| expr >= expr
| expr = expr
| expr != expr (or: expr <> expr)
# boolean operations
| ! expr (or: not expr)
| expr & expr (or: expr and expr)
| expr | expr (or: expr or expr)
| if[else] ( expr , expr (if true) , expr (if false) )
# arithmetics
| expr + expr
| expr - expr
| expr * expr
| expr / expr
| expr ^ expr (power of)
| expr % expr (modulo)
;
# numeric functions
| abs ( expr | cell )
| sqrt ( expr | cell )
| log ( expr | cell )
| exp ( expr | cell )
| sin ( expr | cell )
| cos ( expr | cell )
| tan ( expr | cell )
| rint ( expr | cell )
| floor ( expr | cell )
| pow[er] ( expr | cell , expr | cell )
| ceil ( expr | cell )
| sum ( cell1 : cell2 )
| min ( cell1 : cell2 )
| max ( cell1 : cell2 )
| average ( cell1 : cell2 )
| stdev ( cell1 : cell2 )
| stdevp ( cell1 : cell2 )
| countif ( cell1 : cell2 ; expr )
| sumif ( cell1 : cell2 ; expr )
| sumif ( cell1 : cell2 ; expr : sumCell1 : sumCell2 )
| intercept ( cellY1 : cellY2 ; cellX1 : cellX2 )
| slope ( cellY1 : cellY2 ; cellX1 : cellX2 )
| countblank ( cell1 : cell2 )
| year ( expr | cell )
| month ( expr | cell )
| day ( expr | cell )
| hour ( expr | cell )
| minute ( expr | cell )
| second ( expr | cell )
| weekday ( expr | cell )
| weeknum ( expr | cell )
# string functions
| substr ( expr , start [, end] )
| left ( expr , len )
| mid ( expr , start , len )
| right ( expr , len )
| rept ( expr , count )
| concatenate ( expr1 , expr2 [, expr3-5] )
| lower[case] ( expr )
| upper[case] ( expr )
| trim ( expr )
| matches ( expr , regexp )
| contains ( str , find ) (checks whether 'str' string contains 'find' string)
| startswith ( str , find ) (checks whether 'str' string starts with 'find' string)
| endswith ( str , find ) (checks whether 'str' string ends with 'find' string)
| trim ( expr )
| len[gth] ( str )
| find ( search , expr [, pos] )
| replace ( str , pos , len , newstr )
| substitute ( str , find , replace [, occurrences] )
;
# obtaining native cell content
| cellobj ( cell )
# obtaining cell content as string
| cellstr ( cell )
Notes:
- Cells are denoted by column in letter and row in digit, e.g., 'C12'.
- 'start' and 'end' for function 'substr' are indices that start at 1.
- Index 'end' for function 'substr' is excluded (like Java's 'String.substring(int,int)' method)
- Line comments start with '#'.
- Semi-colons (';') or commas (',') can be used as separator in the formulas,
e.g., 'pow(2,2)' is equivalent to 'pow(2;2)'
- dates have to be of format 'yyyy-MM-dd' or 'yyyy-MM-dd HH:mm:ss'
- times have to be of format 'HH:mm:ss' or 'yyyy-MM-dd HH:mm:ss'
- the characters in square brackets in function names are optional:
e.g. 'len("abc")' is the same as 'length("abc")'
A lot of the functions have been modeled after LibreOffice:
https://help.libreoffice.org/Calc/Functions_by_Category
Additional functions:
- env(String): String
First argument is the name of the environment variable to retrieve.
The result is the value of the environment variable.
Additional procedures:
- println(...)
One or more arguments are printed as comma-separated list to stdout.
If no argument is provided, a simple line feed is output.
Code example 1:String expr = "pow(BASE,EXPONENT)*MULT"; HashMap symbols = new HashMap(); symbols.put("BASE", 2.0); symbols.put("EXPONENT", 9.0); symbols.put("MULT", 0.1); double result = SpreadSheetFormula.evaluate(expr, symbols); System.out.println(expr + " and " + symbols + " = " + result);
Code Example 2 (uses the "ifelse" construct):String expr = "ifelse(I<0,pow(BASE,I*0.5),pow(BASE,I))"; SpreadSheetFormula.TreeNode tree = SpreadSheetFormula.parse(expr); HashMap symbols = new HashMap(); symbols.put("BASE", 2.0); for (int i = -10; i <= 10; i++) { symbols.put("I", (double) i); double result = SpreadSheetFormula.evaluate(expr, symbols); System.out.println(expr + " and " + symbols + " = " + result); }
-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) The logging level for outputting errors and debugging output. default: WARNING min-user-mode: Expert
-env <java.lang.String> (property: environment) The class to use for determining the environment. default: adams.env.Environment
-expression <java.lang.String> (property: expression) The spreadsheet formula to evaluate (must evaluate to a double). default: = 42
-symbol <adams.core.base.BaseString> [-symbol ...] (property: symbols) The symbols to initialize the parser with, key-value pairs: name=value. default:
-reader <adams.data.io.input.SpreadSheetReader> (property: reader) The spreadsheet reader for loading the spreadsheet to work on. default: adams.data.io.input.CsvSpreadSheetReader -data-row-type adams.data.spreadsheet.DenseDataRow -spreadsheet-type adams.data.spreadsheet.DefaultSpreadSheet
-input <adams.core.io.PlaceholderFile> (property: input) The input file to load with the specified reader; ignored if pointing to directory. default: ${CWD}
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected PlaceholderFile
m_Input
the spreadsheet file to read.protected SpreadSheetReader
m_Reader
the spreadsheet reader for loading the spreadsheet.protected SpreadSheet
m_Sheet
the spreadsheet to use.-
Fields inherited from class adams.parser.AbstractSymbolEvaluator
m_Symbols
-
Fields inherited from class adams.parser.AbstractExpressionEvaluator
m_Environment, m_Expression
-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description SpreadSheetFormula()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
defineOptions()
Adds options to the internal list of options.protected Object
doEvaluate(HashMap symbols)
Performs the actual evaluation.Object
evaluate()
Performs the evaluation.static Object
evaluate(String expr, HashMap symbols, SpreadSheet sheet)
Parses and evaluates the given expression.String
expressionTipText()
Returns the tip text for this property.protected String
getDefaultExpression()
Returns the default expression to use.String
getGrammar()
Returns a string representation of the grammar.PlaceholderFile
getInput()
Returns the spreadsheet file to load, ignored if pointing to directory.SpreadSheetReader
getReader()
Returns the spreadsheet reader that loads the sheet.SpreadSheet
getSheet()
Returns the underlying spreadsheet.String
globalInfo()
Returns a string describing the object.protected Object
initializeSymbol(String name, String value)
Initializes the symbol.String
inputTipText()
Returns the tip text for this property.protected void
loadSheet()
Loads the spreadsheet from disk, if possible.static void
main(String[] args)
Runs the evaluator from command-line.String
readerTipText()
Returns the tip text for this property.void
setInput(PlaceholderFile value)
Sets the spreadsheet file to load, ignored if pointing to directory.void
setReader(SpreadSheetReader value)
Sets the spreadsheet reader that loads the sheet.void
setSheet(SpreadSheet value)
Sets the underlying spreadsheet.-
Methods inherited from class adams.parser.AbstractSymbolEvaluator
getSymbols, initialize, initializeSymbols, setSymbols, symbolsTipText
-
Methods inherited from class adams.parser.AbstractExpressionEvaluator
environmentTipText, forCommandLine, forName, getEnvironment, getExpression, runEvaluator, setEnvironment, setExpression
-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, destroy, finishInit, getDefaultLoggingLevel, getOptionManager, loggingLevelTipText, newOptionManager, reset, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel
-
-
-
-
Field Detail
-
m_Sheet
protected SpreadSheet m_Sheet
the spreadsheet to use.
-
m_Reader
protected SpreadSheetReader m_Reader
the spreadsheet reader for loading the spreadsheet.
-
m_Input
protected PlaceholderFile m_Input
the spreadsheet file to read.
-
-
Method Detail
-
globalInfo
public String globalInfo()
Returns a string describing the object.- Specified by:
globalInfo
in interfaceGlobalInfoSupporter
- Specified by:
globalInfo
in classAbstractOptionHandler
- Returns:
- a description suitable for displaying in the gui
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptions
in interfaceOptionHandler
- Overrides:
defineOptions
in classAbstractSymbolEvaluator<Object>
-
getGrammar
public String getGrammar()
Returns a string representation of the grammar.- Returns:
- the grammar, null if not available
-
getDefaultExpression
protected String getDefaultExpression()
Returns the default expression to use.- Specified by:
getDefaultExpression
in classAbstractExpressionEvaluator<Object>
- Returns:
- the default expression
-
expressionTipText
public String expressionTipText()
Returns the tip text for this property.- Specified by:
expressionTipText
in classAbstractExpressionEvaluator<Object>
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setReader
public void setReader(SpreadSheetReader value)
Sets the spreadsheet reader that loads the sheet.- Parameters:
value
- the reader
-
getReader
public SpreadSheetReader getReader()
Returns the spreadsheet reader that loads the sheet.- Returns:
- the reader
-
readerTipText
public String readerTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setInput
public void setInput(PlaceholderFile value)
Sets the spreadsheet file to load, ignored if pointing to directory.- Parameters:
value
- the input file
-
getInput
public PlaceholderFile getInput()
Returns the spreadsheet file to load, ignored if pointing to directory.- Returns:
- the input file
-
inputTipText
public String inputTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setSheet
public void setSheet(SpreadSheet value)
Sets the underlying spreadsheet.- Parameters:
value
- the spreadsheet
-
getSheet
public SpreadSheet getSheet()
Returns the underlying spreadsheet.- Returns:
- the spreadsheet
-
initializeSymbol
protected Object initializeSymbol(String name, String value)
Initializes the symbol.- Specified by:
initializeSymbol
in classAbstractSymbolEvaluator<Object>
- Parameters:
name
- the name of the symbolvalue
- the string representation of the symbol- Returns:
- the object representation of the symbol
-
doEvaluate
protected Object doEvaluate(HashMap symbols) throws Exception
Performs the actual evaluation.- Specified by:
doEvaluate
in classAbstractSymbolEvaluator<Object>
- Parameters:
symbols
- the symbols to use- Returns:
- the evaluation, or null in case of error
- Throws:
Exception
- if evaluation fails
-
loadSheet
protected void loadSheet()
Loads the spreadsheet from disk, if possible.
-
evaluate
public Object evaluate() throws Exception
Performs the evaluation.- Overrides:
evaluate
in classAbstractSymbolEvaluator<Object>
- Returns:
- the evaluation, or null in case of error
- Throws:
Exception
- if evaluation fails
-
evaluate
public static Object evaluate(String expr, HashMap symbols, SpreadSheet sheet) throws Exception
Parses and evaluates the given expression. Returns the result of the mathematical expression, based on the given values of the symbols.- Parameters:
expr
- the expression to evaluatesymbols
- the symbol/value mapping- Returns:
- the evaluated result
- Throws:
Exception
- if something goes wrong
-
main
public static void main(String[] args)
Runs the evaluator from command-line.- Parameters:
args
- the command-line options, use "-help" to list them
-
-