lixiaojun
2024-11-11 00bcee19c5dff21a9848c16b45419efb74bf07e9
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
using System.Text;
using System.Text.RegularExpressions;
 
namespace Yw.EPAnet
{
    /// <summary>
    /// Inp交互辅助类
    /// </summary>
    public static class InpInteropHelper
    {
        /// <summary>
        /// 转换至Inp字符串
        /// </summary>
        public static string ToInpString(this Network network, CalcuResult minorResult = null)
        {
            //模板文件
            var templateFilePath = TemplateHelper.GetPath();
            if (!File.Exists(templateFilePath))
            {
                throw new Exception("模板文件不存在," + templateFilePath);
            }
            var templateString = File.ReadAllText(templateFilePath);
 
            //状态
            var statusSb = new StringBuilder();
            statusSb.AppendLine(";ID              \tStatus/Setting\r\n");
 
            //扩散器
            var emitterSb = new StringBuilder();
            emitterSb.AppendLine(";Junction        \tCoefficient");
 
            //坐标
            var coorSb = new StringBuilder();
            coorSb.AppendLine(";Node                X-Coord               Y-Coord");
 
            Dictionary<string, string> dictExchange = new Dictionary<string, string>() {
                {"{junctions}","{0}" },
                {"{reservoirs}","{1}" },
                {"{tanks}","{2}" },
                {"{pipes}","{3}" },
                {"{valves}","{4}" },
                {"{pumps}","{5}" },
 
                {"{coor}","{6}" },
                {"{curve}","{7}" },
                {"{emitters}","{8}" },
            };
            dictExchange.ToList().ForEach(m => templateString = templateString.Replace(m.Key, m.Value));
 
            //连接节点
            var junctionSb = new StringBuilder();
            junctionSb.AppendLine(";ID                  Elev            Demand          Pattern         Type");
            //连接节点处理
            network.Junctions?.ForEach(x =>
            {
                var demandPattern = x.DemandPattern;
                if (string.IsNullOrEmpty(demandPattern) || demandPattern == ";" || demandPattern == "_")
                {
                    demandPattern = "";
                }
                junctionSb.AppendLine($"{x.Id}\t{x.Elev}\t{x.Demand}\t{demandPattern}\t;\t" + $"0\tJunction");
                coorSb.AppendLine(x.Id + "    " + x.Position.X + "    " + x.Position.Y);
            });
            //水表处理
            network.Meters?.ForEach(x =>
            {
                var demandPattern = x.DemandPattern;
                if (string.IsNullOrEmpty(demandPattern) || demandPattern == ";" || demandPattern == "_")
                {
                    demandPattern = "";
                }
                junctionSb.AppendLine($"{x.Id}\t{x.Elev}\t{x.Demand}\t{demandPattern}\t;\t" + $"0\tMeters");
                coorSb.AppendLine(x.Id + "    " + x.Position.X + "    " + x.Position.Y);
            });
            //喷头处理
            network.Nozzles?.ForEach(x =>
            {
                var demandPattern = x.DemandPattern;
                if (string.IsNullOrEmpty(demandPattern) || demandPattern == ";" || demandPattern == "_")
                {
                    demandPattern = "";
                }
                junctionSb.AppendLine($"{x.Id}\t{x.Elev}\t{x.Demand}\t{demandPattern}\t;\t" + $"0\tNozzle\t{x.Coefficient}");
                double coefficient = x.Coefficient * Math.Pow(10 / 101.972, 0.5) / 1000 * 60;
                emitterSb.AppendLine(x.Id + "    " + coefficient);
                coorSb.AppendLine(x.Id + "    " + x.Position.X + "    " + x.Position.Y);
            });
            var junctionString = junctionSb.ToString();
 
            //水库处理
            var reservoirSb = new StringBuilder();
            reservoirSb.AppendLine(";ID                  Head            Pattern ");
            network.Reservoirs?.ForEach(x =>
            {
                reservoirSb.AppendLine($"{x.Id}\t{x.Head}\t{x.HeadPattern}\t;\t" + $"0\t{x.PoolElev}");
                coorSb.AppendLine(x.Id + "    " + x.Position.X + "    " + x.Position.Y);
            });
            string reserverString = reservoirSb.ToString();
 
            //水池处理
            var tankSb = new StringBuilder();
            tankSb.AppendLine(";ID                  Elevation       InitLevel       MinLevel        MaxLevel        Diameter        MinVol          VolCurve            Overflow");
            network.GetTanks()?.ForEach(o =>
            {
                // tankStringBuilder.AppendLine($"{o.Id}\t{o.PoolElev}\t{o.InitLevel}\t{o.MinLevel}\t{o.MaxLevel}\t{o.Diameter}\t{o.MinVol}\t{o.VolCurve}\t{o.Overflow}\t;\t");// + $"0");
                tankSb.AppendLine($"{o.Id}\t{o.PoolElev}\t{o.InitLevel}\t{o.MinLevel}\t{o.MaxLevel}\t{o.Diameter}\t{o.MinVol}\t{o.VolCurve}\t;\t");// + $"0");
                coorSb.AppendLine(o.Id + "    " + o.Position.X + "    " + o.Position.Y);
            });
            string tankString = tankSb.ToString();
 
            //管道处理
            var pipeSb = new StringBuilder();
            pipeSb.AppendLine(";ID                  Node1               Node2               Length          Diameter        Roughness       MinorLoss       Status");
            network.GetAllPipes()?.ForEach(x =>
            {
                if (x.Roughness == 0)
                {
                    x.Roughness = 110;
                }
                string statusString = x.LinkStatus == PipeStatus.Closed ? "CLOSED" : "";
                pipeSb.AppendLine($"{x.Id}\t{x.StartNode.Id}\t{x.EndNode.Id}\t{x.Length}\t{x.Diameter}\t{x.Roughness}\t{x.MinorLoss}\t{statusString}\t;\t");// + $"{p.Level}");
                if (x.LinkStatus != PipeStatus.Open)
                {
                    statusSb.AppendLine(x.Id + "\t" + statusString);
                }
            });
            string pipeString = pipeSb.ToString();
 
            //阀门处理
            var valveSb = new StringBuilder();
            valveSb.AppendLine(";ID                  Node1               Node2               Diameter        Type    Setting         MinorLoss  ");
            network.Valves?.ForEach(x =>
            {
                valveSb.AppendLine($"{x.Id}\t{x.StartNode.Id}\t{x.EndNode.Id}\t{x.Diameter:F4}\t{x.ValveType}\t{x.ValveSetting}\t{x.MinorLoss:F4}\t;\t");// + $"0");
                if (!string.IsNullOrEmpty(x.LinkStatus))
                {
                    statusSb.AppendLine(x.Id + "\t" + x.LinkStatus);
                }
            });
            network.GetAllResistances()?.ForEach(x =>
            {
                string type = null;
                if (x is Exchanger)
                {
                    type = "Exchanger";
                }
                else if (x is Compressor)
                {
                    type = "Compressor";
                }
                valveSb.AppendLine($"{x.Id}\t{x.StartNode.Id}\t{x.EndNode.Id}\t{x.Diameter:F4}\tGPV\t{x.CurveQL}\t{x.MinorLoss:F4}\t;\t{type}");// + $"0");
                if (!string.IsNullOrEmpty(x.LinkStatus))
                {
                    statusSb.AppendLine(x.Id + "\t" + x.LinkStatus);
                }
            });
            string valveString = valveSb.ToString();
 
            //水泵处理
            var pumpSb = new StringBuilder();
            pumpSb.AppendLine(";ID                  Node1               Node2               Parameters  ");
            network.Pumps?.ForEach(o =>
            {
                pumpSb.AppendLine($"{o.Id}\t{o.StartNode.Id}\t{o.EndNode.Id}\tHead\t{o.CurveQH}\t;\t");// + $"0");
                string statusString = null;
                if (o.LinkStatus == PipeStatus.Closed)
                {
                    statusString = "\tCLOSED";
                }
                else
                {
                    if (o.SpeedRatio == null)
                        statusString = "\tOPEN";
                    else
                        statusString = $"\t{o.SpeedRatio}";
                }
                statusSb.AppendLine(o.Id + statusString);
            });
            string pumpString = pumpSb.ToString();
 
            //曲线处理
            var curveSb = new StringBuilder();
            curveSb.AppendLine(@";ID                  X-Value         Y-Value
                                            ;HEADLOSS: 
                                             GPVDefault          0               0     
                                             GPVDefault          100             0           
                                            ;PUMP: 
                                             PumpDefault         0               45.38       
                                             PumpDefault         83.33333333     45.25       
                                             PumpDefault         111.1111111     45.12       
                                             PumpDefault         138.8888889     44.96       
                                             PumpDefault         166.6666667     44.76       
                                             PumpDefault         194.4444444     44.52       
                                             PumpDefault         222.2222222     44.24       
                                             PumpDefault         250             43.92       
                                             PumpDefault         277.7777778     43.56       
                                             PumpDefault         305.5555556     43.17       
                                             PumpDefault         333.3333333     42.73       
                                             PumpDefault         361.1111111     42.25       
                                             PumpDefault         388.8888889     41.74       
                                             PumpDefault         416.6666667     41.18       
                                             PumpDefault         444.4444444     40.58       
                                             PumpDefault         472.2222222     39.95       
                                             PumpDefault         500             39.28       
                                             PumpDefault         527.7777778     38.56       
                                             PumpDefault         555.5555556     37.81       
                                             PumpDefault         583.3333333     37.02       
                                             PumpDefault         611.1111111     36.19       
                                             PumpDefault         638.8888889     35.32       
                                             PumpDefault         666.6666667     34.41       
                                             PumpDefault         694.4444444     33.46       
                                             PumpDefault         722.2222222     32.47       
                                             PumpDefault         750             31.44       
                                             PumpDefault         777.7777778     30.37       
                                             PumpDefault         805.5555556     29.27    ");
            network.Curves?.ForEach(o =>
            {
                var curvePtList = o.CurveData?.OrderBy(x => x.X).ToList();
                foreach (var curvePt in curvePtList)
                {
                    curveSb.AppendLine($"{o.Id}  {curvePt.X} {curvePt.Y}");
                }
                //curveStringBuilder.AppendLine(o.ToString());
            });
            string curveString = curveSb.ToString();
 
            string coorString = coorSb.ToString();
            string output = "";
            string emitterString = emitterSb.ToString();
            string statusString = statusSb.ToString();
            output = templateString;
            output = ReplaceContent(output, "JUNCTIONS", junctionString);
            output = ReplaceContent(output, "RESERVOIRS", reserverString);
            output = ReplaceContent(output, "TANKS", tankString);
            output = ReplaceContent(output, "PIPES", pipeString);
            output = ReplaceContent(output, "VALVES", valveString);
            output = ReplaceContent(output, "PUMPS", pumpString);
            output = ReplaceContent(output, "CURVES", curveString);
            output = ReplaceContent(output, "COORDINATES", coorString);
            output = ReplaceContent(output, "EMITTERS", emitterString);
            output = ReplaceContent(output, "STATUS", statusString);
 
 
 
 
 
 
            return output;
        }
 
 
        private static string ReplaceContent(string text, string content, string replaceString)
        {
 
 
            string str = replaceString;
 
            string replacedText = ReplaceCoordinatesSection(text, content, str);
 
            return replacedText;
            //Console.WriteLine(replacedText);
        }
        public static string ReplaceCoordinatesSection(string text, string content, string str)
        {
            string pattern = $@"(\[{content}\]).*?(\[|$)";
 
            string replacedText = Regex.Replace(text, pattern, match =>
            {
                string section = match.Groups[2].Value.Trim();
 
                if (!string.IsNullOrEmpty(section))
                {
                    return $"{match.Groups[1].Value}\n{str}\n[";
                }
                else
                {
                    return $"{match.Groups[1].Value}\n{str}\n[";
                }
            }, RegexOptions.Singleline);
 
            return replacedText;
        }
 
        /// <summary>
        /// 从Inp字符串中解析
        /// </summary>
        public static Network FromInpString(string inpString)
        {
            string InpPath = inpString;
            var net = new Network();
            if (InpPath == null || !File.Exists(InpPath)) return null;
            List<InpCoor> points = new List<InpCoor>();
 
            StreamReader sr = new StreamReader(InpPath);
            //try
            {
                string line;
                string section = "";
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Trim().StartsWith("["))
                    {
                        section = line.TrimStart('[').TrimEnd(']');
                    }
                    else
                    {
                        string s = line.Trim('\t').Trim(' ');
                        if (s.Length == 0 || s[0] == ';') continue;
                        line = line.Replace("\t\t", "\t_\t").Replace("\t \t", "\t_\t");
                        InpParts parts = new InpParts(line.Split(new char[] { '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries));
                        switch (section)
                        {
                            case "JUNCTIONS":
                                {
                                    var ID = parts[0];
                                    float elev;
                                    float.TryParse(parts[1], out elev);
                                    var Elev = elev;
                                    float demand;
                                    float.TryParse(parts[2], out demand);
                                    var Demand = demand;
                                    var PatternID = parts[3];
                                    int level;
                                    int.TryParse(parts[6], out level);
                                    var Level = level;
 
                                    if (parts[-2] == "Meter")
                                    {
                                        net.Meters.Add(new Meter()
                                        {
                                            Id = ID,
                                            Elev = Elev,
                                            Demand = Demand,
                                            DemandPattern = PatternID,
                                        });
                                    }
                                    else if (parts[-2] == "Nozzle")
                                    {
                                        net.Nozzles.Add(new Nozzle()
                                        {
                                            Id = ID,
                                            Elev = Elev,
                                            Demand = Demand,
                                            DemandPattern = PatternID,
                                        });
                                    }
                                    else
                                    {
                                        net.Junctions.Add(new Junction()
                                        {
                                            Id = ID,
                                            Elev = Elev,
                                            Demand = Demand,
                                            DemandPattern = PatternID,
                                        });
                                    }
 
                                }
                                break;
                            case "RESERVOIRS":
                                {
                                    var r = new Reservoir();
                                    r.Id = parts[0];
                                    float head;
                                    if (float.TryParse(parts[1], out head))
                                        r.Head = head;
                                    r.HeadPattern = parts.Length > 2 ? parts[2] : "";
                                    int level;
                                    //if (int.TryParse(parts[3], out level))
                                    //    r.Level = level;
                                    net.Reservoirs.Add(r);
                                }
                                break;
                            case "TANKS":
                                {
                                    //TankModel tank = new TankModel(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7], parts[8]);
                                    float initLevel = 0;
                                    float.TryParse(parts[2], out initLevel);
 
                                    float minLevel = 0;
                                    float.TryParse(parts[3], out minLevel);
 
                                    float maxLevel = 0;
                                    float.TryParse(parts[4], out maxLevel);
 
                                    float diamter = 0;
                                    float.TryParse(parts[5], out diamter);
 
                                    float minVol = 0;
                                    float.TryParse(parts[6], out minVol);
                                    var tank = new Tank()
                                    {
                                        Id = parts[0],
                                        PoolElev = float.Parse(parts[1]),
                                        InitLevel = initLevel,
                                        MinLevel = minLevel,
                                        MaxLevel = maxLevel,
                                        Diameter = diamter,
                                        MinVol = minVol,
                                        VolCurve = "",
                                    };
                                    //int level;
                                    //if (int.TryParse(parts[9], out level))
                                    //    tank.Level = level;
                                    net.Tanks.Add(tank);
                                }
                                break;
                            case "PIPES":
                                {
                                    InpPipe p = new InpPipe();
                                    p.Id = parts[0];
                                    p.Node1 = parts[1];
                                    p.Node2 = parts[2];
                                    float length;
                                    if (float.TryParse(parts[3], out length))
                                        p.Length = length;
                                    float diameter;
                                    if (float.TryParse(parts[4], out diameter))
                                        p.Diameter = diameter;
                                    float roughness;
                                    if (float.TryParse(parts[5], out roughness))
                                        p.Roughness = roughness;
                                    float minorLoss;
                                    if (float.TryParse(parts[6], out minorLoss))
                                        p.MinorLoss = minorLoss;
                                    p.LinkStatus = parts.Length > 7 ? PipeStatus.Closed : PipeStatus.Open;
                                    //int level;
                                    //if (int.TryParse(parts[8], out level))
                                    //    p.Level = level;
                                    net.Pipes.Add(p);
                                }
                                break;
                            case "VALVES":
                                {
 
                                    if (parts[-2] == "Exchanger")
                                    {
                                        InpHeatExchanger valve = new InpHeatExchanger();
                                        valve.Id = parts[0];
 
                                        // 取出Node1和Node2中的字母部分,例如“S201326593”被取出为“201326593”
                                        valve.Node1 = parts[1]; // Regex.Replace(parts[1], "[^0-9]", "");
                                        valve.Node2 = parts[2]; // Regex.Replace(parts[2], "[^0-9]", "");
 
                                        float diameter;
                                        if (float.TryParse(parts[3], out diameter))
                                            valve.Diameter = diameter;
                                        valve.CurveQL = parts[5];
                                        float minorLoss;
                                        if (float.TryParse(parts[6], out minorLoss))
                                            valve.MinorLoss = minorLoss;
 
                                        net.Exchangers.Add(valve);
                                    }
                                    else if (parts[-2] == "Compressor")
                                    {
                                        InpAirCompressor valve = new InpAirCompressor();
                                        valve.Id = parts[0];
 
                                        // 取出Node1和Node2中的字母部分,例如“S201326593”被取出为“201326593”
                                        valve.Node1 = parts[1]; // Regex.Replace(parts[1], "[^0-9]", "");
                                        valve.Node2 = parts[2]; // Regex.Replace(parts[2], "[^0-9]", "");
 
                                        float diameter;
                                        if (float.TryParse(parts[3], out diameter))
                                            valve.Diameter = diameter;
                                        valve.CurveQL = parts[5];
                                        float minorLoss;
                                        if (float.TryParse(parts[6], out minorLoss))
                                            valve.MinorLoss = minorLoss;
 
                                        net.Compressors.Add(valve);
                                    }
                                    else
                                    {
                                        InpValve valve = new InpValve();
                                        valve.Id = parts[0];
 
                                        // 取出Node1和Node2中的字母部分,例如“S201326593”被取出为“201326593”
                                        valve.Node1 = parts[1]; // Regex.Replace(parts[1], "[^0-9]", "");
                                        valve.Node2 = parts[2]; // Regex.Replace(parts[2], "[^0-9]", "");
 
                                        float diameter;
                                        if (float.TryParse(parts[3], out diameter))
                                            valve.Diameter = diameter;
                                        valve.ValveType = parts[4];
                                        valve.ValveSetting = parts[5];
                                        float minorLoss;
                                        if (float.TryParse(parts[6], out minorLoss))
                                            valve.MinorLoss = minorLoss;
                                        net.Valves.Add(valve);
                                    }
 
                                }
                                break;
 
                            case "COORDINATES":
                                {
                                    string id = parts[0];
                                    double x;
                                    double y;
                                    if (double.TryParse(parts[1], out x) && double.TryParse(parts[2], out y))
                                    {
                                        points.Add(new InpCoor(id, new Position2d(x, y)));
                                    }
                                }
                                break;
                            case "EMITTERS":
                                {
                                    string id = parts[0];
                                    if (double.TryParse(parts[1], out double x))
                                    {
                                        double Coefficient = x / Math.Pow(10 / 101.972, 0.5) * 1000 / 60;
                                        net.Nozzles.Find(o => o.Id == id).Coefficient = Coefficient;
                                    }
                                }
                                break;
                        }
                    }
                }
                sr.Close();
 
 
                #region 优化方案
                int k1 = 0;
                int k2 = 0;
 
                var Nodes = net.GetAllNodes();
                var Links = net.GetAllLinks();
                Nodes.Sort((a, b) => string.Compare(a.Id, b.Id));
                points.Sort((a, b) => string.Compare(a.Id, b.Id));
                k1 = 0;
                k2 = 0;
                while (k1 < Nodes.Count)
                {
                    var J = Nodes[k1];
                    var coor = points[k2];
 
                    while (J.Id != coor.Id && k2 < points.Count)
                    {
                        k2++;
                        if (k2 < points.Count) coor = points[k2];
 
                    }
                    if (k2 == points.Count)
                    {
                        break;
                        //throw new Exception($"未找到Node[{J.Id}]的坐标");
                    }
 
                    J.Position = new Position2d();
                    J.Position.X = coor.Position.X;
                    J.Position.Y = coor.Position.Y;
 
                    k1++;
                }
                #endregion
 
 
 
                //建立点线关系链表StartNode,先将管线以Node1(节点1的ID)排序,再将Nodes按ID排序,建立两个游标k1、k2,正向一次循环,建立链表关系
                //时间复杂度 O(n)
                Links.Sort((a, b) => string.Compare(((IInpParser)a).Node1, ((IInpParser)b).Node1));
                k1 = 0;
                k2 = 0;
                while (k2 < Links.Count)
                {
                    var p = Links[k2];
                    var J = Nodes[k1];
                    while (J.Id != ((IInpParser)p).Node1 && k1 < Nodes.Count)
                    {
                        k1++;
                        if (k1 < Nodes.Count) J = Nodes[k1];
 
                    }
                    if (k1 == Nodes.Count)
                    {
                        break;
                        //throw new Exception($"未找到Link[{p.Id}]的起始节点[{((IInpParser)p).Node1}]");
                    }
                    p.StartNode = J;
                    if (J.Links == null) J.Links = new List<Link>();
                    J.Links.Add(p);
                    k2++;
                }
 
                //建立点线关系链表StartNode,先将管线以Node2(节点1的ID)排序,再将Nodes按ID排序,建立两个游标k1、k2,正向一次循环,建立链表关系
                //时间复杂度 O(n)
                Links.Sort((a, b) => string.Compare(((IInpParser)a).Node2, ((IInpParser)b).Node2));
                k1 = 0;
                k2 = 0;
                while (k2 < Links.Count)
                {
                    var p = Links[k2];
                    var J = Nodes[k1];
                    while (J.Id != ((IInpParser)p).Node2 && k1 < Nodes.Count)
                    {
                        k1++;
                        if (k1 < Nodes.Count) J = Nodes[k1];
                    }
                    if (k1 == Nodes.Count)
                    {
                        break;
                        //throw new Exception($"未找到Link[{p.Id}]的终止节点[{((IInpParser)p).Node2}]");
                    }
                    p.EndNode = J;
                    if (J.Links == null) J.Links = new List<Link>();
                    J.Links.Add(p);
                    k2++;
                }
                return net;
            }
        }
 
    }
}