{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://fa-spec.com/spec/v1/flowchart.schema.json",
  "title": "spec-platform/v1 flowchart（フローチャート）",
  "description": "記号（nodes）と、その間の線（edges）で表す。仕様の正は https://fa-spec.com/json-format の 5 章。",
  "allOf": [{ "$ref": "common.schema.json" }],
  "type": "object",
  "properties": {
    "kind": { "const": "flowchart" },
    "docId": { "type": "string", "description": "図の ID。装置ファイルに同じ kind の図が何枚もあるとき、保存でどれを差し替えるかを見分ける。エディタが付ける。省略可。" },
    "scope": { "$ref": "common.schema.json#/$defs/scope" },
    "partitions": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/partition" } },
    "controller": { "type": "object", "description": "この図がどの PLC の工程か（タイミングチャート・メモリマップの controller と同じ { id, name, machineId }）。タイミングチャートから作ると、チャートの PLC を引き継ぐ。装置の整合チェックで、記号の割付デバイス（device）をその PLC のメモリマップの変数の番地と照らし合わせる。省略可。", "properties": { "id": { "type": "string" }, "name": { "type": "string" }, "machineId": { "type": "string" } } },
    "source": {
      "type": "object",
      "description": "タイミングチャートから作ったときの元の図（「作り直す」でこの図を使う）。エディタが付ける。省略可。",
      "properties": {
        "docId": { "type": "string", "description": "元のタイミングチャートの docId。" },
        "title": { "type": "string" },
        "opt": { "type": "object", "description": "作ったときの設定（書き方・待ちの形など）。" }
      }
    },
    "nodes": {
      "type": "array",
      "description": "記号。",
      "items": {
        "type": "object",
        "properties": {
          "id": { "$ref": "common.schema.json#/$defs/id" },
          "type": {
            "enum": ["start", "end", "process", "decision", "switch", "fork", "join", "io", "wait", "sub", "loopStart", "loopEnd", "connector", "note", "state", "anyState"],
            "description": "switch は多分岐（cases の値ごとに出口）。fork は並列の始まり（branches 本の枝が同時に始まる）、join は並列の終わり（入ってくる枝が全部終わったら 1 本で出る）。state は状態遷移図の状態（events のイベントごとに出口）、anyState は「全状態から」の遷移の出どころ（scope の状態すべてから、events のイベントで移る）。省略すると process として読む。"
          },
          "cases": { "type": "array", "items": { "type": "string" }, "description": "switch の値。i 番目の値の出口は port \"c<i>\"。" },
          "events": { "type": "array", "items": { "type": "string" }, "description": "state・anyState のイベントの名前（例 [\"起動\", \"停止\", \"異常\"]）。i 番目のイベントの出口は port \"c<i>\"。" },
          "entry": { "type": "string", "description": "state に入ったときの処理。" },
          "ignore": { "type": "array", "items": { "type": "string" }, "description": "state で受け付けない（何もしない）イベント（状態 × イベントの表の ／）。" },
          "actions": {
            "type": "array",
            "description": "state で、状態は変えずに処理だけするイベント（表のマスにその文字を出す）。",
            "items": { "type": "object", "required": ["event", "text"], "properties": { "event": { "type": "string", "description": "イベントの名前。" }, "text": { "type": "string", "description": "そのとき行う処理。" } } }
          },
          "never": { "type": "array", "items": { "type": "string" }, "description": "state では起こりえないイベント（表の ×）。" },
          "scope": { "type": "array", "items": { "type": "string" }, "description": "anyState の対象の状態（state の id）。省略すると図の全部の状態。空の配列は対象なし（対象の状態を消したとき）。" },
          "branches": { "type": "integer", "minimum": 2, "maximum": 8, "description": "fork の枝の数（既定 2）。i 番目の枝の出口は port \"c<i>\"。" },
          "text": { "type": "string", "description": "記号の中の文字。改行は \\n で書く。" },
          "timeout": { "type": "number", "minimum": 0, "description": "wait と decision（自分に戻る待ち）の時間の上限［ms］。書くと、時間切れの出口 port \"timeout\" ができる。" },
          "x": { "type": "number", "description": "省略可（自動整列で並べられる）。" },
          "y": { "type": "number", "description": "省略可（自動整列で並べられる）。" },
          "lane": { "type": "string", "description": "lanes[].id。" },
          "gen": { "type": "string", "description": "タイミングチャートから自動で作った記号の鍵（エディタが付ける）。作り直すとき、同じ鍵の記号を差し替え、鍵の無い記号（手で足したもの）は残す。" },
          "gh": { "type": "string", "description": "自動で作った時点の中身の指紋（エディタが付ける）。今の中身と違えば手で直した記号として、作り直しても直した文字を残す。" },
          "step": { "type": "string", "description": "固定の工程番号（例 \"S10\"・\"10\"・\"P100\"）。1 つでも書いた図では自動の番号（settings.stepNo）の代わりにこれを出し、図を直しても変わらない。「工程番号を振り直す」でだけ書き換わる。" },
          "device": { "type": "string", "description": "この工程に割り付けるデバイス（例 \"M110\"。ステップリレー）。" },
          "ref": {
            "type": "object",
            "description": "定義済み処理（type が sub）と状態（type が state。その状態で動かす工程のフロー）の参照先の図。同じ装置のフローチャートの docId と、そのときの題名。",
            "properties": {
              "docId": { "type": "string", "description": "参照先のフローチャートの docId。" },
              "title": { "type": "string", "description": "参照先の題名（表示用。正は docId）。" }
            }
          },
          "refs": {
            "type": "array",
            "description": "この記号が扱う信号。元のタイミングチャート（source.docId）の行を指す。記号の文字は今のまま（参照は照らし合わせに使う）。省略可。",
            "items": {
              "type": "object",
              "required": ["name"],
              "properties": {
                "varId": { "type": "string", "description": "元のタイミングチャートの rows[].id（variables[].id と同じ）。チャートで名前を変えても変わらない。" },
                "name": { "type": "string", "description": "参照を付けたときの信号名。記号の文字に入っていることを確かめる。" }
              }
            }
          }
        }
      }
    },
    "edges": {
      "type": "array",
      "description": "記号の間の線。判断の no を自分自身に戻すと「成立するまで待つ」ループになる。",
      "items": {
        "type": "object",
        "properties": {
          "id": { "$ref": "common.schema.json#/$defs/id" },
          "from": { "type": "string", "description": "nodes[].id。" },
          "port": { "anyOf": [{ "enum": ["out", "yes", "no", "timeout"] }, { "type": "string", "pattern": "^c[0-9]+$" }], "description": "\"out\"（通常の出口）/ \"yes\" / \"no\"（判断の分岐）/ \"c0\"・\"c1\"…（switch の値ごと、fork の枝ごと）/ \"timeout\"（timeout を書いた待ちの時間切れ）。state・anyState ではイベントごとの \"c0\"・\"c1\"…。1 つの出口から出せる線は 1 本。省略すると、出口が 1 つの記号（処理など）では \"out\"。" },
          "to": { "type": "string", "description": "nodes[].id。" },
          "label": { "type": "string", "description": "線の文字。省略すると出口の文字（判断は YES / NO、switch は値）。state・anyState から出る線は使わない（名前はイベント）。" },
          "waypoints": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/point" }, "description": "その点を通る経路で描かれる。ふつうは省略する。" }
        }
      }
    },
    "lanes": {
      "type": "array",
      "description": "担当の列。",
      "items": {
        "type": "object",
        "properties": {
          "id": { "$ref": "common.schema.json#/$defs/id" },
          "role": { "$ref": "common.schema.json#/$defs/role" },
          "name": { "type": "string" }
        }
      }
    },
    "links": {
      "type": "array",
      "description": "レーンをまたぐ信号の受け渡し（点線の矢印）。",
      "items": {
        "type": "object",
        "properties": {
          "id": { "$ref": "common.schema.json#/$defs/id" },
          "from": { "type": "string", "description": "nodes[].id。" },
          "to": { "type": "string", "description": "nodes[].id。" },
          "label": { "type": "string" }
        }
      }
    },
    "settings": {
      "type": "object",
      "properties": {
        "yesNo": { "type": "string", "description": "分岐の文字（例 \"YES/NO\"）。" },
        "yesDir": { "enum": ["down", "right"], "description": "YES の出る向き。" },
        "stepNo": { "type": "boolean" },
        "stepStart": { "type": "number" },
        "stepInc": { "type": "number" },
        "stepPerLane": { "type": "boolean", "description": "工程番号をレーン（担当）ごとに振ったなら true。番号の重なりはレーンの中で確かめる。" }
      }
    }
  }
}
