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 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_UseNative

        protected boolean m_UseNative
        whether to output native objects rather than strings.
    • Constructor Detail

      • LookUpUpdate

        public LookUpUpdate()
    • Method Detail

      • getGrammar

        public String getGrammar()
        Returns a string representation of the grammar.
        Returns:
        the grammar, null if not available
      • 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
      • loadSheet

        protected void loadSheet()
        Loads the spreadsheet from disk, if possible.
      • 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 convert
        keyCol - the column with the symbol names
        valueCol - 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/update
        keyCol - the column with with the symbol names
        valueCol - the column with the values
        updated - 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 evaluate
        symbols - 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 evaluate
        symbols - the symbol/value mapping
        sheet - the spreadsheet to update
        keyCol - 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