ontology_views.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. r"""本体视图适配(P12):把 ontology MCP 的返回值包成**与 Python 详情层相同**的响应形状。
  3. 用途:Java 业务端点只需代理本模块的输出(再按对外口径脱敏),一致性由裁判逐值验收。
  4. 形状来源:`app_backEnd/app_backEnd_guanlan/serve.py` 的 `/api/ont_list`、`/api/ont_obj` 分支。
  5. """
  6. from __future__ import annotations
  7. from typing import Any
  8. def _om():
  9. from src.ontology import mcp_server as om
  10. return om
  11. def ont_list_rows(type: str = '', t: str = '', limit: int = 60) -> dict[str, Any]:
  12. """`/api/ont_list`:`rows = om.ont_query(type=…, turbine=…, limit=…).get('data', [])`"""
  13. got = _om().ont_query(type=type or '', turbine=t or '', limit=int(limit or 60))
  14. return dict(rows=(got or {}).get('data', []))
  15. def ont_obj_bundle(id: str = '') -> dict[str, Any]:
  16. """`/api/ont_obj`:`{obj: ont_get(id).data, links: ont_links(id).data, err: ont_get(id).error}`"""
  17. om = _om()
  18. r = om.ont_get(id=id) or {}
  19. lk = (om.ont_links(id=id) or {}).get('data', {})
  20. return dict(obj=r.get('data'), links=lk, err=r.get('error'))
  21. def ont_chain(kind: str = 'fault', code: str = '', system: str = '', top: int = 10,
  22. keyword: str = '', dkind: str = '') -> dict[str, Any]:
  23. """`/api/ont_chain`:**逐字照抄**详情层的五路分派与错误形状(含"未知链"与异常包装)。"""
  24. om = _om()
  25. try:
  26. fn = {'fault': lambda: om.ont_chain_fault(code=code or ''),
  27. 'prev': lambda: om.ont_chain_preventive(system=system or ''),
  28. 'tree': lambda: om.ont_fault_tree(system=system or ''),
  29. 'plan': lambda: om.ont_maint_plan(top=int(top or 10)),
  30. 'doc': lambda: om.ont_doc_search(keyword=keyword or '', kind=dkind or '')}
  31. return fn[kind]() if kind in fn else dict(text='未知链 %s' % kind, error=True)
  32. except Exception as e: # noqa: BLE001
  33. return dict(text=('本体查询失败: %s' % e)[:200], error=True)