xyc
2024-03-14 42655cbb3b63d6af964c5350035e6db3ea9fadd5
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
package com.zt.life.modules.taskReliability.service;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.zt.common.service.BaseService;
import com.zt.common.utils.JsonUtils2;
import com.zt.common.utils.UUIDUtil;
import com.zt.life.modules.taskReliability.dao.*;
import com.zt.life.modules.taskReliability.dto.ModelLinePair;
import com.zt.life.modules.taskReliability.model.*;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zt.common.db.query.QueryFilter;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
 
/**
 * model_line
 *
 * @author zt generator 
 * @since 1.0.0 2024-02-28
 */
@Service
public class ModelLineService  extends BaseService<ModelLineDao, ModelLine> {
    @Autowired
    private ModelLineDao modelLineDao;
    @Autowired
    private ModelNodeDao modelNodeDao;
    @Autowired
    private ModelRbdDao modelRbdDao;
    @Autowired
    private AlgorithmDao algorithmDao;
    @Autowired
    private ModelNodeAlgorithmDao modelNodeAlgorithmDao;
 
    /**
     * 分页查询
     *
     * @param queryFilter
     * @return
     */
    public List<ModelLine> page(QueryFilter queryFilter) {
        return baseDao.getList(queryFilter.getQueryParams());
    }
 
    public ModelRbd getDiagram(Long modelId) {
        return modelRbdDao.getDiagram(modelId);
    }
 
