chore: 搭建项目骨架

- pyproject:hatchling + src 布局,核心依赖 psycopg/pydantic/pyyaml/cel-python
- FastAPI 归入 [api] 可选依赖,开发依赖走 PEP 735 dependency-groups
- 建立分层目录:ontology/compiler/storage/repository/graph/timeseries/expr/rules/api
- errors.py 统一异常基类;py.typed 标记类型完整
- ruff(100) + pytest 配置;冒烟测试校验核心包不引入 fastapi/uvicorn
This commit is contained in:
Donny committed 2026-11-05 02:18:10 +08:00
1 parent ecd5925b2c
commit ace505cc6b
16 files changed
+1544 -11

No files matched your search

+18 -11
View File
@@ -177,7 +177,7 @@ constraints:
```python
repo.save(Device(id="D-0001", serial="SN-88", model="X200", meta={"vendor": "acme"}))
repo.save_many(devices)
repo.patch("D-0001", {"meta.vendor": "acme-new"}) # 部分更新
repo.patch("D-0001", {"meta.vendor": "acme-new"}) # 部分更新
```
**时序写入**——数字孪生的遥测流,按时间追加:
@@ -185,7 +185,7 @@ repo.patch("D-0001", {"meta.vendor": "acme-new"}) # 部分更新
```python
ts = adapter.timeseries("SensorReading")
ts.write([{"ts": now, "metric": "temp", "value": 36.5, "tags": {"device": "D-0001"}}])
ts.write_batch(stream, batch_size=1000) # 批量/流式
ts.write_batch(stream, batch_size=1000) # 批量/流式
```
写入语义约定:`save` 按 `id` 幂等 upsert;`link()` 幂等建边;`constraints` 在提交前校验,失败抛 `ConstraintViolation`;批量写入单事务,超限自动分块。
@@ -229,18 +229,25 @@ from adapter_python_pg import Adapter, ConstraintViolation
adapter = Adapter.load("ontology/factory-twin.yaml", dsn="postgresql://...")
repo = adapter.repository("Device") # 实体仓储
repo.save(obj); repo.get(id); repo.find(**where); repo.patch(id, patch); repo.delete(id)
repo = adapter.repository("Device") # 实体仓储
repo.save(obj)
repo.get(id)
repo.find(**where)
repo.patch(id, patch)
repo.delete(id)
graph = adapter.graph() # 图操作
graph.link(src, "mounted_on", dst, since=...); graph.traverse(...); graph.path(...)
graph = adapter.graph() # 图操作
graph.link(src, "mounted_on", dst, since=...)
graph.traverse(...)
graph.path(...)
ts = adapter.timeseries("SensorReading") # 时序
ts.write(rows); ts.query(bucket="5m", agg="avg", **filters)
ts = adapter.timeseries("SensorReading") # 时序
ts.write(rows)
ts.query(bucket="5m", agg="avg", **filters)
adapter.invoke("startLine", on="L-01", input={"deviceIds": ["D-0001"]}) # Actions
adapter.functions("Line")["utilization"] # Functions
adapter.validate("SensorReading", row) # Constraints
adapter.invoke("startLine", on="L-01", input={"deviceIds": ["D-0001"]}) # Actions
adapter.functions("Line")["utilization"] # Functions
adapter.validate("SensorReading", row) # Constraints
```
FastAPI 侧不提供独立服务:`build_router()` 返回一个 `APIRouter`,由宿主项目的 `app.include_router()` 挂进去。路由从本体自动生成,同一份 YAML 不重复声明: