mirror of
https://github.com/dptech-corp/Uni-Lab-OS.git
synced 2026-02-05 05:45:10 +00:00
Compare commits
76 Commits
56eb7e2ab4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5179a7e48e | ||
|
|
3a2d9e9603 | ||
|
|
a277bd2bed | ||
|
|
176de521b4 | ||
|
|
38c5c267af | ||
|
|
2a5ddd611d | ||
|
|
8580b84167 | ||
|
|
3f80349d7d | ||
|
|
024156848e | ||
|
|
8066c200b9 | ||
|
|
266366cc25 | ||
|
|
121c3985cc | ||
|
|
6ca5c72fc6 | ||
|
|
bc8c49ddda | ||
|
|
28f93737ac | ||
|
|
5dc81ec9be | ||
|
|
13a6795657 | ||
|
|
53219d8b04 | ||
|
|
b1cdef9185 | ||
|
|
9854ed8c9c | ||
|
|
52544a2c69 | ||
|
|
5ce433e235 | ||
|
|
c7c14d2332 | ||
|
|
6fdd482649 | ||
|
|
d390236318 | ||
|
|
ed8ee29732 | ||
|
|
ffc583e9d5 | ||
|
|
f1ad0c9c96 | ||
|
|
8fa3407649 | ||
|
|
d3282822fc | ||
|
|
554bcade24 | ||
|
|
a662c75de1 | ||
|
|
931614fe64 | ||
|
|
d39662f65f | ||
|
|
acf5fdebf8 | ||
|
|
7f7b1c13c0 | ||
|
|
75f09034ff | ||
|
|
549a50220b | ||
|
|
4189a2cfbe | ||
|
|
48895a9bb1 | ||
|
|
891f126ed6 | ||
|
|
4d3475a849 | ||
|
|
b475db66df | ||
|
|
a625a86e3e | ||
|
|
37e0f1037c | ||
|
|
a242253145 | ||
|
|
448e0074b7 | ||
|
|
304827fc8d | ||
|
|
872b3d781f | ||
|
|
813400f2b4 | ||
|
|
b6dfe2b944 | ||
|
|
8807865649 | ||
|
|
5fc7eb7586 | ||
|
|
9bd72b48e1 | ||
|
|
42b78ab4c1 | ||
|
|
9645609a05 | ||
|
|
a2a827d7ac | ||
|
|
bb3ca645a4 | ||
|
|
37ee43d19a | ||
|
|
bc30f23e34 | ||
|
|
166d84afe1 | ||
|
|
1b43c53015 | ||
|
|
d4415f5a35 | ||
|
|
0260cbbedb | ||
|
|
7c440d10ab | ||
|
|
c85c49817d | ||
|
|
c70eafa5f0 | ||
|
|
b64466d443 | ||
|
|
ef3f24ed48 | ||
|
|
2a8e8d014b | ||
|
|
e0da1c7217 | ||
|
|
51d3e61723 | ||
|
|
6b5765bbf3 | ||
|
|
eb1f3fbe1c | ||
|
|
fb93b1cd94 | ||
|
|
9aeffebde1 |
@@ -2,6 +2,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import uuid
|
import uuid
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
@@ -24,15 +25,7 @@ class SimpleGraph:
|
|||||||
|
|
||||||
def add_edge(self, source, target, **attrs):
|
def add_edge(self, source, target, **attrs):
|
||||||
"""添加边"""
|
"""添加边"""
|
||||||
# edge = {"source": source, "target": target, **attrs}
|
edge = {"source": source, "target": target, **attrs}
|
||||||
edge = {
|
|
||||||
"source": source, "target": target,
|
|
||||||
"source_node_uuid": source,
|
|
||||||
"target_node_uuid": target,
|
|
||||||
"source_handle_io": "source",
|
|
||||||
"target_handle_io": "target",
|
|
||||||
**attrs
|
|
||||||
}
|
|
||||||
self.edges.append(edge)
|
self.edges.append(edge)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
@@ -49,7 +42,6 @@ class SimpleGraph:
|
|||||||
"multigraph": False,
|
"multigraph": False,
|
||||||
"graph": {},
|
"graph": {},
|
||||||
"nodes": nodes_list,
|
"nodes": nodes_list,
|
||||||
"edges": self.edges,
|
|
||||||
"links": self.edges,
|
"links": self.edges,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,8 +58,495 @@ def extract_json_from_markdown(text: str) -> str:
|
|||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def convert_to_type(val: str) -> Any:
|
||||||
|
"""将字符串值转换为适当的数据类型"""
|
||||||
|
if val == "True":
|
||||||
|
return True
|
||||||
|
if val == "False":
|
||||||
|
return False
|
||||||
|
if val == "?":
|
||||||
|
return None
|
||||||
|
if val.endswith(" g"):
|
||||||
|
return float(val.split(" ")[0])
|
||||||
|
if val.endswith("mg"):
|
||||||
|
return float(val.split("mg")[0])
|
||||||
|
elif val.endswith("mmol"):
|
||||||
|
return float(val.split("mmol")[0]) / 1000
|
||||||
|
elif val.endswith("mol"):
|
||||||
|
return float(val.split("mol")[0])
|
||||||
|
elif val.endswith("ml"):
|
||||||
|
return float(val.split("ml")[0])
|
||||||
|
elif val.endswith("RPM"):
|
||||||
|
return float(val.split("RPM")[0])
|
||||||
|
elif val.endswith(" °C"):
|
||||||
|
return float(val.split(" ")[0])
|
||||||
|
elif val.endswith(" %"):
|
||||||
|
return float(val.split(" ")[0])
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
|
def refactor_data(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||||
|
"""统一的数据重构函数,根据操作类型自动选择模板"""
|
||||||
|
refactored_data = []
|
||||||
|
|
||||||
|
# 定义操作映射,包含生物实验和有机化学的所有操作
|
||||||
|
OPERATION_MAPPING = {
|
||||||
|
# 生物实验操作
|
||||||
|
"transfer_liquid": "SynBioFactory-liquid_handler.prcxi-transfer_liquid",
|
||||||
|
"transfer": "SynBioFactory-liquid_handler.biomek-transfer",
|
||||||
|
"incubation": "SynBioFactory-liquid_handler.biomek-incubation",
|
||||||
|
"move_labware": "SynBioFactory-liquid_handler.biomek-move_labware",
|
||||||
|
"oscillation": "SynBioFactory-liquid_handler.biomek-oscillation",
|
||||||
|
# 有机化学操作
|
||||||
|
"HeatChillToTemp": "SynBioFactory-workstation-HeatChillProtocol",
|
||||||
|
"StopHeatChill": "SynBioFactory-workstation-HeatChillStopProtocol",
|
||||||
|
"StartHeatChill": "SynBioFactory-workstation-HeatChillStartProtocol",
|
||||||
|
"HeatChill": "SynBioFactory-workstation-HeatChillProtocol",
|
||||||
|
"Dissolve": "SynBioFactory-workstation-DissolveProtocol",
|
||||||
|
"Transfer": "SynBioFactory-workstation-TransferProtocol",
|
||||||
|
"Evaporate": "SynBioFactory-workstation-EvaporateProtocol",
|
||||||
|
"Recrystallize": "SynBioFactory-workstation-RecrystallizeProtocol",
|
||||||
|
"Filter": "SynBioFactory-workstation-FilterProtocol",
|
||||||
|
"Dry": "SynBioFactory-workstation-DryProtocol",
|
||||||
|
"Add": "SynBioFactory-workstation-AddProtocol",
|
||||||
|
}
|
||||||
|
|
||||||
|
UNSUPPORTED_OPERATIONS = ["Purge", "Wait", "Stir", "ResetHandling"]
|
||||||
|
|
||||||
|
for step in data:
|
||||||
|
operation = step.get("action")
|
||||||
|
if not operation or operation in UNSUPPORTED_OPERATIONS:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 处理重复操作
|
||||||
|
if operation == "Repeat":
|
||||||
|
times = step.get("times", step.get("parameters", {}).get("times", 1))
|
||||||
|
sub_steps = step.get("steps", step.get("parameters", {}).get("steps", []))
|
||||||
|
for i in range(int(times)):
|
||||||
|
sub_data = refactor_data(sub_steps)
|
||||||
|
refactored_data.extend(sub_data)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 获取模板名称
|
||||||
|
template = OPERATION_MAPPING.get(operation)
|
||||||
|
if not template:
|
||||||
|
# 自动推断模板类型
|
||||||
|
if operation.lower() in ["transfer", "incubation", "move_labware", "oscillation"]:
|
||||||
|
template = f"SynBioFactory-liquid_handler.biomek-{operation}"
|
||||||
|
else:
|
||||||
|
template = f"SynBioFactory-workstation-{operation}Protocol"
|
||||||
|
|
||||||
|
# 创建步骤数据
|
||||||
|
step_data = {
|
||||||
|
"template": template,
|
||||||
|
"description": step.get("description", step.get("purpose", f"{operation} operation")),
|
||||||
|
"lab_node_type": "Device",
|
||||||
|
"parameters": step.get("parameters", step.get("action_args", {})),
|
||||||
|
}
|
||||||
|
refactored_data.append(step_data)
|
||||||
|
|
||||||
|
return refactored_data
|
||||||
|
|
||||||
|
|
||||||
|
def build_protocol_graph(
|
||||||
|
labware_info: List[Dict[str, Any]], protocol_steps: List[Dict[str, Any]], workstation_name: str
|
||||||
|
) -> SimpleGraph:
|
||||||
|
"""统一的协议图构建函数,根据设备类型自动选择构建逻辑"""
|
||||||
|
G = SimpleGraph()
|
||||||
|
resource_last_writer = {}
|
||||||
|
LAB_NAME = "SynBioFactory"
|
||||||
|
|
||||||
|
protocol_steps = refactor_data(protocol_steps)
|
||||||
|
|
||||||
|
# 检查协议步骤中的模板来判断协议类型
|
||||||
|
has_biomek_template = any(
|
||||||
|
("biomek" in step.get("template", "")) or ("prcxi" in step.get("template", ""))
|
||||||
|
for step in protocol_steps
|
||||||
|
)
|
||||||
|
|
||||||
|
if has_biomek_template:
|
||||||
|
# 生物实验协议图构建
|
||||||
|
for labware_id, labware in labware_info.items():
|
||||||
|
node_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
labware_attrs = labware.copy()
|
||||||
|
labware_id = labware_attrs.pop("id", labware_attrs.get("name", f"labware_{uuid.uuid4()}"))
|
||||||
|
labware_attrs["description"] = labware_id
|
||||||
|
labware_attrs["lab_node_type"] = (
|
||||||
|
"Reagent" if "Plate" in str(labware_id) else "Labware" if "Rack" in str(labware_id) else "Sample"
|
||||||
|
)
|
||||||
|
labware_attrs["device_id"] = workstation_name
|
||||||
|
|
||||||
|
G.add_node(node_id, template=f"{LAB_NAME}-host_node-create_resource", **labware_attrs)
|
||||||
|
resource_last_writer[labware_id] = f"{node_id}:labware"
|
||||||
|
|
||||||
|
# 处理协议步骤
|
||||||
|
prev_node = None
|
||||||
|
for i, step in enumerate(protocol_steps):
|
||||||
|
node_id = str(uuid.uuid4())
|
||||||
|
G.add_node(node_id, **step)
|
||||||
|
|
||||||
|
# 添加控制流边
|
||||||
|
if prev_node is not None:
|
||||||
|
G.add_edge(prev_node, node_id, source_port="ready", target_port="ready")
|
||||||
|
prev_node = node_id
|
||||||
|
|
||||||
|
# 处理物料流
|
||||||
|
params = step.get("parameters", {})
|
||||||
|
if "sources" in params and params["sources"] in resource_last_writer:
|
||||||
|
source_node, source_port = resource_last_writer[params["sources"]].split(":")
|
||||||
|
G.add_edge(source_node, node_id, source_port=source_port, target_port="labware")
|
||||||
|
|
||||||
|
if "targets" in params:
|
||||||
|
resource_last_writer[params["targets"]] = f"{node_id}:labware"
|
||||||
|
|
||||||
|
# 添加协议结束节点
|
||||||
|
end_id = str(uuid.uuid4())
|
||||||
|
G.add_node(end_id, template=f"{LAB_NAME}-liquid_handler.biomek-run_protocol")
|
||||||
|
if prev_node is not None:
|
||||||
|
G.add_edge(prev_node, end_id, source_port="ready", target_port="ready")
|
||||||
|
|
||||||
|
else:
|
||||||
|
# 有机化学协议图构建
|
||||||
|
WORKSTATION_ID = workstation_name
|
||||||
|
|
||||||
|
# 为所有labware创建资源节点
|
||||||
|
for item_id, item in labware_info.items():
|
||||||
|
# item_id = item.get("id") or item.get("name", f"item_{uuid.uuid4()}")
|
||||||
|
node_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
# 判断节点类型
|
||||||
|
if item.get("type") == "hardware" or "reactor" in str(item_id).lower():
|
||||||
|
if "reactor" not in str(item_id).lower():
|
||||||
|
continue
|
||||||
|
lab_node_type = "Sample"
|
||||||
|
description = f"Prepare Reactor: {item_id}"
|
||||||
|
liquid_type = []
|
||||||
|
liquid_volume = []
|
||||||
|
else:
|
||||||
|
lab_node_type = "Reagent"
|
||||||
|
description = f"Add Reagent to Flask: {item_id}"
|
||||||
|
liquid_type = [item_id]
|
||||||
|
liquid_volume = [1e5]
|
||||||
|
|
||||||
|
G.add_node(
|
||||||
|
node_id,
|
||||||
|
template=f"{LAB_NAME}-host_node-create_resource",
|
||||||
|
description=description,
|
||||||
|
lab_node_type=lab_node_type,
|
||||||
|
res_id=item_id,
|
||||||
|
device_id=WORKSTATION_ID,
|
||||||
|
class_name="container",
|
||||||
|
parent=WORKSTATION_ID,
|
||||||
|
bind_locations={"x": 0.0, "y": 0.0, "z": 0.0},
|
||||||
|
liquid_input_slot=[-1],
|
||||||
|
liquid_type=liquid_type,
|
||||||
|
liquid_volume=liquid_volume,
|
||||||
|
slot_on_deck="",
|
||||||
|
role=item.get("role", ""),
|
||||||
|
)
|
||||||
|
resource_last_writer[item_id] = f"{node_id}:labware"
|
||||||
|
|
||||||
|
last_control_node_id = None
|
||||||
|
|
||||||
|
# 处理协议步骤
|
||||||
|
for step in protocol_steps:
|
||||||
|
node_id = str(uuid.uuid4())
|
||||||
|
G.add_node(node_id, **step)
|
||||||
|
|
||||||
|
# 控制流
|
||||||
|
if last_control_node_id is not None:
|
||||||
|
G.add_edge(last_control_node_id, node_id, source_port="ready", target_port="ready")
|
||||||
|
last_control_node_id = node_id
|
||||||
|
|
||||||
|
# 物料流
|
||||||
|
params = step.get("parameters", {})
|
||||||
|
input_resources = {
|
||||||
|
"Vessel": params.get("vessel"),
|
||||||
|
"ToVessel": params.get("to_vessel"),
|
||||||
|
"FromVessel": params.get("from_vessel"),
|
||||||
|
"reagent": params.get("reagent"),
|
||||||
|
"solvent": params.get("solvent"),
|
||||||
|
"compound": params.get("compound"),
|
||||||
|
"sources": params.get("sources"),
|
||||||
|
"targets": params.get("targets"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for target_port, resource_name in input_resources.items():
|
||||||
|
if resource_name and resource_name in resource_last_writer:
|
||||||
|
source_node, source_port = resource_last_writer[resource_name].split(":")
|
||||||
|
G.add_edge(source_node, node_id, source_port=source_port, target_port=target_port)
|
||||||
|
|
||||||
|
output_resources = {
|
||||||
|
"VesselOut": params.get("vessel"),
|
||||||
|
"FromVesselOut": params.get("from_vessel"),
|
||||||
|
"ToVesselOut": params.get("to_vessel"),
|
||||||
|
"FiltrateOut": params.get("filtrate_vessel"),
|
||||||
|
"reagent": params.get("reagent"),
|
||||||
|
"solvent": params.get("solvent"),
|
||||||
|
"compound": params.get("compound"),
|
||||||
|
"sources_out": params.get("sources"),
|
||||||
|
"targets_out": params.get("targets"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for source_port, resource_name in output_resources.items():
|
||||||
|
if resource_name:
|
||||||
|
resource_last_writer[resource_name] = f"{node_id}:{source_port}"
|
||||||
|
|
||||||
|
return G
|
||||||
|
|
||||||
|
|
||||||
|
def draw_protocol_graph(protocol_graph: SimpleGraph, output_path: str):
|
||||||
|
"""
|
||||||
|
(辅助功能) 使用 networkx 和 matplotlib 绘制协议工作流图,用于可视化。
|
||||||
|
"""
|
||||||
|
if not protocol_graph:
|
||||||
|
print("Cannot draw graph: Graph object is empty.")
|
||||||
|
return
|
||||||
|
|
||||||
|
G = nx.DiGraph()
|
||||||
|
|
||||||
|
for node_id, attrs in protocol_graph.nodes.items():
|
||||||
|
label = attrs.get("description", attrs.get("template", node_id[:8]))
|
||||||
|
G.add_node(node_id, label=label, **attrs)
|
||||||
|
|
||||||
|
for edge in protocol_graph.edges:
|
||||||
|
G.add_edge(edge["source"], edge["target"])
|
||||||
|
|
||||||
|
plt.figure(figsize=(20, 15))
|
||||||
|
try:
|
||||||
|
pos = nx.nx_agraph.graphviz_layout(G, prog="dot")
|
||||||
|
except Exception:
|
||||||
|
pos = nx.shell_layout(G) # Fallback layout
|
||||||
|
|
||||||
|
node_labels = {node: data["label"] for node, data in G.nodes(data=True)}
|
||||||
|
nx.draw(
|
||||||
|
G,
|
||||||
|
pos,
|
||||||
|
with_labels=False,
|
||||||
|
node_size=2500,
|
||||||
|
node_color="skyblue",
|
||||||
|
node_shape="o",
|
||||||
|
edge_color="gray",
|
||||||
|
width=1.5,
|
||||||
|
arrowsize=15,
|
||||||
|
)
|
||||||
|
nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=8, font_weight="bold")
|
||||||
|
|
||||||
|
plt.title("Chemical Protocol Workflow Graph", size=15)
|
||||||
|
plt.savefig(output_path, dpi=300, bbox_inches="tight")
|
||||||
|
plt.close()
|
||||||
|
print(f" - Visualization saved to '{output_path}'")
|
||||||
|
|
||||||
|
|
||||||
|
from networkx.drawing.nx_agraph import to_agraph
|
||||||
|
import re
|
||||||
|
|
||||||
|
COMPASS = {"n","e","s","w","ne","nw","se","sw","c"}
|
||||||
|
|
||||||
|
def _is_compass(port: str) -> bool:
|
||||||
|
return isinstance(port, str) and port.lower() in COMPASS
|
||||||
|
|
||||||
|
def draw_protocol_graph_with_ports(protocol_graph, output_path: str, rankdir: str = "LR"):
|
||||||
|
"""
|
||||||
|
使用 Graphviz 端口语法绘制协议工作流图。
|
||||||
|
- 若边上的 source_port/target_port 是 compass(n/e/s/w/...),直接用 compass。
|
||||||
|
- 否则自动为节点创建 record 形状并定义命名端口 <portname>。
|
||||||
|
最终由 PyGraphviz 渲染并输出到 output_path(后缀决定格式,如 .png/.svg/.pdf)。
|
||||||
|
"""
|
||||||
|
if not protocol_graph:
|
||||||
|
print("Cannot draw graph: Graph object is empty.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 1) 先用 networkx 搭建有向图,保留端口属性
|
||||||
|
G = nx.DiGraph()
|
||||||
|
for node_id, attrs in protocol_graph.nodes.items():
|
||||||
|
label = attrs.get("description", attrs.get("template", node_id[:8]))
|
||||||
|
# 保留一个干净的“中心标签”,用于放在 record 的中间槽
|
||||||
|
G.add_node(node_id, _core_label=str(label), **{k:v for k,v in attrs.items() if k not in ("label",)})
|
||||||
|
|
||||||
|
edges_data = []
|
||||||
|
in_ports_by_node = {} # 收集命名输入端口
|
||||||
|
out_ports_by_node = {} # 收集命名输出端口
|
||||||
|
|
||||||
|
for edge in protocol_graph.edges:
|
||||||
|
u = edge["source"]
|
||||||
|
v = edge["target"]
|
||||||
|
sp = edge.get("source_port")
|
||||||
|
tp = edge.get("target_port")
|
||||||
|
|
||||||
|
# 记录到图里(保留原始端口信息)
|
||||||
|
G.add_edge(u, v, source_port=sp, target_port=tp)
|
||||||
|
edges_data.append((u, v, sp, tp))
|
||||||
|
|
||||||
|
# 如果不是 compass,就按“命名端口”先归类,等会儿给节点造 record
|
||||||
|
if sp and not _is_compass(sp):
|
||||||
|
out_ports_by_node.setdefault(u, set()).add(str(sp))
|
||||||
|
if tp and not _is_compass(tp):
|
||||||
|
in_ports_by_node.setdefault(v, set()).add(str(tp))
|
||||||
|
|
||||||
|
# 2) 转为 AGraph,使用 Graphviz 渲染
|
||||||
|
A = to_agraph(G)
|
||||||
|
A.graph_attr.update(rankdir=rankdir, splines="true", concentrate="false", fontsize="10")
|
||||||
|
A.node_attr.update(shape="box", style="rounded,filled", fillcolor="lightyellow", color="#999999", fontname="Helvetica")
|
||||||
|
A.edge_attr.update(arrowsize="0.8", color="#666666")
|
||||||
|
|
||||||
|
# 3) 为需要命名端口的节点设置 record 形状与 label
|
||||||
|
# 左列 = 输入端口;中间 = 核心标签;右列 = 输出端口
|
||||||
|
for n in A.nodes():
|
||||||
|
node = A.get_node(n)
|
||||||
|
core = G.nodes[n].get("_core_label", n)
|
||||||
|
|
||||||
|
in_ports = sorted(in_ports_by_node.get(n, []))
|
||||||
|
out_ports = sorted(out_ports_by_node.get(n, []))
|
||||||
|
|
||||||
|
# 如果该节点涉及命名端口,则用 record;否则保留原 box
|
||||||
|
if in_ports or out_ports:
|
||||||
|
def port_fields(ports):
|
||||||
|
if not ports:
|
||||||
|
return " " # 必须留一个空槽占位
|
||||||
|
# 每个端口一个小格子,<p> name
|
||||||
|
return "|".join(f"<{re.sub(r'[^A-Za-z0-9_:.|-]', '_', p)}> {p}" for p in ports)
|
||||||
|
|
||||||
|
left = port_fields(in_ports)
|
||||||
|
right = port_fields(out_ports)
|
||||||
|
|
||||||
|
# 三栏:左(入) | 中(节点名) | 右(出)
|
||||||
|
record_label = f"{{ {left} | {core} | {right} }}"
|
||||||
|
node.attr.update(shape="record", label=record_label)
|
||||||
|
else:
|
||||||
|
# 没有命名端口:普通盒子,显示核心标签
|
||||||
|
node.attr.update(label=str(core))
|
||||||
|
|
||||||
|
# 4) 给边设置 headport / tailport
|
||||||
|
# - 若端口为 compass:直接用 compass(e.g., headport="e")
|
||||||
|
# - 若端口为命名端口:使用在 record 中定义的 <port> 名(同名即可)
|
||||||
|
for (u, v, sp, tp) in edges_data:
|
||||||
|
e = A.get_edge(u, v)
|
||||||
|
|
||||||
|
# Graphviz 属性:tail 是源,head 是目标
|
||||||
|
if sp:
|
||||||
|
if _is_compass(sp):
|
||||||
|
e.attr["tailport"] = sp.lower()
|
||||||
|
else:
|
||||||
|
# 与 record label 中 <port> 名一致;特殊字符已在 label 中做了清洗
|
||||||
|
e.attr["tailport"] = re.sub(r'[^A-Za-z0-9_:.|-]', '_', str(sp))
|
||||||
|
|
||||||
|
if tp:
|
||||||
|
if _is_compass(tp):
|
||||||
|
e.attr["headport"] = tp.lower()
|
||||||
|
else:
|
||||||
|
e.attr["headport"] = re.sub(r'[^A-Za-z0-9_:.|-]', '_', str(tp))
|
||||||
|
|
||||||
|
# 可选:若想让边更贴边缘,可设置 constraint/spline 等
|
||||||
|
# e.attr["arrowhead"] = "vee"
|
||||||
|
|
||||||
|
# 5) 输出
|
||||||
|
A.draw(output_path, prog="dot")
|
||||||
|
print(f" - Port-aware workflow rendered to '{output_path}'")
|
||||||
|
|
||||||
|
|
||||||
|
def flatten_xdl_procedure(procedure_elem: ET.Element) -> List[ET.Element]:
|
||||||
|
"""展平嵌套的XDL程序结构"""
|
||||||
|
flattened_operations = []
|
||||||
|
TEMP_UNSUPPORTED_PROTOCOL = ["Purge", "Wait", "Stir", "ResetHandling"]
|
||||||
|
|
||||||
|
def extract_operations(element: ET.Element):
|
||||||
|
if element.tag not in ["Prep", "Reaction", "Workup", "Purification", "Procedure"]:
|
||||||
|
if element.tag not in TEMP_UNSUPPORTED_PROTOCOL:
|
||||||
|
flattened_operations.append(element)
|
||||||
|
|
||||||
|
for child in element:
|
||||||
|
extract_operations(child)
|
||||||
|
|
||||||
|
for child in procedure_elem:
|
||||||
|
extract_operations(child)
|
||||||
|
|
||||||
|
return flattened_operations
|
||||||
|
|
||||||
|
|
||||||
|
def parse_xdl_content(xdl_content: str) -> tuple:
|
||||||
|
"""解析XDL内容"""
|
||||||
|
try:
|
||||||
|
xdl_content_cleaned = "".join(c for c in xdl_content if c.isprintable())
|
||||||
|
root = ET.fromstring(xdl_content_cleaned)
|
||||||
|
|
||||||
|
synthesis_elem = root.find("Synthesis")
|
||||||
|
if synthesis_elem is None:
|
||||||
|
return None, None, None
|
||||||
|
|
||||||
|
# 解析硬件组件
|
||||||
|
hardware_elem = synthesis_elem.find("Hardware")
|
||||||
|
hardware = []
|
||||||
|
if hardware_elem is not None:
|
||||||
|
hardware = [{"id": c.get("id"), "type": c.get("type")} for c in hardware_elem.findall("Component")]
|
||||||
|
|
||||||
|
# 解析试剂
|
||||||
|
reagents_elem = synthesis_elem.find("Reagents")
|
||||||
|
reagents = []
|
||||||
|
if reagents_elem is not None:
|
||||||
|
reagents = [{"name": r.get("name"), "role": r.get("role", "")} for r in reagents_elem.findall("Reagent")]
|
||||||
|
|
||||||
|
# 解析程序
|
||||||
|
procedure_elem = synthesis_elem.find("Procedure")
|
||||||
|
if procedure_elem is None:
|
||||||
|
return None, None, None
|
||||||
|
|
||||||
|
flattened_operations = flatten_xdl_procedure(procedure_elem)
|
||||||
|
return hardware, reagents, flattened_operations
|
||||||
|
|
||||||
|
except ET.ParseError as e:
|
||||||
|
raise ValueError(f"Invalid XDL format: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def convert_xdl_to_dict(xdl_content: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
将XDL XML格式转换为标准的字典格式
|
||||||
|
|
||||||
|
Args:
|
||||||
|
xdl_content: XDL XML内容
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
转换结果,包含步骤和器材信息
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
hardware, reagents, flattened_operations = parse_xdl_content(xdl_content)
|
||||||
|
if hardware is None:
|
||||||
|
return {"error": "Failed to parse XDL content", "success": False}
|
||||||
|
|
||||||
|
# 将XDL元素转换为字典格式
|
||||||
|
steps_data = []
|
||||||
|
for elem in flattened_operations:
|
||||||
|
# 转换参数类型
|
||||||
|
parameters = {}
|
||||||
|
for key, val in elem.attrib.items():
|
||||||
|
converted_val = convert_to_type(val)
|
||||||
|
if converted_val is not None:
|
||||||
|
parameters[key] = converted_val
|
||||||
|
|
||||||
|
step_dict = {
|
||||||
|
"operation": elem.tag,
|
||||||
|
"parameters": parameters,
|
||||||
|
"description": elem.get("purpose", f"Operation: {elem.tag}"),
|
||||||
|
}
|
||||||
|
steps_data.append(step_dict)
|
||||||
|
|
||||||
|
# 合并硬件和试剂为统一的labware_info格式
|
||||||
|
labware_data = []
|
||||||
|
labware_data.extend({"id": hw["id"], "type": "hardware", **hw} for hw in hardware)
|
||||||
|
labware_data.extend({"name": reagent["name"], "type": "reagent", **reagent} for reagent in reagents)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"steps": steps_data,
|
||||||
|
"labware": labware_data,
|
||||||
|
"message": f"Successfully converted XDL to dict format. Found {len(steps_data)} steps and {len(labware_data)} labware items.",
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"XDL conversion failed: {str(e)}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return {"error": error_msg, "success": False}
|
||||||
|
|
||||||
|
|
||||||
def create_workflow(
|
def create_workflow(
|
||||||
|
|||||||
@@ -439,7 +439,7 @@ class MessageProcessor:
|
|||||||
self.connected = True
|
self.connected = True
|
||||||
self.reconnect_count = 0
|
self.reconnect_count = 0
|
||||||
|
|
||||||
logger.trace(f"[MessageProcessor] Connected to {self.websocket_url}")
|
logger.info(f"[MessageProcessor] Connected to {self.websocket_url}")
|
||||||
|
|
||||||
# 启动发送协程
|
# 启动发送协程
|
||||||
send_task = asyncio.create_task(self._send_handler())
|
send_task = asyncio.create_task(self._send_handler())
|
||||||
@@ -517,7 +517,7 @@ class MessageProcessor:
|
|||||||
|
|
||||||
async def _send_handler(self):
|
async def _send_handler(self):
|
||||||
"""处理发送队列中的消息"""
|
"""处理发送队列中的消息"""
|
||||||
logger.trace("[MessageProcessor] Send handler started")
|
logger.debug("[MessageProcessor] Send handler started")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while self.connected and self.websocket:
|
while self.connected and self.websocket:
|
||||||
@@ -1026,7 +1026,7 @@ class QueueProcessor:
|
|||||||
|
|
||||||
def _run(self):
|
def _run(self):
|
||||||
"""运行队列处理主循环"""
|
"""运行队列处理主循环"""
|
||||||
logger.trace("[QueueProcessor] Queue processor started")
|
logger.debug("[QueueProcessor] Queue processor started")
|
||||||
|
|
||||||
while self.is_running:
|
while self.is_running:
|
||||||
try:
|
try:
|
||||||
@@ -1236,6 +1236,7 @@ class WebSocketClient(BaseCommunicationClient):
|
|||||||
else:
|
else:
|
||||||
url = f"{scheme}://{parsed.netloc}/api/v1/ws/schedule"
|
url = f"{scheme}://{parsed.netloc}/api/v1/ws/schedule"
|
||||||
|
|
||||||
|
logger.debug(f"[WebSocketClient] URL: {url}")
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
@@ -1248,11 +1249,13 @@ class WebSocketClient(BaseCommunicationClient):
|
|||||||
logger.error("[WebSocketClient] WebSocket URL not configured")
|
logger.error("[WebSocketClient] WebSocket URL not configured")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
logger.info(f"[WebSocketClient] Starting connection to {self.websocket_url}")
|
||||||
|
|
||||||
# 启动两个核心线程
|
# 启动两个核心线程
|
||||||
self.message_processor.start()
|
self.message_processor.start()
|
||||||
self.queue_processor.start()
|
self.queue_processor.start()
|
||||||
|
|
||||||
logger.trace("[WebSocketClient] All threads started")
|
logger.info("[WebSocketClient] All threads started")
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
"""停止WebSocket客户端"""
|
"""停止WebSocket客户端"""
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import inspect
|
import inspect
|
||||||
import importlib
|
import importlib
|
||||||
import threading
|
|
||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Union, Tuple
|
from typing import Any, Dict, List, Union, Tuple
|
||||||
|
|
||||||
@@ -62,7 +60,6 @@ class Registry:
|
|||||||
self.device_module_to_registry = {}
|
self.device_module_to_registry = {}
|
||||||
self.resource_type_registry = {}
|
self.resource_type_registry = {}
|
||||||
self._setup_called = False # 跟踪setup是否已调用
|
self._setup_called = False # 跟踪setup是否已调用
|
||||||
self._registry_lock = threading.Lock() # 多线程加载时的锁
|
|
||||||
# 其他状态变量
|
# 其他状态变量
|
||||||
# self.is_host_mode = False # 移至BasicConfig中
|
# self.is_host_mode = False # 移至BasicConfig中
|
||||||
|
|
||||||
@@ -180,7 +177,8 @@ class Registry:
|
|||||||
"result": {},
|
"result": {},
|
||||||
"schema": test_latency_schema,
|
"schema": test_latency_schema,
|
||||||
"goal_default": {
|
"goal_default": {
|
||||||
arg["name"]: arg["default"] for arg in test_latency_method_info.get("args", [])
|
arg["name"]: arg["default"]
|
||||||
|
for arg in test_latency_method_info.get("args", [])
|
||||||
},
|
},
|
||||||
"handles": {},
|
"handles": {},
|
||||||
},
|
},
|
||||||
@@ -264,115 +262,67 @@ class Registry:
|
|||||||
# 标记setup已被调用
|
# 标记setup已被调用
|
||||||
self._setup_called = True
|
self._setup_called = True
|
||||||
|
|
||||||
def _load_single_resource_file(
|
|
||||||
self, file: Path, complete_registry: bool, upload_registry: bool
|
|
||||||
) -> Tuple[Dict[str, Any], Dict[str, Any], bool]:
|
|
||||||
"""
|
|
||||||
加载单个资源文件 (线程安全)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
(data, complete_data, is_valid): 资源数据, 完整数据, 是否有效
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
with open(file, encoding="utf-8", mode="r") as f:
|
|
||||||
data = yaml.safe_load(io.StringIO(f.read()))
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"[UniLab Registry] 读取资源文件失败: {file}, 错误: {e}")
|
|
||||||
return {}, {}, False
|
|
||||||
|
|
||||||
if not data:
|
|
||||||
return {}, {}, False
|
|
||||||
|
|
||||||
complete_data = {}
|
|
||||||
for resource_id, resource_info in data.items():
|
|
||||||
if "version" not in resource_info:
|
|
||||||
resource_info["version"] = "1.0.0"
|
|
||||||
if "category" not in resource_info:
|
|
||||||
resource_info["category"] = [file.stem]
|
|
||||||
elif file.stem not in resource_info["category"]:
|
|
||||||
resource_info["category"].append(file.stem)
|
|
||||||
elif not isinstance(resource_info.get("category"), list):
|
|
||||||
resource_info["category"] = [resource_info["category"]]
|
|
||||||
if "config_info" not in resource_info:
|
|
||||||
resource_info["config_info"] = []
|
|
||||||
if "icon" not in resource_info:
|
|
||||||
resource_info["icon"] = ""
|
|
||||||
if "handles" not in resource_info:
|
|
||||||
resource_info["handles"] = []
|
|
||||||
if "init_param_schema" not in resource_info:
|
|
||||||
resource_info["init_param_schema"] = {}
|
|
||||||
if "config_info" in resource_info:
|
|
||||||
del resource_info["config_info"]
|
|
||||||
if "file_path" in resource_info:
|
|
||||||
del resource_info["file_path"]
|
|
||||||
complete_data[resource_id] = copy.deepcopy(dict(sorted(resource_info.items())))
|
|
||||||
if upload_registry:
|
|
||||||
class_info = resource_info.get("class", {})
|
|
||||||
if len(class_info) and "module" in class_info:
|
|
||||||
if class_info.get("type") == "pylabrobot":
|
|
||||||
res_class = get_class(class_info["module"])
|
|
||||||
if callable(res_class) and not isinstance(res_class, type):
|
|
||||||
res_instance = res_class(res_class.__name__)
|
|
||||||
res_ulr = tree_to_list([resource_plr_to_ulab(res_instance)])
|
|
||||||
resource_info["config_info"] = res_ulr
|
|
||||||
resource_info["registry_type"] = "resource"
|
|
||||||
resource_info["file_path"] = str(file.absolute()).replace("\\", "/")
|
|
||||||
|
|
||||||
complete_data = dict(sorted(complete_data.items()))
|
|
||||||
complete_data = copy.deepcopy(complete_data)
|
|
||||||
|
|
||||||
if complete_registry:
|
|
||||||
try:
|
|
||||||
with open(file, "w", encoding="utf-8") as f:
|
|
||||||
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"[UniLab Registry] 写入资源文件失败: {file}, 错误: {e}")
|
|
||||||
|
|
||||||
return data, complete_data, True
|
|
||||||
|
|
||||||
def load_resource_types(self, path: os.PathLike, complete_registry: bool, upload_registry: bool):
|
def load_resource_types(self, path: os.PathLike, complete_registry: bool, upload_registry: bool):
|
||||||
abs_path = Path(path).absolute()
|
abs_path = Path(path).absolute()
|
||||||
resource_path = abs_path / "resources"
|
resource_path = abs_path / "resources"
|
||||||
files = list(resource_path.glob("*/*.yaml"))
|
files = list(resource_path.glob("*/*.yaml"))
|
||||||
logger.debug(f"[UniLab Registry] resources: {resource_path.exists()}, total: {len(files)}")
|
logger.debug(f"[UniLab Registry] resources: {resource_path.exists()}, total: {len(files)}")
|
||||||
|
|
||||||
if not files:
|
|
||||||
return
|
|
||||||
|
|
||||||
# 使用线程池并行加载
|
|
||||||
max_workers = min(8, len(files))
|
|
||||||
results = []
|
|
||||||
|
|
||||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
|
||||||
future_to_file = {
|
|
||||||
executor.submit(self._load_single_resource_file, file, complete_registry, upload_registry): file
|
|
||||||
for file in files
|
|
||||||
}
|
|
||||||
for future in as_completed(future_to_file):
|
|
||||||
file = future_to_file[future]
|
|
||||||
try:
|
|
||||||
data, complete_data, is_valid = future.result()
|
|
||||||
if is_valid:
|
|
||||||
results.append((file, data))
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"[UniLab Registry] 处理资源文件异常: {file}, 错误: {e}")
|
|
||||||
|
|
||||||
# 线程安全地更新注册表
|
|
||||||
current_resource_number = len(self.resource_type_registry) + 1
|
current_resource_number = len(self.resource_type_registry) + 1
|
||||||
with self._registry_lock:
|
for i, file in enumerate(files):
|
||||||
for i, (file, data) in enumerate(results):
|
with open(file, encoding="utf-8", mode="r") as f:
|
||||||
|
data = yaml.safe_load(io.StringIO(f.read()))
|
||||||
|
complete_data = {}
|
||||||
|
if data:
|
||||||
|
# 为每个资源添加文件路径信息
|
||||||
|
for resource_id, resource_info in data.items():
|
||||||
|
if "version" not in resource_info:
|
||||||
|
resource_info["version"] = "1.0.0"
|
||||||
|
if "category" not in resource_info:
|
||||||
|
resource_info["category"] = [file.stem]
|
||||||
|
elif file.stem not in resource_info["category"]:
|
||||||
|
resource_info["category"].append(file.stem)
|
||||||
|
elif not isinstance(resource_info.get("category"), list):
|
||||||
|
resource_info["category"] = [resource_info["category"]]
|
||||||
|
if "config_info" not in resource_info:
|
||||||
|
resource_info["config_info"] = []
|
||||||
|
if "icon" not in resource_info:
|
||||||
|
resource_info["icon"] = ""
|
||||||
|
if "handles" not in resource_info:
|
||||||
|
resource_info["handles"] = []
|
||||||
|
if "init_param_schema" not in resource_info:
|
||||||
|
resource_info["init_param_schema"] = {}
|
||||||
|
if "config_info" in resource_info:
|
||||||
|
del resource_info["config_info"]
|
||||||
|
if "file_path" in resource_info:
|
||||||
|
del resource_info["file_path"]
|
||||||
|
complete_data[resource_id] = copy.deepcopy(dict(sorted(resource_info.items())))
|
||||||
|
if upload_registry:
|
||||||
|
class_info = resource_info.get("class", {})
|
||||||
|
if len(class_info) and "module" in class_info:
|
||||||
|
if class_info.get("type") == "pylabrobot":
|
||||||
|
res_class = get_class(class_info["module"])
|
||||||
|
if callable(res_class) and not isinstance(
|
||||||
|
res_class, type
|
||||||
|
): # 有的是类,有的是函数,这里暂时只登记函数类的
|
||||||
|
res_instance = res_class(res_class.__name__)
|
||||||
|
res_ulr = tree_to_list([resource_plr_to_ulab(res_instance)])
|
||||||
|
resource_info["config_info"] = res_ulr
|
||||||
|
resource_info["registry_type"] = "resource"
|
||||||
|
resource_info["file_path"] = str(file.absolute()).replace("\\", "/")
|
||||||
|
complete_data = dict(sorted(complete_data.items()))
|
||||||
|
complete_data = copy.deepcopy(complete_data)
|
||||||
|
if complete_registry:
|
||||||
|
with open(file, "w", encoding="utf-8") as f:
|
||||||
|
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
||||||
|
|
||||||
self.resource_type_registry.update(data)
|
self.resource_type_registry.update(data)
|
||||||
logger.trace(
|
logger.trace( # type: ignore
|
||||||
f"[UniLab Registry] Resource-{current_resource_number} File-{i+1}/{len(results)} "
|
f"[UniLab Registry] Resource-{current_resource_number} File-{i+1}/{len(files)} "
|
||||||
+ f"Add {list(data.keys())}"
|
+ f"Add {list(data.keys())}"
|
||||||
)
|
)
|
||||||
current_resource_number += 1
|
current_resource_number += 1
|
||||||
|
else:
|
||||||
# 记录无效文件
|
logger.debug(f"[UniLab Registry] Res File-{i+1}/{len(files)} Not Valid YAML File: {file.absolute()}")
|
||||||
valid_files = {r[0] for r in results}
|
|
||||||
for file in files:
|
|
||||||
if file not in valid_files:
|
|
||||||
logger.debug(f"[UniLab Registry] Res File Not Valid YAML File: {file.absolute()}")
|
|
||||||
|
|
||||||
def _extract_class_docstrings(self, module_string: str) -> Dict[str, str]:
|
def _extract_class_docstrings(self, module_string: str) -> Dict[str, str]:
|
||||||
"""
|
"""
|
||||||
@@ -724,244 +674,213 @@ class Registry:
|
|||||||
"handles": {},
|
"handles": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
def _load_single_device_file(
|
|
||||||
self, file: Path, complete_registry: bool, get_yaml_from_goal_type
|
|
||||||
) -> Tuple[Dict[str, Any], Dict[str, Any], bool, List[str]]:
|
|
||||||
"""
|
|
||||||
加载单个设备文件 (线程安全)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
(data, complete_data, is_valid, device_ids): 设备数据, 完整数据, 是否有效, 设备ID列表
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
with open(file, encoding="utf-8", mode="r") as f:
|
|
||||||
data = yaml.safe_load(io.StringIO(f.read()))
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"[UniLab Registry] 读取设备文件失败: {file}, 错误: {e}")
|
|
||||||
return {}, {}, False, []
|
|
||||||
|
|
||||||
if not data:
|
|
||||||
return {}, {}, False, []
|
|
||||||
|
|
||||||
complete_data = {}
|
|
||||||
action_str_type_mapping = {
|
|
||||||
"UniLabJsonCommand": "UniLabJsonCommand",
|
|
||||||
"UniLabJsonCommandAsync": "UniLabJsonCommandAsync",
|
|
||||||
}
|
|
||||||
status_str_type_mapping = {}
|
|
||||||
device_ids = []
|
|
||||||
|
|
||||||
for device_id, device_config in data.items():
|
|
||||||
if "version" not in device_config:
|
|
||||||
device_config["version"] = "1.0.0"
|
|
||||||
if "category" not in device_config:
|
|
||||||
device_config["category"] = [file.stem]
|
|
||||||
elif file.stem not in device_config["category"]:
|
|
||||||
device_config["category"].append(file.stem)
|
|
||||||
if "config_info" not in device_config:
|
|
||||||
device_config["config_info"] = []
|
|
||||||
if "description" not in device_config:
|
|
||||||
device_config["description"] = ""
|
|
||||||
if "icon" not in device_config:
|
|
||||||
device_config["icon"] = ""
|
|
||||||
if "handles" not in device_config:
|
|
||||||
device_config["handles"] = []
|
|
||||||
if "init_param_schema" not in device_config:
|
|
||||||
device_config["init_param_schema"] = {}
|
|
||||||
if "class" in device_config:
|
|
||||||
if "status_types" not in device_config["class"] or device_config["class"]["status_types"] is None:
|
|
||||||
device_config["class"]["status_types"] = {}
|
|
||||||
if (
|
|
||||||
"action_value_mappings" not in device_config["class"]
|
|
||||||
or device_config["class"]["action_value_mappings"] is None
|
|
||||||
):
|
|
||||||
device_config["class"]["action_value_mappings"] = {}
|
|
||||||
enhanced_info = {}
|
|
||||||
if complete_registry:
|
|
||||||
device_config["class"]["status_types"].clear()
|
|
||||||
enhanced_info = get_enhanced_class_info(device_config["class"]["module"], use_dynamic=True)
|
|
||||||
if not enhanced_info.get("dynamic_import_success", False):
|
|
||||||
continue
|
|
||||||
device_config["class"]["status_types"].update(
|
|
||||||
{k: v["return_type"] for k, v in enhanced_info["status_methods"].items()}
|
|
||||||
)
|
|
||||||
for status_name, status_type in device_config["class"]["status_types"].items():
|
|
||||||
if isinstance(status_type, tuple) or status_type in ["Any", "None", "Unknown"]:
|
|
||||||
status_type = "String"
|
|
||||||
device_config["class"]["status_types"][status_name] = status_type
|
|
||||||
try:
|
|
||||||
target_type = self._replace_type_with_class(status_type, device_id, f"状态 {status_name}")
|
|
||||||
except ROSMsgNotFound:
|
|
||||||
continue
|
|
||||||
if target_type in [dict, list]:
|
|
||||||
target_type = String
|
|
||||||
status_str_type_mapping[status_type] = target_type
|
|
||||||
device_config["class"]["status_types"] = dict(sorted(device_config["class"]["status_types"].items()))
|
|
||||||
if complete_registry:
|
|
||||||
old_action_configs = {}
|
|
||||||
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
|
||||||
old_action_configs[action_name] = action_config
|
|
||||||
|
|
||||||
device_config["class"]["action_value_mappings"] = {
|
|
||||||
k: v
|
|
||||||
for k, v in device_config["class"]["action_value_mappings"].items()
|
|
||||||
if not k.startswith("auto-")
|
|
||||||
}
|
|
||||||
device_config["class"]["action_value_mappings"].update(
|
|
||||||
{
|
|
||||||
f"auto-{k}": {
|
|
||||||
"type": "UniLabJsonCommandAsync" if v["is_async"] else "UniLabJsonCommand",
|
|
||||||
"goal": {},
|
|
||||||
"feedback": {},
|
|
||||||
"result": {},
|
|
||||||
"schema": self._generate_unilab_json_command_schema(
|
|
||||||
v["args"],
|
|
||||||
k,
|
|
||||||
v.get("return_annotation"),
|
|
||||||
old_action_configs.get(f"auto-{k}", {}).get("schema"),
|
|
||||||
),
|
|
||||||
"goal_default": {i["name"]: i["default"] for i in v["args"]},
|
|
||||||
"handles": old_action_configs.get(f"auto-{k}", {}).get("handles", []),
|
|
||||||
"placeholder_keys": {
|
|
||||||
i["name"]: (
|
|
||||||
"unilabos_resources"
|
|
||||||
if i["type"] == "unilabos.registry.placeholder_type:ResourceSlot"
|
|
||||||
or i["type"] == ("list", "unilabos.registry.placeholder_type:ResourceSlot")
|
|
||||||
else "unilabos_devices"
|
|
||||||
)
|
|
||||||
for i in v["args"]
|
|
||||||
if i.get("type", "")
|
|
||||||
in [
|
|
||||||
"unilabos.registry.placeholder_type:ResourceSlot",
|
|
||||||
"unilabos.registry.placeholder_type:DeviceSlot",
|
|
||||||
("list", "unilabos.registry.placeholder_type:ResourceSlot"),
|
|
||||||
("list", "unilabos.registry.placeholder_type:DeviceSlot"),
|
|
||||||
]
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for k, v in enhanced_info["action_methods"].items()
|
|
||||||
if k not in device_config["class"]["action_value_mappings"]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
for action_name, old_config in old_action_configs.items():
|
|
||||||
if action_name in device_config["class"]["action_value_mappings"]:
|
|
||||||
old_schema = old_config.get("schema", {})
|
|
||||||
if "description" in old_schema and old_schema["description"]:
|
|
||||||
device_config["class"]["action_value_mappings"][action_name]["schema"][
|
|
||||||
"description"
|
|
||||||
] = old_schema["description"]
|
|
||||||
device_config["init_param_schema"] = {}
|
|
||||||
device_config["init_param_schema"]["config"] = self._generate_unilab_json_command_schema(
|
|
||||||
enhanced_info["init_params"], "__init__"
|
|
||||||
)["properties"]["goal"]
|
|
||||||
device_config["init_param_schema"]["data"] = self._generate_status_types_schema(
|
|
||||||
enhanced_info["status_methods"]
|
|
||||||
)
|
|
||||||
|
|
||||||
device_config.pop("schema", None)
|
|
||||||
device_config["class"]["action_value_mappings"] = dict(
|
|
||||||
sorted(device_config["class"]["action_value_mappings"].items())
|
|
||||||
)
|
|
||||||
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
|
||||||
if "handles" not in action_config:
|
|
||||||
action_config["handles"] = {}
|
|
||||||
elif isinstance(action_config["handles"], list):
|
|
||||||
if len(action_config["handles"]):
|
|
||||||
logger.error(f"设备{device_id} {action_name} 的handles配置错误,应该是字典类型")
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
action_config["handles"] = {}
|
|
||||||
if "type" in action_config:
|
|
||||||
action_type_str: str = action_config["type"]
|
|
||||||
if not action_type_str.startswith("UniLabJsonCommand"):
|
|
||||||
try:
|
|
||||||
target_type = self._replace_type_with_class(
|
|
||||||
action_type_str, device_id, f"动作 {action_name}"
|
|
||||||
)
|
|
||||||
except ROSMsgNotFound:
|
|
||||||
continue
|
|
||||||
action_str_type_mapping[action_type_str] = target_type
|
|
||||||
if target_type is not None:
|
|
||||||
action_config["goal_default"] = yaml.safe_load(
|
|
||||||
io.StringIO(get_yaml_from_goal_type(target_type.Goal))
|
|
||||||
)
|
|
||||||
action_config["schema"] = ros_action_to_json_schema(target_type)
|
|
||||||
else:
|
|
||||||
logger.warning(
|
|
||||||
f"[UniLab Registry] 设备 {device_id} 的动作 {action_name} 类型为空,跳过替换"
|
|
||||||
)
|
|
||||||
complete_data[device_id] = copy.deepcopy(dict(sorted(device_config.items())))
|
|
||||||
for status_name, status_type in device_config["class"]["status_types"].items():
|
|
||||||
device_config["class"]["status_types"][status_name] = status_str_type_mapping[status_type]
|
|
||||||
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
|
||||||
if action_config["type"] not in action_str_type_mapping:
|
|
||||||
continue
|
|
||||||
action_config["type"] = action_str_type_mapping[action_config["type"]]
|
|
||||||
self._add_builtin_actions(device_config, device_id)
|
|
||||||
device_config["file_path"] = str(file.absolute()).replace("\\", "/")
|
|
||||||
device_config["registry_type"] = "device"
|
|
||||||
device_ids.append(device_id)
|
|
||||||
|
|
||||||
complete_data = dict(sorted(complete_data.items()))
|
|
||||||
complete_data = copy.deepcopy(complete_data)
|
|
||||||
try:
|
|
||||||
with open(file, "w", encoding="utf-8") as f:
|
|
||||||
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"[UniLab Registry] 写入设备文件失败: {file}, 错误: {e}")
|
|
||||||
|
|
||||||
return data, complete_data, True, device_ids
|
|
||||||
|
|
||||||
def load_device_types(self, path: os.PathLike, complete_registry: bool):
|
def load_device_types(self, path: os.PathLike, complete_registry: bool):
|
||||||
|
# return
|
||||||
abs_path = Path(path).absolute()
|
abs_path = Path(path).absolute()
|
||||||
devices_path = abs_path / "devices"
|
devices_path = abs_path / "devices"
|
||||||
device_comms_path = abs_path / "device_comms"
|
device_comms_path = abs_path / "device_comms"
|
||||||
files = list(devices_path.glob("*.yaml")) + list(device_comms_path.glob("*.yaml"))
|
files = list(devices_path.glob("*.yaml")) + list(device_comms_path.glob("*.yaml"))
|
||||||
logger.trace(
|
logger.trace( # type: ignore
|
||||||
f"[UniLab Registry] devices: {devices_path.exists()}, device_comms: {device_comms_path.exists()}, "
|
f"[UniLab Registry] devices: {devices_path.exists()}, device_comms: {device_comms_path.exists()}, "
|
||||||
+ f"total: {len(files)}"
|
+ f"total: {len(files)}"
|
||||||
)
|
)
|
||||||
|
current_device_number = len(self.device_type_registry) + 1
|
||||||
if not files:
|
|
||||||
return
|
|
||||||
|
|
||||||
from unilabos.app.web.utils.action_utils import get_yaml_from_goal_type
|
from unilabos.app.web.utils.action_utils import get_yaml_from_goal_type
|
||||||
|
|
||||||
# 使用线程池并行加载
|
for i, file in enumerate(files):
|
||||||
max_workers = min(8, len(files))
|
with open(file, encoding="utf-8", mode="r") as f:
|
||||||
results = []
|
data = yaml.safe_load(io.StringIO(f.read()))
|
||||||
|
complete_data = {}
|
||||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
action_str_type_mapping = {
|
||||||
future_to_file = {
|
"UniLabJsonCommand": "UniLabJsonCommand",
|
||||||
executor.submit(self._load_single_device_file, file, complete_registry, get_yaml_from_goal_type): file
|
"UniLabJsonCommandAsync": "UniLabJsonCommandAsync",
|
||||||
for file in files
|
|
||||||
}
|
}
|
||||||
for future in as_completed(future_to_file):
|
status_str_type_mapping = {}
|
||||||
file = future_to_file[future]
|
if data:
|
||||||
try:
|
# 在添加到注册表前处理类型替换
|
||||||
data, complete_data, is_valid, device_ids = future.result()
|
for device_id, device_config in data.items():
|
||||||
if is_valid:
|
# 添加文件路径信息 - 使用规范化的完整文件路径
|
||||||
results.append((file, data, device_ids))
|
if "version" not in device_config:
|
||||||
except Exception as e:
|
device_config["version"] = "1.0.0"
|
||||||
logger.warning(f"[UniLab Registry] 处理设备文件异常: {file}, 错误: {e}")
|
if "category" not in device_config:
|
||||||
|
device_config["category"] = [file.stem]
|
||||||
|
elif file.stem not in device_config["category"]:
|
||||||
|
device_config["category"].append(file.stem)
|
||||||
|
if "config_info" not in device_config:
|
||||||
|
device_config["config_info"] = []
|
||||||
|
if "description" not in device_config:
|
||||||
|
device_config["description"] = ""
|
||||||
|
if "icon" not in device_config:
|
||||||
|
device_config["icon"] = ""
|
||||||
|
if "handles" not in device_config:
|
||||||
|
device_config["handles"] = []
|
||||||
|
if "init_param_schema" not in device_config:
|
||||||
|
device_config["init_param_schema"] = {}
|
||||||
|
if "class" in device_config:
|
||||||
|
if (
|
||||||
|
"status_types" not in device_config["class"]
|
||||||
|
or device_config["class"]["status_types"] is None
|
||||||
|
):
|
||||||
|
device_config["class"]["status_types"] = {}
|
||||||
|
if (
|
||||||
|
"action_value_mappings" not in device_config["class"]
|
||||||
|
or device_config["class"]["action_value_mappings"] is None
|
||||||
|
):
|
||||||
|
device_config["class"]["action_value_mappings"] = {}
|
||||||
|
enhanced_info = {}
|
||||||
|
if complete_registry:
|
||||||
|
device_config["class"]["status_types"].clear()
|
||||||
|
enhanced_info = get_enhanced_class_info(device_config["class"]["module"], use_dynamic=True)
|
||||||
|
if not enhanced_info.get("dynamic_import_success", False):
|
||||||
|
continue
|
||||||
|
device_config["class"]["status_types"].update(
|
||||||
|
{k: v["return_type"] for k, v in enhanced_info["status_methods"].items()}
|
||||||
|
)
|
||||||
|
for status_name, status_type in device_config["class"]["status_types"].items():
|
||||||
|
if isinstance(status_type, tuple) or status_type in ["Any", "None", "Unknown"]:
|
||||||
|
status_type = "String" # 替换成ROS的String,便于显示
|
||||||
|
device_config["class"]["status_types"][status_name] = status_type
|
||||||
|
try:
|
||||||
|
target_type = self._replace_type_with_class(
|
||||||
|
status_type, device_id, f"状态 {status_name}"
|
||||||
|
)
|
||||||
|
except ROSMsgNotFound:
|
||||||
|
continue
|
||||||
|
if target_type in [
|
||||||
|
dict,
|
||||||
|
list,
|
||||||
|
]: # 对于嵌套类型返回的对象,暂时处理成字符串,无法直接进行转换
|
||||||
|
target_type = String
|
||||||
|
status_str_type_mapping[status_type] = target_type
|
||||||
|
device_config["class"]["status_types"] = dict(
|
||||||
|
sorted(device_config["class"]["status_types"].items())
|
||||||
|
)
|
||||||
|
if complete_registry:
|
||||||
|
# 保存原有的 action 配置(用于保留 schema 的 description 和 handles 等)
|
||||||
|
old_action_configs = {}
|
||||||
|
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
||||||
|
old_action_configs[action_name] = action_config
|
||||||
|
|
||||||
# 线程安全地更新注册表
|
device_config["class"]["action_value_mappings"] = {
|
||||||
current_device_number = len(self.device_type_registry) + 1
|
k: v
|
||||||
with self._registry_lock:
|
for k, v in device_config["class"]["action_value_mappings"].items()
|
||||||
for file, data, device_ids in results:
|
if not k.startswith("auto-")
|
||||||
self.device_type_registry.update(data)
|
}
|
||||||
for device_id in device_ids:
|
# 处理动作值映射
|
||||||
logger.trace(
|
device_config["class"]["action_value_mappings"].update(
|
||||||
f"[UniLab Registry] Device-{current_device_number} Add {device_id} "
|
{
|
||||||
|
f"auto-{k}": {
|
||||||
|
"type": "UniLabJsonCommandAsync" if v["is_async"] else "UniLabJsonCommand",
|
||||||
|
"goal": {},
|
||||||
|
"feedback": {},
|
||||||
|
"result": {},
|
||||||
|
"schema": self._generate_unilab_json_command_schema(
|
||||||
|
v["args"],
|
||||||
|
k,
|
||||||
|
v.get("return_annotation"),
|
||||||
|
# 传入旧的 schema 以保留字段 description
|
||||||
|
old_action_configs.get(f"auto-{k}", {}).get("schema"),
|
||||||
|
),
|
||||||
|
"goal_default": {i["name"]: i["default"] for i in v["args"]},
|
||||||
|
# 保留原有的 handles 配置
|
||||||
|
"handles": old_action_configs.get(f"auto-{k}", {}).get("handles", []),
|
||||||
|
"placeholder_keys": {
|
||||||
|
i["name"]: (
|
||||||
|
"unilabos_resources"
|
||||||
|
if i["type"] == "unilabos.registry.placeholder_type:ResourceSlot"
|
||||||
|
or i["type"]
|
||||||
|
== ("list", "unilabos.registry.placeholder_type:ResourceSlot")
|
||||||
|
else "unilabos_devices"
|
||||||
|
)
|
||||||
|
for i in v["args"]
|
||||||
|
if i.get("type", "")
|
||||||
|
in [
|
||||||
|
"unilabos.registry.placeholder_type:ResourceSlot",
|
||||||
|
"unilabos.registry.placeholder_type:DeviceSlot",
|
||||||
|
("list", "unilabos.registry.placeholder_type:ResourceSlot"),
|
||||||
|
("list", "unilabos.registry.placeholder_type:DeviceSlot"),
|
||||||
|
]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
# 不生成已配置action的动作
|
||||||
|
for k, v in enhanced_info["action_methods"].items()
|
||||||
|
if k not in device_config["class"]["action_value_mappings"]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# 恢复原有的 description 信息(非 auto- 开头的动作)
|
||||||
|
for action_name, old_config in old_action_configs.items():
|
||||||
|
if action_name in device_config["class"]["action_value_mappings"]: # 有一些会被删除
|
||||||
|
old_schema = old_config.get("schema", {})
|
||||||
|
if "description" in old_schema and old_schema["description"]:
|
||||||
|
device_config["class"]["action_value_mappings"][action_name]["schema"][
|
||||||
|
"description"
|
||||||
|
] = old_schema["description"]
|
||||||
|
device_config["init_param_schema"] = {}
|
||||||
|
device_config["init_param_schema"]["config"] = self._generate_unilab_json_command_schema(
|
||||||
|
enhanced_info["init_params"], "__init__"
|
||||||
|
)["properties"]["goal"]
|
||||||
|
device_config["init_param_schema"]["data"] = self._generate_status_types_schema(
|
||||||
|
enhanced_info["status_methods"]
|
||||||
|
)
|
||||||
|
|
||||||
|
device_config.pop("schema", None)
|
||||||
|
device_config["class"]["action_value_mappings"] = dict(
|
||||||
|
sorted(device_config["class"]["action_value_mappings"].items())
|
||||||
|
)
|
||||||
|
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
||||||
|
if "handles" not in action_config:
|
||||||
|
action_config["handles"] = {}
|
||||||
|
elif isinstance(action_config["handles"], list):
|
||||||
|
if len(action_config["handles"]):
|
||||||
|
logger.error(f"设备{device_id} {action_name} 的handles配置错误,应该是字典类型")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
action_config["handles"] = {}
|
||||||
|
if "type" in action_config:
|
||||||
|
action_type_str: str = action_config["type"]
|
||||||
|
# 通过Json发放指令,而不是通过特殊的ros action进行处理
|
||||||
|
if not action_type_str.startswith("UniLabJsonCommand"):
|
||||||
|
try:
|
||||||
|
target_type = self._replace_type_with_class(
|
||||||
|
action_type_str, device_id, f"动作 {action_name}"
|
||||||
|
)
|
||||||
|
except ROSMsgNotFound:
|
||||||
|
continue
|
||||||
|
action_str_type_mapping[action_type_str] = target_type
|
||||||
|
if target_type is not None:
|
||||||
|
action_config["goal_default"] = yaml.safe_load(
|
||||||
|
io.StringIO(get_yaml_from_goal_type(target_type.Goal))
|
||||||
|
)
|
||||||
|
action_config["schema"] = ros_action_to_json_schema(target_type)
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
f"[UniLab Registry] 设备 {device_id} 的动作 {action_name} 类型为空,跳过替换"
|
||||||
|
)
|
||||||
|
complete_data[device_id] = copy.deepcopy(dict(sorted(device_config.items()))) # 稍后dump到文件
|
||||||
|
for status_name, status_type in device_config["class"]["status_types"].items():
|
||||||
|
device_config["class"]["status_types"][status_name] = status_str_type_mapping[status_type]
|
||||||
|
for action_name, action_config in device_config["class"]["action_value_mappings"].items():
|
||||||
|
if action_config["type"] not in action_str_type_mapping:
|
||||||
|
continue
|
||||||
|
action_config["type"] = action_str_type_mapping[action_config["type"]]
|
||||||
|
# 添加内置的驱动命令动作
|
||||||
|
self._add_builtin_actions(device_config, device_id)
|
||||||
|
device_config["file_path"] = str(file.absolute()).replace("\\", "/")
|
||||||
|
device_config["registry_type"] = "device"
|
||||||
|
logger.trace( # type: ignore
|
||||||
|
f"[UniLab Registry] Device-{current_device_number} File-{i+1}/{len(files)} Add {device_id} "
|
||||||
+ f"[{data[device_id].get('name', '未命名设备')}]"
|
+ f"[{data[device_id].get('name', '未命名设备')}]"
|
||||||
)
|
)
|
||||||
current_device_number += 1
|
current_device_number += 1
|
||||||
|
complete_data = dict(sorted(complete_data.items()))
|
||||||
# 记录无效文件
|
complete_data = copy.deepcopy(complete_data)
|
||||||
valid_files = {r[0] for r in results}
|
with open(file, "w", encoding="utf-8") as f:
|
||||||
for file in files:
|
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
|
||||||
if file not in valid_files:
|
self.device_type_registry.update(data)
|
||||||
logger.debug(f"[UniLab Registry] Device File Not Valid YAML File: {file.absolute()}")
|
else:
|
||||||
|
logger.debug(
|
||||||
|
f"[UniLab Registry] Device File-{i+1}/{len(files)} Not Valid YAML File: {file.absolute()}"
|
||||||
|
)
|
||||||
|
|
||||||
def obtain_registry_device_info(self):
|
def obtain_registry_device_info(self):
|
||||||
devices = []
|
devices = []
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ def canonicalize_nodes_data(
|
|||||||
Returns:
|
Returns:
|
||||||
ResourceTreeSet: 标准化后的资源树集合
|
ResourceTreeSet: 标准化后的资源树集合
|
||||||
"""
|
"""
|
||||||
print_status(f"{len(nodes)} Resources loaded", "info")
|
print_status(f"{len(nodes)} Resources loaded:", "info")
|
||||||
|
|
||||||
# 第一步:基本预处理(处理graphml的label字段)
|
# 第一步:基本预处理(处理graphml的label字段)
|
||||||
outer_host_node_id = None
|
outer_host_node_id = None
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ class ResourceDict(BaseModel):
|
|||||||
klass: str = Field(alias="class", description="Resource class name")
|
klass: str = Field(alias="class", description="Resource class name")
|
||||||
pose: ResourceDictPosition = Field(description="Resource position", default_factory=ResourceDictPosition)
|
pose: ResourceDictPosition = Field(description="Resource position", default_factory=ResourceDictPosition)
|
||||||
config: Dict[str, Any] = Field(description="Resource configuration")
|
config: Dict[str, Any] = Field(description="Resource configuration")
|
||||||
data: Dict[str, Any] = Field(description="Resource data, eg: container liquid data")
|
data: Dict[str, Any] = Field(description="Resource data")
|
||||||
extra: Dict[str, Any] = Field(description="Extra data, eg: slot index")
|
extra: Dict[str, Any] = Field(description="Extra data")
|
||||||
|
|
||||||
@field_serializer("parent_uuid")
|
@field_serializer("parent_uuid")
|
||||||
def _serialize_parent(self, parent_uuid: Optional["ResourceDict"]):
|
def _serialize_parent(self, parent_uuid: Optional["ResourceDict"]):
|
||||||
|
|||||||
@@ -1,241 +0,0 @@
|
|||||||
import ast
|
|
||||||
import json
|
|
||||||
from typing import Dict, List, Any, Tuple, Optional
|
|
||||||
|
|
||||||
from .common import WorkflowGraph, RegistryAdapter
|
|
||||||
|
|
||||||
Json = Dict[str, Any]
|
|
||||||
|
|
||||||
# ---------------- Converter ----------------
|
|
||||||
|
|
||||||
class DeviceMethodConverter:
|
|
||||||
"""
|
|
||||||
- 字段统一:resource_name(原 device_class)、template_name(原 action_key)
|
|
||||||
- params 单层;inputs 使用 'params.' 前缀
|
|
||||||
- SimpleGraph.add_workflow_node 负责变量连线与边
|
|
||||||
"""
|
|
||||||
def __init__(self, device_registry: Optional[Dict[str, Any]] = None):
|
|
||||||
self.graph = WorkflowGraph()
|
|
||||||
self.variable_sources: Dict[str, Dict[str, Any]] = {} # var -> {node_id, output_name}
|
|
||||||
self.instance_to_resource: Dict[str, Optional[str]] = {} # 实例名 -> resource_name
|
|
||||||
self.node_id_counter: int = 0
|
|
||||||
self.registry = RegistryAdapter(device_registry or {})
|
|
||||||
|
|
||||||
# ---- helpers ----
|
|
||||||
def _new_node_id(self) -> int:
|
|
||||||
nid = self.node_id_counter
|
|
||||||
self.node_id_counter += 1
|
|
||||||
return nid
|
|
||||||
|
|
||||||
def _assign_targets(self, targets) -> List[str]:
|
|
||||||
names: List[str] = []
|
|
||||||
import ast
|
|
||||||
if isinstance(targets, ast.Tuple):
|
|
||||||
for elt in targets.elts:
|
|
||||||
if isinstance(elt, ast.Name):
|
|
||||||
names.append(elt.id)
|
|
||||||
elif isinstance(targets, ast.Name):
|
|
||||||
names.append(targets.id)
|
|
||||||
return names
|
|
||||||
|
|
||||||
def _extract_device_instantiation(self, node) -> Optional[Tuple[str, str]]:
|
|
||||||
import ast
|
|
||||||
if not isinstance(node.value, ast.Call):
|
|
||||||
return None
|
|
||||||
callee = node.value.func
|
|
||||||
if isinstance(callee, ast.Name):
|
|
||||||
class_name = callee.id
|
|
||||||
elif isinstance(callee, ast.Attribute) and isinstance(callee.value, ast.Name):
|
|
||||||
class_name = callee.attr
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
if isinstance(node.targets[0], ast.Name):
|
|
||||||
instance = node.targets[0].id
|
|
||||||
return instance, class_name
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _extract_call(self, call) -> Tuple[str, str, Dict[str, Any], str]:
|
|
||||||
import ast
|
|
||||||
owner_name, method_name, call_kind = "", "", "func"
|
|
||||||
if isinstance(call.func, ast.Attribute):
|
|
||||||
method_name = call.func.attr
|
|
||||||
if isinstance(call.func.value, ast.Name):
|
|
||||||
owner_name = call.func.value.id
|
|
||||||
call_kind = "instance" if owner_name in self.instance_to_resource else "class_or_module"
|
|
||||||
elif isinstance(call.func.value, ast.Attribute) and isinstance(call.func.value.value, ast.Name):
|
|
||||||
owner_name = call.func.value.attr
|
|
||||||
call_kind = "class_or_module"
|
|
||||||
elif isinstance(call.func, ast.Name):
|
|
||||||
method_name = call.func.id
|
|
||||||
call_kind = "func"
|
|
||||||
|
|
||||||
def pack(node):
|
|
||||||
if isinstance(node, ast.Name):
|
|
||||||
return {"type": "variable", "value": node.id}
|
|
||||||
if isinstance(node, ast.Constant):
|
|
||||||
return {"type": "constant", "value": node.value}
|
|
||||||
if isinstance(node, ast.Dict):
|
|
||||||
return {"type": "dict", "value": self._parse_dict(node)}
|
|
||||||
if isinstance(node, ast.List):
|
|
||||||
return {"type": "list", "value": self._parse_list(node)}
|
|
||||||
return {"type": "raw", "value": ast.unparse(node) if hasattr(ast, "unparse") else str(node)}
|
|
||||||
|
|
||||||
args: Dict[str, Any] = {}
|
|
||||||
pos: List[Any] = []
|
|
||||||
for a in call.args:
|
|
||||||
pos.append(pack(a))
|
|
||||||
for kw in call.keywords:
|
|
||||||
args[kw.arg] = pack(kw.value)
|
|
||||||
if pos:
|
|
||||||
args["_positional"] = pos
|
|
||||||
return owner_name, method_name, args, call_kind
|
|
||||||
|
|
||||||
def _parse_dict(self, node) -> Dict[str, Any]:
|
|
||||||
import ast
|
|
||||||
out: Dict[str, Any] = {}
|
|
||||||
for k, v in zip(node.keys, node.values):
|
|
||||||
if isinstance(k, ast.Constant):
|
|
||||||
key = str(k.value)
|
|
||||||
if isinstance(v, ast.Name):
|
|
||||||
out[key] = f"var:{v.id}"
|
|
||||||
elif isinstance(v, ast.Constant):
|
|
||||||
out[key] = v.value
|
|
||||||
elif isinstance(v, ast.Dict):
|
|
||||||
out[key] = self._parse_dict(v)
|
|
||||||
elif isinstance(v, ast.List):
|
|
||||||
out[key] = self._parse_list(v)
|
|
||||||
return out
|
|
||||||
|
|
||||||
def _parse_list(self, node) -> List[Any]:
|
|
||||||
import ast
|
|
||||||
out: List[Any] = []
|
|
||||||
for elt in node.elts:
|
|
||||||
if isinstance(elt, ast.Name):
|
|
||||||
out.append(f"var:{elt.id}")
|
|
||||||
elif isinstance(elt, ast.Constant):
|
|
||||||
out.append(elt.value)
|
|
||||||
elif isinstance(elt, ast.Dict):
|
|
||||||
out.append(self._parse_dict(elt))
|
|
||||||
elif isinstance(elt, ast.List):
|
|
||||||
out.append(self._parse_list(elt))
|
|
||||||
return out
|
|
||||||
|
|
||||||
def _normalize_var_tokens(self, x: Any) -> Any:
|
|
||||||
if isinstance(x, str) and x.startswith("var:"):
|
|
||||||
return {"__var__": x[4:]}
|
|
||||||
if isinstance(x, list):
|
|
||||||
return [self._normalize_var_tokens(i) for i in x]
|
|
||||||
if isinstance(x, dict):
|
|
||||||
return {k: self._normalize_var_tokens(v) for k, v in x.items()}
|
|
||||||
return x
|
|
||||||
|
|
||||||
def _make_params_payload(self, resource_name: Optional[str], template_name: str, call_args: Dict[str, Any]) -> Dict[str, Any]:
|
|
||||||
input_keys = self.registry.get_action_input_keys(resource_name, template_name) if resource_name else []
|
|
||||||
defaults = self.registry.get_action_goal_default(resource_name, template_name) if resource_name else {}
|
|
||||||
params: Dict[str, Any] = dict(defaults)
|
|
||||||
|
|
||||||
def unpack(p):
|
|
||||||
t, v = p.get("type"), p.get("value")
|
|
||||||
if t == "variable":
|
|
||||||
return {"__var__": v}
|
|
||||||
if t == "dict":
|
|
||||||
return self._normalize_var_tokens(v)
|
|
||||||
if t == "list":
|
|
||||||
return self._normalize_var_tokens(v)
|
|
||||||
return v
|
|
||||||
|
|
||||||
for k, p in call_args.items():
|
|
||||||
if k == "_positional":
|
|
||||||
continue
|
|
||||||
params[k] = unpack(p)
|
|
||||||
|
|
||||||
pos = call_args.get("_positional", [])
|
|
||||||
if pos:
|
|
||||||
if input_keys:
|
|
||||||
for i, p in enumerate(pos):
|
|
||||||
if i >= len(input_keys):
|
|
||||||
break
|
|
||||||
name = input_keys[i]
|
|
||||||
if name in params:
|
|
||||||
continue
|
|
||||||
params[name] = unpack(p)
|
|
||||||
else:
|
|
||||||
for i, p in enumerate(pos):
|
|
||||||
params[f"arg_{i}"] = unpack(p)
|
|
||||||
return params
|
|
||||||
|
|
||||||
# ---- handlers ----
|
|
||||||
def _on_assign(self, stmt):
|
|
||||||
import ast
|
|
||||||
inst = self._extract_device_instantiation(stmt)
|
|
||||||
if inst:
|
|
||||||
instance, code_class = inst
|
|
||||||
resource_name = self.registry.resolve_resource_by_classname(code_class)
|
|
||||||
self.instance_to_resource[instance] = resource_name
|
|
||||||
return
|
|
||||||
|
|
||||||
if isinstance(stmt.value, ast.Call):
|
|
||||||
owner, method, call_args, kind = self._extract_call(stmt.value)
|
|
||||||
if kind == "instance":
|
|
||||||
device_key = owner
|
|
||||||
resource_name = self.instance_to_resource.get(owner)
|
|
||||||
else:
|
|
||||||
device_key = owner
|
|
||||||
resource_name = self.registry.resolve_resource_by_classname(owner)
|
|
||||||
|
|
||||||
module = self.registry.get_device_module(resource_name)
|
|
||||||
params = self._make_params_payload(resource_name, method, call_args)
|
|
||||||
|
|
||||||
nid = self._new_node_id()
|
|
||||||
self.graph.add_workflow_node(
|
|
||||||
nid,
|
|
||||||
device_key=device_key,
|
|
||||||
resource_name=resource_name, # ✅
|
|
||||||
module=module,
|
|
||||||
template_name=method, # ✅
|
|
||||||
params=params,
|
|
||||||
variable_sources=self.variable_sources,
|
|
||||||
add_ready_if_no_vars=True,
|
|
||||||
prev_node_id=(nid - 1) if nid > 0 else None,
|
|
||||||
)
|
|
||||||
|
|
||||||
out_vars = self._assign_targets(stmt.targets[0])
|
|
||||||
for var in out_vars:
|
|
||||||
self.variable_sources[var] = {"node_id": nid, "output_name": "result"}
|
|
||||||
|
|
||||||
def _on_expr(self, stmt):
|
|
||||||
import ast
|
|
||||||
if not isinstance(stmt.value, ast.Call):
|
|
||||||
return
|
|
||||||
owner, method, call_args, kind = self._extract_call(stmt.value)
|
|
||||||
if kind == "instance":
|
|
||||||
device_key = owner
|
|
||||||
resource_name = self.instance_to_resource.get(owner)
|
|
||||||
else:
|
|
||||||
device_key = owner
|
|
||||||
resource_name = self.registry.resolve_resource_by_classname(owner)
|
|
||||||
|
|
||||||
module = self.registry.get_device_module(resource_name)
|
|
||||||
params = self._make_params_payload(resource_name, method, call_args)
|
|
||||||
|
|
||||||
nid = self._new_node_id()
|
|
||||||
self.graph.add_workflow_node(
|
|
||||||
nid,
|
|
||||||
device_key=device_key,
|
|
||||||
resource_name=resource_name, # ✅
|
|
||||||
module=module,
|
|
||||||
template_name=method, # ✅
|
|
||||||
params=params,
|
|
||||||
variable_sources=self.variable_sources,
|
|
||||||
add_ready_if_no_vars=True,
|
|
||||||
prev_node_id=(nid - 1) if nid > 0 else None,
|
|
||||||
)
|
|
||||||
|
|
||||||
def convert(self, python_code: str):
|
|
||||||
tree = ast.parse(python_code)
|
|
||||||
for stmt in tree.body:
|
|
||||||
if isinstance(stmt, ast.Assign):
|
|
||||||
self._on_assign(stmt)
|
|
||||||
elif isinstance(stmt, ast.Expr):
|
|
||||||
self._on_expr(stmt)
|
|
||||||
return self
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
from typing import List, Any, Dict
|
|
||||||
import xml.etree.ElementTree as ET
|
|
||||||
|
|
||||||
|
|
||||||
def convert_to_type(val: str) -> Any:
|
|
||||||
"""将字符串值转换为适当的数据类型"""
|
|
||||||
if val == "True":
|
|
||||||
return True
|
|
||||||
if val == "False":
|
|
||||||
return False
|
|
||||||
if val == "?":
|
|
||||||
return None
|
|
||||||
if val.endswith(" g"):
|
|
||||||
return float(val.split(" ")[0])
|
|
||||||
if val.endswith("mg"):
|
|
||||||
return float(val.split("mg")[0])
|
|
||||||
elif val.endswith("mmol"):
|
|
||||||
return float(val.split("mmol")[0]) / 1000
|
|
||||||
elif val.endswith("mol"):
|
|
||||||
return float(val.split("mol")[0])
|
|
||||||
elif val.endswith("ml"):
|
|
||||||
return float(val.split("ml")[0])
|
|
||||||
elif val.endswith("RPM"):
|
|
||||||
return float(val.split("RPM")[0])
|
|
||||||
elif val.endswith(" °C"):
|
|
||||||
return float(val.split(" ")[0])
|
|
||||||
elif val.endswith(" %"):
|
|
||||||
return float(val.split(" ")[0])
|
|
||||||
return val
|
|
||||||
|
|
||||||
|
|
||||||
def flatten_xdl_procedure(procedure_elem: ET.Element) -> List[ET.Element]:
|
|
||||||
"""展平嵌套的XDL程序结构"""
|
|
||||||
flattened_operations = []
|
|
||||||
TEMP_UNSUPPORTED_PROTOCOL = ["Purge", "Wait", "Stir", "ResetHandling"]
|
|
||||||
|
|
||||||
def extract_operations(element: ET.Element):
|
|
||||||
if element.tag not in ["Prep", "Reaction", "Workup", "Purification", "Procedure"]:
|
|
||||||
if element.tag not in TEMP_UNSUPPORTED_PROTOCOL:
|
|
||||||
flattened_operations.append(element)
|
|
||||||
|
|
||||||
for child in element:
|
|
||||||
extract_operations(child)
|
|
||||||
|
|
||||||
for child in procedure_elem:
|
|
||||||
extract_operations(child)
|
|
||||||
|
|
||||||
return flattened_operations
|
|
||||||
|
|
||||||
|
|
||||||
def parse_xdl_content(xdl_content: str) -> tuple:
|
|
||||||
"""解析XDL内容"""
|
|
||||||
try:
|
|
||||||
xdl_content_cleaned = "".join(c for c in xdl_content if c.isprintable())
|
|
||||||
root = ET.fromstring(xdl_content_cleaned)
|
|
||||||
|
|
||||||
synthesis_elem = root.find("Synthesis")
|
|
||||||
if synthesis_elem is None:
|
|
||||||
return None, None, None
|
|
||||||
|
|
||||||
# 解析硬件组件
|
|
||||||
hardware_elem = synthesis_elem.find("Hardware")
|
|
||||||
hardware = []
|
|
||||||
if hardware_elem is not None:
|
|
||||||
hardware = [{"id": c.get("id"), "type": c.get("type")} for c in hardware_elem.findall("Component")]
|
|
||||||
|
|
||||||
# 解析试剂
|
|
||||||
reagents_elem = synthesis_elem.find("Reagents")
|
|
||||||
reagents = []
|
|
||||||
if reagents_elem is not None:
|
|
||||||
reagents = [{"name": r.get("name"), "role": r.get("role", "")} for r in reagents_elem.findall("Reagent")]
|
|
||||||
|
|
||||||
# 解析程序
|
|
||||||
procedure_elem = synthesis_elem.find("Procedure")
|
|
||||||
if procedure_elem is None:
|
|
||||||
return None, None, None
|
|
||||||
|
|
||||||
flattened_operations = flatten_xdl_procedure(procedure_elem)
|
|
||||||
return hardware, reagents, flattened_operations
|
|
||||||
|
|
||||||
except ET.ParseError as e:
|
|
||||||
raise ValueError(f"Invalid XDL format: {e}")
|
|
||||||
|
|
||||||
|
|
||||||
def convert_xdl_to_dict(xdl_content: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
将XDL XML格式转换为标准的字典格式
|
|
||||||
|
|
||||||
Args:
|
|
||||||
xdl_content: XDL XML内容
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
转换结果,包含步骤和器材信息
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
hardware, reagents, flattened_operations = parse_xdl_content(xdl_content)
|
|
||||||
if hardware is None:
|
|
||||||
return {"error": "Failed to parse XDL content", "success": False}
|
|
||||||
|
|
||||||
# 将XDL元素转换为字典格式
|
|
||||||
steps_data = []
|
|
||||||
for elem in flattened_operations:
|
|
||||||
# 转换参数类型
|
|
||||||
parameters = {}
|
|
||||||
for key, val in elem.attrib.items():
|
|
||||||
converted_val = convert_to_type(val)
|
|
||||||
if converted_val is not None:
|
|
||||||
parameters[key] = converted_val
|
|
||||||
|
|
||||||
step_dict = {
|
|
||||||
"operation": elem.tag,
|
|
||||||
"parameters": parameters,
|
|
||||||
"description": elem.get("purpose", f"Operation: {elem.tag}"),
|
|
||||||
}
|
|
||||||
steps_data.append(step_dict)
|
|
||||||
|
|
||||||
# 合并硬件和试剂为统一的labware_info格式
|
|
||||||
labware_data = []
|
|
||||||
labware_data.extend({"id": hw["id"], "type": "hardware", **hw} for hw in hardware)
|
|
||||||
labware_data.extend({"name": reagent["name"], "type": "reagent", **reagent} for reagent in reagents)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"success": True,
|
|
||||||
"steps": steps_data,
|
|
||||||
"labware": labware_data,
|
|
||||||
"message": f"Successfully converted XDL to dict format. Found {len(steps_data)} steps and {len(labware_data)} labware items.",
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
error_msg = f"XDL conversion failed: {str(e)}"
|
|
||||||
return {"error": error_msg, "success": False}
|
|
||||||
Reference in New Issue
Block a user