Flocc
    Preparing search index...

    Class Rule

    The Rule class is an experimental interface for adding behavior to Agents. A Rule object may be used in place of a tick function to be added as Agent behavior using agent.set('tick', tickRule). As a trivial example, consider the following Rule, which increments the Agent's x value with every time step:

    const rule = new Rule(environment, [
    "set", "x", [
    "add", 1, [
    "get", "x"
    ]
    ]
    ]);
    agent.set("tick", rule);

    Reading from the outer arrays inward, the steps of this Rule instructs the Agent to:

    • set the Agent's "x" value to...
      • The result of adding 1 and...
        • The Agent's current "x" value

    Generally, Rule steps are a deeply nested array, where the first value of any given array is always an instruction or operator (e.g. "set", "add", "filter"). See the constructor function for more information about steps.

    0.3.0

    Index

    Constructors

    • A single step may be as simple as ["get", "x"]. This returns the Agent's "x" value to the outer step that contains it. So, for example, the step ["add", 1, ["get", "x"]], working from the inside out, retrieves the "x" value and then adds 1 to it. More complex steps function similarly, always traversing to the deepest nested step, evaluating it, and 'unwrapping' until all steps have been executed.

      A step's first element should be a string that is one of the allowed operators, followed by a certain number of arguments.

      Operator Arguments Notes
      "add" 2 Pass 2 numbers, or two steps that evaluate to numbers
      "subtract" 2 ""
      "multiply" 2 ""
      "divide" 2 ""
      "mod" 2 ""
      "power" 2 ""
      "get" 1 Pass the key of Agent data to retrieve
      "set" 2 Pass the key and value to set
      "enqueue" 2 Pass the key and value to enqueue
      "local" 2 Pass the key and value to set as local variables
      "if" 3 Pass the conditional (usually a step that evaluates to a boolean), the step to run when true, and the step to run when `false
      "and" 2 Pass the two steps to logically evaluate
      "or" 2 ""
      "gt" 2 ""
      "gte" 2 ""
      "lt" 2 ""
      "lte" 2 ""
      "eq" 2 ""
      "map" 2 Pass an array and a lambda. For simple lambdas like ["add", 2], the element is spliced as the first operand. For compound lambdas (using and, or, if), use ["local", "_"] to reference the current element, e.g. ["if", ["gt", ["local", "_"], 5], ...]
      "filter" 2 Same as map — use ["local", "_"] for compound predicates, e.g. ["and", ["gte", ["local", "_"], 2], ["lte", ["local", "_"], 5]]
      "key" 2 Pass an object (or step that evaluates to an object) and the key to retrieve from that object
      "agent" 0 No arguments; returns the Agent
      "environment" 0 No arguments, returns the Environment
      "vector" any Creates an n-dimensional {@linkcode Vector} from the supplied arguments
      "log" any Logs expression(s) and returns the last evaluated value
      "random" 0-2 Returns a random number. No args: float 0-1. One arg (max): int 0-max. Two args (min, max): int min-max. Uses seeded PRNG if utils.seed() was called.

      Parameters

      Returns Rule

    Properties

    environment: Environment

    Points to the Environment that is passed in the Rule's constructor function (should be the same Environment of the Agent invoking this Rule)

    trace: boolean = false

    When true, evaluation traces are logged and captured.

    traceLog: string[] = []
    operatorInfo: { [key: string]: OperatorArity } = operatorInfo

    Static operator info mapping operator names to their expected argument counts.

    Methods

    • Returns a formatted S-expression representation of the rule.

      Returns string