C3032
2025-12-19 6bb231b53dcb5e21585d54e26ec872e3026fc8fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using LB_VisionProcesses;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.Json.Nodes;
using System.Threading;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView;
 
namespace LB_VisionFlowNode
{
    /// <summary>
    /// 插件节点方法信息
    /// </summary>
    public class PluginNodeMethodInfo
    {
        public string DisplayName { get; set; }
        public string FullTypeName { get; set; }
        public string Category { get; set; }
        public string Group { get; set; }
        public string Description { get; set; }
    }
    public class IFlowContext
    {
        public readonly Dictionary<string, MethodInfo> _nodeMethods
            = new Dictionary<string, MethodInfo>();
 
        public readonly Dictionary<string, List<MethodInfo>> _groupedMethods
            = new Dictionary<string, List<MethodInfo>>();
 
        public readonly Dictionary<string, Func<FlowNode, bool>> _nodeDelegates
            = new Dictionary<string, Func<FlowNode, bool>>();
 
        public void InitializeMethods()
        {
            var methods = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
 
            foreach (var method in methods)
            {
                var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
                if (nodeAttr != null)
                {
                    // 使用描述作为键值
                    _nodeMethods[nodeAttr.Description] = method;
 
                    // 按组分类
                    if (!_groupedMethods.ContainsKey(nodeAttr.Group))
                    {
                        _groupedMethods[nodeAttr.Group] = new List<MethodInfo>();
                    }
                    _groupedMethods[nodeAttr.Group].Add(method);
                }
            }
        }
 
        public void InitializeDelegates()
        {
            foreach (var kvp in _nodeMethods)
            {
                var method = kvp.Value;
                var parameters = method.GetParameters();
 
                if (parameters.Length == 0)
                {
                    // 无参数方法
                    var action = (Action)Delegate.CreateDelegate(typeof(Action), this, method);
                    _nodeDelegates[kvp.Key] = (node) => { action(); return node.Result; };
                }
                else if (parameters.Length == 1 && parameters[0].ParameterType == typeof(FlowNode))
                {
                    // 带FlowNode参数的方法
                    var action = (Action<FlowNode>)Delegate.CreateDelegate(typeof(Action<FlowNode>), this, method);
                    _nodeDelegates[kvp.Key] = (node) => { action(node); return node.Result; };
                }
            }
        }
 
        public virtual bool ExecuteNode(FlowNode node)
        {
            if (string.IsNullOrEmpty(node.Description))
                return false;
 
            // 使用委托缓存,避免反射开销
            if (_nodeDelegates.TryGetValue(node.Description, out var nodeDelegate))
            {
                try
                {
                    return nodeDelegate(node);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"执行节点失败: {ex.Message}");
                    return false;
                }
            }
 
            return false;
        }
 
