{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://fa-spec.com/spec/v1/timingchart.schema.json",
  "title": "spec-platform/v1 timingchart（タイミングチャート）",
  "description": "行（rows）が信号、列が Step。波形は values[] の配列で持つ。仕様の正は https://fa-spec.com/json-format の 4 章。",
  "allOf": [{ "$ref": "common.schema.json" }],
  "type": "object",
  "required": ["steps"],
  "properties": {
    "kind": { "const": "timingchart" },
    "docId": { "type": "string", "description": "図の ID。装置ファイルに同じ kind の図が何枚もあるとき、保存でどれを差し替えるかを見分ける。エディタが付ける。省略可。" },
    "steps": { "type": "number", "description": "Step 数（列の数）。" },
    "meta": {
      "type": "object",
      "properties": {
        "title": { "type": "string" },
        "note": { "type": "string" },
        "stepMs": { "type": "number", "description": "1 Step の時間［ms］。0 なら Step 番号だけを表示する。" },
        "master": { "type": "string", "description": "マスターの機器名。方向欄とフローチャートのレーン名になる。" },
        "slave": { "type": "string", "description": "スレーブの機器名。" }
      }
    },
    "rows": {
      "type": "array",
      "description": "信号 1 本ぶん。",
      "items": { "$ref": "#/$defs/row" }
    },
    "triggers": {
      "type": "array",
      "description": "因果関係の矢印。フローチャートへ渡すと、待ちと処理の順序になる。",
      "items": { "$ref": "common.schema.json#/$defs/trigger" }
    },
    "variables": {
      "type": "array",
      "description": "チャートの行を 4 ツール共通の形で並べたもの。id は rows[].id と同じ。rows が無い JSON では、これを変数リストに取り込む。",
      "items": { "$ref": "common.schema.json#/$defs/variable" }
    },
    "library": {
      "type": "array",
      "description": "左の変数リスト（チャートに置く前の候補）。省略可。",
      "items": {
        "type": "object",
        "properties": {
          "id": { "$ref": "common.schema.json#/$defs/id" },
          "name": { "type": "string" },
          "type": { "type": "string" },
          "comment": { "type": "string" },
          "role": { "$ref": "common.schema.json#/$defs/role" },
          "deviceId": { "type": "string", "description": "どの機器（区画）の変数か。メモリマップから受け取ったときに付く。" },
          "device": { "type": "string", "description": "機器の名前（表示用）。" },
          "blockId": { "type": "string" },
          "block": { "type": "string" },
          "address": { "type": "string", "description": "割り付けた先（例 D100）。" },
          "bit": { "type": ["number", "null"], "description": "ワードの中のビット番号。" },
          "addrText": { "type": "string", "description": "表示用のアドレス（ビットを 16 進で書くデバイスの D100.F など）。address.bit と違うときだけ。" },
          "mapId": { "type": "string", "description": "メモリマップの変数 id（variables[].id）。メモリマップから受け取った変数に付く。名前が変わっても同じ変数と分かる。" },
          "unit": { "type": "string", "description": "単位（メモリマップの変数と同じ）。行にすると行の unit になる。" },
          "engMin": { "type": "number", "description": "工学値の下限（メモリマップの変数と同じ）。行にすると行の min になる。" },
          "engMax": { "type": "number", "description": "工学値の上限。行にすると行の max になる。" },
          "rawMin": { "type": "number", "description": "生の値の下限（メモリマップの変数と同じ）。" },
          "rawMax": { "type": "number", "description": "生の値の上限。" },
          "retain": { "type": "boolean", "description": "停電保持の変数なら true（メモリマップのブロックから引き継いだものも入る）。" },
          "safety": { "type": "boolean", "description": "安全の信号なら true（安全の型の変数は書かなくても安全）。行の名前の横に「安全」の印が出る。" }
        }
      }
    },
    "scope": { "$ref": "common.schema.json#/$defs/scope" },
    "edgeMode": { "type": "string", "description": "BOOL の切り替わりの描き方。\"v\"（垂直・既定）/ \"s1\"（斜め・小）/ \"s2\"（斜め・大）。ほかの値は \"v\" として扱う。" },
    "controller": { "type": "object", "description": "このチャートのマスターの PLC（メモリマップの controller と同じ { id, name, machineId }）。メモリマップから受け取るとき、マスターがその PLC なら付く。省略可。", "properties": { "id": { "type": "string" }, "name": { "type": "string" }, "machineId": { "type": "string" } } },
    "showAddr": { "type": "boolean", "description": "true なら信号名の前にアドレス（変数リストの address、BOOL はビットも）を出す。省略すると出さない。" },
    "stepTimes": {
      "type": "array",
      "description": "1 Step の時間（meta.stepMs）と違う時間がかかる Step（時間を省略して描いた Step など）。時刻は各 Step の時間を先頭から足し上げて出す。meta.stepMs が 0 なら時刻を出さない。無ければ全 Step が meta.stepMs。",
      "items": {
        "type": "object",
        "required": ["step", "ms"],
        "properties": {
          "step": { "type": "integer", "minimum": 0, "description": "Step の番号（0 から）。" },
          "ms": { "type": "number", "exclusiveMinimum": 0, "description": "その Step の時間［ms］。0 より大きい。" }
        }
      }
    },
    "caseName": { "type": "string", "description": "この図（もとのケース）の名前（例：OK）。cases があるとき、フローチャートの分かれ道の出口の名前になる。省略すると「通常」。" },
    "cases": {
      "type": "array",
      "description": "分かれ道（OK / NG / タイムアウトなど）。S0〜S{at-1} はこの図と同じで、S{at} から先だけが違うシーケンス。フローチャートを作るとき、S{at} に多分岐が入る。",
      "items": {
        "type": "object",
        "required": ["name", "at", "rows"],
        "properties": {
          "id": { "type": "string", "description": "ケースの識別子（エディタが付ける）。" },
          "name": { "type": "string", "description": "ケースの名前（例：NG）。" },
          "at": { "type": "integer", "minimum": 1, "description": "分かれる Step（steps − 1 以下）。" },
          "rows": {
            "type": "array",
            "description": "行ごとの S{at} から最後の Step までの値（長さ steps − at）。無い行はこの図の値のまま。",
            "items": {
              "type": "object",
              "required": ["id", "values"],
              "properties": {
                "id": { "type": "string", "description": "rows[].id。" },
                "values": { "type": "array", "description": "rows[].values と同じ書き方（BOOL は true / false、ほかは文字か null）。" }
              }
            }
          },
          "triggers": { "type": "array", "description": "このケースの因果関係の矢印のうち、結果（to）の Step が at 以上のもの。形は triggers と同じ。" }
        }
      }
    },
    "bands": {
      "type": "array",
      "description": "区間（Step の範囲に付けた名前）と繰り返し。目盛りの上に帯で描き、フローチャートを作るとき repeat のある区間は、区間の初めへ戻る判断になる。入れ子はよい。",
      "items": {
        "type": "object",
        "required": ["from", "to"],
        "properties": {
          "id": { "type": "string", "description": "区間の識別子（エディタが付ける）。" },
          "from": { "type": "integer", "minimum": 0, "description": "区間の最初の Step。" },
          "to": { "type": "integer", "minimum": 0, "description": "区間の最後の Step（from 以上）。" },
          "name": { "type": "string", "description": "区間の名前（例：検査中、1 サイクル）。" },
          "repeat": {
            "type": "object",
            "description": "あれば、区間の最後まで進んだら区間の初めへ戻る。",
            "properties": {
              "cond": { "type": "string", "description": "戻る条件（例：NG の間、次のワークがある）。" },
              "max": { "type": "integer", "minimum": 1, "description": "最大回数（リトライの回数など）。省略すると決めない。" }
            }
          }
        }
      }
    }
  },
  "$defs": {
    "row": {
      "type": "object",
      "required": ["id", "name", "values"],
      "properties": {
        "id": { "$ref": "common.schema.json#/$defs/id", "description": "この行の識別子。triggers から参照される。" },
        "lib": { "type": "string", "description": "この行の変数（library[].id）。行の名前だけを変えても、アドレスはこの変数のもの。省略すると同じ名前の変数。" },
        "name": { "type": "string", "description": "信号名。" },
        "type": { "type": "string", "description": "BOOL（と SAFEBOOL）なら ON/OFF の波形、それ以外は値を表示するバス。" },
        "role": { "$ref": "common.schema.json#/$defs/role" },
        "peer": { "type": "string", "description": "この行の相手（3 者以上のやり取り）。省略するとスレーブ（meta.slave）。role が M なら master → peer、S なら peer → master。" },
        "comment": { "type": "string", "description": "信号名の下に小さく出る。" },
        "values": {
          "type": "array",
          "description": "長さは steps と同じ。BOOL は true / false、数値・状態は文字列、未設定は null。",
          "items": { "type": ["boolean", "string", "null"] }
        },
        "edge": { "type": "string", "description": "その行だけの切り替わりの描き方（値は edgeMode と同じ）。" },
        "disp": { "type": "string", "description": "\"wave\" なら値のブロックではなく波形でプロットする。" },
        "unit": { "type": "string", "description": "mm・ms・V など。" },
        "min": { "type": "number", "description": "縦の目盛りの下端。" },
        "max": { "type": "number", "description": "縦の目盛りの上端。" }
      }
    }
  }
}
