JavaProjectRepo/推理接口对材料类型的支持.md
2026-03-20 19:00:16 +08:00

113 lines
4.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 推理接口对材料类型的支持分析与实施方案
根据最新的需求,`algorithm_model` 表已支持 `material_type` 字段,用于区分不同材料(如 `Pu`、`U` 等)的专用模型。为了在仿真推理阶段能够正确加载对应的模型,需要对现有的推理数据流进行改造,确保“材料类型”这一关键上下文能够从仿真计算传递到推理服务。
## 1. 现状分析 (As-Is)
### 1.1 数据流现状
1. **SimController**: 调用 `simBuilder.buildUnits` 构建仿真单元。
2. **SimBuilder**: 从 `Topology``MaterialService` 读取数据,构建 `SimUnit`
- *当前缺失*: `SimUnit` 仅包含 `materialId`**未包含** `materialType`
- *当前缺失*: `Material` 实体类中也未明确发现 `materialType` 字段(仅有 `name`, `concentrations` 等)。
3. **SimInferService**: 将 `SimContext` 转换为 `DeviceStepInfo`,并按 `deviceType` 分组。
- *当前缺失*: 分组逻辑未考虑材料类型。
4. **DeviceInferService**: 接收分组数据,再次按 `algorithmType` 分组,然后调用 `algorithmModelService.getCurrentModelPath(algo, device)`
- *当前缺失*: 调用获取模型路径接口时,缺少 `materialType` 参数,导致无法精确定位到特定材料的模型(可能默认返回第一个或报错)。
### 1.2 关键断点
- **源头缺失**: 仿真单元SimUnit不知道自己处理的是什么类型的材料。
- **传递中断**: 推理服务InferService无法获取材料类型信息。
- **查询不足**: 模型匹配逻辑未利用材料类型进行过滤。
## 2. 实施方案 (To-Be)
### 2.1 第一步:完善领域模型 (Domain & Model)
#### 1. 不修改 `Material` 实体
需要根据材料表获取的是U浓度值或者Pu浓度值决定Material\_type = U  或者Pu
这个主要是模型根据材料类型训练来的,所以只能把数据也做了材料分类。
#### 2. 修改 `SimUnit` Record
`SimUnit` 中增加 `materialType` 字段,用于在内存中流转该信息。
```java
public record SimUnit(
String unitId,
String deviceId,
String materialId,
String deviceType,
String materialType, // 新增
Map<String, Double> staticProperties
) {}
```
#### 3. 修改 `DeviceStepInfo`
`DeviceStepInfo` 中增加 `materialType` 字段,确保分组时能携带此信息。
```java
public class DeviceStepInfo {
// ... 原有字段
private String materialType; // 新增
// getter/setter ...
}
```
### 2.2 第二步:更新构建逻辑 (SimBuilder)
`SimBuilder.buildUnits` 方法中:
1. 在查询 `Material` 信息时根据材料属性U浓度/Pu浓度等推导 `materialType`
- 规则示例:若 `pu_concentration > 0` 则为 `Pu`,否则若 `u_concentration > 0` 则为 `U`,否则为 `unknown`
2. 在构造 `SimUnit` 时,将 `material_type` 注入。
### 2.3 第三步:更新推理准备逻辑 (SimInferService)
`SimInferService.asyncInferAndSave` 方法中:
1. 在将 `SimContext` 转换为 `DeviceStepInfo` 时,从对应的 `SimUnit` 中提取 `materialType` 并赋值给 `DeviceStepInfo`
2. **分组策略变更**:
- 原: `Map<String, List<DeviceStepInfo>>` (Key: DeviceType)
- 新: 保持外层按 `DeviceType` 分组不变,利用 `DeviceStepInfo` 内部携带的 `materialType` 在下一步进行细分。
### 2.4 第四步:更新推理执行逻辑 (DeviceInferService)
`DeviceInferService.processDeviceInference` 方法中:
1. 接收到的数据是按 `DeviceType` 分组的。
2. **三级分组**: 遍历设备列表,进一步按 `AlgorithmType` **AND** `MaterialType` 进行分组。
- 构造复合键或嵌套 Map: `Map<String, Map<String, List<DeviceStepInfo>>>` (AlgoType -> MaterialType -> List)。
3. **模型匹配**:
```java
String modelPath = algorithmModelService.getCurrentModelPath(currentAlgoType, deviceType, currentMaterialType);
```
4. **请求构建**: 确保找到的模型是与当前批次数据的材料类型匹配的。
### 2.5 第五步:更新模型查询接口 (AlgorithmModelService)
完善 `getCurrentModelPath` 方法,增加 `materialType` 参数,并更新 MyBatis-Plus 查询条件。
```java
String getCurrentModelPath(String algorithmType, String deviceType, String materialType);
```
## 3. 任务清单 (Action Items)
1. **Domain & Model**:
- [ ] 更新 `SimUnit.java` 增加 `materialType`
- [ ] 更新 `DeviceStepInfo.java` 增加 `materialType`
2. **Builder**:
- [ ] 修改 `SimBuilder.java`,实现材料类型推导逻辑并填充 `SimUnit`
3. **Service**:
- [ ] 修改 `SimInferService.java`,在转换时将 `materialType` 传递给 `DeviceStepInfo`
- [ ] 修改 `DeviceInferService.java`,实现 (Algo + Material) 的组合分组与模型匹配调用。
- [ ] 修改 `AlgorithmModelService.java` 及实现类,增加带 `materialType` 的路径查询方法。
4. **Controller**:
- [ ] `SimController` 无需修改代码,验证端到端流程。