        public Dictionary<string, MethodInfo> GetAvailableNodes()
        {
            return _nodeMethods.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value
            );
        }
 
        // 获取按组分组的可用节点
        public Dictionary<string, List<ToolStripMenuItem>> GetGroupedMenuItems(EventHandler clickHandler)
        {
            var groupedMenuItems = new Dictionary<string, List<ToolStripMenuItem>>();
 
            foreach (var group in _groupedMethods)
            {
                var menuItems = new List<ToolStripMenuItem>();
 
                foreach (var method in group.Value)
                {
                    var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
                    var menuItem = new ToolStripMenuItem
                    {
                        Text = nodeAttr.Name,
                        Tag = nodeAttr.Description, // 存储描述作为标识
                        ToolTipText = nodeAttr.Description
                    };
 
                    if (clickHandler != null)
                    {
                        menuItem.Click += clickHandler;
                    }
 
                    menuItems.Add(menuItem);
                }
 
                groupedMenuItems[group.Key] = menuItems;
            }
 
            return groupedMenuItems;
        }
 
 
        // 获取按类别分组的菜单项
        public Dictionary<string, List<ToolStripMenuItem>> GetCategorizedMenuItems(EventHandler clickHandler)
        {
            var categorizedMethods = new Dictionary<string, List<MethodInfo>>();
 
            foreach (var method in _nodeMethods.Values)
            {
                var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
                if (!categorizedMethods.ContainsKey(nodeAttr.Category))
                {
                    categorizedMethods[nodeAttr.Category] = new List<MethodInfo>();
                }
                categorizedMethods[nodeAttr.Category].Add(method);
            }
 
            var categorizedMenuItems = new Dictionary<string, List<ToolStripMenuItem>>();
 
            foreach (var category in categorizedMethods)
            {
                var menuItems = new List<ToolStripMenuItem>();
 
                foreach (var method in category.Value)
                {
                    var nodeAttr = method.GetCustomAttribute<NodeAttribute>();
                    var menuItem = new ToolStripMenuItem
                    {
                        Text = nodeAttr.Name,
                        Tag = nodeAttr.Description,
                        ToolTipText = $"{nodeAttr.Group} - {nodeAttr.Description}"
                    };
 
                    if (clickHandler != null)
                    {
                        menuItem.Click += clickHandler;
                    }
 
                    menuItems.Add(menuItem);
                }
 
                categorizedMenuItems[category.Key] = menuItems;
            }
 
            return categorizedMenuItems;
        }
 
        [Node("开始", "控制", "Logic", "开始")]
        public virtual void 开始(FlowNode node) { node.Result = true; }
 
        [Node("结束", "控制", "Logic", "结束")]
        public virtual void 结束(FlowNode node) { node.Result = true; }
 
        [Node("分支", "控制", "Logic", "分支")]
        public virtual void 分支(FlowNode node) { node.Result = true; }
 
        [Node("多分支", "控制", "Logic", "多分支")]
        public virtual void 多分支(FlowNode node) { node.Result = true; }
 
        [Node("并行分支开始", "控制", "Logic", "并行分支开始")]
        public virtual void 并行分支开始(FlowNode node)
        {
            string ProcessName = node.Text;
            node.BranchIndex = "-1";
            try
            {
                for (int i = 0; i < node.BranchNodes.Count; i++)
                {
                    if (i == 0)
                        node.BranchIndex = "0;";
                    else
                        node.BranchIndex += $"{i.ToString()};";
                }
                node.Result = true;
            }
            catch { node.Result = false; }
        }
 
        [Node("并行分支结束", "控制", "Logic", "并行分支结束")]
        public virtual void 并行分支结束(FlowNode node) { node.Result = true; }
    }
    public class FlowContext : IFlowContext
    {
        /// <summary>
        /// 运行标记
        /// </summary>
        public bool bRuning = false;
 
        /// <summary>
        /// 运行完成
        /// </summary>
        public bool bCompleted = true;
 
        /// <summary>
        /// 运行结果
        /// </summary>
        public bool Result = false;
 
        /// <summary>
        /// 运行消息
        /// </summary>
        public string Msg = string.Empty;
 
        /// <summary>
        /// 运行时间
        /// </summary>
        public double RunTime = 0;
 
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime = DateTime.Now;
 
        public FlowContext()
        {
            //GenerateFixedNodeMethods();
            InitializeMethods();
            InitializeDelegates();
        }
 
 
        // 统一的动态节点执行辅助方法(可选)
        private void RunNodeAsync(FlowNode node)
        {
            // 调用基类的ExecuteNode
            node.Result = ExecuteNode(node);
            if (node.Result)
            {
                Debug.WriteLine($"{node.Text} 运行完成");
            }
            else
            {
                Msg += $"[{node.Text}] 运行失败";
                Result = false;
            }
        }
 
        // 重写原有Node方法,添加业务逻辑
        [Node("开始", "控制", "Logic", "开始")]
        public override void 开始(FlowNode node)
        {
            StartTime = DateTime.Now;
            RunTime = 0;
 
            Result = true;
            bRuning = true;
            bCompleted = false;
            Msg = string.Empty;
            node.Result = true;
        }
 
        [Node("结束", "控制", "Logic", "结束")]
        public override void 结束(FlowNode node)
        {
            bRuning = false;
            bCompleted = true;
 
            if (Result)
                Msg = "运行成功";
 
            RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
            node.Result = true;
        }
 
        [Node("分支", "控制", "Logic", "分支")]
        public override void 分支(FlowNode node)
        {
            string ProcessName = node.Text;
            node.BranchIndex = "-1";
            node.Result = true;
            try
            {
                if (node.Break)
                {
                    node.BranchIndex = "0";
                    node.Result = true;
                    return;
                }
 
                node.BranchIndex = 0.ToString();
                Debug.WriteLine($"{ProcessName},分支{node.BranchIndex}运行完成");
            }
            catch (Exception ex)
            {
                node.Result = false;
                Result &= false;
                Msg = $"[{ProcessName}]运行发生意外,{ex.Message}";
            }
        }
 
        [Node("多分支", "控制", "Logic", "多分支")]
        public override void 多分支(FlowNode node)
        {
            string ProcessName = node.Text;
            node.BranchIndex = "-1";
            node.Result = true;
            try
            {
                if (node.Break)
                {
                    node.BranchIndex = "0";
                    node.Result = true;
                    return;
                }
 
                node.BranchIndex = 0.ToString();
                Debug.WriteLine($"{ProcessName},分支{node.BranchIndex}运行完成");
            }
            catch (Exception ex)
            {
                node.Result = false;
                Result &= false;
                Msg = $"[{ProcessName}]运行发生意外,{ex.Message}";
            }
        }
 
    }
 
}