序列化当前仿真资源配置为可保存的场景对象,并支持从场景还原。 ``save`` / ``load`` 操作仅操作资源数量配置(地面站、LEO 卫星), 不保存正在进行中的传输请求或实时仿真状态(快照功能由独立的 snapshot 模块负责)。
| 92 | |
| 93 | |
| 94 | class ScenarioManager: |
| 95 | """序列化当前仿真资源配置为可保存的场景对象,并支持从场景还原。 |
| 96 | |
| 97 | ``save`` / ``load`` 操作仅操作资源数量配置(地面站、LEO 卫星), |
| 98 | 不保存正在进行中的传输请求或实时仿真状态(快照功能由独立的 snapshot 模块负责)。 |
| 99 | """ |
| 100 | |
| 101 | @staticmethod |
| 102 | def save(engine: Any, name: str = "") -> Dict[str, Any]: |
| 103 | """将引擎当前资源配置序列化为场景字典。 |
| 104 | |
| 105 | Parameters |
| 106 | ---------- |
| 107 | engine: |
| 108 | ``SimulationEngine`` 实例。 |
| 109 | name: |
| 110 | 场景名称(可选,便于人类识别)。 |
| 111 | |
| 112 | Returns |
| 113 | ------- |
| 114 | dict |
| 115 | 符合 Scenario Schema 的字典,可直接序列化为 JSON 或 YAML。 |
| 116 | """ |
| 117 | return { |
| 118 | "version": SCENARIO_VERSION, |
| 119 | "name": name or "unnamed", |
| 120 | "saved_at": datetime.now(tz=timezone.utc).isoformat(), |
| 121 | "ground_station_count": len(engine.ground_stations), |
| 122 | "leo_satellite_count": len(engine.leo_satellites), |
| 123 | "geo_relay_count": len(engine.geo_relays), |
| 124 | "data_types": list(engine.DATA_TYPES.keys()) if hasattr(engine, "DATA_TYPES") else [], |
| 125 | } |
| 126 | |
| 127 | @staticmethod |
| 128 | def load(engine: Any, data: Dict[str, Any]) -> Dict[str, Any]: |
| 129 | """从场景字典恢复引擎资源配置。 |
| 130 | |
| 131 | 只恢复可在运行时动态调整的资源数量(地面站、LEO 卫星)。 |
| 132 | |
| 133 | Parameters |
| 134 | ---------- |
| 135 | engine: |
| 136 | ``SimulationEngine`` 实例。 |
| 137 | data: |
| 138 | 符合 Scenario Schema 的字典(由 :meth:`save` 生成或导入)。 |
| 139 | |
| 140 | Returns |
| 141 | ------- |
| 142 | dict |
| 143 | 包含恢复结果摘要的字典。 |
| 144 | """ |
| 145 | errors = ScenarioManager.validate(data) |
| 146 | if errors: |
| 147 | raise ValueError("场景数据非法: " + "; ".join(errors)) |
| 148 | |
| 149 | changes: List[str] = [] |
| 150 | |
| 151 | gs_count = data.get("ground_station_count") |
nothing calls this directly
no outgoing calls
no test coverage detected