Package adams.parser
Class LookUpUpdate
-
- All Implemented Interfaces:
Destroyable
,GlobalInfoSupporter
,LoggingLevelHandler
,LoggingSupporter
,OptionHandler
,SizeOfHandler
,GrammarSupplier
,Serializable
public class LookUpUpdate extends AbstractSymbolEvaluator<SpreadSheet>
Evaluates lookup update rules updating the spreadsheet.
The following grammar is used:
expr_list ::= expr_list expr_part | expr_part
expr_part ::= conditional | assignment
conditional ::= if expr then assignments end
| if expr then assignments else assignments end
assignments ::= assignments assignment | assignment
assignment ::=
VARIABLE := expr;
| all ( "regexp" ) := expr;
expr ::= ( expr )
| NUMBER
| STRING
| BOOLEAN
| VARIABLE
| true
| false
| -expr
| expr < expr
| expr <= expr
| expr > expr
| expr >= expr
| expr = expr
| expr != expr
| not expr
| expr and expr
| expr or expr
# 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 )
| len[gth] ( str )
| find ( search , expr [, pos] ) (find 'search' in 'expr', return 1-based position)
| contains ( str , find ) (checks whether 'str' string contains 'find' string)
| replace ( str , pos , len , newstr )
| replaceall ( str , regexp , replace ) (applies regular expression to 'str' and replaces all matches with 'replace')
| substitute ( str , find , replace [, occurrences] )
| str ( expr )
| str ( expr , numdecimals )
| str ( expr , decimalformat )
| expr + expr
| expr - expr
| expr * expr
| expr / expr
| expr % expr
| expr ^ expr
| abs ( expr )
| sqrt ( expr )
| cbrt ( expr )
| log ( expr )
| log10 ( expr )
| exp ( expr )
| sin ( expr )
| sinh ( expr )
| cos ( expr )
| cosh ( expr )
| tan ( expr )
| tanh ( expr )
| atan ( expr )
| atan2 ( exprY , exprX )
| hypot ( exprX , exprY )
| signum ( expr )
| rint ( expr )
| floor ( expr )
| pow[er] ( expr , expr )
| ceil ( expr )
| min ( expr1 , expr2 )
| max ( expr1 , expr2 )
| has ( variable )
Notes:
- Variables are either all alphanumeric and -/_ (e.g., "ABc_1-2"), any character
apart from "]" enclosed by "[" and "]" (e.g., "[Hello World]") or
enclosed by single quotes (e.g., "'Hello World'").
- The 'all' method applies the value to all the values in the lookup table
that match the regular expression.
- Positions are 1-based.
- 'str' uses java.text.DecimalFormat when supplying a format string
- Variables starting with '_' (inside the [] or '') are considered local and don't get transferred back out.
- The 'has' function checks whether a variable/symbol is present.
-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) The logging level for outputting errors and debugging output. default: WARNING
-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 lookup update rules to evaluate. default:
-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}
-key-column <adams.data.spreadsheet.SpreadSheetColumnIndex> (property: keyColumn) The index of the column in the spreadsheet to use as key; An index is a number starting with 1; column names (case-sensitive) as well as the following placeholders can be used: first, second, third, last_2, last_1, last; numeric indices can be enforced by preceding them with '#' (eg '#12'); column names can be surrounded by double quotes. default: 1 example: An index is a number starting with 1; column names (case-sensitive) as well as the following placeholders can be used: first, second, third, last_2, last_1, last; numeric indices can be enforced by preceding them with '#' (eg '#12'); column names can be surrounded by double quotes.
-value-column <adams.data.spreadsheet.SpreadSheetColumnIndex> (property: valueColumn) The index of the column in the spreadsheet to use as value; An index is a number starting with 1; column names (case-sensitive) as well as the following placeholders can be used: first, second, third, last_2, last_1, last; numeric indices can be enforced by preceding them with '#' (eg '#12'); column names can be surrounded by double quotes. default: 2 example: An index is a number starting with 1; column names (case-sensitive) as well as the following placeholders can be used: first, second, third, last_2, last_1, last; numeric indices can be enforced by preceding them with '#' (eg '#12'); column names can be surrounded by double quotes.
-use-native <boolean> (property: useNative) If enabled, native objects are used as value rather than strings. default: false
- 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 SpreadSheetColumnIndex
m_KeyColumn
the index of the column to use as key.protected SpreadSheetReader
m_Reader
the spreadsheet reader for loading the spreadsheet.protected SpreadSheet
m_Sheet
the lookup to use.protected boolean
m_UseNative
whether to output native objects rather than strings.protected SpreadSheetColumnIndex
m_ValueColumn
the index of the column to use as value.-
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 LookUpUpdate()
-
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 SpreadSheet
doEvaluate(HashMap symbols)
Performs the actual evaluation.SpreadSheet
evaluate()
Performs the evaluation.static HashMap
evaluate(String expr, HashMap symbols)
Parses and evaluates the given expression.static SpreadSheet
evaluate(String expr, HashMap symbols, SpreadSheet sheet, int keyCol, int valueCol)
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.SpreadSheetColumnIndex
getKeyColumn()
Returns the index of the column to act as key in the lookup table.SpreadSheetReader
getReader()
Returns the spreadsheet reader that loads the sheet.SpreadSheet
getSheet()
Returns the underlying spreadsheet.boolean
getUseNative()
Returns whether native objects are output rather than strings.SpreadSheetColumnIndex
getValueColumn()
Returns the index of the column to act as value in the lookup table.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.String
keyColumnTipText()
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
setKeyColumn(SpreadSheetColumnIndex value)
Sets the index of the column to act as key in the lookup table.void
setReader(SpreadSheetReader value)
Sets the spreadsheet reader that loads the sheet.void
setSheet(SpreadSheet value)
Sets the underlying spreadsheet.void
setUseNative(boolean value)
Sets whether to output native objects rather than strings.void
setValueColumn(SpreadSheetColumnIndex value)
Sets the index of the column to act as value in the lookup table.static HashMap
spreadsheetToSymbols(SpreadSheet sheet, int keyCol, int valueCol)
Turns the values from the spreadsheet into symbols for the parser.static SpreadSheet
updateSpreadSheet(SpreadSheet sheet, int keyCol, int valueCol, HashMap updated)
Creates a copy of the spreadsheet and updates its values with the ones from updated symbols map.String
useNativeTipText()
Returns the tip text for this property.String
valueColumnTipText()
Returns the tip text for this property.-
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 lookup 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.
-
m_KeyColumn
protected SpreadSheetColumnIndex m_KeyColumn
the index of the column to use as key.
-
m_ValueColumn
protected SpreadSheetColumnIndex m_ValueColumn
the index of the column to use as value.
-
m_UseNative
protected boolean m_UseNative
whether to output native objects rather than strings.
-
-
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<SpreadSheet>
-
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<SpreadSheet>
- Returns:
- the default expression
-
expressionTipText
public String expressionTipText()
Returns the tip text for this property.- Specified by:
expressionTipText
in classAbstractExpressionEvaluator<SpreadSheet>
- 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.
-
setKeyColumn
public void setKeyColumn(SpreadSheetColumnIndex value)
Sets the index of the column to act as key in the lookup table.- Parameters:
value
- the index
-
getKeyColumn
public SpreadSheetColumnIndex getKeyColumn()
Returns the index of the column to act as key in the lookup table.- Returns:
- the index
-
keyColumnTipText
public String keyColumnTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setValueColumn
public void setValueColumn(SpreadSheetColumnIndex value)
Sets the index of the column to act as value in the lookup table.- Parameters:
value
- the index
-
getValueColumn
public SpreadSheetColumnIndex getValueColumn()
Returns the index of the column to act as value in the lookup table.- Returns:
- the index
-
valueColumnTipText
public String valueColumnTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setUseNative
public void setUseNative(boolean value)
Sets whether to output native objects rather than strings.- Parameters:
value
- true if to output native objects
-
getUseNative
public boolean getUseNative()
Returns whether native objects are output rather than strings.- Returns:
- true if native objects are used
-
useNativeTipText
public String useNativeTipText()
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<SpreadSheet>
- Parameters:
name
- the name of the symbolvalue
- the string representation of the symbol- Returns:
- the object representation of the symbol
-
doEvaluate
protected SpreadSheet doEvaluate(HashMap symbols) throws Exception
Performs the actual evaluation.- Specified by:
doEvaluate
in classAbstractSymbolEvaluator<SpreadSheet>
- 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 SpreadSheet evaluate() throws Exception
Performs the evaluation.- Overrides:
evaluate
in classAbstractSymbolEvaluator<SpreadSheet>
- Returns:
- the evaluation, or null in case of error
- Throws:
Exception
- if evaluation fails
-
spreadsheetToSymbols
public static HashMap spreadsheetToSymbols(SpreadSheet sheet, int keyCol, int valueCol)
Turns the values from the spreadsheet into symbols for the parser.- Parameters:
sheet
- the spreadsheet to convertkeyCol
- the column with the symbol namesvalueCol
- the column with the symbol values- Returns:
- the generated symbols
-
updateSpreadSheet
public static SpreadSheet updateSpreadSheet(SpreadSheet sheet, int keyCol, int valueCol, HashMap updated)
Creates a copy of the spreadsheet and updates its values with the ones from updated symbols map.- Parameters:
sheet
- the spreadsheet to copy/updatekeyCol
- the column with with the symbol namesvalueCol
- the column with the valuesupdated
- the updated symbols- Returns:
- the updated copy of the spreadsheet
-
evaluate
public static HashMap evaluate(String expr, HashMap symbols) 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 updated symbols
- Throws:
Exception
- if something goes wrong
-
evaluate
public static SpreadSheet evaluate(String expr, HashMap symbols, SpreadSheet sheet, int keyCol, int valueCol) 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 mappingsheet
- the spreadsheet to updatekeyCol
- the key column (0-based)valueCol
- the value column (0-based)- 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
-
-