FSA Description

This page describes the JSON format used to describe Finite State Automata.

Automata

The FSA is represented by a JSON object, with the following attributes:

fsa.states

A JSON object (required).

Its attributes are state names, and its values are state objects. It must contain one state named start, which will be the starting state.

fsa.allow_overlap

A boolean (optional).

If false (default value), the automaton will drop all pending matches whenever a match is found. If true, the automaton may yield overlapping matches, possibly involving the same events. See Overlapping matches for more details.

fsa.state_defaults

A state object (optional).

It provides default values for all states of this automaton.

NB: it can not have transitions.

fsa.default_matcher

A string (optional).

If provided, it indicates the function used to compare incoming events to the condition. It must be a key present in fsa4streams.matcher.DIRECTORY.

If not provided, a simple matcher will be used, checking whether the event is equal to the transition.condition.

State

Every state is represented by a JSON object, with the following attributes:

state.terminal

A boolean (optional).

If false (default value), this state can not yield any match. If true, this is a terminal state so it may yield matches (but may not do, if the automaton manages to progress to a further terminal state).

state.transitions

A list of transition objects (optional).

Note that a state without any transitions must be terminal:attr`.

state.default_transition

A transition object with some restrictions (optional).

It provides a transition to follow when an unrecognized event is encountered. This special transition can not have any condition or matcher.

This attribute can not be set together with max_noise.

state.max_noise

A non-negative integer (optional).

It indicates how many unrecognized events can occur on this state before the automaton fails (in the limit imposed by max_total_noise, if any). It defaults to 0, so by default states are not noise-tolerant.

This attribute can not be set together with default_transition.

state.max_total_noise

A non-negative integer (optional).

This attribute is similar to max_noise above, but instead of considering only the unrecognized events occuring on the this state, it also considers all unrecognized events encountered from the start state. If it is unspecified, the state will not be affected by previous noise.

This attribute is particularly useful on terminal states, to prevent a match when it was too noisy overall. However, if it is the same for all the terminal states of the automaton, it is a good idea to set it in the fsa.state_defaults, so that the engine can prune searches as soon as a candidate match is too noisy (rather than wait for it to reach the terminal state to reject it).

state.max_duration

A positive integer (optional).

It indicates the maximum in which the FSA will stay in this state. If no matching event occurs during this duration, the FSA will fail to match. It defaults to infinity: if no max duration is specified, the FSA may stay an arbitrary long time in that state (in the limit imposed by fsa.max_duration).

fsa.max_total_duration

A positive integer (optional).

This attribute is similar to max_duration above, but instead of considering the duration since the automaton entered this state, it considers the total duration since the automaton left the start state. If it is unspecified, the automaton will accept any duration.

This attribute is particularly useful on terminal states, to prevent a match when it was too long overall. However, if it is the same for all the terminal states of the automaton, it is a good idea to set it in the fsa.state_defaults, so that the engine can prune searches as soon as a candidate match is too long (rather than wait for it to reach the terminal state to reject it).

Transition

Every transition is represented by a JSON object, with the following attributes:

transition.condition

Any JSON object (required).

The condition is compared to incoming events to check if the transition can be used. See also transition.matcher.

transition.target

A string (required).

This is the identifier (in fsa.states) of the target state of this transition.

transition.matcher

A string (optional).

If provided, it indicates the function used to compare incoming events to the condition. It must be a key present in fsa4streams.matcher.DIRECTORY.

If not provided, the default_matcher will be used.

transition.silent

A boolean (optional).

If it is set to false (default value), the event matching this transition will be included in the match. Otherwise, the transition will be used but the event will be simply discarded.

Example

{
    "allow_overlap": true,
    "states": {
        "start": {
            "max_noise": 0,
            "transitions": [
                {
                    "condition": "a",
                    "target": "start"
                },
                {
                    "condition": "a",
                    "target": "s1"
                },
                {
                    "condition": "d",
                    "target": "s2"
                }
            ]
        },
        "s1": {
            "transitions": [
                {
                    "condition": "b",
                    "target": "s1"
                },
                {
                    "condition": "c",
                    "target": "success"
                },
                {
                    "condition": "d",
                    "target": "error"
                }
            ]
        },
        "s2": {
            "max_noise": 4,
            "transitions": [
                {
                    "condition": "d",
                    "target": "success"
                }
            ]
        },
        "success": {
            "terminal": true
        },
        "error": {
            "terminal": true
        }
    }
}
digraph examples_structure {
rankdir=LR
node[shape=circle]
start -> start [label=a]
start -> s1 [label=a]
start -> s2 [label=d]
s1 -> s1 [label=b]
s1 -> success [label=c]
s1 -> error [label=d]
s2 -> success [label=d]
s2 [label="s2 [4]"]
success [shape=doublecircle]
error [shape=doublecircle]
}

Todo

May be also describe the format of the JSON files used to save tokens?...