{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Logiop State Machine Definition",
  "description": "The standardized export format for State Machines in Logiop Studio.",
  "type": "object",
  "required": [
    "Id",
    "Name",
    "Vertices"
  ],
  "properties": {
    "Id": {
      "description": "The unique identifier for this State Machine definition.",
      "$ref": "#/definitions/guid"
    },
    "Name": {
      "type": "string",
      "description": "The display name of the State Machine."
    },
    "SchemaVersion": {
      "type": "string",
      "default": "1.0.0",
      "description": "The version of the Logiop schema used to export this file."
    },
    "BehaviorRegistry": {
      "type": "array",
      "description": "A global lookup table of all unique behaviors (Guards, Actions, DoActivity Actions and Events) referenced by the machine.",
      "items": {
        "$ref": "#/definitions/behavior"
      }
    },
    "Vertices": {
      "type": "array",
      "description": "The topmost Vertices of the machine.",
      "items": {
        "$ref": "#/definitions/vertex"
      }
    },
    "Submachines": {
      "type": "array",
      "description": "A collection of reusable State Machine definitions that can be referenced as Submachine States.",
      "items": {
        "$ref": "#/definitions/submachine"
      }
    },
    "Notes": {
      "type": [ "string", "null" ],
      "description": "Internal developer documentation regarding this State Machine's purpose."
    }
  },
  "definitions": {
    "guid": {
      "type": "string",
      "description": "A standard UUID/GUID string format.",
      "format": "uuid",
      "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    },
    "behavior": {
      "type": "object",
      "description": "Defines a reusable logic block that can be referenced by the behaviors of States and Transitions.",
      "required": [
        "Id",
        "Name",
        "BehaviorType"
      ],
      "properties": {
        "Id": {
          "description": "Unique identifier for this behavior.",
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": "string",
          "description": "The name of this behavior which is unique within the State Machine."
        },
        "BehaviorType": {
          "enum": [
            "Event",
            "Action",
            "Guard",
            "DoActivityAction"
          ]
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this Behavior's purpose."
        }
      }
    },
    "elementBehavior": {
      "type": "object",
      "description": "An instance-specific reference to a global behavior with execution metadata.",
      "required": [
        "BehaviorId",
        "ExecutionOrder"
      ],
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "BehaviorId": {
          "description": "The ID of the behavior to execute from the BehaviorRegistry.",
          "$ref": "#/definitions/guid"
        },
        "ExecutionOrder": {
          "type": "integer",
          "default": 0,
          "description": "The sequence in which this behavior runs relative to others in the same collection."
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this behavior instance."
        }
      }
    },
    "internalTransition": {
      "type": "object",
      "description": "A Transition contained within a State that executes logic upon an Event without causing a State exit/entry change.",
      "required": [
        "Id",
        "IsDeferred"
      ],
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": [ "string", "null" ],
          "description": "The display name of the internal transition."
        },
        "IsDeferred": {
          "type": "boolean",
          "description": "If true, indicates that matching events are deferred rather than executing actions/transitions."
        },
        "Events": {
          "type": "array",
          "description": "Triggers that match this internal transition or deferred event.",
          "items": {
            "$ref": "#/definitions/elementBehavior"
          }
        },
        "Guards": {
          "type": "array",
          "description": "Conditions that must evaluate to True for the internal transition to fire.",
          "items": {
            "$ref": "#/definitions/elementBehavior"
          }
        },
        "Effect": {
          "type": "array",
          "description": "Actions executed when this internal transition fires.",
          "items": {
            "$ref": "#/definitions/elementBehavior"
          }
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this internal transition."
        }
      },
      "if": {
        "properties": {
          "IsDeferred": {
            "const": true
          }
        },
        "required": [ "IsDeferred" ]
      },
      "then": {
        "properties": {
          "Guards": false,
          "Effect": false
        },
        "not": {
          "anyOf": [
            { "required": [ "Guards" ] },
            { "required": [ "Effect" ] }
          ]
        }
      }
    },
    "connectionPointReference": {
      "type": [ "object", "null" ],
      "description": "References an entry or exit point on an underlying submachine definition.",
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "SubmachineId": {
          "$ref": "#/definitions/guid"
        }
      }
    },
    "connectionPoint": {
      "type": "object",
      "description": "An entry or exit connection point attached to a Submachine State.",
      "required": [
        "Id",
        "Type"
      ],
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": [ "string", "null" ]
        },
        "Type": {
          "enum": [
            "EntryPoint",
            "ExitPoint"
          ]
        },
        "ReferencedConnectionPoint": {
          "$ref": "#/definitions/connectionPointReference"
        },
        "OutgoingTransitions": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/transition"
          }
        },
        "Notes": {
          "type": [ "string", "null" ]
        }
      }
    },
    "region": {
      "type": "object",
      "description": "A container for hierarchical sub-states within a parent State.",
      "required": [
        "Id",
        "Priority"
      ],
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": [ "string", "null" ],
          "description": "The display name of the region."
        },
        "Priority": {
          "type": "integer",
          "minimum": 0,
          "description": "0-indexed evaluation and execution order of this Region within its parent State."
        },
        "Children": {
          "type": "array",
          "description": "The Vertices contained directly within this Region.",
          "items": {
            "$ref": "#/definitions/vertex"
          }
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this Region's purpose."
        }
      }
    },
    "transition": {
      "type": "object",
      "description": "A directed relationship between a source Vertex and a target Vertex.",
      "required": [
        "Id",
        "TargetId"
      ],
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": [ "string", "null" ]
        },
        "TargetId": {
          "description": "The ID of the Vertex where this Transition ends.",
          "$ref": "#/definitions/guid"
        },
        "Events": {
          "type": "array",
          "description": "The triggers that cause this Transition to fire.",
          "items": {
            "$ref": "#/definitions/elementBehavior"
          }
        },
        "Guards": {
          "type": "array",
          "description": "Conditions that must evaluate to True for the Transition to be taken.",
          "items": {
            "$ref": "#/definitions/elementBehavior"
          }
        },
        "Effect": {
          "type": "array",
          "description": "Actions executed during the Transition process.",
          "items": {
            "$ref": "#/definitions/elementBehavior"
          }
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this Transition's purpose."
        }
      }
    },
    "vertex": {
      "type": "object",
      "required": [
        "Id",
        "Name",
        "Type"
      ],
      "properties": {
        "Id": {
          "description": "Unique identifier for this vertex.",
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": "string",
          "description": "The name of this Vertex which is unique within the State Machine."
        },
        "Type": {
          "description": "The UML-compliant type of the Vertex which dictates its logic.",
          "enum": [
            "Choice",
            "DeepHistory",
            "EntryPoint",
            "ExitPoint",
            "Final",
            "Fork",
            "Initial",
            "Join",
            "Junction",
            "ShallowHistory",
            "State",
            "SubmachineMainState",
            "SubmachineState",
            "Terminate"
          ]
        },
        "OutgoingTransitions": {
          "type": "array",
          "description": "A list of paths leading from this Vertex to others.",
          "items": {
            "$ref": "#/definitions/transition"
          }
        },
        "SubmachineId": {
          "$ref": "#/definitions/guid"
        },
        "ConnectionPoints": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/connectionPoint"
          }
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this Vertex's purpose."
        }
      },
      "if": {
        "properties": {
          "Type": {
            "enum": [
              "State",
              "SubmachineMainState"
            ]
          }
        },
        "required": [ "Type" ]
      },
      "then": {
        "properties": {
          "Entry": {
            "type": "array",
            "items": { "$ref": "#/definitions/elementBehavior" }
          },
          "DoActivity": {
            "type": "array",
            "items": { "$ref": "#/definitions/elementBehavior" }
          },
          "Exit": {
            "type": "array",
            "items": { "$ref": "#/definitions/elementBehavior" }
          },
          "InternalTransitions": {
            "type": "array",
            "items": { "$ref": "#/definitions/internalTransition" }
          },
          "Regions": {
            "type": "array",
            "items": { "$ref": "#/definitions/region" }
          }
        }
      },
      "else": {
        "not": {
          "anyOf": [
            { "required": [ "Entry" ] },
            { "required": [ "DoActivity" ] },
            { "required": [ "Exit" ] },
            { "required": [ "InternalTransitions" ] },
            { "required": [ "Regions" ] }
          ]
        }
      }
    },
    "submachine": {
      "type": "object",
      "description": "A partial State Machine definition used as a component within a larger system.",
      "required": [
        "Id",
        "Name",
        "Vertices"
      ],
      "properties": {
        "Id": {
          "$ref": "#/definitions/guid"
        },
        "Name": {
          "type": "string",
          "description": "The name of the Submachine."
        },
        "Vertices": {
          "type": "array",
          "description": "The Vertices defining the internal structure of the Submachine.",
          "items": {
            "$ref": "#/definitions/vertex"
          }
        },
        "Notes": {
          "type": [ "string", "null" ],
          "description": "Internal developer documentation regarding this Submachine's purpose."
        }
      }
    }
  }
}