IfConditionPlugin.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using HandyControl.Interactivity;
  2. using HandyControl.Tools;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using TeamAAS.FlowEditor.Models;
  7. using TeamAAS.FlowEditor.Plugins;
  8. using Plugins.Standard.Models;
  9. namespace Plugins.Standard
  10. {
  11. /// <summary>
  12. /// 条件判断插件 - 无自定义视图,双击弹出PropertyGrid编辑器
  13. /// </summary>
  14. [PluginAttribute("条件判断", PluginCategory.逻辑判断, typeof(IfConditionModel),
  15. "CallSplit",
  16. Description = "条件判断,运行满足条件的下节点", NodeShape = NodeCategory.Decision)]
  17. [System.Serializable]
  18. public class IfConditionPlugin : BasePlugin<IfConditionModel>
  19. {
  20. public IfConditionPlugin()
  21. {
  22. // 设置节点形状为判断
  23. }
  24. /// <summary>
  25. /// 动态输出声明:固定的 结果/执行分支数,加上按条件数量声明的 [分支]执行流程N
  26. /// (与运行时写入的键格式一致,使分支节点的输出跑之前就能被绑定树索引)。
  27. /// </summary>
  28. public override List<OutputField> DeclareOutputs()
  29. {
  30. var list = new List<OutputField>
  31. {
  32. new OutputField("结果", typeof(bool)),
  33. new OutputField("执行分支数", typeof(int)),
  34. };
  35. int n = Model?.IFModels?.Count ?? 0;
  36. for (int i = 1; i <= n; i++)
  37. list.Add(new OutputField($"[分支]执行流程{i}", typeof(string)));
  38. return list;
  39. }
  40. public override NodeRunStatus PluginRun(CancellationToken token, out Dictionary<string, object> results)
  41. {
  42. results = new Dictionary<string, object>();
  43. BranchTargets = new List<string>();
  44. int index = 0;
  45. try
  46. {
  47. Log(3, $"开始条件判断,共 {Model.IFModels?.Count ?? 0} 个条件");
  48. for (int i = 0; i < Model.IFModels.Count; i++)
  49. {
  50. bool Result = GetValue(Model.IFModels[i].DisplayIndex);
  51. if (Model.IFModels[i].IsNegate)
  52. {
  53. Result = !Result;
  54. }
  55. Log(4, $"条件{i + 1} 判定={Result}{(Model.IFModels[i].IsNegate ? "(取反)" : "")} → 目标[{Model.IFModels[i].ToolName}]");
  56. if (Result)
  57. {
  58. BranchTargets.Add(Model.IFModels[i].ToolName);
  59. results[$"[分支]执行流程{index + 1}"] = Model.IFModels[i].ToolName;
  60. index++;
  61. }
  62. }
  63. results["执行分支数"] = index;
  64. results["结果"] = index > 0;
  65. if (index == 0)
  66. {
  67. results["程序异常:"] = "无执行流程";
  68. Log(1, "条件判断无任何分支命中", TeamAAS.LogLevel.Warning);
  69. return NodeRunStatus.Failed;
  70. }
  71. Log(2, $"条件判断命中 {index} 个分支: {string.Join(", ", BranchTargets)}");
  72. return NodeRunStatus.Success;
  73. }
  74. catch (Exception ex)
  75. {
  76. results["结果"] = false;
  77. Log(1, $"条件判断异常: {ex.Message}", TeamAAS.LogLevel.Error);
  78. return NodeRunStatus.Failed;
  79. }
  80. }
  81. }
  82. }