{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://catalog.lintel.tools/schemas/schemastore/bpod-finite-state-machine/latest.json",
  "title": "State Machine",
  "description": "Definition of a Bpod finite-state machine.",
  "x-lintel": {
    "source": "https://raw.githubusercontent.com/int-brain-lab/bpod-core/main/.schema/statemachine.json",
    "sourceSha256": "700c0ba4a91037f1c97fdcc0ea9471896189cf4d8099fcf46b5f01a9bc085698",
    "fileMatch": [
      "*.bpod-fsm.json",
      "*.bpod-fsm.yaml",
      "*.bpod-fsm.yml",
      "bpod-fsm.json",
      "bpod-fsm.yaml",
      "bpod-fsm.yml"
    ],
    "parsers": [
      "json",
      "yaml"
    ]
  },
  "type": "object",
  "properties": {
    "name": {
      "default": "State Machine",
      "description": "The name of the state machine",
      "minLength": 1,
      "title": "State Machine Name",
      "type": "string"
    },
    "states": {
      "$ref": "#/$defs/States",
      "default": {}
    },
    "global_timers": {
      "$ref": "#/$defs/GlobalTimers",
      "default": {}
    },
    "global_counters": {
      "$ref": "#/$defs/GlobalCounters",
      "default": {}
    },
    "conditions": {
      "$ref": "#/$defs/Conditions",
      "default": {}
    }
  },
  "$defs": {
    "Actions": {
      "type": "object",
      "description": "A collection of actions.",
      "propertyNames": {
        "description": "The name of the output action",
        "minLength": 1
      },
      "title": "Actions",
      "additionalProperties": {
        "description": "The integer value of the output action",
        "maximum": 255,
        "minimum": 0,
        "type": "integer"
      }
    },
    "Condition": {
      "description": "A condition in the state machine.",
      "properties": {
        "channel": {
          "description": "The channel or global timer attached to the condition",
          "minLength": 1,
          "title": "Channel",
          "type": "string"
        },
        "value": {
          "description": "The value of the condition channel if the condition is met",
          "title": "Value",
          "type": "boolean"
        }
      },
      "required": [
        "channel",
        "value"
      ],
      "title": "Condition",
      "type": "object"
    },
    "Conditions": {
      "description": "A collection of conditions.",
      "type": "object",
      "title": "Conditions",
      "patternProperties": {
        "^\\d+$": {
          "$ref": "#/$defs/Condition"
        }
      }
    },
    "GlobalCounter": {
      "description": "A global counter in the state machine.",
      "properties": {
        "event": {
          "description": "A state machine event",
          "minLength": 1,
          "title": "Event",
          "type": "string"
        },
        "threshold": {
          "description": "The count threshold to generate an event",
          "maximum": 4294967295,
          "minimum": 0,
          "title": "Threshold",
          "type": "integer"
        }
      },
      "required": [
        "event",
        "threshold"
      ],
      "title": "Global Counter",
      "type": "object"
    },
    "GlobalCounters": {
      "description": "A collection of global counters.",
      "type": "object",
      "title": "Global Counters",
      "patternProperties": {
        "^\\d+$": {
          "$ref": "#/$defs/GlobalCounter"
        }
      }
    },
    "GlobalTimer": {
      "description": "A global timer in the state machine.",
      "properties": {
        "duration": {
          "description": "The duration of the global timer in seconds",
          "minimum": 0.0,
          "title": "Global Timer Duration",
          "type": "number"
        },
        "onset_delay": {
          "default": 0.0,
          "description": "The onset delay of the global timer in seconds",
          "minimum": 0.0,
          "title": "Onset Delay",
          "type": "number"
        },
        "channel": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "default": null,
          "title": "Channel"
        },
        "value_on": {
          "default": 0,
          "description": "The value a channel is set to",
          "maximum": 255,
          "minimum": 0,
          "title": "Channel Value",
          "type": "integer"
        },
        "value_off": {
          "default": 0,
          "description": "The value a channel is set to",
          "maximum": 255,
          "minimum": 0,
          "title": "Channel Value",
          "type": "integer"
        },
        "send_events": {
          "default": true,
          "description": "Whether the global timer is sending events",
          "title": "Send Events",
          "type": "boolean"
        },
        "loop": {
          "default": 0,
          "description": "Whether the global timer is looping or not",
          "maximum": 255,
          "minimum": 0,
          "title": "Loop Mode",
          "type": "integer"
        },
        "loop_interval": {
          "default": 0.0,
          "description": "The interval in seconds that the global timer is looping",
          "minimum": 0.0,
          "title": "Loop Interval",
          "type": "number"
        },
        "onset_trigger": {
          "default": 0,
          "description": "An integer whose bits indicate other global timers to trigger",
          "minimum": 0,
          "title": "Onset Trigger",
          "type": "integer"
        }
      },
      "required": [
        "duration"
      ],
      "title": "Global Timer",
      "type": "object"
    },
    "GlobalTimers": {
      "description": "A collection of global timers.",
      "type": "object",
      "title": "Global Timers",
      "patternProperties": {
        "^\\d+$": {
          "$ref": "#/$defs/GlobalTimer"
        }
      }
    },
    "State": {
      "description": "A state in the state machine.",
      "properties": {
        "timer": {
          "default": 0.0,
          "description": "The state's timer in seconds",
          "minimum": 0.0,
          "title": "State Timer",
          "type": "number"
        },
        "transitions": {
          "$ref": "#/$defs/Transitions",
          "default": {}
        },
        "actions": {
          "$ref": "#/$defs/Actions",
          "default": {}
        },
        "comment": {
          "anyOf": [
            {
              "description": "A comment describing the state.",
              "title": "Comment",
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "default": null,
          "title": "Comment"
        }
      },
      "title": "State",
      "type": "object"
    },
    "States": {
      "description": "A collection of states.",
      "type": "object",
      "propertyNames": {
        "description": "The name of the state",
        "minLength": 1
      },
      "title": "States",
      "patternProperties": {
        "^(?!>)(?!exit$)(?!back$).+$": {
          "$ref": "#/$defs/State"
        }
      }
    },
    "Transitions": {
      "type": "object",
      "description": "A collection of state transitions.",
      "propertyNames": {
        "description": "A state machine event",
        "minLength": 1
      },
      "title": "State Transitions",
      "additionalProperties": {
        "anyOf": [
          {
            "description": "The name of the state",
            "minLength": 1,
            "pattern": "^(?!>)(?!exit$)(?!back$).+$",
            "title": "State Name",
            "type": "string"
          },
          {
            "description": "A state machine operator",
            "examples": [
              ">exit",
              ">back"
            ],
            "minLength": 2,
            "pattern": "^>.+$",
            "title": "State Machine Operator",
            "type": "string"
          }
        ]
      }
    }
  }
}
