Package VASSAL.script

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
    • getProperty

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

      public Object getLocalizedProperty(String name)
    • getString

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

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

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

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

      public Object getAttachmentProperty(String attachment, String property, String indexOrName, PropertySource ps)
    • sumStack

      public Object sumStack(String 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
    • countStack

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

      public Object maxAttachment(String attachment, String 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(String attachment, String 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(String attachment, String property, PropertySource ps)
      SumAttachment(attachment, property) function Total the value of the named property in all pieces attached
      Parameters:
      attachment - Attachment Name
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • countAttachment

      public Object countAttachment(String attachment, String property, PropertySource ps)
      CountAttachment(property) function count the number of pieces attached to this piece via a named Attachment trait that *contain* the specified property
      Parameters:
      attachment - Attachment Name
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • countAttachmentExpression

      public Object countAttachmentExpression(String attachment, String expression, PropertySource ps)
      CountAttachmentExpression(attachment, expression) function count the number of pieces attached to this piece via a named Attachment trait for which the expression is true
      Parameters:
      attachment - Attachment Name e.g. "MyTurrets"
      expression - Expression e.g. " { Ammo > 2 } "
      ps - GamePiece
      Returns:
      total
    • sumMat

      public Object sumMat(String property, 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:
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • countMat

      public Object countMat(String property, 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:
      property - Property Name
      ps - GamePiece
      Returns:
      total
    • sumLocation

      public Object sumLocation(String 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(String property, String 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
    • sumMapLocation

      public Object sumMapLocation(String property, String location, String 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
    • sumMapLocation

      public Object sumMapLocation(String property, String location, String map, String 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)
      CountLocation() function. Return count of pieces in the same location as the current piece
      Returns:
      Piece Count
    • countLocation

      public Object countLocation(String propertyOrExpression, PropertySource ps)
      CountLocation(property) and CountLocation(expression) functions. Return count of pieces in the same location as the current piece that have a non-nlank value for a property or that match an expression
      Parameters:
      propertyOrExpression - Property Name or Match Expression
      Returns:
      Piece Count
    • countLocation

      public Object countLocation(String property, String expression, PropertySource ps)
      CountLocation(property, expression) functions. Return count of pieces in the same location as the current piece that have a non-nlank value for a property and that match an expression
      Parameters:
      property - Property Name
      expression - Match Expression
      Returns:
      Piece Count
    • countMapLocation

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

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

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

      public Object sumZone(String property, PropertySource ps)
      SumZone(property) function Total the value of the named property in all counters in the same zone as the specified piece.

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

      public Object sumZone(String property, String expression, PropertySource ps)
      SumZone(property, expression) function Total the value of the named property in all counters in the same zone as the specified piece that meet the supplied expression.

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

      public Object sumMapZone(String property, String zoneName, String map, PropertySource ps)
      SumMapZone(property, location, map) function Total the value of the named property in all counters in the specified map and zone

      Parameters:
      property - Property Name
      zoneName - Zone Name
      map - Map Name
      ps - GamePiece source
      Returns:
      total
    • sumMapZone

      public Object sumMapZone(String property, String zoneName, String map, String expression, PropertySource ps)
      SumMapZone(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
      zoneName - 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(String propertyOrExpression, PropertySource ps)
      CountZone(property) and CountZone(expression) functions. Return count of pieces in the same zone as the current piece that have a non-blank value for a property or that match an expression
      Parameters:
      propertyOrExpression - Property Name or Match Expression
      Returns:
      Piece Count
    • countZone

      public Object countZone(String property, String expression, PropertySource ps)
      CountLocation(zone, expression) function Return count of pieces in the same zome as the current piece that have a non-blank value for a property and that match an expression
      Parameters:
      property - property name
      expression - match expression
      Returns:
      Piece Count
    • countMapZone

      public Object countMapZone(String zoneName, String mapName, PropertySource ps)
      CountMapZone(Zone, Map) function Return the count of pieces in the specified Zone/Map
      Parameters:
      zoneName - Target Zone Name
      mapName - Target Map Name
      ps - Source Piece
      Returns:
      Count
    • countMapZone

      public Object countMapZone(String zoneName, String mapName, String propertyOrExpression, PropertySource ps)
      CountMapZone(Zone, Map, property) and CountMapZone(Zone, Map, expression) function Return the count of pieces in the specified Zone/Map that match the supplied expression OR have a non-blank value for the supplied property name
      Parameters:
      zoneName - Target Zone Name
      mapName - Target Map Name
      propertyOrExpression - A Property Name or a Beanshell expression
      ps - Source Piece
      Returns:
      Count
    • countMapZone

      public Object countMapZone(String zoneName, String mapName, String property, String expression, PropertySource ps)
      CountMapZone(Zone, Map, property, expression) function Return the count of pieces in the specified Zone/Map that match the supplied expression AND have a non-blank value for the supplied property name
      Parameters:
      zoneName - Target Zone Name
      mapName - Target Map Name
      property - A Property Name
      expression - A Beanshell expression
      ps - Source Piece
      Returns:
      Count
    • rangeInPixels

      public int rangeInPixels(String attachmentName, PropertySource ps)
      Implementation of Range functions
    • rangeInCells

      public int rangeInCells(String attachmentName, PropertySource ps)
    • rangeInPixels

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

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

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

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

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

      public int rangeInCells(Object x1, Object y1, Object x2, Object y2, String mapName, PropertySource ps)
    • 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)