Class ExpressionInterpreter

java.lang.Object
bsh.Interpreter
VASSAL.script.AbstractInterpreter
VASSAL.script.ExpressionInterpreter
All Implemented Interfaces:
bsh.ConsoleInterface, Serializable, Runnable, Auditable, RecursionLimiter.Loopable

public class ExpressionInterpreter extends AbstractInterpreter implements RecursionLimiter.Loopable
A BeanShell Interpreter customised to evaluate a single Vassal expression containing Vassal property references. All traits with the same expression will share the same Interpreter Each ExpressionInterpreter has 2 levels of NameSpace: 1. Top level is a single global NameSpace that contains utility methods available to all ExpressionInterpreters. It is the parent of all level 2 NameSpaces. 2. Level 2 is a NameSpace for each unique expression that contains the parsed expression. All expressions in all traits that are the same will use the one Expression NameSpace.
See Also:
  • Field Details

  • Constructor Details

    • ExpressionInterpreter

      public ExpressionInterpreter(String expr) throws ExpressionException
      Private constructor to build an ExpressionInterpreter. Interpreters can only be created by createInterpreter.
      Parameters:
      expr - Expression
      Throws:
      ExpressionException - Invalid Expression details
  • Method Details

    • getComponentTypeName

      public String getComponentTypeName()
      Description copied from interface: Auditable
      Return a description of the Type of trait or Component an Auditable is
      Specified by:
      getComponentTypeName in interface Auditable
      Returns:
      Component Type
    • getComponentName

      public String getComponentName()
      Description copied from interface: Auditable
      Return the name of the trait or Component an Auditable is
      Specified by:
      getComponentName in interface Auditable
      Returns:
      Component name
    • strip

      protected static String strip(String expr)
    • initialiseStatic

      protected void initialiseStatic()
      Initialise the static elements of this class. Create a Top Level NameSpace using the Vassal class loader, load useful classes and read and process the init_expression.bsh file to load scripted methods available to expressions.
    • getExpression

      public String getExpression()
      Return the current expression
      Returns:
      expression
    • evaluate

      public String evaluate(PropertySource ps) throws ExpressionException
      Evaluate the expression, setting the value of any undefined values to the matching Vassal property value. Primitives must be wrapped.
      Returns:
      result
      Throws:
      ExpressionException
    • evaluate

      public String evaluate(PropertySource ps, boolean localized) throws ExpressionException
      Throws:
      ExpressionException
    • evaluate

      public String evaluate(PropertySource ps, boolean localized, Auditable owner, AuditTrail audit) throws ExpressionException
      Throws:
      ExpressionException
    • evaluate

      public String evaluate(PropertySource ps, Map<String,String> properties, boolean localized, Auditable owner, AuditTrail audit) throws ExpressionException
      Throws:
      ExpressionException
    • evaluate

      public String evaluate() throws ExpressionException
      Throws:
      ExpressionException
    • wrap

      public Object wrap(String value)
      Convert a String value into a wrapped primitive object if possible. Note this is a non-static copy of BeanShell.wrap(). Callbacks from beanshell (e.g. getProperty) fail if an attempt is made to call a static method.
      Parameters:
      value - Value to wrap
      Returns:
      wrapped value
    • wrap

      public Object wrap(Object value)
      Wrap a possibly already wrapped value.
      Parameters:
      value - A value that may already be wrapped (due to auto-boxing)
      Returns:
      Wrapped value
    • getProperty

      public Object getProperty(Object name)
      Callbacks from BeanShell Expressions to Vassal
    • getLocalizedProperty

      public Object getLocalizedProperty(Object name)
    • getString

      public Object getString(Object name)
      getProperty minus the wrap
    • getZoneProperty

      public Object getZoneProperty(Object propertyName, Object zoneName)
    • getZoneProperty

      public Object getZoneProperty(Object propertyName, Object zoneName, Object mapName)
    • getMapProperty

      public Object getMapProperty(Object propertyName, Object mapName)
    • getAttachmentProperty

      public Object getAttachmentProperty(Object attachment, Object property, Object indexOrNameOrexpression, PropertySource ps)
      Callback for Beanshell getAttachProperty functions
      Parameters:
      attachment - Attachment name
      property - Property name
      indexOrNameOrexpression - An index number, a piece Basic Name or an Expression
      ps - Source Piece
      Returns:
      Property value
    • sumStack

      public Object sumStack(Object property, PropertySource ps)
      SumStack(property) function Total the value of the named property in all counters in the same stack as the specified piece.
      Parameters:
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • sumStack

      public Object sumStack(Object property, Object expression, PropertySource ps)
      SumStack(property, expression) function Total the value of the named property in all counters in the same stack as the specified piece that meet the specified expression
      Parameters:
      property - Property Name
      expression - Expression
      ps - GamePiece
      Returns:
      total
    • countStack

      public Object countStack(Object propertyOrExpression, PropertySource ps)
      CountStack(property) and CountStack(expression) function count the number of pieces in the same stack which have any non-blank value for the specified property.
      Parameters:
      propertyOrExpression - Property Name or Expression
      ps - GamePiece
      Returns:
      total
    • countStack

      public Object countStack(Object property, Object expression, PropertySource ps)
    • maxAttachment

      public Object maxAttachment(Object attachment, Object property, PropertySource ps)
      MaxAttachment(attachment, property) function Highest value of the named property among all pieces attached, or 0 if no pieces are attached
      Parameters:
      attachment - Attachment Name
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • minAttachment

      public Object minAttachment(Object attachment, Object property, PropertySource ps)
      MinAttachment(attachment, property) function Lowest value of the named property among all pieces attached, or 0 if no pieces are attached
      Parameters:
      attachment - Attachment Name
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • sumAttachment

      public Object sumAttachment(Object attachment, Object property, PropertySource ps)
      SumAttach Beanshell function implementation
    • sumAttachment

      public Object sumAttachment(Object attachment, Object property, Object expression, PropertySource ps)
    • countAttachment

      public Object countAttachment(Object attachment, Object propertyOrExpression, PropertySource ps)
      CountAttach Beanshell function implementation
    • countAttachment

      public Object countAttachment(Object attachment, Object property, Object expression, PropertySource ps)
    • sumMat

      public Object sumMat(Object propertyOrExpression, PropertySource ps)
      SumMat(property) function Total the value of the named property in all counters among the Mat-and-MatCargo grouping of the current piece
      Parameters:
      propertyOrExpression - Property Name
      ps - GamePiece
      Returns:
      total
    • sumMat

      public Object sumMat(Object property, Object expression, PropertySource ps)
    • countMat

      public Object countMat(Object propertyOrExpression, PropertySource ps)
      CountMat(property) function Return the total number of counters with a non-blank value for the specified property among the Mat-and-MatCargo grouping of the current piece
      Parameters:
      propertyOrExpression - Property Name
      ps - GamePiece
      Returns:
      total
    • countMat

      public Object countMat(PropertySource ps)
    • countMat

      public Object countMat(Object property, Object expression, PropertySource ps)
    • sumLocation

      public Object sumLocation(Object property, PropertySource ps)
      SumLocation(property) function Total the value of the named property in all counters in the same location as the specified piece.

      Parameters:
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • sumLocation

      public Object sumLocation(Object property, Object expression, PropertySource ps)
      SumLocation(property, expression) function Total the value of the named property in all counters in the same location as the specified piece that meet the supplied expression.

      Parameters:
      property - Property Name
      expression - Expression
      ps - GamePiece Source
      Returns:
      total
    • sumLocation

      public Object sumLocation(Object property, Object location, Object map, PropertySource ps)
      SumMapLocation(property, location, map) function Total the value of the named property in all counters in the specified map and location

      Parameters:
      property - Property Name
      location - Location Name
      map - Map Name
      ps - GamePiece source
      Returns:
      total
    • sumLocation

      public Object sumLocation(Object property, Object location, Object map, Object expression, PropertySource ps)
      SumMapLocation(property, location, map, expression) function Total the value of the named property in all counters in the specified map and location that match the supplied expression

      Parameters:
      property - Property Name
      location - Location Name
      map - Map Name
      expression - Expression
      ps - GamePiece source
      Returns:
      total
    • countLocation

      public Object countLocation(PropertySource ps)
      BeanShell CountLocation implementation Return count of pieces in the same location as the current piece
      Returns:
      Piece Count
    • countLocation

      public Object countLocation(Object propertyOrExpression, PropertySource ps)
    • countLocation

      public Object countLocation(Object propertyOrLocation, Object expressionOrMap, PropertySource ps)
    • countMapLocation

      public Object countMapLocation(Object locationName, Object mapName, Object propertyOrExpression, PropertySource ps)
    • countMapLocation

      public Object countMapLocation(Object locationName, Object mapName, Object property, Object expression, PropertySource ps)
    • sumZone

      public Object sumZone(Object property, Object zone, Object map, Object expression, PropertySource ps)
      SumZone(property, location, map, expression) function Total the value of the named property in all counters in the specified map and zone that match the supplied expression

      Parameters:
      property - Property Name
      zone - Zone Name
      map - Map Name
      expression - Expression
      ps - GamePiece source
      Returns:
      total
    • countZone

      public Object countZone(PropertySource ps)
      CountZone() function. Return count of pieces in the same zone as the current piece
      Returns:
      Piece Count
    • countZone

      public Object countZone(Object propertyOrExpression, PropertySource ps)
    • countZone

      public Object countZone(Object propertyOrZone, Object expressionOrMap, PropertySource ps)
    • countCurrentZone

      public Object countCurrentZone(Object propertyName, Object expression, PropertySource ps)
    • countZone

      public Object countZone(Object zoneName, Object mapName, Object propertyOrExpression, PropertySource ps)
    • countZone

      public Object countZone(Object zoneName, Object mapName, Object property, Object expression, PropertySource ps)
    • sumMap

      public Object sumMap(Object property, PropertySource ps)
    • sumMap

      public Object sumMap(Object property, Object mapOrExpression, PropertySource ps)
    • sumMap

      public Object sumMap(Object property, Object map, Object expression, PropertySource ps)
    • countMap

      public Object countMap(PropertySource ps)
    • countMap

      public Object countMap(Object propertyOrExpressionOrMap, PropertySource ps)
    • countMap

      public Object countMap(Object propertyOrMap, Object expressionOrProperty, PropertySource ps)
    • countMap

      public Object countMap(Object map, Object property, Object expression, PropertySource ps)
    • rangeAttach

      public Object rangeAttach(Object attachmentName, Object expression, boolean asPixels, PropertySource ps)
      Implementation of Range functions
    • rangeInPixels

      public Object rangeInPixels(Object x, Object y, PropertySource ps)
    • rangeInCells

      public Object rangeInCells(Object x, Object y, PropertySource ps)
    • rangeInPixels

      public Object rangeInPixels(Object x1, Object y1, Object x2, Object y2, PropertySource ps)
    • rangeInCells

      public Object rangeInCells(Object x1, Object y1, Object x2, Object y2, PropertySource ps)
    • rangeInPixels

      public Object rangeInPixels(Object x1, Object y1, Object x2, Object y2, Object mapName, PropertySource ps)
    • rangeInCells

      public Object rangeInCells(Object x1, Object y1, Object x2, Object y2, Object mapName, PropertySource ps)
    • sumRange

      public Object sumRange(Object propertyName, Object expression, Object minRange, Object maxRange, Boolean asPixels, PropertySource ps)
      Implementation of all flavours of SumRange and SumRangePx functions
      Parameters:
      propertyName - Property name to sum
      expression - Optional expression to select targets
      minRange - minimum Range
      maxRange - maximum range
      asPixels - true to force range check to be pixels
      ps - Source piece
      Returns:
      Sum of properties
    • countRange

      public Object countRange(Object propertyOrExpression, Object expression, Object minRange, Object maxRange, Boolean asPixels, PropertySource ps)
      Implementation of all flavours of CountRange and CounrRangePx functions
      Parameters:
      propertyOrExpression - Property name to sum OR Expression if no property supplied
      expression - Optional expression to select targets
      minRange - minimum Range
      maxRange - maximum range
      asPixels - true to force range check to be pixels
      ps - Source piece
      Returns:
      Count of matching pieces
    • random

      public Object random(Object src, Object minString, Object maxString)
    • isRandom

      public Object isRandom(Object src, Object percentString)
    • sum

      public Object sum(Object src, Object propertyName, Object propertyMatch)
    • sum

      public Object sum(Object src, Object propertyName, Object propertyMatch, Object mapName)
    • count

      public Object count(Object src, Object propertyMatch)
    • count

      public Object count(Object src, Object propertyMatch, Object mapName)
    • audit

      public Object audit(Object message, Object conditionOrOptionList, Object optionList, PropertySource ps)
      Audit Write a message to the errorlog
      Parameters:
      message - Message to display. Can be an Expression
      conditionOrOptionList - If provided, is an expression that will be evaluated and the message will only be written if true
      optionList - A String containing any of the following letters to invoke various options 'F' - Include full audit of the current expression 'C' - Display in Chatter 'S' - Suppress logging to errorLog
      Returns:
      Returns a blank string
    • sleep

      public Object sleep(Object ms, PropertySource ps)
      Refresh the screen, then delay for the specified number of milliseconds This is not pretty, but is the only way I have found to force a proper UI refresh - Open a modal dialog box way offscreen. This forces a an actual real Swing UI refresh, then hangs the UI - Start a new thread that closes the dialog box after a specified delay
      Parameters:
      ms - Milliseconds to delay
      ps - A Property source
      Returns:
      blank String