Action generators #

In addition to exporting properties in a specification, you export action generators. A generator is an object of the ActionGenerator class. An action generator generates values of type Tree<ActionTemplate>.

Like with default properties, there are default action generators provided by Bombadil. These will get you a long way, but there are times where you’ll need to define your own action generators.

For every state that Bombadil captures, all action generators are run, contributing to a tree structure of possible action templates. These are called action templates and are parameterized by ranges of values. Bombadil then randomly picks one in that tree, and picks random values within the ranges for the parameters. Why a tree, though? It’s because the branches are weighted — equally, by default. But you can override this to control the probability of an action being picked.

To define a custom action generator, you use the actions function, which takes a thunk that returns an array of action templates:

export const myAction = actions(() => {
    return [
        ...
    ];
});

In the returned array, each element is a value of the ActionTemplate type, provided by the NPM package. See the TypeScript source for reference.

Here’s a generator that sends either a help command or an incr command with an numeric argument:

export const sendCommand = actions(() => {
    return [
        { TypeText: { Regexp: "help\n" } },
        { TypeText: { Regexp: "incr \d{2,4}\n" } },
    ];
});

The actions you return must be possible to perform in the current state. Your action generators should therefore depend on cells and validate your actions before returning them.

To give actions different weights, use the weighted combinator and wrap each subgenerator in an array with the weight as the first element:

export const inputs = weighted([
  [10, typeFromSet(CharSets.UNICODE_SAFE)],
  [1, typeFromSet(CharSets.CONTROL_COMMON)],
]);