InputSourceDataProvider.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using HandyControl.Tools.Extension;
  2. using PropertyGridLib.Controls;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using TeamAAS.Communication;
  7. using TeamAAS.FlowEditor.Execution;
  8. using TeamAAS.FlowEditor.Models;
  9. using TeamAAS.FlowEngine;
  10. namespace TeamAAS.FlowEngine.FormulaData
  11. {
  12. /// <summary>
  13. /// 给「组合模块输入参数 SourceValue」用的公式树数据源。
  14. /// 查外层主流程中组合模块节点的前置节点结果(而不是子流程内部节点)。
  15. /// 依赖 <see cref="PluginLoader.ParentGroupModel"/> 判断上下文。
  16. /// </summary>
  17. [Serializable]
  18. public class InputSourceDataProvider : IFormulaTreeProvider
  19. {
  20. /// <summary>
  21. /// 生成公式树节点列表:
  22. /// 1) 全局变量(Gloab)
  23. /// 2) 主流程中组合模块节点的所有前置节点输出结果
  24. /// </summary>
  25. public List<FormulaTreeNode> GetFormulaTree(PropertyItem propertyItem)
  26. {
  27. var nodes = new List<FormulaTreeNode>();
  28. try
  29. {
  30. var select = PluginLoader.Instance.ParentGroupModel;
  31. if (select == null) return nodes;
  32. var flowId = select.FlowId;
  33. var findName = select.NodeName;
  34. // 使用选中节点所属流程绑定的注册表(编辑态=Debug,监控态挂接运行图=Main)
  35. var registry = PluginLoader.Instance.SelectFlow?.Registry ?? ResultRegistry.Debug;
  36. registry.GetFlowResults(flowId, out var flowResults);
  37. flowResults.DeleteIfExistsKey(findName);
  38. if (propertyItem.Value == null)
  39. propertyItem.Value = new object();
  40. var targetType = propertyItem.Value.GetType();
  41. var formulaType = propertyItem.PropertyType;
  42. nodes.AddRange(flowResults.Select(kvp =>
  43. new FormulaTreeNode
  44. {
  45. Header = kvp.Key,
  46. Children = kvp.Value
  47. .Where(p => targetType == null || targetType == typeof(object)
  48. || (p.Value != null && (p.Value.GetType() == targetType || p.Value.GetType() == formulaType)))
  49. .Select(p =>
  50. new FormulaTreeNode
  51. {
  52. Header = p.Key,
  53. Formula = $"&{{{kvp.Key}.{p.Key}}}"
  54. }).ToList()
  55. }).ToList());
  56. // 全局变量
  57. try
  58. {
  59. registry.RefreshGlobalVariables();
  60. var gloabNode = new FormulaTreeNode
  61. {
  62. Header = "Gloab",
  63. Children = GlobalVariableManager.Instance.Variables
  64. .Where(v => !string.IsNullOrWhiteSpace(v.Name))
  65. .Select(v => new FormulaTreeNode
  66. {
  67. Header = v.Name,
  68. Formula = $"&{{Gloab.{v.Name}}}"
  69. }).ToList()
  70. };
  71. if (gloabNode.Children.Count > 0)
  72. nodes.Insert(0, gloabNode);
  73. }
  74. catch { }
  75. }
  76. catch { }
  77. return nodes;
  78. }
  79. /// <summary>
  80. /// 反向 BFS 追溯指定节点的所有前置节点名称(通过连接线追溯到起点)。
  81. /// </summary>
  82. private static HashSet<string> GetPredecessorNodeNames(string graphId, string nodeId)
  83. {
  84. var result = new HashSet<string>();
  85. var graph = FindGraphById(graphId);
  86. if (graph == null) return result;
  87. var predecessorIds = new HashSet<string>();
  88. var queue = new Queue<string>();
  89. foreach (var conn in graph.Connections)
  90. {
  91. if (conn.TargetNodeId == nodeId) queue.Enqueue(conn.SourceNodeId);
  92. }
  93. while (queue.Count > 0)
  94. {
  95. var cur = queue.Dequeue();
  96. if (predecessorIds.Add(cur))
  97. {
  98. foreach (var conn in graph.Connections)
  99. {
  100. if (conn.TargetNodeId == cur) queue.Enqueue(conn.SourceNodeId);
  101. }
  102. }
  103. }
  104. foreach (var node in graph.Nodes)
  105. {
  106. if (predecessorIds.Contains(node.NodeId))
  107. result.Add(node.NodeName);
  108. }
  109. return result;
  110. }
  111. /// <summary>
  112. /// 按 GraphId 查找 FlowGraph:先查顶层 FlowTabs,找不到再递归所有组合模块 SubGraph。
  113. /// </summary>
  114. private static FlowGraph FindGraphById(string graphId)
  115. {
  116. var flowTabs = PluginLoader.Instance.FlowTabs;
  117. if (flowTabs != null)
  118. {
  119. var tab = flowTabs.FirstOrDefault(t => t.Graph != null && t.Graph.GraphId == graphId);
  120. if (tab?.Graph != null) return tab.Graph;
  121. foreach (var t in flowTabs)
  122. {
  123. if (t?.Graph == null) continue;
  124. var found = FindSubGraphById(t.Graph, graphId);
  125. if (found != null) return found;
  126. }
  127. }
  128. return null;
  129. }
  130. /// <summary>
  131. /// 在图中递归查找所有 SubGraph(支持任意嵌套组合模块)。
  132. /// </summary>
  133. private static FlowGraph FindSubGraphById(FlowGraph parent, string graphId)
  134. {
  135. foreach (var node in parent.Nodes)
  136. {
  137. var model = node.PluginModel?.GetModel;
  138. if (model == null) continue;
  139. var graphProp = model.GetType().GetProperty("SubGraph");
  140. if (graphProp?.GetValue(model) is FlowGraph sub)
  141. {
  142. if (sub.GraphId == graphId) return sub;
  143. var nested = FindSubGraphById(sub, graphId);
  144. if (nested != null) return nested;
  145. }
  146. }
  147. return null;
  148. }
  149. }
  150. }