{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openssom.org/schema/episode.json",
  "title": "SSOM Episode",
  "description": "DRAFT (RFC-0004). A bounded stretch of the life of one Asset that names the readings, alarms, events, failures, work and outcome that belong together, so that they can be studied as one case. An Episode holds references and no copy of a record. It states how it was bounded, whether its membership is complete, and whether its outcome is known.",
  "type": "object",
  "required": [
    "episode_id",
    "ssom_version",
    "subject_ref",
    "episode_type",
    "start_time",
    "end_time",
    "boundary",
    "members",
    "membership_state",
    "outcome_state",
    "assembly",
    "provenance"
  ],
  "properties": {
    "episode_id": {
      "type": "string",
      "pattern": "^urn:ssom:episode:"
    },
    "ssom_version": {
      "type": "string"
    },
    "subject_ref": {
      "type": "string",
      "description": "The Asset whose life the stretch belongs to."
    },
    "related_subject_refs": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "string"
      },
      "description": "Other Assets whose records may be members, for example the driven machine of a drive train. Each is joined to the subject by a Relationship."
    },
    "episode_type": {
      "type": "string",
      "description": "A governed code for the kind of stretch, for example a failure and its repair, a start-up, or a run between two overhauls."
    },
    "start_time": {
      "type": "string",
      "format": "date-time"
    },
    "end_time": {
      "type": "string",
      "format": "date-time"
    },
    "boundary": {
      "type": "object",
      "required": [
        "rule"
      ],
      "properties": {
        "rule": {
          "type": "string",
          "description": "A governed code for the rule that set the two ends."
        },
        "start_ref": {
          "type": "string",
          "description": "The member record that marks the start, where one does."
        },
        "end_ref": {
          "type": "string",
          "description": "The member record, or the outcome, that marks the end, where one does."
        }
      },
      "additionalProperties": true
    },
    "members": {
      "type": "object",
      "minProperties": 1,
      "properties": {
        "signal_refs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          },
          "description": "Signals whose readings between the two ends belong to the episode. The readings are not listed one by one."
        },
        "observation_refs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          },
          "description": "Single readings named on their own, for example the reading that raised an alarm."
        },
        "alarm_refs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        },
        "event_refs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        },
        "failure_event_refs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        },
        "action_refs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": false
    },
    "membership_state": {
      "enum": [
        "complete",
        "partial",
        "not_assessed"
      ],
      "description": "`complete`: every Alarm, Failure Event and Action known for the subject and the related subjects between the two ends is a member, so the absence of a record means that none was recorded. `partial`: some are known to be left out. `not_assessed`: not stated."
    },
    "outcome_state": {
      "enum": [
        "known",
        "not_yet_known",
        "not_recorded"
      ],
      "description": "`known`: an Outcome is named. `not_yet_known`: the stretch is closed and its result has not been assessed. `not_recorded`: no Outcome will be supplied."
    },
    "outcome_ref": {
      "type": "string"
    },
    "assembly": {
      "type": "object",
      "required": [
        "method",
        "assembled_at"
      ],
      "properties": {
        "method": {
          "enum": [
            "rule",
            "person"
          ],
          "description": "Whether a stated rule or a named person or team put the episode together."
        },
        "rule_ref": {
          "type": "string"
        },
        "assembled_by": {
          "$ref": "common.json#/$defs/responsibleParty"
        },
        "assembled_at": {
          "type": "string",
          "format": "date-time"
        }
      },
      "additionalProperties": true
    },
    "state_lineage": {
      "$ref": "common.json#/$defs/stateLineage"
    },
    "provenance": {
      "$ref": "common.json#/$defs/provenance"
    },
    "extensions": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "outcome_state": {
            "const": "known"
          }
        },
        "required": [
          "outcome_state"
        ]
      },
      "then": {
        "required": [
          "outcome_ref"
        ]
      },
      "else": {
        "not": {
          "required": [
            "outcome_ref"
          ]
        }
      }
    }
  ],
  "additionalProperties": true
}
