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. |
Points to the Environment that is passed in the Rule's constructor function (should be the same Environment of the Agent invoking this Rule)
When true, evaluation traces are logged and captured.
StaticoperatorStatic operator info mapping operator names to their expected argument counts.
Pretty-print the rule's step tree as S-expressions.
Optionaloptions: RuleFormatOptionsReturns a formatted S-expression representation of the rule.
Validate the rule's step tree and return an array of diagnostics. Does not throw — returns diagnostics for inspection.
StaticformatFormat arbitrary steps as pretty-printed S-expressions.
Optionaloptions: RuleFormatOptions
The
Ruleclass is an experimental interface for adding behavior toAgents. ARuleobject may be used in place of atickfunction to be added asAgentbehavior usingagent.set('tick', tickRule). As a trivial example, consider the followingRule, which increments theAgent'sxvalue with every time step:Reading from the outer arrays inward, the steps of this
Ruleinstructs theAgentto:settheAgent's"x"value to...adding1and...Agent's current"x"valueGenerally,
Rulesteps 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 theconstructorfunction for more information about steps.Since
0.3.0