Class Variable

All Implemented Interfaces:
Described, Expression, Logical, Unique, Comparable<Variable>

public final class Variable extends SignedAsset implements Comparable<Variable>, Expression
A variable is a signed asset that represents a feature of the story world state that can change. A state assign a value to each of a story world's variables.
Author:
Stephen G. Ware
  • Field Details

    • encoding

      public final String encoding
      The name of the encoding this variable uses to encode its values
  • Constructor Details

    • Variable

      public Variable(int id, Signature signature, String encoding, String description)
      Constructs a new variable.
      Parameters:
      id - the variable's unique ID number
      signature - the variable's unique signature
      encoding - the name of the encoding used to encode the variable's value
      description - the variable's description
  • Method Details

    • compareTo

      public int compareTo(Variable other)
      Specified by:
      compareTo in interface Comparable<Variable>
    • setID

      protected Variable setID(int id)
      Description copied from class: Asset
      Returns a new asset that is the same as this asset, except that its ID number is the given value.
      Specified by:
      setID in class Asset
      Parameters:
      id - the ID number the new asset should have
      Returns:
      an asset identical to this asset, except with the given ID number
    • setSignature

      protected Variable setSignature(Signature signature)
      Description copied from class: SignedAsset
      Returns a new signed asset that is the same as this asset, except that its signature and name are the given value.
      Specified by:
      setSignature in class SignedAsset
      Parameters:
      signature - the signature and name the new asset should have
      Returns:
      a signed asset identical to this asset, except with the given signature and name
    • setEncoding

      protected Variable setEncoding(String encoding)
      Returns a new variable that is the same as this variable, except that its value encoding is the given value.
      Parameters:
      encoding - the name of the value encoding the new variable should have
      Returns:
      a variable identical to this variable, except with the given value encoding
    • setDescription

      public Variable setDescription(String description)
      Description copied from class: Asset
      Returns a new asset that is the same as this asset, except that its description is the given value.
      Specified by:
      setDescription in class Asset
      Parameters:
      description - the description the new asset should have
      Returns:
      an asset identical to this asset, except with the given description
    • substitute

      public Variable substitute(Function<Object,Object> substitution)
      Description copied from interface: Logical
      Returns a logical object identical to this one, except that its logical elements have been replaced according to the given substitution. A substitution defines how some logical formula (the input to the function) should be replaced with a different formula (the output of the function).
      Specified by:
      substitute in interface Expression
      Specified by:
      substitute in interface Logical
      Specified by:
      substitute in class SignedAsset
      Parameters:
      substitution - a function which maps logical formula that should be replaced to the formula they should be replaced with
      Returns:
      an identical object, except that its logical formula have been replaced according to the substitution
    • evaluate

      public Value evaluate(State state)
      Description copied from interface: Expression
      Returns the value of this logical expression in the given state.
      Specified by:
      evaluate in interface Expression
      Parameters:
      state - a mapping of values to all of a story world's variables
      Returns:
      the value of the expression in the given state
    • encode

      public String encode(Object value)
      Uses the variable's value encoding to encode the given object.
      Parameters:
      value - the object to be encoded
      Returns:
      the encoding of the given value
      Throws:
      RuntimeException - if the object cannot be encoded
    • decode

      public Value decode(Object value)
      Uses the variable's value encoding to decode the given object and return it as a value.
      Parameters:
      value - the object to be decoded
      Returns:
      the object that results from decoding the given object, as a logical value
      Throws:
      RuntimeException - if the object cannot be decoded