Files
ssw-ontology/scripts/optimize_ontology_v2.py
adminandClaude Code aef8b83e24 docs(release): v1.8.0 发布说明 + 本体导入设计约束落地记录 + 对标分析
- 新增 docs/ONTOLOGY_IMPORT.md:本体 schema 导入的硬约束(fail-fast 闸门、整事务、
  幂等键、IRI 双空间映射、不做范围对账删除),并记录实现落点与逐条偏差
- 新增 docs/auto-ontology-benchmark-20261106.md:NVIDIA auto-ontology 对标分析与
  Top-8 路线图,本版已落地项逐条标注
- docs/README.md 同步 API 清单与 MCP 工具表(54 个:只读 27 / 写入 27)
- 项目 CLAUDE.md 补「本体 schema 导入」小节:闸门顺序、事务与串行理由、幂等表、
  RDF Schema 命名空间年份坑
- README.md 镜像标签更新到 1.8.0,本地构建镜像补上 Linux 静态二进制交叉编译步骤,
  健康检查端点改回 /api/v1/version
- scripts/ 收拢本体 v2 迁移与分级校正脚本(一次性运维工具,不参与构建)

Co-Authored-By: Claude Code <[email protected]>
2026-11-06 13:53:25 +08:00

165 lines
8.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
尚数网本体 v2 修订后优化脚本(基于 ontology_v2_audit.md 建议)
- 阶段 A (schema 补定义): 给 Standard/PolicyDocument/DataProduct 补定义高频公共属性,
使 P4 拆分遗留的"超纲属性"合法化、纳入检索/脱敏 schema (P0-① / P1-⑤)
- 阶段 B (启用分级): DigitalContract/UsageAction/UsageConstraint 启用 L0-L4 分级 (P1-③)
- 阶段 C (实例值映射): 把 Standard/PolicyDocument/DataProduct 实例上的超纲字段
值归一到类型定义字段, 并清掉冗余超纲 key (P0-① / P1-⑤ 增强)
- 阶段 D (recompute): 跑分级重算(默认 dry_run, 无副作用)确认一致性
运行: python3 scripts/optimize_ontology_v2.py --phase A [--dry-run]
python3 scripts/optimize_ontology_v2.py --phase B
python3 scripts/optimize_ontology_v2.py --phase C [--dry-run]
python3 scripts/optimize_ontology_v2.py --phase D
REST 调用需 dangerouslyDisableSandbox (出口代理会剥 X-API-Key 头)
"""
import sys, json, argparse
sys.path.insert(0, "/Users/xs/Desktop/workspace/ontology")
import scripts.import_ontology_v2 as m # 复用 api()
# ---------- 阶段 A: 补定义属性 ----------
# (type, api_name, display_name, data_type, required, description)
ADD_PROPS = [
# Standard: ComplianceStandard 原字段, 对标准有意义
("Standard", "migrated_from", "迁移溯源", "string", False, "源自 ComplianceStandard 拆分前的实例主键"),
("Standard", "standard_code", "标准代号", "string", False, "标准编号/代号, 如 GB/T 19001"),
("Standard", "related_space", "关联空间", "string", False, "所属可信数据空间标识"),
("Standard", "standard_type", "原分类", "string", False, "拆分前 standard_type 取值(国家标准/行业标准/技术文件/政策文件/法律法规)"),
("Standard", "status", "标准状态", "string", False, "现行/废止/征求意见稿等"),
# PolicyDocument
("PolicyDocument", "scope", "适用范围", "string", False, "文件适用地域/行业范围"),
("PolicyDocument", "standard_code", "文件号", "string", False, "文件编号/标准代号"),
("PolicyDocument", "standard_type", "原分类", "string", False, "拆分前 standard_type 取值(溯源)"),
("PolicyDocument", "related_space", "关联空间", "string", False, "所属可信数据空间标识"),
("PolicyDocument", "migrated_from", "迁移溯源", "string", False, "源自 ComplianceStandard 拆分前的实例主键"),
("PolicyDocument", "english_title", "英文标题", "string", False, "文件英文标题"),
# DataProduct: AI/行业/管辖扩展属性
("DataProduct", "jurisdiction", "管辖域", "string", False, "数据产品适用管辖地域"),
("DataProduct", "scene", "应用场景", "string", False, "数据产品应用场景描述"),
("DataProduct", "industry", "所属行业", "string", False, "数据产品所属行业"),
("DataProduct", "supports_inference", "支持推理", "boolean", False, "是否可作为模型推理输入"),
("DataProduct", "supports_training", "支持训练", "boolean", False, "是否可作为模型训练输入"),
("DataProduct", "tech_stack", "技术栈", "string", False, "实现技术栈"),
]
def _exists_prop(tname, api_name):
code, r = m.api("GET", f"/ontology/object-types/{tname}")
ot = r.get("object_type", r) if isinstance(r, dict) else {}
props = ot.get("properties", []) if isinstance(ot, dict) else []
return any(p.get("api_name") == api_name for p in props)
def phase_a(dry):
print("[A] 补定义高频公共属性 (P0-① / P1-⑤)")
for tname, api_name, disp, dt, req, desc in ADD_PROPS:
if _exists_prop(tname, api_name):
print(f" 跳过(已存在) {tname}.{api_name}")
continue
body = {"api_name": api_name, "display_name": disp, "data_type": dt,
"required": req, "description": desc}
if dry:
print(f" [dry] POST {tname}/properties {api_name}")
continue
code, r = m.api("POST", f"/ontology/object-types/{tname}/properties", body)
print(f" {tname}.{api_name}: {code} {'ok' if code < 400 else r}")
# ---------- 阶段 B: 启用 3 类型分级 ----------
def phase_b(dry):
print("[B] 启用 DigitalContract/UsageAction/UsageConstraint 分级 (P1-③)")
levels = ["L0", "L1", "L2", "L3", "L4"]
for tname in ("DigitalContract", "UsageAction", "UsageConstraint"):
body = {"classification_property": "classification",
"classification_levels": levels, "classification_default": "L3"}
if dry:
print(f" [dry] PUT {tname}/classification classification_property=classification default=L3")
continue
code, r = m.api("PUT", f"/ontology/object-types/{tname}/classification", body)
print(f" {tname}: {code} {'ok' if code < 400 else r}")
# ---------- 阶段 C: 实例值映射 + 清冗余 ----------
# Standard 映射: 超纲 -> 定义字段
STD_MAP = [
("title", "title_cn"), ("english_title", "title_en"),
("effective_date", "implement_date"), ("code", "doc_no"),
("name", "title_cn"), ("implementation_date", "implement_date"),
("issued_by", "publisher"), ("issuer", "publisher"),
("issuing_body", "publisher"), ("standard_name", "title_cn"),
]
STD_CLEAR = ["title", "english_title", "effective_date", "code", "name",
"implementation_date", "issued_by", "issuer", "standard_name",
"issuing_body", "note", "reference_docs"] # 映射后仍残留就清
# PolicyDocument 映射
PD_MAP = [("validity_period", "valid_period"), ("name", "title"),
("code", "doc_no"), ("issuing_org", "issuer"),
("issuing_body", "issuer")]
PD_CLEAR = ["validity_period", "name", "code", "issuing_body", "issuing_org",
"level", "key_entities", "core_target_2027", "ai_scenarios",
"key_projects", "project_target", "scenario_target", "talent_target"]
# DataProduct 映射
DP_MAP = [("billing_model", "price_model"), ("description", "desc")]
DP_CLEAR = ["billing_model", "description"]
def _migrate_type(tname, mapping, clear, dry):
off = 0; n = 0; migrated = 0; batch = []
dst_set = set(d for _, d in mapping)
def flush(batch):
if not batch:
return
body = {"items": batch, "stop_on_error": False, "validate_enum": False}
c2, r2 = m.api("POST", "/objects/batch", body)
if c2 >= 400:
print(f" ! {tname} batch 失败 {c2} {str(r2)[:200]}")
while True:
code, r = m.api("GET", f"/objects?type={tname}&limit=300&offset={off}")
if not isinstance(r, dict):
print(f" {tname} ERR {code} {r}"); break
items = r.get("items", [])
for o in items:
n += 1
props = o.get("properties") or {}
new_props = dict(props)
changed = False
for src, dst in mapping:
if props.get(src) not in (None, ""):
if not new_props.get(dst):
new_props[dst] = props[src]
new_props[src] = None # merge 语义: null=删除字段
changed = True
for k in clear:
if k in new_props and k not in dst_set:
new_props[k] = None # 标记删除
changed = True
if changed:
migrated += 1
if dry:
continue
batch.append({"type": tname, "pk": o.get("pk"), "properties": new_props})
if len(batch) >= 100:
flush(batch); batch = []
if len(items) < 300:
break
off += 300
flush(batch)
print(f" {tname}: 扫描 {n} 实例, 需迁移 {migrated}")
def phase_c(dry):
print("[C] 实例值映射 + 清冗余超纲 (P0-① / P1-⑤ 增强)")
_migrate_type("Standard", STD_MAP, STD_CLEAR, dry)
_migrate_type("PolicyDocument", PD_MAP, PD_CLEAR, dry)
_migrate_type("DataProduct", DP_MAP, DP_CLEAR, dry)
# ---------- 阶段 D: recompute ----------
def phase_d(dry):
print("[D] 分级重算 (默认 dry_run)")
code, r = m.api("POST", "/ontology/classification/recompute", {"dry_run": True})
print(f" recompute: {code}")
print(" ", json.dumps(r, ensure_ascii=False)[:500] if isinstance(r, dict) else r)
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--phase", required=True, choices=["A", "B", "C", "D"])
ap.add_argument("--dry-run", action="store_true")
a = ap.parse_args()
{"A": phase_a, "B": phase_b, "C": phase_c, "D": phase_d}[a.phase](a.dry_run)