{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://fa-spec.com/spec/v1/addressmap.v3.schema.json",
  "title": "spec-platform/v1 addressmap v3（メモリマップ）",
  "description": "v2（addressmap.schema.json）との違い：ブロックは平らな表 blocks[]（parentId で任意の深さの入れ子）、ブロックの範囲はデバイスごとに 0 個以上の ranges[]（番地の文字列）、変数の所属は variables[].blockId（ブロックの varIds は無い）、見た目は layout。エディタは v3 で書き出し、v1 / v2 も読む。仕様の正は https://fa-spec.com/json-format の 3 章。",
  "allOf": [{ "$ref": "common.schema.json" }],
  "type": "object",
  "properties": {
    "kind": { "const": "addressmap" },
    "v": { "const": 3 },
    "plc": { "$ref": "#/$defs/plc" },
    "controller": { "type": "object", "description": "このメモリマップの PLC（構成図の PLC の { id, name }）。メモリは PLC ごとにあるので、1 ファイルは PLC 1 台ぶん。省略可。", "properties": { "id": { "type": "string" }, "name": { "type": "string" }, "machineId": { "type": "string", "description": "構成図の装置 ID。同じ id の PLC でも、別の装置なら別のメモリマップ。" } } },
    "settings": {
      "type": "object",
      "description": "コード生成の設定。",
      "properties": {
        "structName": { "type": "string" },
        "iecWordIndex": {}
      }
    },
    "naming": {
      "type": "object",
      "description": "命名規則（決めた項目だけ）。変数の名前が合わなければ確認事項に出す。省略すると検査しない。",
      "properties": {
        "typePrefix": { "type": "object", "additionalProperties": { "type": "string" }, "description": "型ごとの接頭辞（例 { \"BOOL\": \"b\", \"INT\": \"i\" }）。大文字小文字を区別して、名前がこの文字で始まるかを見る。BIT・BOOLEAN は BOOL として引く。" },
        "maxLen": { "type": "integer", "minimum": 1, "description": "名前の長さの上限（文字数）。" },
        "forbidden": { "type": "array", "items": { "type": "string" }, "description": "使わない語。名前に含まれていれば違反（大文字小文字は区別しない）。" },
        "pattern": { "type": "string", "description": "名前の全体が合う正規表現（JavaScript の書き方。/ で囲まない）。" },
        "note": { "type": "string", "description": "規則の説明（仕様書に出す）。" }
      }
    },
    "devices": {
      "type": "array",
      "description": "機器（区画の最上位）。ブロックは持たない（blocks[] の deviceId で結ぶ）。",
      "items": {
        "type": "object",
        "required": ["id"],
        "properties": {
          "id": { "$ref": "common.schema.json#/$defs/id" },
          "name": { "type": "string" },
          "comment": { "type": "string" },
          "addr": { "type": "string", "description": "IP アドレスや局番。構成図から受け取ると、構成図の機器の番地（無ければ線の端の番地）が入る。" }
        }
      }
    },
    "blocks": {
      "type": "array",
      "description": "ブロックの表。親を先に並べる。parentId が null（または無し）なら機器の最上位。",
      "items": { "$ref": "#/$defs/block" }
    },
    "variables": {
      "type": "array",
      "description": "変数の一覧。blockId が所属の正（いちばん内側のブロック）。device / block は表示用の名前（block は「親／子」）。",
      "items": { "$ref": "common.schema.json#/$defs/variable" }
    },
    "triggers": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/trigger" } },
    "layout": {
      "type": "object",
      "description": "見た目だけ（無くても図になる）。",
      "properties": {
        "collapsed": { "type": "array", "items": { "type": "string" }, "description": "たたんだ機器・ブロックの id。" }
      }
    }
  },
  "$defs": {
    "block": {
      "type": "object",
      "required": ["id", "name"],
      "properties": {
        "id": { "$ref": "common.schema.json#/$defs/id" },
        "deviceId": { "type": "string", "description": "どの機器のブロックか（devices[].id）。入れ子なら親と同じ機器。" },
        "parentId": { "type": ["string", "null"], "description": "親のブロックの id。null なら最上位。深さの上限は無い。" },
        "name": { "type": "string", "description": "ブロックの名前。階層の名前は持たず、表示は「親／子」のパス。" },
        "comment": { "type": "string", "description": "用途。" },
        "owner": { "type": "string", "description": "書き込む区画（機器かブロックの id）。無ければ親から継承する。" },
        "addr": { "type": "string", "description": "局番や IP アドレス（リモート I/O・ユニットのブロックなど）。" },
        "retain": { "type": "boolean", "description": "中の変数をすべて停電保持にするなら true。入れ子の子のブロックと変数が引き継ぐ。" },
        "safety": { "type": "boolean", "description": "中の変数をすべて安全の信号にするなら true。入れ子の子のブロックと変数が引き継ぐ。" },
        "ranges": { "type": "array", "items": { "$ref": "#/$defs/range" }, "description": "確保した範囲。デバイスごとに 0 個以上。" }
      }
    },
    "range": {
      "type": "object",
      "required": ["dev", "from"],
      "properties": {
        "dev": { "type": "string", "description": "デバイスの記号（plc.devices[].code。例 \"X\"）。" },
        "from": { "type": "string", "description": "先頭の番地（例 \"X100\"）。" },
        "to": { "type": "string", "description": "末尾の番地（例 \"X11F\"）。省略すると from と同じ。" },
        "type": { "type": "string", "description": "行を足したときの型（例 \"BOOL\" / \"INT\"）。" },
        "link": {
          "type": "object",
          "required": ["from"],
          "description": "リンク側の範囲（この範囲をリフレッシュ先とするリンクデバイス。例 { \"from\": \"RX0\", \"to\": \"RX1F\" }）。RX / RY / RWr / RWw / LB / LW / LX / LY の 16 進の書き方なら、点数（ビット）がリフレッシュ先と合うか・ほかの範囲のリンク側と重ならないかをエディタが確かめる。ほかの書き方は比べない。ネットワークが 2 つ以上あるときは J1\\RX0・J2\\RX0 のように J 番号を付けて書き分ける（J 番号が違う範囲は比べない）。",
          "properties": {
            "from": { "type": "string", "description": "リンク側の先頭（例 \"RX0\"・\"RWr0\"・\"LB100\"）。" },
            "to": { "type": "string", "description": "リンク側の末尾（例 \"RX1F\"）。省略すると from と同じ。" }
          }
        }
      }
    },
    "plc": {
      "type": "object",
      "description": "メーカーと機種、使えるデバイス範囲（v2 と同じ）。",
      "properties": {
        "vendor": { "type": "string" },
        "model": { "type": "string" },
        "bitDev": { "type": "string" },
        "wordDev": { "type": "string" },
        "devices": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "code": { "type": "string" },
              "name": { "type": "string" },
              "style": { "enum": ["bit", "word", "chbit", "byte", "iec"] },
              "bits": { "type": "number" },
              "radix": { "enum": [8, 10, 16], "description": "番号の進数。8 は三菱 iQ-F の X / Y。" },
              "notation": { "enum": ["s7"], "description": "iec の書き方。s7 は Siemens（%M0.0、%MW10 の 10 はバイト番号）。無ければ IEC（%MX0.0）。" },
              "start": { "type": "number" },
              "end": { "type": "number" },
              "latchStart": { "type": "number", "description": "停電保持（ラッチ）の範囲の先頭の番号。無ければ保持の範囲を持たない。" },
              "latchEnd": { "type": "number", "description": "停電保持（ラッチ）の範囲の最後の番号。" }
            }
          }
        }
      }
    }
  }
}
