162 lines
6.3 KiB
C#
162 lines
6.3 KiB
C#
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using YamlDotNet.Serialization;
|
||
using YamlDotNet.Serialization.NamingConventions;
|
||
|
||
namespace SopHelper.Engine.Sop;
|
||
|
||
/// <summary>
|
||
/// 配方加载器:YAML → SopConfig,并做可用性校验(校验结果进 Warnings,界面要显示)。
|
||
/// 反向的 ToYaml/Save 给配方编辑器用。
|
||
/// </summary>
|
||
public static partial class SopConfigLoader
|
||
{
|
||
/// <summary>
|
||
/// 读一个配方文件
|
||
/// </summary>
|
||
public static SopConfig Load(string yamlPath)
|
||
{
|
||
var config = Parse(File.ReadAllText(yamlPath));
|
||
|
||
if (string.IsNullOrWhiteSpace(config.Sop.Name))
|
||
config.Warnings.Add("配方缺少 sop.name");
|
||
|
||
return config;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从 YAML 文本解析(与 Load 同一套校验,便于单测)
|
||
/// </summary>
|
||
public static SopConfig Parse(string yaml)
|
||
{
|
||
var deserializer = new DeserializerBuilder()
|
||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||
.WithCaseInsensitivePropertyMatching()
|
||
.IgnoreUnmatchedProperties()
|
||
.Build();
|
||
|
||
var config = deserializer.Deserialize<SopConfig>(yaml) ?? new SopConfig();
|
||
|
||
Validate(config);
|
||
|
||
return config;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 把配方写回 YAML 文本(编辑器保存用)。注释会丢——YamlDotNet 不保留注释,
|
||
/// 所以 demo.yaml 里那些说明性注释只在手写的原始文件里有。
|
||
/// </summary>
|
||
public static string ToYaml(SopConfig config)
|
||
{
|
||
var serializer = new SerializerBuilder()
|
||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||
.WithNewLine("\n")
|
||
.WithQuotingNecessaryStrings()
|
||
.Build();
|
||
|
||
var yaml = serializer.Serialize(config);
|
||
|
||
// 没配模型时别留一个空的 model: 节点——它会被读成"有 model 但 path 为空",
|
||
// 校验里那几条"需要模型"的提醒就哑了(本可空手告诉你的手已经空了)。
|
||
if (config.Sop.Model is null)
|
||
yaml = string.Join("\n", yaml.Split('\n').Where(l => l.TrimEnd() != " model:"));
|
||
|
||
return yaml.TrimEnd('\n') + "\n";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 写回文件,并顺手校验一遍(校验提醒随返回值给出,编辑器要显示)。
|
||
/// </summary>
|
||
public static SopConfig Save(SopConfig config, string yamlPath)
|
||
{
|
||
File.WriteAllText(yamlPath, ToYaml(config), new UTF8Encoding(false));
|
||
|
||
// 落盘后再读一遍:校验的是文件里真正的内容,不是内存里那份
|
||
return Load(yamlPath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校验:配方里写了引擎没实现的方法,一定要在这里报出来,
|
||
/// 否则现场表现是"这一步永远不推进",极难定位。
|
||
/// </summary>
|
||
public static void Validate(SopConfig config)
|
||
{
|
||
var supported = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||
{
|
||
ConditionMethod.ObjectPresent,
|
||
ConditionMethod.ObjectInZone,
|
||
ConditionMethod.HandInRegion,
|
||
ConditionMethod.HandMove,
|
||
ConditionMethod.HandNearObject,
|
||
ConditionMethod.TimeElapsed,
|
||
};
|
||
|
||
var regionNames = new HashSet<String>(config.Sop.Regions.Keys, StringComparer.OrdinalIgnoreCase);
|
||
|
||
if (config.Sop.Steps.Count == 0)
|
||
config.Warnings.Add("配方没有任何步骤");
|
||
|
||
// 起始步骤取最小 Id(配方步骤不固定从 1 开始)
|
||
var ordered = config.Sop.Steps.OrderBy(s => s.Id).ToList();
|
||
if (ordered.Count > 0 && ordered[0].Id != config.Sop.Steps.Min(s => s.Id))
|
||
config.Warnings.Add("步骤顺序与 Id 不一致,执行顺序按 Id 升序");
|
||
|
||
foreach (var step in ordered)
|
||
{
|
||
var condition = step.Detection;
|
||
if (condition is null)
|
||
{
|
||
config.Warnings.Add($"步骤 {step.Id}({step.Name})未设置完成条件,请选择判定方法;等待后完成请选择 time_elapsed");
|
||
continue;
|
||
}
|
||
|
||
if (!supported.Contains(condition.Method))
|
||
{
|
||
config.Warnings.Add(
|
||
$"步骤 {step.Id} 的方法 '{condition.Method}' 本骨架未实现(支持:{string.Join(", ", supported)}),该条件视为不满足");
|
||
}
|
||
|
||
// 区域引用检查(区域没标定 → 现场就是不推进)
|
||
foreach (var name in new[] { condition.Region, condition.FromRegion, condition.ToRegion })
|
||
{
|
||
if (string.IsNullOrWhiteSpace(name)) continue;
|
||
|
||
if (!regionNames.Contains(name))
|
||
config.Warnings.Add($"步骤 {step.Id} 引用了未定义的区域 '{name}'");
|
||
}
|
||
|
||
if (NeedsModel(condition.Method) && string.IsNullOrWhiteSpace(config.Sop.Model?.Path))
|
||
config.Warnings.Add($"步骤 {step.Id} 的方法 '{condition.Method}' 需要模型,配方模型尚未就绪,请先训练或重新导出");
|
||
|
||
foreach (var rule in step.ViolationRules)
|
||
{
|
||
if (rule.Type.Equals(ViolationType.Timeout, StringComparison.OrdinalIgnoreCase)
|
||
&& step.Timeout <= 0)
|
||
{
|
||
config.Warnings.Add($"步骤 {step.Id} 配了超时违规但 timeout=0,不会触发");
|
||
}
|
||
}
|
||
}
|
||
|
||
// 没有模型时,靠模型的条件全部无法判定
|
||
if (string.IsNullOrWhiteSpace(config.Sop.Model?.Path))
|
||
{
|
||
var needsModel = config.Sop.Steps.Any(s => s.Detection is not null && NeedsModel(s.Detection.Method));
|
||
if (needsModel)
|
||
config.Warnings.Add("配方需要检测模型但尚未配置模型路径 —— 只有手部与计时类条件能工作");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 该方法是否依赖 YOLO 模型。手部类(hand_in_region / hand_move)
|
||
/// 与计时只用 MediaPipe,不在此列;hand_near_object 要比对物体,算依赖。
|
||
/// </summary>
|
||
private static bool NeedsModel(string method) => method.ToLowerInvariant() switch
|
||
{
|
||
ConditionMethod.ObjectPresent => true,
|
||
ConditionMethod.ObjectInZone => true,
|
||
ConditionMethod.HandNearObject => true,
|
||
_ => false,
|
||
};
|
||
}
|