    /**
     * 删除
     *
     * @param ids
     */
    public void delete(Long[] ids) {
        super.deleteLogic(ids);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void insert(ModelRbd modelRbd) {
        if (modelRbd==null) return;
        if (modelRbd.getId()==null) return;
        Long modelId = modelRbd.getId();
 
        // 删除既有数据
        modelRbdDao.deleteByModelId(modelId);
 
        // 插入数据
        modelRbd.setId(UUIDUtil.generateId());
        modelRbdDao.insert(modelRbd);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void update(ModelRbd modelRbd) {
        if (modelRbd==null) return;
        if (modelRbd.getId()==null) return;
        Long modelId = modelRbd.getId();
 
        // 删除既有数据
        modelRbdDao.deleteByModelId(modelId);
 
        modelRbdDao.insert(modelRbd);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public boolean analyze(ModelRbd modelRbd) {
        boolean result = true;
        if (modelRbd==null) return result;
        Long modelId = modelRbd.getId();
        result = analyzeRbd(modelId, modelRbd.getContent(), true);
        return result;
    }
 
    private boolean analyzeRbd(Long modelId, String content, boolean saveFlag) {
        boolean result = true;
        String rbdsonStr = content;
 
        JSONArray rbdJsonArray = new JSONObject(rbdsonStr).getJSONArray("cells");
        if (rbdJsonArray == null) return result;
 
        // 解析结果存放list
        List<ModelNode> modelNodeList = new ArrayList<>();
        List<ModelLine> modelLineList = new ArrayList<>();
        List<Algorithm> algorithmList = new ArrayList<>();
 
        // 1. 解析出节点与边
        getNodeAndLineFromRbd(modelId, rbdJsonArray, modelNodeList, modelLineList);
        // 2. 计算所有节点的入口线数及出口线数
        calcInOutLineNumAllNode(modelNodeList, modelLineList);
        // 3. 复制产品节点(node)到list
        List<ModelNode> modelNodeAndVnodeList = modelNodeList.stream().filter(item ->
                "node".equals(item.getNodeType())).collect(Collectors.toList());
        // 4. 不断将基本模型(串联、并联、旁联、表决、桥联)替换为虚节点而简化图形,直至无法简化为止。
        result = getAlgorithmFromRbd(modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList);
 
        // 5. 保存模型
        if (saveFlag) saveModel(modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList);
 
        return result;
    }
 
    private boolean getAlgorithmFromRbd(Long modelId,
                                     List<ModelNode> modelNodeList,
                                     List<ModelLine> modelLineList,
                                     List<Algorithm> algorithmList,
                                     List<ModelNode> modelNodeAndVnodeList) {
        // 根据以下的构图规则来进行算法分解:
        // 1、节点的定义
        // 1-1) 运算节点:共2个:旁联、表决,且运算节点需放在被运算节点的右侧。
        // 1-2) 产品节点:产品模型中的节点
        // 1-3) 连接节点:共3个:start、end、connect
        // 1-4) 虚节点:代替基本模型,构图规则上等效于产品节点
        // 2、基本模型的定义
        // 2-1) 串联:没有专门的表示符号,由节点间的连接关系定义。2个及以上的节点(产品节点或虚节点)只构成1条路径,终端节点只有1个出口(入口个数无限制)。
        // 2-2) 并联:没有专门的表示符号,由节点间的连接关系定义。2个及以上的节点(产品节点或虚节点)都只有1个入口和出口,且这些节点拥有同一个入口节点和同一个出口节点。
        // 2-3) 旁联:由专门的旁联运算符表示,每条旁联支路只有1个节点(产品节点或虚节点),这些节点都以该旁联节点为出口节点,且这些节点拥有同一个入口节点。
        // 2-4) 表决:由专门的表决运算符表示,每条表决支路只有1个节点(产品节点或虚节点),这些节点都以该表决节点为出口节点,且这些节点拥有同一个入口节点。
        // 2-5) 桥联:没有专门的表示符号,由节点间的连接关系定义。
        // 3、根据以上基本模型的定义,不断将基本模型简化为虚节点,直至简化完毕,或者无法简化为止。
        boolean isEnd = false; // 图形简化完毕(全部简化没了)
        boolean hasSimplified = false; // 至少简化了一处图形
        do {
            hasSimplified = false;
            hasSimplified = simplifySeries(modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList, hasSimplified);
            hasSimplified = simplifyParallel(modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList, hasSimplified);
            hasSimplified = simplifyOperator("switch", modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList, hasSimplified);
            hasSimplified = simplifyOperator("vote", modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList, hasSimplified);
            hasSimplified = simplifyBridge(modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList, hasSimplified);
            isEnd = simplifyEnd(modelId, modelNodeList, modelLineList, algorithmList, modelNodeAndVnodeList);
        } while (!isEnd && hasSimplified);
 
        return isEnd;
    }
 
    private boolean simplifyEnd(Long modelId,
                                List<ModelNode> modelNodeList,
                                List<ModelLine> modelLineList,
                                List<Algorithm> algorithmList,
                                List<ModelNode> modelNodeAndVnodeList) {
        ModelNode endNode = modelNodeList.stream().filter(item ->
                "end".equals(item.getNodeType())).collect(Collectors.toList()).get(0);
        if (endNode.getInLineNum()!=1) return false;
 
        ModelLine lineToEnd = modelLineList.stream().filter(item ->
                item.getEndCell().equals(endNode.getPicId())).collect(Collectors.toList()).get(0);
        ModelNode node = modelNodeList.stream().filter(item ->
                lineToEnd.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        if (node.getInLineNum()!=1 || node.getOutLineNum()!=1) return false;
 
        ModelLine lineToNode = modelLineList.stream().filter(item ->
                item.getEndCell().equals(node.getPicId())).collect(Collectors.toList()).get(0);
        ModelNode startNode = modelNodeList.stream().filter(item ->
                lineToNode.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        if (!"start".equals(startNode.getNodeType()) || startNode.getOutLineNum()!=1) return false;
 
        // 删除节点及连线
        modelLineList.remove(lineToEnd);
        modelLineList.remove(lineToNode);
        modelNodeList.remove(endNode);
        modelNodeList.remove(node);
        modelNodeList.remove(startNode);
 
        // 生成结束的算法步骤
        // 新增algorithm
        Algorithm algorithm = new Algorithm();
        Long id = UUIDUtil.generateId();
        algorithm.setId(id);
        algorithm.setModelId(modelId);
        algorithm.setComputerId(id);
        algorithm.setModelType(endNode.getNodeType());
        algorithm.setAlgorithmType(endNode.getNodeType());
        algorithm.setComputerList(node.getId().toString());
        algorithm.setObjectList(node.getName());
        algorithm.setStep(algorithmList.size()==0 ? 0 : algorithmList.get(algorithmList.size()-1).getStep()+1);
        algorithmList.add(algorithm);
 
        // 新增结束的虚节点
        ModelNode vnode = new ModelNode();
        vnode.setId(id);
        vnode.setPicId(id.toString());
        vnode.setModelId(modelId);
        vnode.setNodeType("vnode");
        vnode.setName("v"+algorithm.getStep());
        modelNodeAndVnodeList.add(vnode);
 
        return true;
    }
 
    private boolean simplifySeries(Long modelId,
                                   List<ModelNode> modelNodeList,
                                   List<ModelLine> modelLineList,
                                   List<Algorithm> algorithmList,
                                   List<ModelNode> modelNodeAndVnodeList,
                                   boolean hasSimplified) {
        List<ModelNode> startNodes = modelNodeList.stream().filter(item ->
                "node,vnode,connect,end".contains(item.getNodeType())).collect(Collectors.toList());
        if (startNodes.size()==0) return hasSimplified;
 
        for (ModelNode startNode : startNodes) {
            if (startNode.getInLineNum()!=1) continue;
 
            List<ModelNode> result = new ArrayList<>();
            if (!"end".equals(startNode.getNodeType())) result.add(startNode);
            seekPathSeries(modelNodeList, modelLineList, startNode, result);
            if (result.size()<2) continue;
 
            ModelNode endNode = result.get(result.size()-1);
            if ("start,switch,vote".contains(endNode.getNodeType()) || endNode.getOutLineNum()!=1) {
                result.remove(endNode);
            }
            List<ModelNode> realSeriesNodes = result.stream().filter(item ->
                    !"connect".equals(item.getNodeType())).collect(Collectors.toList());
            if (realSeriesNodes.size()<1) {
                if (result.size() < 2) continue;
                // 替换成连线
                replaceToLineSeries(modelNodeList, modelLineList, result);
            } else if (realSeriesNodes.size()==1) {
                if (result.size() < 2) continue;   // path上只有该产品节点(node/vnode)自己,无需做什么
                // 将path替换成该节点
                replaceToTheNodeSeries(modelNodeList, modelLineList,
                        result, realSeriesNodes.get(0));
            } else {
                // 将path替换成虚节点
                replaceToVnodeSeries(modelId, modelNodeList, modelLineList,
                        algorithmList, modelNodeAndVnodeList, result, realSeriesNodes);
            }
            hasSimplified = true;
        }
 
        return hasSimplified;
    }
 
    private void replaceToLineSeries(List<ModelNode> modelNodeList,
                                     List<ModelLine> modelLineList,
                                     List<ModelNode> path) {
        // 获取path的起点的出口线
        ModelNode finalNodeStart = path.get(0);
        ModelNode finalNodeEnd = path.get(path.size()-1);
        List<ModelLine> outLines = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(finalNodeStart.getPicId())).collect(Collectors.toList());
 
        // 删除path上的所有中间连线及path的结束点以外的节点
        for (int i=0; i<path.size(); i++) {
            ModelNode finalNode = path.get(i);
            if (i < path.size() - 1) {
                // 将入口线删除
                List<ModelLine> lines = modelLineList.stream().filter(item ->
                        item.getEndCell().equals(finalNode.getPicId())).collect(Collectors.toList());
                modelLineList.removeAll(lines);
                // 删除节点
                modelNodeList.remove(finalNode);
                calcInOutLineNum(finalNode, modelLineList);
            }
        }
 
        // 将path的起点的出口线改为连接到path的结束点
        for (ModelLine line : outLines) {
            line.setBeginCell(finalNodeEnd.getPicId());
        }
 
        calcInOutLineNum(finalNodeStart, modelLineList);
        calcInOutLineNum(finalNodeEnd, modelLineList);
    }
 
    private void replaceToTheNodeSeries(List<ModelNode> modelNodeList,
                                      List<ModelLine> modelLineList,
                                      List<ModelNode> path,
                                      ModelNode theNode) {
        // 获取path的起点的出口线
        ModelNode finalNodeStart = path.get(0);
        List<ModelLine> outLines = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(finalNodeStart.getPicId())).collect(Collectors.toList());
        // 获取path的结束点的入口线
        ModelNode finalNodeEnd = path.get(path.size()-1);
        List<ModelLine> inLines = modelLineList.stream().filter(item ->
                item.getEndCell().equals(finalNodeEnd.getPicId())).collect(Collectors.toList());
 
        // 删除path上的所有中间连线及theNode以外的节点
        for (int i=0; i<path.size(); i++) {
            ModelNode finalNode = path.get(i);
            if (i < path.size() - 1) {
                // 将入口线删除
                List<ModelLine> lines = modelLineList.stream().filter(item ->
                        item.getEndCell().equals(finalNode.getPicId())).collect(Collectors.toList());
                modelLineList.removeAll(lines);
            }
            if (!finalNode.getId().equals(theNode.getId())) {
                modelNodeList.remove(finalNode);
                calcInOutLineNum(finalNode, modelLineList);
            }
        }
 
        // 将path的起点的出口线改为连接到theNode
        for (ModelLine line : outLines) {
            line.setBeginCell(theNode.getPicId());
        }
        // 将path的结束点的入口线改为连接到theNode
        for (ModelLine line : inLines) {
            line.setEndCell(theNode.getPicId());
        }
 
        calcInOutLineNum(finalNodeStart, modelLineList);
        calcInOutLineNum(finalNodeEnd, modelLineList);
        calcInOutLineNum(theNode, modelLineList);
    }
 
    private void replaceToVnodeSeries(Long modelId,
                                      List<ModelNode> modelNodeList,
                                      List<ModelLine> modelLineList,
                                      List<Algorithm> algorithmList,
                                      List<ModelNode> modelNodeAndVnodeList,
                                      List<ModelNode> path,
                                      List<ModelNode> seriesNodes) {
        // 新增algorithm
        List<String> computerList = createVnodeComputerList("series", algorithmList, modelNodeAndVnodeList, seriesNodes);
        Algorithm algorithm = new Algorithm();
        Long id = UUIDUtil.generateId();
        algorithm.setId(id);
        algorithm.setModelId(modelId);
        algorithm.setComputerId(id);
        algorithm.setModelType("series");
        algorithm.setAlgorithmType("series");
        algorithm.setComputerList(computerList.get(0));
        algorithm.setObjectList(computerList.get(1));
        algorithm.setStep(algorithmList.size()==0 ? 0 : algorithmList.get(algorithmList.size()-1).getStep()+1);
        algorithmList.add(algorithm);
 
        // 新增虚节点
        ModelNode vnode = new ModelNode();
        vnode.setId(id);
        vnode.setPicId(id.toString());
        vnode.setModelId(modelId);
        vnode.setNodeType("vnode");
        vnode.setName("v"+algorithm.getStep());
        vnode.setPositionX(path.get(0).getPositionX());
        vnode.setPositionY(path.get(0).getPositionY());
        modelNodeList.add(vnode);
        modelNodeAndVnodeList.add(vnode);
 
        // 将path替换为该虚节点
        for (int i=0; i<path.size(); i++) {
            ModelNode finalNode = path.get(i);
            if (i==0) {
                // 将该节点的出口线改为连接到虚节点
                List<ModelLine> outLines = modelLineList.stream().filter(item ->
                        item.getBeginCell().equals(finalNode.getPicId())).collect(Collectors.toList());
                for (ModelLine line : outLines) {
                    line.setBeginCell(vnode.getPicId());
                }
                // 将该节点的入口线删除
                List<ModelLine> inLines = modelLineList.stream().filter(item ->
                        item.getEndCell().equals(finalNode.getPicId())).collect(Collectors.toList());
                modelLineList.removeAll(inLines);
            }
            if (i==path.size()-1) {
                // 将该节点的入口线改为连接到虚节点
                List<ModelLine> inLines = modelLineList.stream().filter(item ->
                        item.getEndCell().equals(finalNode.getPicId())).collect(Collectors.toList());
                for (ModelLine line : inLines) {
                    line.setEndCell(vnode.getPicId());
                }
                // 将该节点的出口线删除
                List<ModelLine> outLines = modelLineList.stream().filter(item ->
                        item.getBeginCell().equals(finalNode.getPicId())).collect(Collectors.toList());
                modelLineList.removeAll(outLines);
            }
            // 删除该节点的出入口线
            List<ModelLine> inOutLines = modelLineList.stream().filter(item ->
                    item.getEndCell().equals(finalNode.getPicId()) || item.getBeginCell().equals(finalNode.getPicId())
            ).collect(Collectors.toList());
            modelLineList.removeAll(inOutLines);
            // 删除该节点
            modelNodeList.remove(finalNode);
            calcInOutLineNum(finalNode, modelLineList);
        }
 
        calcInOutLineNum(vnode, modelLineList);
    }
 
    private List<String> createVnodeComputerList(String type,
                                                 List<Algorithm> algorithmList,
                                                 List<ModelNode> modelNodeAndVnodeList,
                                                 List<ModelNode> nodes) {
        // 调整节点顺序
        List<ModelNode> sortedNodes = null;
        if ("bridge".equals(type)) {
            if (nodes.get(1).getPositionY() < nodes.get(4).getPositionY()) {
                sortedNodes = nodes;
            } else {
                sortedNodes = new ArrayList<>();
                sortedNodes.add(nodes.get(3));
                sortedNodes.add(nodes.get(4));
                sortedNodes.add(nodes.get(2));
                sortedNodes.add(nodes.get(0));
                sortedNodes.add(nodes.get(1));
            }
        } else if ("parallel".equals(type)) {
            // 把虚节点内部的并联节点全部拿出来,进行大排序
            sortedNodes = new ArrayList<>();
            for (ModelNode node : nodes) {
                if ("node".equals(node.getNodeType())) {
                    sortedNodes.add(node);
                } else {
                    // vnode
                    Algorithm algorithm = algorithmList.stream().filter(item ->
                            item.getId().equals(node.getId())).collect(Collectors.toList()).get(0);
                    if (type.equals(algorithm.getAlgorithmType())) {
                        String[] pNodesStrArr = algorithm.getComputerList().split(",");
                        for (String pNodeStr : pNodesStrArr) {
                            sortedNodes.add(modelNodeAndVnodeList.stream().filter(item ->
                                    pNodeStr.equals(item.getId().toString())).collect(Collectors.toList()).get(0));
                        }
                        algorithmList.remove(algorithm);
                        modelNodeAndVnodeList.remove(node);
                    } else {
                        sortedNodes.add(node);
                    }
                }
            }
            sortedNodes.sort(Comparator.comparing(ModelNode::getPositionY));
        } else {
            sortedNodes = Arrays.asList(new ModelNode[nodes.size()]);
            Collections.copy(sortedNodes, nodes);
            if ("series".equals(type)) {
                Collections.reverse(sortedNodes);
            } else {
                sortedNodes.sort(Comparator.comparing(ModelNode::getPositionY));
            }
        }
 
        String computerIdList = "";
        String objectList = "";
        if ("parallel".equals(type)) {
            for (ModelNode node : sortedNodes) {
                objectList += "".equals(objectList) ? node.getName() : "," + node.getName();
                computerIdList += "".equals(computerIdList) ? node.getId().toString() : "," + node.getId().toString();
            }
        } else {
            for (ModelNode node : sortedNodes) {
                if ("node".equals(node.getNodeType())) {
                    objectList += "".equals(objectList) ? node.getName() : "," + node.getName();
                    computerIdList += "".equals(computerIdList) ? node.getId().toString() : "," + node.getId().toString();
                } else {
                    // vnode
                    Algorithm algorithm = algorithmList.stream().filter(item ->
                            item.getId().equals(node.getId())).collect(Collectors.toList()).get(0);
                    if ("series".equals(type) && type.equals(algorithm.getAlgorithmType())) {
                        objectList += "".equals(objectList) ? algorithm.getObjectList() : "," + algorithm.getObjectList();
                        computerIdList += "".equals(computerIdList) ? algorithm.getComputerList() : "," + algorithm.getComputerList();
                        algorithmList.remove(algorithm);
                        modelNodeAndVnodeList.remove(node);
                    } else {
                        objectList += "".equals(objectList) ? node.getName() : "," + node.getName();
                        computerIdList += "".equals(computerIdList) ? node.getId().toString() : "," + node.getId().toString();
                    }
                }
            }
        }
 
        List<String> result = new ArrayList<>();
        result.add(computerIdList);
        result.add(objectList);
        return result;
    }
 
    private void seekPathSeries(List<ModelNode> modelNodeList,
                                List<ModelLine> modelLineList,
                                ModelNode startNode,
                                List<ModelNode> result) {
        ModelLine inLine = modelLineList.stream().filter(item ->
                item.getEndCell().equals(startNode.getPicId())).collect(Collectors.toList()).get(0);
        ModelNode pathNode = modelNodeList.stream().filter(item ->
                inLine.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        result.add(pathNode);
        if (pathNode.getOutLineNum()!=1 || pathNode.getInLineNum()!=1) return;
        if ("switch,vote".contains(pathNode.getNodeType())) return;
        seekPathSeries(modelNodeList, modelLineList, pathNode, result);
    }
 
    private boolean simplifyParallel(Long modelId,
                                     List<ModelNode> modelNodeList,
                                     List<ModelLine> modelLineList,
                                     List<Algorithm> algorithmList,
                                     List<ModelNode> modelNodeAndVnodeList,
                                     boolean hasSimplified) {
        boolean hasSimplifiedMe = false;
        List<ModelNode> startNodes = modelNodeList.stream().filter(item ->
                "node,vnode,connect,end".contains(item.getNodeType())).collect(Collectors.toList());
        if (startNodes.size()==0) return hasSimplified;
 
        for (ModelNode startNode : startNodes) {
            if (startNode.getInLineNum()<2) continue;
            List<ModelLine> inLines = modelLineList.stream().filter(item ->
                    item.getEndCell().equals(startNode.getPicId())).collect(Collectors.toList());
            hasSimplifiedMe = simplifyParallelGroup(modelId, modelNodeList, modelLineList,
                    algorithmList, modelNodeAndVnodeList, startNode, inLines);
            if (hasSimplifiedMe) {
                hasSimplified = true;
            }
        }
 
        return hasSimplified;
    }
 
    private boolean simplifyParallelGroup(Long modelId,
                                        List<ModelNode> modelNodeList,
                                        List<ModelLine> modelLineList,
                                        List<Algorithm> algorithmList,
                                        List<ModelNode> modelNodeAndVnodeList,
                                        ModelNode startNode,
                                        List<ModelLine> lines) {
        boolean hasSimplified = false;
        List<List<ModelNode>> pathList = new ArrayList<>();
        for (ModelLine line : lines) {
            List<ModelNode> path = getOneBranchParallel(modelNodeList, modelLineList, line);
            if (path.size() > 0) pathList.add(path);
        }
        Map<ModelNode, List<List<ModelNode>>> endNodePathsMap = groupingPathByEndNode(pathList);
        for (Map.Entry<ModelNode, List<List<ModelNode>>> entry : endNodePathsMap.entrySet()) {
            List<List<ModelNode>> pathOneGroup = entry.getValue();
            if (pathOneGroup.size()>1) {
                List<ModelNode> branchNodeList = new ArrayList<>();
                ModelNode endNode = getBranchNodesOneParallel(pathOneGroup, branchNodeList);
                if ("connect".equals(startNode.getNodeType()) && pathOneGroup.size()==lines.size()) {
                    // 替换成虚节点
                    replaceToVnode("parallel", modelId, modelNodeList, modelLineList,
                            algorithmList, modelNodeAndVnodeList, startNode, endNode, branchNodeList);
                } else {
                    // 新增虚节点
                    ModelNode vnode = new ModelNode();
                    Long id = UUIDUtil.generateId();
                    vnode.setId(id);
                    vnode.setPicId(id.toString());
                    vnode.setModelId(modelId);
                    vnode.setNodeType("vnode");
                    // 位置设置为并联中第一个节点的位置
                    vnode.setPositionX(branchNodeList.get(0).getPositionX());
                    vnode.setPositionY(branchNodeList.get(0).getPositionY());
//                    vnode.setPositionX(startNode.getPositionX());
//                    vnode.setPositionY(startNode.getPositionY());
                    modelNodeList.add(vnode);
                    modelNodeAndVnodeList.add(vnode);
                    ModelLine modelLineNew = new ModelLine();
                    Long picId2 = UUIDUtil.generateId();
                    modelLineNew.setId(picId2);
                    modelLineNew.setPicId(picId2.toString());
                    modelLineNew.setModelId(modelId);
                    modelLineNew.setBeginCell(vnode.getPicId());
                    modelLineNew.setEndCell(startNode.getPicId());
                    modelLineList.add(modelLineNew);
                    replaceToVnode("parallel", modelId, modelNodeList, modelLineList,
                            algorithmList, modelNodeAndVnodeList, vnode, endNode, branchNodeList);
                    calcInOutLineNum(startNode, modelLineList);
                }
                hasSimplified = true;
            }
        }
 
        return hasSimplified;
    }
 
    private List<ModelNode> getOneBranchParallel(List<ModelNode> modelNodeList,
                                                 List<ModelLine> modelLineList,
                                                 ModelLine line) {
        List<ModelNode> path = new ArrayList<>();
        ModelNode branchNode = modelNodeList.stream().filter(item ->
                line.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        List<ModelLine> inLines = modelLineList.stream().filter(item ->
                item.getEndCell().equals(branchNode.getPicId())).collect(Collectors.toList());
        List<ModelLine> outLines = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(branchNode.getPicId())).collect(Collectors.toList());
        if (inLines.size()!=1 || outLines.size()!=1) return path;
        ModelLine lineToBranchNode = inLines.get(0);
        ModelNode endNode = modelNodeList.stream().filter(item ->
                lineToBranchNode.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        path.add(branchNode);
        path.add(endNode);
 
        return path;
    }
 
    private Map<ModelNode, List<List<ModelNode>>> groupingPathByEndNode(List<List<ModelNode>> pathList) {
        Map<ModelNode, List<List<ModelNode>>> endNodePathsMap = new HashMap<>();
        for (List<ModelNode> path : pathList) {
            ModelNode endNode = path.get(path.size()-1);
            if (endNodePathsMap.containsKey(endNode)) {
                endNodePathsMap.get(endNode).add(path);
            } else {
                List<List<ModelNode>> paths = new ArrayList<>();
                paths.add(path);
                endNodePathsMap.put(endNode, paths);
            }
        }
        return endNodePathsMap;
    }
 
    private ModelNode getBranchNodesOneParallel(List<List<ModelNode>> paths,
                                           List<ModelNode> branchNodeList) {
        ModelNode endNode = null;
        for (List<ModelNode> path : paths) {
            branchNodeList.add(path.get(0));
            endNode = path.get(path.size()-1);
        }
        return endNode;
    }
 
    private boolean simplifyBridge(Long modelId,
                                   List<ModelNode> modelNodeList,
                                   List<ModelLine> modelLineList,
                                   List<Algorithm> algorithmList,
                                   List<ModelNode> modelNodeAndVnodeList,
                                   boolean hasSimplified) {
        boolean hasSimplifiedMe = false;
        List<ModelNode> startNodes = modelNodeList.stream().filter(item ->
                "node,vnode,connect,end".contains(item.getNodeType())).collect(Collectors.toList());
        if (startNodes.size()==0) return hasSimplified;
 
        for (ModelNode startNode : startNodes) {
            List<ModelLine> lines = modelLineList.stream().filter(item ->
                    item.getEndCell().equals(startNode.getPicId())).collect(Collectors.toList());
            if (lines.size()<2) continue;
            List<ModelLinePair> linePairs = getLinePairs(lines);
            for (ModelLinePair linePair : linePairs) {
                hasSimplifiedMe = simplifyBridgeOneLinePair(modelId, modelNodeList, modelLineList,
                        algorithmList, modelNodeAndVnodeList, startNode, linePair);
                if (hasSimplifiedMe) {
                    hasSimplified = true;
                    break;
                }
            }
        }
 
        return hasSimplified;
    }
 
    private boolean simplifyBridgeOneLinePair(Long modelId,
                                              List<ModelNode> modelNodeList,
                                              List<ModelLine> modelLineList,
                                              List<Algorithm> algorithmList,
                                              List<ModelNode> modelNodeAndVnodeList,
                                              ModelNode startNode,
                                              ModelLinePair linePair) {
        ModelNode node1 = null;
        ModelNode node2 = null;
        ModelNode node3 = null;
        ModelNode node4 = null;
        ModelNode node5 = null;
        ModelNode nodeTmp1 = modelNodeList.stream().filter(item ->
                linePair.getLine1().getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        ModelNode nodeTmp2 = modelNodeList.stream().filter(item ->
                linePair.getLine2().getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        calcInOutLineNum(nodeTmp1, modelLineList);
        calcInOutLineNum(nodeTmp2, modelLineList);
        if (nodeTmp1.getOutLineNum()!=1 || nodeTmp2.getOutLineNum()!=1) return false;
        if (nodeTmp1.getInLineNum()<1 || nodeTmp1.getInLineNum()>2) return false;
        if (nodeTmp2.getInLineNum()<1 || nodeTmp2.getInLineNum()>2) return false;
        if ((nodeTmp1.getInLineNum() + nodeTmp2.getInLineNum())!=3) return false;
        if (nodeTmp1.getInLineNum()==1) {
            node2 = nodeTmp1;
            node5 = nodeTmp2;
        } else {
            node2 = nodeTmp2;
            node5 = nodeTmp1;
        }
        ModelNode finalNode = node2;
        ModelLine line1To2 = modelLineList.stream().filter(item ->
                item.getEndCell().equals(finalNode.getPicId())).collect(Collectors.toList()).get(0);
        node1 = modelNodeList.stream().filter(item ->
                line1To2.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        if (node1.getInLineNum()!=1 || node1.getOutLineNum()!=2) return false;
        ModelNode finalNode1 = node1;
        List<ModelLine> outLinesFrom1 = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(finalNode1.getPicId())).collect(Collectors.toList());
        if (outLinesFrom1.get(0).getEndCell().equals(node2.getPicId())) {
            node3 = modelNodeList.stream().filter(item ->
                    outLinesFrom1.get(1).getEndCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        } else {
            node3 = modelNodeList.stream().filter(item ->
                    outLinesFrom1.get(0).getEndCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        }
        if (node3.getId().equals(node5.getId())) return false;
        if (node3.getInLineNum()!=1 || node3.getOutLineNum()!=1) return false;
        ModelNode finalNode2 = node3;
        ModelLine outLineFrom3 = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(finalNode2.getPicId())).collect(Collectors.toList()).get(0);
        if (!outLineFrom3.getEndCell().equals(node5.getPicId())) return false;
        ModelNode finalNode3 = node5;
        List<ModelLine> inLinesTo5 = modelLineList.stream().filter(item ->
                item.getEndCell().equals(finalNode3.getPicId())).collect(Collectors.toList());
        if (inLinesTo5.get(0).getBeginCell().equals(node3.getPicId())) {
            node4 = modelNodeList.stream().filter(item ->
                    inLinesTo5.get(1).getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        } else {
            node4 = modelNodeList.stream().filter(item ->
                    inLinesTo5.get(0).getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        }
        if (node4.getId().equals(node1.getId())) return false;
        if (node4.getId().equals(node2.getId())) return false;
        if (node4.getInLineNum()!=1 || node4.getOutLineNum()!=1) return false;
        ModelNode finalNode4 = node1;
        ModelLine lineTo1 = modelLineList.stream().filter(item ->
                item.getEndCell().equals(finalNode4.getPicId())).collect(Collectors.toList()).get(0);
        ModelNode finalNode5 = node4;
        ModelLine lineTo4 = modelLineList.stream().filter(item ->
                item.getEndCell().equals(finalNode5.getPicId())).collect(Collectors.toList()).get(0);
        nodeTmp1 = modelNodeList.stream().filter(item ->
                lineTo1.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        nodeTmp2 = modelNodeList.stream().filter(item ->
                lineTo4.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        if (!nodeTmp1.getId().equals(nodeTmp2.getId())) return false;
        ModelNode endNode = nodeTmp1;
        List<ModelNode> branchNodeList = new ArrayList<>();
        branchNodeList.add(node1);
        branchNodeList.add(node2);
        branchNodeList.add(node3);
        branchNodeList.add(node4);
        branchNodeList.add(node5);
 
        if ("connect".equals(startNode.getNodeType()) && startNode.getInLineNum()==2) {
            // 替换成虚节点
            replaceToVnode("bridge", modelId, modelNodeList, modelLineList,
                    algorithmList, modelNodeAndVnodeList, startNode, endNode, branchNodeList);
        } else {
            // 新增虚节点
            ModelNode vnode = new ModelNode();
            Long id = UUIDUtil.generateId();
            vnode.setId(id);
            vnode.setPicId(id.toString());
            vnode.setModelId(modelId);
            vnode.setNodeType("vnode");
            // 将位置设置为第一个分支的
            vnode.setPositionX(node2.getPositionX());
            vnode.setPositionY(node2.getPositionY());
//            vnode.setPositionX(startNode.getPositionX());
//            vnode.setPositionY(startNode.getPositionY());
            modelNodeList.add(vnode);
            modelNodeAndVnodeList.add(vnode);
            ModelLine modelLineNew = new ModelLine();
            Long picId2 = UUIDUtil.generateId();
            modelLineNew.setId(picId2);
            modelLineNew.setPicId(picId2.toString());
            modelLineNew.setModelId(modelId);
            modelLineNew.setBeginCell(vnode.getPicId());
            modelLineNew.setEndCell(startNode.getPicId());
            modelLineList.add(modelLineNew);
            replaceToVnode("bridge", modelId, modelNodeList, modelLineList,
                    algorithmList, modelNodeAndVnodeList, vnode, endNode, branchNodeList);
            calcInOutLineNum(startNode, modelLineList);
        }
 
        return true;
    }
 
    private void calcInOutLineNumAllNode(List<ModelNode> modelNodeList, List<ModelLine> modelLineList) {
        for (ModelNode node : modelNodeList) {
            calcInOutLineNum(node, modelLineList);
        }
    }
 
    private void calcInOutLineNum(ModelNode node, List<ModelLine> modelLineList) {
        List<ModelLine> inLines = modelLineList.stream().filter(item ->
                item.getEndCell().equals(node.getPicId())).collect(Collectors.toList());
        List<ModelLine> outLines = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(node.getPicId())).collect(Collectors.toList());
        node.setInLineNum(inLines.size());
        node.setOutLineNum(outLines.size());
    }
 
    // 找出所有2根线的组合
    private List<ModelLinePair> getLinePairs(List<ModelLine> lines) {
        List<ModelLinePair> linePairs = new ArrayList<>();
        for (int i=0; i<lines.size()-1; i++) {
            for (int j=i+1; j<lines.size(); j++) {
                ModelLinePair linePair = new ModelLinePair();
                linePair.setLine1(lines.get(i));
                linePair.setLine2(lines.get(j));
                linePairs.add(linePair);
            }
        }
        return linePairs;
    }
 
    private boolean simplifyOperator(String operator,
                                     Long modelId,
                                     List<ModelNode> modelNodeList,
                                     List<ModelLine> modelLineList,
                                     List<Algorithm> algorithmList,
                                     List<ModelNode> modelNodeAndVnodeList,
                                     boolean hasSimplified) {
        List<ModelNode> opNodes = modelNodeList.stream().filter(item ->
                operator.equals(item.getNodeType())).collect(Collectors.toList());
        if (opNodes.size()==0) return hasSimplified;
 
        for (ModelNode opNode : opNodes) {
            List<ModelLine> lines = modelLineList.stream().filter(item ->
                    item.getEndCell().equals(opNode.getPicId())).collect(Collectors.toList());
            boolean thisNodeSuccess = true;
            ModelNode endNode = null;
            List<ModelNode> branchNodeList = new ArrayList<>();
            for (ModelLine line : lines) {
                ModelNode curEndNode = handleOneBranch(modelNodeList, modelLineList, line, branchNodeList);
                if (curEndNode == null) {
                    thisNodeSuccess = false;
                    break;
                }
                if (endNode == null) {
                    endNode = curEndNode;
                } else {
                    if (!endNode.getId().equals(curEndNode.getId())) {
                        thisNodeSuccess = false;
                        break;
                    }
                }
            }
            if (thisNodeSuccess) {
                replaceToVnode(opNode.getNodeType(), modelId, modelNodeList, modelLineList,
                        algorithmList, modelNodeAndVnodeList, opNode, endNode, branchNodeList);
                hasSimplified = true;
            }
        }
 
        return hasSimplified;
    }
 
    private void replaceToVnode(String type,
                                Long modelId,
                                List<ModelNode> modelNodeList,
                                List<ModelLine> modelLineList,
                                List<Algorithm> algorithmList,
                                List<ModelNode> modelNodeAndVnodeList,
                                ModelNode startNode,
                                ModelNode endNode,
                                List<ModelNode> branchNodeList) {
        // 新增algorithm
        List<String> computerList = createVnodeComputerList(type, algorithmList, modelNodeAndVnodeList, branchNodeList);
        Algorithm algorithm = new Algorithm();
        Long id = UUIDUtil.generateId();
        algorithm.setId(id);
        algorithm.setModelId(modelId);
        algorithm.setComputerId(id);
        algorithm.setModelType(type);
        algorithm.setAlgorithmType(type);
        algorithm.setComputerList(computerList.get(0));
        algorithm.setObjectList(computerList.get(1));
        algorithm.setStep(algorithmList.size()==0 ? 0 : algorithmList.get(algorithmList.size()-1).getStep()+1);
        algorithmList.add(algorithm);
 
        // 替换为虚节点
        for (ModelNode node : branchNodeList) {
            List<ModelLine> branchNodeLineList = modelLineList.stream().filter(item ->
                    item.getEndCell().equals(node.getPicId()) || item.getBeginCell().equals(node.getPicId())
            ).collect(Collectors.toList());
            modelLineList.removeAll(branchNodeLineList);
            modelNodeList.remove(node);
            calcInOutLineNum(node, modelLineList);
        }
        startNode.setId(id);
        startNode.setNodeType("vnode");
        startNode.setName("v"+algorithm.getStep());
 
        ModelLine modelLineNew = new ModelLine();
        Long picId2 = UUIDUtil.generateId();
        modelLineNew.setId(picId2);
        modelLineNew.setPicId(picId2.toString());
        modelLineNew.setModelId(modelId);
        modelLineNew.setBeginCell(endNode.getPicId());
        modelLineNew.setEndCell(startNode.getPicId());
        modelLineList.add(modelLineNew);
 
        calcInOutLineNum(startNode, modelLineList);
        calcInOutLineNum(endNode, modelLineList);
    }
 
    private ModelNode handleOneBranch(List<ModelNode> modelNodeList,
                                      List<ModelLine> modelLineList,
                                      ModelLine line,
                                      List<ModelNode> branchNodeList
                                      ) {
        ModelNode branchNode = modelNodeList.stream().filter(item ->
                line.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
        List<ModelLine> inLines = modelLineList.stream().filter(item ->
                item.getEndCell().equals(branchNode.getPicId())).collect(Collectors.toList());
        List<ModelLine> outLines = modelLineList.stream().filter(item ->
                item.getBeginCell().equals(branchNode.getPicId())).collect(Collectors.toList());
        if (inLines.size()!=1 || outLines.size()!=1) return null;
        ModelLine lineToBranchNode = inLines.get(0);
        ModelNode endNode = modelNodeList.stream().filter(item ->
                lineToBranchNode.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
 
        branchNodeList.add(branchNode);
        return endNode;
    }
 
    private void getNodeAndLineFromRbd(Long modelId,
                                       JSONArray rbdJsonArray,
                                       List<ModelNode> modelNodeList,
                                       List<ModelLine> modelLineList) {
        for (int i = 0; i < rbdJsonArray.size(); i++) {
            JSONObject jsonObject = rbdJsonArray.getJSONObject(i);
            String shape = jsonObject.get("shape").toString();
            if ("edge".equals(shape)) {
                ModelLine modelLine = new ModelLine();
                modelLine.setId(UUIDUtil.generateId());
                modelLine.setModelId(modelId);
                modelLine.setPicId(jsonObject.get("id").toString());
                modelLine.setBeginCell(JsonUtils2.getJsonValueByPath(jsonObject, "source/cell".split("/")).toString());
                modelLine.setEndCell(JsonUtils2.getJsonValueByPath(jsonObject, "target/cell".split("/")).toString());
                modelLineList.add(modelLine);
            } else if ("image".equals(shape)){
                ModelNode modelNode = new ModelNode();
                modelNode.setId(UUIDUtil.generateId());
                modelNode.setModelId(modelId);
                modelNode.setPicId(jsonObject.get("id").toString());
                modelNode.setNodeType(JsonUtils2.getJsonValueByPath(jsonObject, "data/nodeType".split("/")).toString());
                modelNode.setPositionX(Double.valueOf(JsonUtils2.getJsonValueByPath(jsonObject, "position/x".split("/")).toString()));
                modelNode.setPositionY(Double.valueOf(JsonUtils2.getJsonValueByPath(jsonObject, "position/y".split("/")).toString()));
                if ("node".equals(modelNode.getNodeType())) {
                    modelNode.setDataId(Long.valueOf(JsonUtils2.getJsonValueByPath(jsonObject, "data/dataId".split("/")).toString()));
                    modelNode.setNodeTypeExt(JsonUtils2.getJsonValueByPath(jsonObject, "data/nodeTypeExt".split("/")).toString());
                    Object name = JsonUtils2.getJsonValueByPath(jsonObject, "attrs/text/text".split("/"));
                    modelNode.setName(name==null ? "" : name.toString());
                }
                modelNodeList.add(modelNode);
            }
        }
//        calcLineSortAll(modelNodeList, modelLineList);
    }
 
/*
    private void calcLineSortAll(List<ModelNode> modelNodeList,
                                 List<ModelLine> modelLineList) {
        for (ModelNode node : modelNodeList) {
            List<ModelLine> inLines = modelLineList.stream().filter(item ->
                    item.getEndCell().equals(node.getPicId())).collect(Collectors.toList());
            if (inLines.size() < 1) continue;
            for (ModelLine line : inLines) {
                ModelNode beginNode = modelNodeList.stream().filter(item ->
                        line.getBeginCell().equals(item.getPicId())).collect(Collectors.toList()).get(0);
                line.setLineSort(beginNode.getPositionY());
            }
        }
    }
*/
 
    private void analyzeRbdAndSave(Long modelId, String content, boolean saveFlag) {
        String diagramJsonStr = content;
//        String diagramJsonStr = "{\"cells\":[{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"26d1a5a6-0be8-4890-86a0-a33d429e6673\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"f91765b1-af48-4396-84ba-eb16e3476aa5\",\"port\":\"839cb2d9-59ca-4a39-a63c-26bf60cc6989\"},\"target\":{\"cell\":\"31585e99-58c7-4a98-8824-8000743b364d\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"f2286e46-84c7-4702-8670-d7cda22c34e5\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"f91765b1-af48-4396-84ba-eb16e3476aa5\",\"port\":\"839cb2d9-59ca-4a39-a63c-26bf60cc6989\"},\"target\":{\"cell\":\"5123ad82-18bb-46fe-9d93-138b24b54a15\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"2ab8b7f8-7fe2-490f-89c5-4250d4a62a78\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"31585e99-58c7-4a98-8824-8000743b364d\",\"port\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},\"target\":{\"cell\":\"6df9adc8-cb83-405c-8482-633db0f3644f\",\"port\":\"f93ccd2f-dedd-47b1-9ad0-9be20c5db8a4\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"700c05a5-f151-4b28-8135-705ccf013522\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"5123ad82-18bb-46fe-9d93-138b24b54a15\",\"port\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},\"target\":{\"cell\":\"6df9adc8-cb83-405c-8482-633db0f3644f\",\"port\":\"f93ccd2f-dedd-47b1-9ad0-9be20c5db8a4\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"feef2a57-3c72-4d69-92a6-c828c736b61a\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"6df9adc8-cb83-405c-8482-633db0f3644f\",\"port\":\"80d3bd61-1ed3-493a-b4e8-d4576e6fbfda\"},\"target\":{\"cell\":\"da634b2e-5ffc-4e1b-a636-f86ace9eb082\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"49e36d6f-6a94-4edc-9894-6dd825091706\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"f91765b1-af48-4396-84ba-eb16e3476aa5\",\"port\":\"839cb2d9-59ca-4a39-a63c-26bf60cc6989\"},\"target\":{\"cell\":\"2b6df966-4e19-4055-bb13-c4c083b18e58\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"ed8c1bfa-c0a6-4e9b-8697-862aef109bcf\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"f91765b1-af48-4396-84ba-eb16e3476aa5\",\"port\":\"839cb2d9-59ca-4a39-a63c-26bf60cc6989\"},\"target\":{\"cell\":\"323f5abe-05ed-419d-9d81-d25c7d3b19f3\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"70c6b5fb-1b48-4a2f-bacd-be0284134818\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"2b6df966-4e19-4055-bb13-c4c083b18e58\",\"port\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},\"target\":{\"cell\":\"1f378f5e-066d-49bb-a6cc-de24a8882d65\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"6a134969-623b-428e-9b0c-436bae2d6608\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"323f5abe-05ed-419d-9d81-d25c7d3b19f3\",\"port\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},\"target\":{\"cell\":\"1f378f5e-066d-49bb-a6cc-de24a8882d65\",\"port\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#5F95FF\"}},\"id\":\"51de5faf-1766-4dd1-abd0-eec2e867a322\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"1f378f5e-066d-49bb-a6cc-de24a8882d65\",\"port\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},\"target\":{\"cell\":\"0bb2ba89-a92d-4b32-b3cd-45df2300fa34\",\"port\":\"61eaafd2-095b-41d6-8e34-862fb470b7a6\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#A2B1C3\"}},\"id\":\"161f78dc-1f44-4e43-9ced-e39d345aac78\",\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"#5F95FF\"},\"text\":{\"text\":\"\"}},\"position\":{\"distance\":0.5,\"angle\":180,\"options\":{\"keepGradient\":true,\"ensureLegibility\":true}}}],\"zIndex\":0,\"source\":{\"cell\":\"da634b2e-5ffc-4e1b-a636-f86ace9eb082\",\"port\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},\"target\":{\"cell\":\"0bb2ba89-a92d-4b32-b3cd-45df2300fa34\",\"port\":\"61eaafd2-095b-41d6-8e34-862fb470b7a6\"}},{\"position\":{\"x\":-350,\"y\":-280},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"logo\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/logo.36cbc06d.png\"},\"body\":{\"stroke\":\"#5F95FF\"},\"line\":{\"stroke\":\"orange\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"31585e99-58c7-4a98-8824-8000743b364d\",\"data\":{\"dataId\":\"123456\",\"nodeType\":\"node\",\"nodeTypeExt\":\"aaa\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"bc0336ec-7912-4720-aea7-e5869de77692\"},{\"group\":\"right\",\"id\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},{\"group\":\"bottom\",\"id\":\"fef0f533-9581-4831-ba65-427f097a3d17\"},{\"group\":\"left\",\"id\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}]},\"zIndex\":1,\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"orange\"}}}]},{\"position\":{\"x\":-350,\"y\":-180},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"logo\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/logo.36cbc06d.png\"},\"body\":{\"stroke\":\"#5F95FF\"},\"line\":{\"stroke\":\"orange\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"5123ad82-18bb-46fe-9d93-138b24b54a15\",\"data\":{\"dataId\":\"123456\",\"nodeType\":\"node\",\"nodeTypeExt\":\"aaa\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"bc0336ec-7912-4720-aea7-e5869de77692\"},{\"group\":\"right\",\"id\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},{\"group\":\"bottom\",\"id\":\"fef0f533-9581-4831-ba65-427f097a3d17\"},{\"group\":\"left\",\"id\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}]},\"zIndex\":2,\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"orange\"}}}]},{\"position\":{\"x\":-620,\"y\":-200},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"start\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/start.88f586e1.png\"},\"body\":{\"stroke\":\"#5F95FF\"},\"line\":{\"stroke\":\"orange\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"f91765b1-af48-4396-84ba-eb16e3476aa5\",\"data\":{\"dataId\":\"\",\"nodeType\":\"start\",\"nodeTypeExt\":\"\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"2ad6a8d5-d86d-49e6-908a-d317b61997c1\"},{\"group\":\"right\",\"id\":\"839cb2d9-59ca-4a39-a63c-26bf60cc6989\"},{\"group\":\"bottom\",\"id\":\"7048578f-94d9-4f75-b653-eb5f43ff55db\"},{\"group\":\"left\",\"id\":\"e7bb1134-4b4c-401d-89ea-e77ae24cbd03\"}]},\"zIndex\":3,\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"orange\"}}}]},{\"position\":{\"x\":-153,\"y\":-240},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"switchRight\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/switchRight.74768797.png\"},\"body\":{\"stroke\":\"#5F95FF\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"6df9adc8-cb83-405c-8482-633db0f3644f\",\"data\":{\"dataId\":\"\",\"nodeType\":\"switchRight\",\"nodeTypeExt\":\"\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"adceedb7-f842-4d1e-be3a-8b3cb0a7e3ce\"},{\"group\":\"right\",\"id\":\"80d3bd61-1ed3-493a-b4e8-d4576e6fbfda\"},{\"group\":\"bottom\",\"id\":\"8fa84779-80b2-4c91-8366-7dd4fa0d93ff\"},{\"group\":\"left\",\"id\":\"f93ccd2f-dedd-47b1-9ad0-9be20c5db8a4\"}]},\"zIndex\":4},{\"position\":{\"x\":-21,\"y\":-240},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"logo\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/logo.36cbc06d.png\"},\"body\":{\"stroke\":\"#5F95FF\"},\"line\":{\"stroke\":\"orange\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"da634b2e-5ffc-4e1b-a636-f86ace9eb082\",\"data\":{\"dataId\":\"123456\",\"nodeType\":\"node\",\"nodeTypeExt\":\"aaa\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"bc0336ec-7912-4720-aea7-e5869de77692\"},{\"group\":\"right\",\"id\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},{\"group\":\"bottom\",\"id\":\"fef0f533-9581-4831-ba65-427f097a3d17\"},{\"group\":\"left\",\"id\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}]},\"zIndex\":5,\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"orange\"}}}]},{\"position\":{\"x\":170,\"y\":-100},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"end\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/end.814a7041.png\"},\"body\":{\"stroke\":\"#5F95FF\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"0bb2ba89-a92d-4b32-b3cd-45df2300fa34\",\"data\":{\"dataId\":\"\",\"nodeType\":\"end\",\"nodeTypeExt\":\"\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"223c9b3a-6baa-4bb8-99ba-c3300db9a942\"},{\"group\":\"right\",\"id\":\"2806bc8a-4e11-4657-9af9-089907985671\"},{\"group\":\"bottom\",\"id\":\"c56eed35-95fa-4e3c-adba-9847ef705e1c\"},{\"group\":\"left\",\"id\":\"61eaafd2-095b-41d6-8e34-862fb470b7a6\"}]},\"zIndex\":6},{\"position\":{\"x\":-350,\"y\":-54},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"logo\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/logo.36cbc06d.png\"},\"body\":{\"stroke\":\"#5F95FF\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"2b6df966-4e19-4055-bb13-c4c083b18e58\",\"data\":{\"dataId\":\"123456\",\"nodeType\":\"node\",\"nodeTypeExt\":\"aaa\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"bc0336ec-7912-4720-aea7-e5869de77692\"},{\"group\":\"right\",\"id\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},{\"group\":\"bottom\",\"id\":\"fef0f533-9581-4831-ba65-427f097a3d17\"},{\"group\":\"left\",\"id\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}]},\"zIndex\":7},{\"position\":{\"x\":-330,\"y\":40},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"logo\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/logo.36cbc06d.png\"},\"body\":{\"stroke\":\"#5F95FF\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"323f5abe-05ed-419d-9d81-d25c7d3b19f3\",\"data\":{\"dataId\":\"123456\",\"nodeType\":\"node\",\"nodeTypeExt\":\"aaa\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"bc0336ec-7912-4720-aea7-e5869de77692\"},{\"group\":\"right\",\"id\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},{\"group\":\"bottom\",\"id\":\"fef0f533-9581-4831-ba65-427f097a3d17\"},{\"group\":\"left\",\"id\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}]},\"zIndex\":8},{\"position\":{\"x\":-153,\"y\":-20},\"size\":{\"width\":60,\"height\":60},\"attrs\":{\"text\":{\"refY\":\"100%\",\"textVerticalAnchor\":\"top\",\"text\":\"logo\",\"style\":{\"color\":\"#080808\"},\"refY2\":4},\"image\":{\"xlink:href\":\"dist/img/logo.36cbc06d.png\"},\"body\":{\"stroke\":\"#5F95FF\"},\"line\":{\"stroke\":\"orange\"}},\"visible\":true,\"shape\":\"image\",\"id\":\"1f378f5e-066d-49bb-a6cc-de24a8882d65\",\"data\":{\"dataId\":\"123456\",\"nodeType\":\"node\",\"nodeTypeExt\":\"aaa\"},\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"bc0336ec-7912-4720-aea7-e5869de77692\"},{\"group\":\"right\",\"id\":\"a977d555-b8c8-4578-9a8b-3097c405c1a8\"},{\"group\":\"bottom\",\"id\":\"fef0f533-9581-4831-ba65-427f097a3d17\"},{\"group\":\"left\",\"id\":\"01eb49d9-4b1c-466f-8263-935542436c7e\"}]},\"zIndex\":9,\"labels\":[{\"attrs\":{\"body\":{\"stroke\":\"orange\"}}}]}]}";
 
        JSONObject diagramJson = new JSONObject(diagramJsonStr);
        JSONArray diagramJsonArray = diagramJson.getJSONArray("cells");
        if (diagramJsonArray == null) return;
 
        List<ModelLine> modelLineList = new ArrayList<>();
        List<ModelNode> modelNodeList = new ArrayList<>();
        // 1. 解析出节点与边
        for (int i = 0; i < diagramJsonArray.size(); i++) {
            JSONObject jsonObject = diagramJsonArray.getJSONObject(i);
            String shape = jsonObject.get("shape").toString();
            if (shape.equals("edge")) {
                ModelLine modelLine = new ModelLine();
                modelLine.setId(UUIDUtil.generateId());
                modelLine.setPicId(jsonObject.get("id").toString());
                modelLine.setModelId(modelId);
                modelLine.setBeginCell(JsonUtils2.getJsonValueByPath(jsonObject, "source/cell".split("/")).toString());
                modelLine.setEndCell(JsonUtils2.getJsonValueByPath(jsonObject, "target/cell".split("/")).toString());
                modelLineList.add(modelLine);
            } else if (shape.equals("image")){
                ModelNode modelNode = new ModelNode();
                modelNode.setId(UUIDUtil.generateId());
                modelNode.setPicId(jsonObject.get("id").toString());
                modelNode.setModelId(modelId);
                modelNode.setNodeType(JsonUtils2.getJsonValueByPath(jsonObject, "data/nodeType".split("/")).toString());
                if ("node".equals(modelNode.getNodeType())) {
                    modelNode.setDataId(Long.valueOf(JsonUtils2.getJsonValueByPath(jsonObject, "data/dataId".split("/")).toString()));
                    modelNode.setNodeTypeExt(JsonUtils2.getJsonValueByPath(jsonObject, "data/nodeTypeExt".split("/")).toString());
                    modelNode.setName(JsonUtils2.getJsonValueByPath(jsonObject, "attrs/label/textWrap/text".split("/")).toString());
                }
                modelNodeList.add(modelNode);
            }
        }
 
        // 2. 对于有多根入口线的产品节点,将其上的表决、旁联、并联关系剥离成运算符节点,添加到该产品节点的前面,并添加相应的边
        List<ModelNode> nodesToAdd = new ArrayList<>();
        List<ModelLine> linesToAdd = new ArrayList<>();
        for (ModelNode modelNode: modelNodeList) {
            String picId = modelNode.getPicId();
            List<ModelLine> lineList = modelLineList.stream().filter(item->item.getEndCell().equals(picId)).collect(Collectors.toList());
            if (lineList.size()>1){
                if ("node,end".contains(modelNode.getNodeType())){
                    String nodeTypeExt = "";
                    if ("node".equals(modelNode.getNodeType())) {
                        nodeTypeExt = modelNode.getNodeTypeExt();
                    } else if ("end".equals(modelNode.getNodeType())) {
                        nodeTypeExt = "parallel";
                    }
                    if (StringUtils.isNotBlank(nodeTypeExt)){
                        ModelNode modelNodeNew =  new ModelNode();
                        Long nodeNewId = UUIDUtil.generateId();
                        modelNodeNew.setId(nodeNewId);
                        modelNodeNew.setPicId(nodeNewId.toString());
                        modelNodeNew.setModelId(modelId);
                        modelNodeNew.setNodeType(nodeTypeExt);
                        nodesToAdd.add(modelNodeNew);
 
                        ModelLine modelLineNew = new ModelLine();
                        Long lineNewId = UUIDUtil.generateId();
                        modelLineNew.setId(lineNewId);
                        modelLineNew.setPicId(lineNewId.toString());
                        modelLineNew.setModelId(modelId);
                        modelLineNew.setBeginCell(nodeNewId.toString());
                        modelLineNew.setEndCell(modelNode.getPicId());
                        linesToAdd.add(modelLineNew);
 
                        for(ModelLine nodelLine: lineList){
                            nodelLine.setEndCell(nodeNewId.toString());
                        }
                    }
                }
            }
        }
        modelNodeList.addAll(nodesToAdd);
        modelLineList.addAll(linesToAdd);
 
        // 3. 将最基本的串、并、旁联、表决替换为虚节点,不断简化图形,直至无法简化为止
        List<Algorithm> algoList = new ArrayList<>();
        boolean hasLeastOne = false;
        int stepNo = 0;
        do {
            List<ModelNode> opNodeList = modelNodeList.stream().filter(item ->
                    "parallel,switch,vote,end".contains(item.getNodeType())).collect(Collectors.toList());
            if (opNodeList.size() <= 0) break;
 
            hasLeastOne = false; // 至少能简化1个图形
            for (ModelNode opNode: opNodeList) {
                // 右边运算符循环
                List<ModelLine> lineToOpNodeList = modelLineList.stream().filter(item ->
                        item.getEndCell().equals(opNode.getPicId())).collect(Collectors.toList());
                List<List<ModelNode>> listPath = new ArrayList<>();
                ModelNode lastNode = null;
                boolean thisNodeSucc= true;
                for (ModelLine modelLine: lineToOpNodeList) {
                    // 右边运算符的逐个路径循环
                    List<ModelNode> result = new ArrayList<>();
                    handleOneLine(modelLineList, modelNodeList, modelLine, result);
                    listPath.add(result);
                    ModelNode curLastNode = result.get(result.size()-1);
                    if (lastNode == null) {
                        lastNode = curLastNode;
                    } else {
                        if (!lastNode.getId().equals(curLastNode.getId())){
                            thisNodeSucc = false;
                            break;
                        }
                    }
                }
                if (thisNodeSucc) {
                    //形成虚节点算法
                    stepNo = replaceToVNode(modelId, modelNodeList, modelLineList, opNode, listPath, lastNode, algoList, stepNo);
                    hasLeastOne = true;
                }
            } // end for
        } while (hasLeastOne);
 
//        if (saveFlag) saveModel(modelId, modelNodeList, modelLineList, algoList);
    }
 
    private void handleOneLine(List<ModelLine> modelLineList,
                               List<ModelNode> modelNodeList,
                               ModelLine modelLine,
                               List<ModelNode> result) {
        ModelNode modelNode = modelNodeList.stream().filter(item ->
                item.getPicId().equals(modelLine.getBeginCell())).collect(Collectors.toList()).get(0);
        result.add(modelNode);
        if ("start".equals(modelNode.getNodeType())) {
            return;
        }
        List<ModelLine> lines =modelLineList.stream().filter(item ->
                item.getBeginCell().equals(modelNode.getPicId())).collect(Collectors.toList());
        List<ModelLine> lines2 =modelLineList.stream().filter(item ->
                item.getEndCell().equals(modelNode.getPicId())).collect(Collectors.toList());
        if (lines.size()>1 || lines2.size()>1) {
            return;
        } else {
            handleOneLine(modelLineList, modelNodeList, lines2.get(0), result);
        }
    }
 
    private int replaceToVNode(Long modelId,
                                List<ModelNode> modelNodeList,
                                List<ModelLine> modelLineList,
                                ModelNode opNode,
                                List<List<ModelNode>> listPath,
                                ModelNode lastNode,
                                List<Algorithm> algoList,
                                int stepNo) {
        String computerList = "";
        String objectList = "";
        for (List<ModelNode> path : listPath) {
            path.remove(path.get(path.size()-1));
            Long id = null;
            String name = "";
            if (path.size()==0) {
                return stepNo;
            } else if (path.size()==1) {
                if ("end".equals(opNode.getNodeType()) && !"start".equals(lastNode.getNodeType())) return stepNo;
                id = path.get(path.size()-1).getId();
                name = path.get(path.size()-1).getName();
            } else {
                id = UUIDUtil.generateId();
                name = id.toString();
                Algorithm algo = new Algorithm();
                algo.setId(id);
                algo.setModelId(modelId);
                algo.setComputerId(id);
                algo.setModelType("series");
                algo.setAlgorithmType("series");
                algo.setComputerList(joinNodeId(path, ","));
                algo.setObjectList(joinNodeName(path, ","));
                algo.setStep(stepNo);
                stepNo++;
                algoList.add(algo);
                name = "v"+ algo.getStep();
            }
            computerList = computerList.equals("") ? id.toString() : computerList + "," + id.toString();
            objectList = objectList.equals("") ? name : objectList + "," + name;
            for (ModelNode node : path) {
                List<ModelLine> lineList4 = modelLineList.stream().filter(item ->
                        item.getEndCell().equals(node.getPicId()) || item.getBeginCell().equals(node.getPicId())).collect(Collectors.toList());
                modelLineList.removeAll(lineList4);
                modelNodeList.remove(node);
            }
        }
        Algorithm algo = new Algorithm();
        Long id = UUIDUtil.generateId();
        algo.setId(id);
        algo.setModelId(modelId);
        algo.setComputerId(id);
        algo.setModelType(opNode.getNodeType());
        algo.setAlgorithmType(opNode.getNodeType());
        algo.setComputerList(computerList);
        algo.setObjectList(objectList);
        algo.setStep(stepNo);
        stepNo++;
        algoList.add(algo);
 
        //将原运算节点改成虚节点
        opNode.setId(id);
        opNode.setNodeType("vnode");
        opNode.setName("v"+algo.getStep());
 
        ModelLine modelLineNew = new ModelLine();
        Long picId2 = UUIDUtil.generateId();
        modelLineNew.setId(picId2);
        modelLineNew.setPicId(picId2.toString());
        modelLineNew.setModelId(modelId);
        modelLineNew.setBeginCell(lastNode.getPicId());
        modelLineNew.setEndCell(opNode.getPicId());
        modelLineList.add(modelLineNew);
 
        return stepNo;
    }
 
    private String joinNodeId(List<ModelNode> nodeList, String sep) {
        String result = "";
        for (int i = 0; i < nodeList.size(); i++) {
            if (i > 0) result = result + sep;
            result = result + nodeList.get(i).getId().toString();
        }
        return result;
    }
 
    private String joinNodeName(List<ModelNode> nodeList, String sep) {
        String result = "";
        for (int i = 0; i < nodeList.size(); i++) {
            if (i > 0) result = result + sep;
            result = result + nodeList.get(i).getName();
        }
        return result;
    }
 
    private void saveModel(Long modelId,
                           List<ModelNode> modelNodeList,
                           List<ModelLine> modelLineList,
                           List<Algorithm> algoList,
                           List<ModelNode> modelNodeAndVnodeList) {
        // 删除既有数据
        modelNodeDao.deleteByModelId(modelId);
        modelLineDao.deleteByModelId(modelId);
        algorithmDao.deleteByModelId(modelId);
        modelNodeAlgorithmDao.deleteByModelId(modelId);
 
        // 插入数据
        for (ModelNode modelNode : modelNodeList) {
            modelNodeDao.insert(modelNode);
        }
        for (ModelLine modelLine : modelLineList) {
            modelLineDao.insert(modelLine);
        }
        for (Algorithm algorithm : algoList) {
            algorithmDao.insert(algorithm);
        }
        for (ModelNode modelNode : modelNodeAndVnodeList) {
            modelNodeAlgorithmDao.insert(ModelNodeAlgorithm.from(modelNode));
        }
    }
 
}