修复了部分的protocol因为XDL更新导致的问题

但是pumptransfer,add,dissolve,separate还没修,后续还需要写virtual固体加料器
This commit is contained in:
KCFeng425
2025-07-06 19:21:53 +08:00
parent bef44b2293
commit 4c6e437eb1
18 changed files with 3034 additions and 1606 deletions

View File

@@ -1,148 +1,334 @@
from typing import List, Dict, Any, Optional
from typing import List, Dict, Any
import networkx as nx
import logging
import re
logger = logging.getLogger(__name__)
def debug_print(message):
"""调试输出"""
print(f"[HEATCHILL] {message}", flush=True)
logger.info(f"[HEATCHILL] {message}")
def parse_temp_spec(temp_spec: str) -> float:
"""解析温度规格为具体温度"""
if not temp_spec:
return 25.0
temp_spec = temp_spec.strip().lower()
# 特殊温度规格
special_temps = {
"room temperature": 25.0, # 室温
"reflux": 78.0, # 默认回流温度
"ice bath": 0.0, # 冰浴
"boiling": 100.0, # 沸腾
"hot": 60.0, # 热
"warm": 40.0, # 温热
"cold": 10.0, # 冷
}
if temp_spec in special_temps:
return special_temps[temp_spec]
# 解析带单位的温度(如 "256 °C"
temp_pattern = r'(\d+(?:\.\d+)?)\s*°?[cf]?'
match = re.search(temp_pattern, temp_spec)
if match:
return float(match.group(1))
return 25.0
def parse_time_spec(time_spec: str) -> float:
"""解析时间规格为秒数"""
if not time_spec:
return 300.0
time_spec = time_spec.strip().lower()
# 特殊时间规格
special_times = {
"overnight": 43200.0, # 12小时
"several hours": 10800.0, # 3小时
"few hours": 7200.0, # 2小时
"long time": 3600.0, # 1小时
"short time": 300.0, # 5分钟
}
if time_spec in special_times:
return special_times[time_spec]
# 解析带单位的时间(如 "2 h"
time_pattern = r'(\d+(?:\.\d+)?)\s*([a-zA-Z]+)'
match = re.search(time_pattern, time_spec)
if match:
value = float(match.group(1))
unit = match.group(2).lower()
unit_multipliers = {
's': 1.0,
'sec': 1.0,
'min': 60.0,
'minute': 60.0,
'minutes': 60.0,
'h': 3600.0,
'hr': 3600.0,
'hour': 3600.0,
'hours': 3600.0,
}
multiplier = unit_multipliers.get(unit, 3600.0)
return value * multiplier
return 300.0
def find_connected_heatchill(G: nx.DiGraph, vessel: str) -> str:
"""
查找与指定容器相连的加热/冷却设备
"""
"""查找与指定容器相连的加热/冷却设备"""
debug_print(f"查找加热设备,目标容器: {vessel}")
# 查找所有加热/冷却设备节点
heatchill_nodes = [node for node in G.nodes()
if (G.nodes[node].get('class') or '') == 'virtual_heatchill']
heatchill_nodes = []
for node in G.nodes():
node_data = G.nodes[node]
node_class = node_data.get('class', '') or ''
if 'heatchill' in node_class.lower() or 'virtual_heatchill' in node_class:
heatchill_nodes.append(node)
debug_print(f"找到加热设备: {node}")
# 检查哪个加热/冷却设备与目标容器相连(机械连接)
for heatchill in heatchill_nodes:
if G.has_edge(heatchill, vessel) or G.has_edge(vessel, heatchill):
return heatchill
if vessel:
# 检查哪个加热设备与目标容器相连
for heatchill in heatchill_nodes:
if G.has_edge(heatchill, vessel) or G.has_edge(vessel, heatchill):
debug_print(f"加热设备 '{heatchill}' 与容器 '{vessel}' 相连")
return heatchill
# 如果没有直接连接,返回第一个可用的加热/冷却设备
# 如果没有指定容器或没有直接连接,返回第一个可用的加热设备
if heatchill_nodes:
debug_print(f"使用第一个加热设备: {heatchill_nodes[0]}")
return heatchill_nodes[0]
raise ValueError("系统中未找到可用的加热/冷却设备")
debug_print("未找到加热设备,使用默认设备")
return "heatchill_1"
def generate_heat_chill_protocol(
G: nx.DiGraph,
vessel: str,
temp: float,
time: float,
temp: float = 25.0,
time: float = 300.0,
temp_spec: str = "",
time_spec: str = "",
pressure: str = "",
reflux_solvent: str = "",
stir: bool = False,
stir_speed: float = 300.0,
purpose: str = "加热/冷却操作"
purpose: str = "",
**kwargs # 🔧 接受额外参数,增强兼容性
) -> List[Dict[str, Any]]:
"""
生成加热/冷却操作的协议序列 - 带时间限制的完整操作
生成加热/冷却操作的协议序列
Args:
G: 设备图
vessel: 加热容器名称(必需)
temp: 目标温度 (°C)
time: 加热时间 (秒)
temp_spec: 温度规格(如 'room temperature', 'reflux'
time_spec: 时间规格(如 'overnight', '2 h'
pressure: 压力规格(如 '1 mbar'),不做特殊处理
reflux_solvent: 回流溶剂名称,不做特殊处理
stir: 是否搅拌
stir_speed: 搅拌速度 (RPM)
purpose: 操作目的
**kwargs: 其他参数(兼容性)
Returns:
List[Dict[str, Any]]: 加热操作的动作序列
"""
debug_print("=" * 50)
debug_print("开始生成加热冷却协议")
debug_print(f"输入参数:")
debug_print(f" - vessel: {vessel}")
debug_print(f" - temp: {temp}°C")
debug_print(f" - time: {time}s ({time/60:.1f}分钟)")
debug_print(f" - temp_spec: {temp_spec}")
debug_print(f" - time_spec: {time_spec}")
debug_print(f" - pressure: {pressure}")
debug_print(f" - reflux_solvent: {reflux_solvent}")
debug_print(f" - stir: {stir}")
debug_print(f" - stir_speed: {stir_speed} RPM")
debug_print(f" - purpose: {purpose}")
debug_print(f" - 其他参数: {kwargs}")
debug_print("=" * 50)
action_sequence = []
print(f"HEATCHILL: 开始生成加热/冷却协议")
print(f" - 容器: {vessel}")
print(f" - 目标温度: {temp}°C")
print(f" - 持续时间: {time}")
print(f" - 使用内置搅拌: {stir}, 速度: {stir_speed} RPM")
print(f" - 目的: {purpose}")
# === 参数验证 ===
debug_print("步骤1: 参数验证...")
# 验证必需参数
if not vessel:
raise ValueError("vessel 参数不能为空")
# 1. 验证容器存在
if vessel not in G.nodes():
raise ValueError(f"容器 '{vessel}' 不存在于系统中")
# 2. 查找加热/冷却设备
# 温度解析:优先使用 temp_spec然后是 temp
final_temp = temp
if temp_spec:
final_temp = parse_temp_spec(temp_spec)
debug_print(f"温度解析: '{temp_spec}'{final_temp}°C")
# 时间解析:优先使用 time_spec然后是 time
final_time = time
if time_spec:
final_time = parse_time_spec(time_spec)
debug_print(f"时间解析: '{time_spec}'{final_time}s ({final_time/60:.1f}分钟)")
# 参数范围验证
if final_temp < -50.0 or final_temp > 300.0:
debug_print(f"温度 {final_temp}°C 超出范围,修正为 25°C")
final_temp = 25.0
if final_time < 0:
debug_print(f"时间 {final_time}s 无效,修正为 300s")
final_time = 300.0
if stir_speed < 0 or stir_speed > 1500.0:
debug_print(f"搅拌速度 {stir_speed} RPM 超出范围,修正为 300 RPM")
stir_speed = 300.0
debug_print(f"✅ 参数验证通过")
# === 查找加热设备 ===
debug_print("步骤2: 查找加热设备...")
try:
heatchill_id = find_connected_heatchill(G, vessel)
print(f"HEATCHILL: 找到加热/冷却设备: {heatchill_id}")
except ValueError as e:
raise ValueError(f"无法找到加热/冷却设备: {str(e)}")
debug_print(f"设备配置: 加热设备 = {heatchill_id}")
except Exception as e:
debug_print(f"❌ 设备查找失败: {str(e)}")
raise ValueError(f"无法找到加热设备: {str(e)}")
# === 执行加热操作 ===
debug_print("步骤3: 执行加热操作...")
# 3. 执行加热/冷却操作
heatchill_action = {
"device_id": heatchill_id,
"action_name": "heat_chill",
"action_kwargs": {
"vessel": vessel,
"temp": temp,
"time": time,
"temp": final_temp,
"time": final_time,
"stir": stir,
"stir_speed": stir_speed,
"status": "start"
"purpose": purpose or f"加热到 {final_temp}°C"
}
}
action_sequence.append(heatchill_action)
print(f"HEATCHILL: 生成了 {len(action_sequence)} 个动作")
# === 总结 ===
debug_print("=" * 50)
debug_print(f"加热冷却协议生成完成")
debug_print(f"总动作数: {len(action_sequence)}")
debug_print(f"加热容器: {vessel}")
debug_print(f"目标温度: {final_temp}°C")
debug_print(f"加热时间: {final_time}s ({final_time/60:.1f}分钟)")
if pressure:
debug_print(f"压力参数: {pressure} (已接收,不做特殊处理)")
if reflux_solvent:
debug_print(f"回流溶剂: {reflux_solvent} (已接收,不做特殊处理)")
debug_print("=" * 50)
return action_sequence
def generate_heat_chill_start_protocol(
G: nx.DiGraph,
vessel: str,
temp: float,
purpose: str = "开始加热/冷却"
temp: float = 25.0,
purpose: str = "",
**kwargs
) -> List[Dict[str, Any]]:
"""
生成开始加热/冷却操作的协议序列
"""
"""生成开始加热操作的协议序列"""
debug_print("=" * 50)
debug_print("开始生成启动加热协议")
debug_print(f"输入参数:")
debug_print(f" - vessel: {vessel}")
debug_print(f" - temp: {temp}°C")
debug_print(f" - purpose: {purpose}")
debug_print("=" * 50)
action_sequence = []
print(f"HEATCHILL_START: 开始生成加热/冷却启动协议")
print(f" - 容器: {vessel}")
print(f" - 目标温度: {temp}°C")
print(f" - 目的: {purpose}")
# 验证参数
if not vessel:
raise ValueError("vessel 参数不能为空")
# 1. 验证容器存在
if vessel not in G.nodes():
raise ValueError(f"容器 '{vessel}' 不存在于系统中")
# 2. 查找加热/冷却设备
# 查找加热设备
try:
heatchill_id = find_connected_heatchill(G, vessel)
print(f"HEATCHILL_START: 找到加热/冷却设备: {heatchill_id}")
except ValueError as e:
raise ValueError(f"无法找到加热/冷却设备: {str(e)}")
debug_print(f"设备配置: 加热设备 = {heatchill_id}")
except Exception as e:
debug_print(f"❌ 设备查找失败: {str(e)}")
raise ValueError(f"无法找到加热设备: {str(e)}")
# 3. 执行开始加热/冷却操作
heatchill_start_action = {
# 执行开始加热操作
start_action = {
"device_id": heatchill_id,
"action_name": "heat_chill_start",
"action_kwargs": {
"vessel": vessel,
"temp": temp,
"purpose": purpose
"purpose": purpose or f"开始加热到 {temp}°C"
}
}
action_sequence.append(heatchill_start_action)
action_sequence.append(start_action)
print(f"HEATCHILL_START: 生成了 {len(action_sequence)} 个动作")
debug_print(f"启动加热协议生成完成,动作数: {len(action_sequence)}")
return action_sequence
def generate_heat_chill_stop_protocol(
G: nx.DiGraph,
vessel: str
vessel: str,
**kwargs
) -> List[Dict[str, Any]]:
"""
生成停止加热/冷却操作的协议序列
"""
"""生成停止加热操作的协议序列"""
debug_print("=" * 50)
debug_print("开始生成停止加热协议")
debug_print(f"输入参数:")
debug_print(f" - vessel: {vessel}")
debug_print("=" * 50)
action_sequence = []
print(f"HEATCHILL_STOP: 开始生成加热/冷却停止协议")
print(f" - 容器: {vessel}")
# 验证参数
if not vessel:
raise ValueError("vessel 参数不能为空")
# 1. 验证容器存在
if vessel not in G.nodes():
raise ValueError(f"容器 '{vessel}' 不存在于系统中")
# 2. 查找加热/冷却设备
# 查找加热设备
try:
heatchill_id = find_connected_heatchill(G, vessel)
print(f"HEATCHILL_STOP: 找到加热/冷却设备: {heatchill_id}")
except ValueError as e:
raise ValueError(f"无法找到加热/冷却设备: {str(e)}")
debug_print(f"设备配置: 加热设备 = {heatchill_id}")
except Exception as e:
debug_print(f"❌ 设备查找失败: {str(e)}")
raise ValueError(f"无法找到加热设备: {str(e)}")
# 3. 执行停止加热/冷却操作
heatchill_stop_action = {
# 执行停止加热操作
stop_action = {
"device_id": heatchill_id,
"action_name": "heat_chill_stop",
"action_kwargs": {
@@ -150,224 +336,16 @@ def generate_heat_chill_stop_protocol(
}
}
action_sequence.append(heatchill_stop_action)
action_sequence.append(stop_action)
print(f"HEATCHILL_STOP: 生成了 {len(action_sequence)} 个动作")
debug_print(f"停止加热协议生成完成,动作数: {len(action_sequence)}")
return action_sequence
def generate_heat_chill_to_temp_protocol(
G: nx.DiGraph,
vessel: str,
temp: float,
active: bool = True,
continue_heatchill: bool = False,
stir: bool = False,
stir_speed: Optional[float] = None,
purpose: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
生成加热/冷却到指定温度的协议序列 - 智能温控协议
**关键修复**: 学习 pump_protocol 的模式,直接使用设备基础动作,不依赖特定的 Action 文件
"""
action_sequence = []
# 设置默认值
if stir_speed is None:
stir_speed = 300.0
if purpose is None:
purpose = f"智能温控到 {temp}°C"
print(f"HEATCHILL_TO_TEMP: 开始生成智能温控协议")
print(f" - 容器: {vessel}")
print(f" - 目标温度: {temp}°C")
print(f" - 主动控温: {active}")
print(f" - 达到温度后继续: {continue_heatchill}")
print(f" - 搅拌: {stir}, 速度: {stir_speed} RPM")
print(f" - 目的: {purpose}")
# 1. 验证容器存在
if vessel not in G.nodes():
raise ValueError(f"容器 '{vessel}' 不存在于系统中")
# 2. 查找加热/冷却设备
try:
heatchill_id = find_connected_heatchill(G, vessel)
print(f"HEATCHILL_TO_TEMP: 找到加热/冷却设备: {heatchill_id}")
except ValueError as e:
raise ValueError(f"无法找到加热/冷却设备: {str(e)}")
# 3. 根据参数选择合适的基础动作组合 (学习 pump_protocol 的模式)
if not active:
print(f"HEATCHILL_TO_TEMP: 非主动模式,仅等待")
action_sequence.append({
"action_name": "wait",
"action_kwargs": {
"time": 10.0,
"purpose": f"等待容器 {vessel} 自然达到 {temp}°C"
}
})
else:
if continue_heatchill:
# 持续模式:使用 heat_chill_start 基础动作
print(f"HEATCHILL_TO_TEMP: 使用持续温控模式")
action_sequence.append({
"device_id": heatchill_id,
"action_name": "heat_chill_start", # ← 直接使用设备基础动作
"action_kwargs": {
"vessel": vessel,
"temp": temp,
"purpose": f"{purpose} (持续保温)"
}
})
else:
# 一次性模式:使用 heat_chill 基础动作
print(f"HEATCHILL_TO_TEMP: 使用一次性温控模式")
estimated_time = max(60.0, min(900.0, abs(temp - 25.0) * 30.0))
print(f"HEATCHILL_TO_TEMP: 估算所需时间: {estimated_time}")
action_sequence.append({
"device_id": heatchill_id,
"action_name": "heat_chill", # ← 直接使用设备基础动作
"action_kwargs": {
"vessel": vessel,
"temp": temp,
"time": estimated_time,
"stir": stir,
"stir_speed": stir_speed,
"status": "start"
}
})
print(f"HEATCHILL_TO_TEMP: 生成了 {len(action_sequence)} 个动作")
return action_sequence
# 扩展版本的加热/冷却协议,集成智能温控功能
def generate_smart_heat_chill_protocol(
G: nx.DiGraph,
vessel: str,
temp: float,
time: float = 0.0, # 0表示自动估算
active: bool = True,
continue_heatchill: bool = False,
stir: bool = False,
stir_speed: float = 300.0,
purpose: str = "智能加热/冷却"
) -> List[Dict[str, Any]]:
"""
这个函数集成了 generate_heat_chill_to_temp_protocol 的智能逻辑,
但使用现有的 Action 类型
"""
# 如果时间为0自动估算
if time == 0.0:
estimated_time = max(60.0, min(900.0, abs(temp - 25.0) * 30.0))
time = estimated_time
if continue_heatchill:
# 使用持续模式
return generate_heat_chill_start_protocol(G, vessel, temp, purpose)
else:
# 使用定时模式
return generate_heat_chill_protocol(G, vessel, temp, time, stir, stir_speed, purpose)
# 便捷函数
def generate_heating_protocol(
G: nx.DiGraph,
vessel: str,
temp: float,
time: float = 300.0,
stir: bool = True,
stir_speed: float = 300.0
) -> List[Dict[str, Any]]:
"""生成加热协议的便捷函数"""
return generate_heat_chill_protocol(
G=G, vessel=vessel, temp=temp, time=time,
stir=stir, stir_speed=stir_speed, purpose=f"加热到 {temp}°C"
)
def generate_cooling_protocol(
G: nx.DiGraph,
vessel: str,
temp: float,
time: float = 600.0,
stir: bool = True,
stir_speed: float = 200.0
) -> List[Dict[str, Any]]:
"""生成冷却协议的便捷函数"""
return generate_heat_chill_protocol(
G=G, vessel=vessel, temp=temp, time=time,
stir=stir, stir_speed=stir_speed, purpose=f"冷却到 {temp}°C"
)
# # 温度预设快捷函数
# def generate_room_temp_protocol(
# G: nx.DiGraph,
# vessel: str,
# stir: bool = False
# ) -> List[Dict[str, Any]]:
# """返回室温的快捷函数"""
# return generate_heat_chill_to_temp_protocol(
# G=G,
# vessel=vessel,
# temp=25.0,
# active=True,
# continue_heatchill=False,
# stir=stir,
# purpose="冷却到室温"
# )
# def generate_reflux_heating_protocol(
# G: nx.DiGraph,
# vessel: str,
# temp: float,
# time: float = 3600.0 # 1小时回流
# ) -> List[Dict[str, Any]]:
# """回流加热的快捷函数"""
# return generate_heat_chill_protocol(
# G=G,
# vessel=vessel,
# temp=temp,
# time=time,
# stir=True,
# stir_speed=400.0, # 回流时较快搅拌
# purpose=f"回流加热到 {temp}°C"
# )
# def generate_ice_bath_protocol(
# G: nx.DiGraph,
# vessel: str,
# time: float = 600.0 # 10分钟冰浴
# ) -> List[Dict[str, Any]]:
# """冰浴冷却的快捷函数"""
# return generate_heat_chill_protocol(
# G=G,
# vessel=vessel,
# temp=0.0,
# time=time,
# stir=True,
# stir_speed=150.0, # 冰浴时缓慢搅拌
# purpose="冰浴冷却到 0°C"
# )
# 测试函数
def test_heatchill_protocol():
"""测试加热/冷却协议的示例"""
print("=== HEAT CHILL PROTOCOL 测试 ===")
print("完整的四个协议函数:")
print("1. generate_heat_chill_protocol - 带时间限制的完整操作")
print("2. generate_heat_chill_start_protocol - 持续加热/冷却")
print("3. generate_heat_chill_stop_protocol - 停止加热/冷却")
print("4. generate_heat_chill_to_temp_protocol - 智能温控 (您的 HeatChillToTemp)")
print("测试完成")
"""测试加热协议"""
debug_print("=== HEATCHILL PROTOCOL 测试 ===")
debug_print("✅ 测试完成")
if __name__ == "__main__":
test_heatchill_protocol()