-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit.java
More file actions
1126 lines (962 loc) · 52.7 KB
/
Copy pathSplit.java
File metadata and controls
1126 lines (962 loc) · 52.7 KB
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
import java.io.File;
import java.security.ProviderException;
import java.util.regex.*;
import java.io.*;
import java.util.*;
class Split{
/**
* @param args
*/
static HashMap<String , Graph> map ;
static HashMap<String , String> refMapParticipant;
static HashMap<String,SubEvents> subprocessEvents;
static ArrayList<String> blackBoxNode;
static ArrayList<String> parallelMultinstance;
static ArrayList<String> sequentialMultinstanceLoop;
static ArrayList<String> timerEvents;
static ArrayList<String> participantOccurence;
static HashMap<String , HashSet<String>> participantWiseOrdering;
static HashMap<String , ArrayList<String>> nameStoring;
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static OutputWriter out = new OutputWriter(outputStream);
static int participants;
public static void main(String[] args) {
try{
// FileFinder filefinderOb = new FileFinder();
// String fileName = filefinderOb.finder();
// System.out.println(fileName);
// Scanner s = new Scanner(System.in);
File file=new File(args[0]);
Scanner sc = new Scanner(file); //file to be scanned
StringBuilder sb = new StringBuilder("");
parallelMultinstance = new ArrayList<String>();
sequentialMultinstanceLoop = new ArrayList<String>();
timerEvents = new ArrayList<String>();
participantOccurence = new ArrayList<>();
blackBoxNode = new ArrayList<>();
participantWiseOrdering = new HashMap<>();
while (sc.hasNextLine()) //returns true if and only if scanner has another token
sb.append(sc.nextLine());
int startProcess = sb.toString().indexOf("<bpmn:process");
if(startProcess == -1)startProcess = 0;
int startCollab = sb.toString().indexOf("<bpmn:collaboration");
if(startCollab == -1)startCollab = 0 ;
int indexOfStartProcess = Math.min ( startProcess , startCollab);
int indexOfEndProcess = sb.toString().lastIndexOf("</bpmn:process>") + 16;
// System.out.println("indexes = "+indexOfStartProcess+" "+indexOfEndProcess);
String processPart = sb.toString().substring(indexOfStartProcess , indexOfEndProcess + 1);
/*creating map to store all ids and their incoming and outgoing nodes */
map = new HashMap<>();
refMapParticipant = new HashMap<>();
// System.out.println("subprocess printing starts");
ArrayList<String> subprocess = subprocessMeta(processPart);
// printMap(subprocessEvents);
/*
* Some elements for new collaboration diagram are not as expected as they are not recovered correctly
* elements like start event and endevent and manual taks there is some issue with parsing i guess need to develop more
* durable and dyanmic code for it/
*/
String[] searchPatters = {"startEvent" , "exclusiveGateway" , "inclusiveGateway" , "endEvent" , "parallelGateway","task" ,"receiveTask", "manualTask","scriptTask","userTask","sendTask","serviceTask", "intermediateCatchEvent","intermediateThrowEvent", "sequenceFlow" , "messageFlow"};
ArrayList<Integer> indexProcess = getOccurence(processPart, "<bpmn:process id=\"");
indexProcess.add(processPart.lastIndexOf("</bpmn:process>"));
nameStoring = new HashMap<>();
refMapParticipant = mappingIdName(processPart);
// out.printLine(refMapParticipant);
participantWiseOrdering = participantWiseOrdering(indexProcess , processPart , searchPatters);
// we are extracting sequence flow at last because it reperesent all elements in bpmn file thus to represent we first extract all elements
// and then establish relationship among them.
int[] types = new int[]{0,0,0,0,1,1,1,1,1,1,1,1,1,1,-1 ,-2}; //here 1 denotes transition , 0 places and -1 denotes seq flows only -2 denotes message flows
for(int i = 0;i < searchPatters.length;i++){
patternsBlock(searchPatters[i], processPart, types[i]);
}
/*
* after extracting patterns we can use it to form connections with invisible processes
* first we get the collaboration block
* then we extract the participant ids
* then we use this block for forming edges or relation between participants and tasks or between participants
(for invisible processes it should be as sequence of start event and tasks)
few things to note:- if the participant is in sourceref then it should fire from transition(place and then transitions)
else it should be received by place (place then transition and transition fires)
*/
int collabStart = getIndexPattern(sb.toString() , "<bpmn:collaboration");
int collabEnd = getIndexPattern(sb.toString(), "</bpmn:collaboration>");
// out.printLine(collabStart , collabEnd);
String collabBlock = sb.toString().substring(collabStart < 0 ? 0 : collabStart, collabEnd < 0 ? 0 : collabEnd );
// out.printLine(collabBlock);
// = new Participant();
Participant ob = new Participant();
for(Map.Entry<String,ArrayList<String>> ele : nameStoring.entrySet())
ob.addLinks(map,collabBlock , ele.getValue().size() , ele.getValue());
// ob.addLinks(map,collabBlock , participants , participantOccurence);
//map all petri net node one by one from map generated
HashMap<String , PetriNetNode> PetriNetmap = generatePetriNet(map);
// Utility.color("red");
printGraph();
// Utility.color("reset");
NodeModification nodeModification = new NodeModification();
// nodeModification.parallelMultinstance(PetriNetmap, parallelMultinstance);
// nodeModification.timerEvents(PetriNetmap, timerEvents);
// printList(sequentialMultinstanceLoop);
// nodeModification.sequentialMultinstanceOrLoop(PetriNetmap, sequentialMultinstanceLoop);
nodeModification.edgePreservation(PetriNetmap, blackBoxNode);
// nodeModification.edgeBalancing(participantWiseOrdering, PetriNetmap);
GeneratePetriNet petri = new GeneratePetriNet();
petri.generate(PetriNetmap);
GenerateBPMN bpmn = new GenerateBPMN();
bpmn.generate(map);
sc.close();
}
catch(Exception e){
e.printStackTrace( );
}
out.close();
} //end Main
public static HashMap<String , String> mappingIdName(String processPart){
int start = 0;
// System.out.println("process part = "+processPart);
HashMap<String , String> map = new HashMap<>();
while( (start = processPart.indexOf("<bpmn:participant id=", start)) != -1){
// System.out.println(start+" "+(str.substring(start, start + pattern.length()+1)));
String participantId = getParameter(processPart , "<bpmn:participant id=" , "\"" , start);
int indexOfName = processPart.indexOf("name=", start);
String nameOfParticipant = getParameter(processPart , "name=" , "\"" , indexOfName);
start=indexOfName;
// System.out.println("pariticipant id = "+participantId+" name of participant = "+nameOfParticipant);
map.put(participantId , nameOfParticipant);
nameStoring.put(participantId , new ArrayList<String>());
}
return map;
}
public static HashMap<String, HashSet<String>> participantWiseOrdering(ArrayList<Integer> indexProcess , String processPart , String[] searchPatters){
HashMap<String, HashSet<String>> participantWiseOrdering = new HashMap<>();
for(int i = 0; i + 1 < indexProcess.size();i++){
String str = processPart.substring(indexProcess.get(i));
String processIds = printId(str, "<bpmn:process id=" , "\"");
HashSet<String> set = new HashSet<>();
String searchString = processPart.substring(indexProcess.get(i), indexProcess.get(i+1));
for(int j = 0 ;j < searchPatters.length-2;j++){
String searchPattern = "<bpmn:"+searchPatters[j]+" id=";
int start = 0;
while( (start = searchString.indexOf(searchPattern , start )) != -1){
String ids = getParameter(searchString, searchPattern , "\"" , start);
if(ids.length() >= 1){
// System.out.println(searchPatters[j]+" "+ids);
set.add(ids);
}
start += searchPattern.length();
}
}
participantWiseOrdering.put(processIds,set);
// System.out.println("key is "+processIds);
// System.out.println(set);
endl();
}
return participantWiseOrdering;
}
public static ArrayList<Integer> getOccurence(String str, String pattern){
int start = 0;
ArrayList<Integer> list = new ArrayList<>();
while( (start = str.indexOf(pattern, start)) != -1){
// System.out.println(start+" "+(str.substring(start, start + pattern.length()+1)));
list.add(start);
start+=pattern.length();
}
return list;
}
public static String getString(int startIndex, char endingPattern , String str){
int start = startIndex;
while(str.charAt(start) != '\'')start++;
return str.substring(startIndex, start);
}
public static int getIndexPattern(String str , String patterns){
return str.toString().indexOf(patterns);
}
public static void printIntList(ArrayList<Integer> list){
for(int i = 0;i < list.size();i++){
int val = list.get(i);
System.out.print(val+" \t ");
}
System.out.println();
return;
}
public static ArrayList<String> printList(ArrayList<String> list){
ArrayList<String> l = new ArrayList<>(list);
if(list.size() == 0)System.out.println("list is empty");
Utility.color("blue");
for(String str: list){
System.out.println(str+" ");
}
Utility.color("reset");
System.out.println();
return l;
}
/**
* Prints the elements of a Petri Net.
*
* @param petrinet A HashMap containing the nodes of the Petri Net
* @return The ID of the start event node
*/
public static String printPetriNet(HashMap<String , PetriNetNode> petrinet){
endl();
endl();
color("blue");
System.out.println("Printing Petri Net Elements printPetNet()");
color("white");
String startId= "";
for(Map.Entry<String , PetriNetNode> ele : petrinet.entrySet()){
String key = ele.getKey() ;
Utility.color("red");
System.out.println( key);
Utility.color("reset");
PetriNetNode value = ele.getValue();
// getColorCode(participants);
System.out.println("name = "+value.getName());
System.out.println("incoming Nodes = "+value.getIncomingNodes());
System.out.println("incoming Edges = "+value.getIncomingEdges());
System.out.println("outgoing Nodes = "+value.getOutgoingNodes());
System.out.println("outgoing Edges = "+value.getOutgoingEdges());
System.out.println("outgoing Edges = "+value.getOutgoingEdgesName());
System.out.println("this node is "+typeOfNode(value));
// System.out.println("Is this node Eplace = "+ (value.getisEPlace() == true));
// System.out.println("Is this node Etransition = "+(value.getisETransition() == true));
// System.out.println("Is this node Place = "+(value.getisPlace() == true));
// System.out.println("Is this node Transition = "+(value.getisTransition()==true));
System.out.println("Is this node MessageFlow = "+(value.getMessageFLow()==true));
System.out.println("type of node is "+getNameOfType(value.getTaskType()));
System.out.println("cardinality of the above node is "+value.getCardinality());
System.out.println("is this timer event = "+(value.getIsTimerEvent() ? "YES" : "NO"));
endl();
}
endl();
return startId;
}
public static String typeOfNode(PetriNetNode node){
if(node.getisEPlace()){
return "Eplace";
}
else if(node.getisETransition()){
return "ETransition";
}
else if(node.getisPlace()){
return "Place";
}
else return "Transition";
}
public static String getNameOfType(int value){
if(value == -1)return "Not task";
else if (value == 0)return "Normal task";
else if(value == 1)return "Loop task";
else if(value == 2)return "Multinstance parallel";
else return "Multiinstance sequential";
}
public static void color(String str){
str = str.toLowerCase();
if(str.equals("red"))getColorCode(1);
else if(str.equals("black"))getColorCode(0);
else if(str.equals(("green ")))getColorCode(2);
else if(str.equals("yellow"))getColorCode(3);
else if(str.equals("blue"))getColorCode(4);
else if(str.equals("magneta"))getColorCode(5);
else if(str.equals("cyan"))getColorCode(6);
else if(str.equals("white"))getColorCode(7);
else getColorCode(8);
}
public static void getColorCode(int index){
/**
* Color codes are as follows :-
*
Black: \u001b[30m
Red: \u001b[31m
Green: \u001b[32m
Yellow: \u001b[33m
Blue: \u001b[34m
Magenta: \u001b[35m
Cyan: \u001b[36m
White: \u001b[37m
Reset: \u001b[0m
*/
if(index == 0)System.out.println("\u001b[30m");
else if(index == 1)System.out.println("\u001b[31m");
else if(index == 2)System.out.println("\u001b[32m");
else if(index == 3)System.out.println("\u001b[33m");
else if(index == 4)System.out.println("\u001b[34m");
else if(index == 5)System.out.println("\u001b[35m");
else if(index == 6)System.out.println("\u001b[36m");
else if(index == 7)System.out.println("\u001b[37m");
else if(index == 8)System.out.println("\u001b[0m");
}
/**
* @param map
* @return
*/
public static HashMap<String , PetriNetNode> generatePetriNet(HashMap<String , Graph> map){
HashMap<String , PetriNetNode> petrinet = new HashMap<>();
Queue<String> keys = new ArrayDeque<>();
Queue<String> addedNodes = new ArrayDeque<>();
HashSet<String> visited = new HashSet<>();
//added all nodes to petrinetnode map
/**
* Converts a map of Graph objects to a PetriNetNode object and adds it to a PetriNet.
*
* @param map The map of Graph objects to convert.
* @param petrinet The PetriNet to add the converted PetriNetNode object to.
* @return void
*/
for(Map.Entry<String , Graph> ele : map.entrySet()){
String key = ele.getKey();
if(key.contains("StartEvent"))keys.add(key);
Graph val = ele.getValue();
PetriNetNode node = new PetriNetNode();
node.setName(val.getName());
int transitionOrPlace = val.petriNetStyle;
if(transitionOrPlace == 1)node.setisTransition();
else if(transitionOrPlace == 0)node.setisPlace();
else if(transitionOrPlace == -2)node.setMessageFlow();
// System.out.println(val.getName()+" "+val.petriNetStyle);
petrinet.put(key , node );
}
// start traversing graph from startId and check if there are loops or backedges
int Enodes = 0; // variable to keep track of Enodes added to petri node
while(!keys.isEmpty()){
String currNode = keys.poll();
Graph currGraph = map.get(currNode);
if(currGraph == null )continue;
int curr_type = currGraph.getPetriNetStyle();
if(visited.contains(currNode))continue;
PetriNetNode currentPetriNetNode = petrinet.get(currNode);
currentPetriNetNode.setTaskType(currGraph.getTaskType());
currentPetriNetNode.setCardinality(currGraph.getCardinality());
currentPetriNetNode.setId(currNode);
currentPetriNetNode.setIsTimerEvent(currGraph.getIsTimerEvent());
currentPetriNetNode.setKind(currGraph.getKind());
// if(currGraph.getTaskType() == 2 && currGraph.getCardinality() > 1)parallelMultinstance.add(currNode);
// if(currGraph.getTaskType() == 3 && currGraph.getCardinality() > 1)sequentialMultinstanceLoop.add(currNode);
// if(currGraph.getTaskType() == 1)sequentialMultinstanceLoop.add(currNode);
// if(currGraph.getIsTimerEvent())timerEvents.add(currNode);//this is key through which we identify it
if(currGraph.getTaskType() >= 1 || currGraph.getIsTimerEvent())blackBoxNode.add(currNode);
visited.add(currNode);
//add outgoing nodes from current element in our queue
// ---------------------------------------to write method-----------------------
// addOutgoingNodes(keys , currNode);
for(String out : map.get(currNode).outgoingNodes)
if(!addedNodes.contains(out))keys.add(out);
addedNodes.addAll(map.get(currNode).outgoingNodes);
// ------------------------------------------------------------------------------------------------
ArrayList<String> outgoingNodesCurrNode = map.get(currNode).getOutgoingNodes();
/*
* march 30 adding outgoingedges for current incoming nodes
*/
ArrayList<Integer> outgoingEdgesCurrNode = map.get(currNode).getOutgoingEdges();
ArrayList<String> outgoingEdgesCurrNodeNames = map.get(currNode).getOutgoingEdgesName();
// System.out.println("Node val = "+currNode);
// System.out.println("outgoingNodes = "+outgoingNodesCurrNode);
// System.out.println("outgoingEdges = "+outgoingEdgesCurrNode);
// endl();
//here 1 denotes transition and 0 denotes place
//added there transition and check need of Eplace and Etransitions
int start = 0;
int edgeName = 0;
for(String outgoingNode : outgoingNodesCurrNode){
String runningNode = outgoingNode;
Graph runningGraph = map.get(runningNode);
int edgeType = outgoingEdgesCurrNode.get(start);
if(runningGraph == null)continue;
int runningNode_type = runningGraph.getPetriNetStyle();
PetriNetNode runningPetriNetNode = petrinet.get(runningNode);
//both are transitions then we need to add place but we also need to check if out running transition was already visited then we need to
// get incoming place of already visited transition and then point our current transition to incoming place of outgoing transition
// this case is possible in loop back in parallel gateway to transitions
if(curr_type == 1 && runningNode_type == 1){
if(visited.contains(runningNode) && runningGraph.getKind().contains("task") && currGraph.getKind().contains("task") ){
// runningGraph.getKind().contains("exclusiveGateway")
// Utility.color("blue");
// System.out.println("current node "+currNode+" "+currGraph.getName()+" running node "+runningNode+" "+runningGraph.getName());
// Utility.color("reset");
ArrayList<String> incomingNodesRunningNode = runningPetriNetNode.getIncomingNodes();//as it was transition then incoming nodes will be places
ArrayList<String> incomingNodesRunningNodeEdges = runningPetriNetNode.getOutgoingEdgesName();
// ArrayList<Integer> incomingEdgesRunningNode = runningPetriNetNode.getIncomingEdges();//as it was transition then incoming nodes will be places
// System.out.println("runnning node "+runningNode);
// System.out.println(" incoming node from running Node "+incomingNodesRunningNode);
// System.out.println(" incoming edge from running Node "+incomingEdgesRunningNode);
//there will be edge from currNode to all these incomingNodes of running Node
// endl();
int init = 0;
for(String incoming:incomingNodesRunningNode){
currentPetriNetNode.setOutgoingNodes(incoming);
//march 30
currentPetriNetNode.setOutgoingEdges(edgeType);
currentPetriNetNode.setOutgoingEdgesName(incomingNodesRunningNodeEdges.get(init++));
petrinet.get(incoming).setIncomingNode(currNode);
petrinet.get(incoming).setIncomingEdges(1);
}
}
else{
PetriNetNode Eplace = new PetriNetNode();
Eplace.setisEPlace();
Eplace.setName("E"+Enodes);
/** only changes performed here are using getId instead of getName and using "<>" for separation */
Eplace.setId("E"+Enodes+"<>"+currentPetriNetNode.getId());
petrinet.put(Eplace.getId() , Eplace);//add this Eplace into petri net nodes
currentPetriNetNode.setOutgoingNodes(Eplace.getId()); //add (Eplace) this node as outgoing node to current petri net transition
// march 30
// adding type on basis of current and running node edges
currentPetriNetNode.setOutgoingEdges(edgeType);
if(edgeName < outgoingEdgesCurrNodeNames.size())
currentPetriNetNode.setOutgoingEdgesName(outgoingEdgesCurrNodeNames.get(edgeName++));//setting empty to avoid extra or less names i.e. the data should match with other edges
else
currentPetriNetNode.setOutgoingEdgesName("");
Eplace.setOutgoingNodes(runningNode);//add this transition as outgoing transition to current Eplace
Eplace.setIncomingNode(currNode);//for this Eplace the incoing node is currentNode
//march 30
Eplace.setIncomingEdges(edgeType);
Eplace.setOutgoingEdges(1);//direct edge
Eplace.setOutgoingEdgesName("");//same reason to match the data with other nodes and edges
runningPetriNetNode.setIncomingNode(Eplace.getId()); //for our running node the incoming place is Eplace
runningPetriNetNode.setIncomingEdges(1);
Enodes++;
}
}
//current type is transition and running node type is place
else if(curr_type == 1 && runningNode_type == 0){
// Utility.color("red");
// System.out.println("current node "+currNode+" "+currGraph.getName()+" running node "+runningNode+" "+runningGraph.getName());
// Utility.color("reset");
currentPetriNetNode.setOutgoingNodes(runningNode);
currentPetriNetNode.setOutgoingEdges(edgeType);//march 30
//added on jun1
if(edgeName < outgoingEdgesCurrNodeNames.size())
currentPetriNetNode.setOutgoingEdgesName(outgoingEdgesCurrNodeNames.get(edgeName++));
else
currentPetriNetNode.setOutgoingEdgesName("");
runningPetriNetNode.setIncomingNode(currNode);
runningPetriNetNode.setIncomingEdges(edgeType);//march 30
}
//current node is place and running node is transition
else if(curr_type == 0 && runningNode_type == 1){
// Utility.color("red");
// System.out.println("current node "+currNode+" "+currGraph.getKind()+" running node "+runningNode+" "+runningGraph.getKind());
// Utility.color("reset");
if(visited.contains(runningNode) && ( runningGraph.getKind().contains("exclusiveGateway") || runningGraph.getKind().contains("task") ) ){
ArrayList<String> incomingNodesRunningNode = runningPetriNetNode.getIncomingNodes();//as it was transition then incoming nodes will be places
//there will be edge from currNode to all these incomingNodes of running Node
//march 30
ArrayList<Integer> incomingEdgesRunningNode = runningPetriNetNode.getIncomingEdges();//as it was transition then incoming nodes will be places
for(int i = incomingNodesRunningNode.size() - 1;i >= 0 ;i--){
String incoming = incomingNodesRunningNode.get(i);
// march 30
int newtype = incomingEdgesRunningNode.get(i);
PetriNetNode Etransition = new PetriNetNode();
Etransition.setisETransition();
Etransition.setName("E"+Enodes);
petrinet.put(Etransition.getName() , Etransition);//add this Eplace into petri net nodes
currentPetriNetNode.setOutgoingNodes(Etransition.getName()); //add (Eplace) this node as outgoing node to current petri net transition
//march 30
currentPetriNetNode.setOutgoingEdges(1);
currentPetriNetNode.setOutgoingEdgesName("");
Etransition.setOutgoingNodes(incoming);//add this transition as outgoing transition to current Eplace
//march 30
Etransition.setOutgoingEdges(newtype);
Etransition.setOutgoingEdgesName("");
Etransition.setIncomingNode(currNode);
//march 30
Etransition.setIncomingEdges(1);
petrinet.get(incoming).setIncomingNode(Etransition.getName());
//following lines are added on march 30
Enodes++;
// currentPetriNetNode.setOutgoingNodes(incoming);
// petrinet.get(incoming).setOutgoingNodes(currNode);
}
}
else{
// Utility.color("red");
// System.out.println("current node "+currNode+" "+currGraph.getName()+" running node "+runningNode+" "+runningGraph.getName());
// Utility.color("reset");
currentPetriNetNode.setOutgoingNodes(runningNode);
currentPetriNetNode.setOutgoingEdges(edgeType);//march 30
if(edgeName < outgoingEdgesCurrNodeNames.size())
currentPetriNetNode.setOutgoingEdgesName(outgoingEdgesCurrNodeNames.get(edgeName++));
else
currentPetriNetNode.setOutgoingEdgesName("");
runningPetriNetNode.setIncomingNode(currNode);
runningPetriNetNode.setIncomingEdges(edgeType);//march 30
}
}
//current node is place and running node is also place
else if(curr_type == 0 && runningNode_type == 0){
PetriNetNode Etransition = new PetriNetNode();
Etransition.setisETransition();
Etransition.setName("E"+Enodes);
petrinet.put(Etransition.getName() , Etransition);//add this Eplace into petri net nodes
currentPetriNetNode.setOutgoingNodes(Etransition.getName()); //add (Eplace) this node as outgoing node to current petri net transition
if(edgeName < outgoingEdgesCurrNodeNames.size())
currentPetriNetNode.setOutgoingEdgesName(outgoingEdgesCurrNodeNames.get(edgeName++));
else
currentPetriNetNode.setOutgoingEdgesName("");
Etransition.setOutgoingNodes(runningNode);//add this transition as outgoing transition to current Eplace
Etransition.setIncomingNode(currNode);
runningPetriNetNode.setIncomingNode(Etransition.getName());
//following 4 lines are added on march 30
currentPetriNetNode.setOutgoingEdges(edgeType);
Etransition.setOutgoingEdges(1);
Etransition.setOutgoingEdgesName("");
Etransition.setIncomingEdges(edgeType);
runningPetriNetNode.setIncomingEdges(1);
Enodes++;
}
start++;
}
}
return petrinet;
}
// public static void addAll
public static void patternsBlock(String pattern , String value , int type){
/*
* this block over here iterates for each pattern like task
* for taks it will find all task ids their name, source ref and target ref one by one
*/
String startingPattern = "<bpmn:"+pattern , endingPattern = !pattern.equals("messageFlow") && !pattern.equals("sequenceFlow") ? "</bpmn:"+pattern : "/>";
String[][] properties = { {"id=" ,"\"" } , {"name=" , "\""} , {"sourceRef=" , "\""} , {"targetRef=" , "\""} , {"loop=" , "\""} , {"timerEventDefinition" , "\'"}};
// System.out.println("current pattern is \u001B[31m"+pattern+" ");
// System.out.println("starting pattern "+startingPattern+" ending pattern "+endingPattern+"\u001B[0m ");
// System.out.println(value.substring(1100)+" ");
int startIndex = value.indexOf(startingPattern);
while (startIndex != -1) {
int endIndex = !endingPattern.equals("/>") ? value.indexOf(endingPattern) : getIndex(endingPattern , startIndex , value);
String substring = value.substring(startIndex , endIndex);
String[][] arr = new String[7][3];//here 5 denotes properties which we will be extracting
int index = 0;
for(int i = 0;i < 4;i++){
// System.out.print(ele[0]+" ");
String[] ele = properties[i];
String ans = printId(substring , ele[0] , ele[1]);
arr[index][0] = ele[0].trim();
arr[index][1] = ans.trim();
arr[index++][2] = type+"";
// System.out.println(ans);
// System.out.println();
}
/**
* Some properties are easy to disect whereas the properties such as loop cardinality and its type, timerEvents and other properties are
* difficult to remove and create a node out of it
* thus we extract it in new way
*/
index += 2;
arr[index-2][0] = properties[index-2][0];
int[] typeAndCardinality = getLoopType(substring, pattern);
arr[index-2][1] = typeAndCardinality[0]+" "+typeAndCardinality[1];
// arr[index-1]
// System.out.println(pattern+" -> "+typeAndCardinality[0]+" "+typeAndCardinality[1]);
// System.out.println(substring+" pattern is "+pattern+" "+arr[index-1][1]);
// printValues(arr);
arr[index-1][0] = properties[index-1][0];
arr[index-1][1] = substring.contains(properties[index-1][0]) == true ? "YES" : "NO";
arr[index][0] = pattern;
// Utility.color("green");
// System.out.println(substring+" pattern is "+pattern+" ");
// Utility.color("reset");
createGraph(arr, map);
// printGraph();
// System.out.println();
value = value.substring(endIndex+endingPattern.length());
startIndex = value.indexOf(startingPattern);
}
}
//date 14 May
public static int[] getLoopType(String str , String pattern){
// System.out.println("printing string input " + str + "pattern is " + pattern);
// str = str.toLowerCase();
if(!pattern.toLowerCase().contains("task"))return new int[]{-1 , 0};
else if(str.contains("standardLoopCharacteristics"))return new int[]{ 1,1};
else if(str.contains("multiInstanceLoopCharacteristics") && str.contains("isSequential"))return new int[]{3 , getCardinality(str)};
else if(str.contains("multiInstanceLoopCharacteristics") && !str.contains("isSequential"))return new int[]{2 , getCardinality(str)};
return new int[]{0,1};
}
public static int getCardinality(String str){
int index = str.indexOf("</bpmn:loopCardinality>");
int start = index - 1;
while((int)(str.charAt(start) - '0') >= 0 && (int)(str.charAt(start) - '0') <= 9)start--;
int val = Integer.parseInt(str.substring(start + 1, index));
// System.out.println(str.substring(start, index));
return val;
}
// date april 17 2023
/*
* for each subprocess there will be list of start and end list which will depicts
* the start and end of subprocess thus it will be better to use hashmap in such case
* Key = subprocess id
* value = call storing start and end events as list
*
* the below function will find all subproces in process and return its index
* or string possible
* for this we will be using regex and use list to store all index of subprocess
* ever occured
*
*/
static class SubEvents{
ArrayList<String> start , end;
String name;
SubEvents(ArrayList<String> start , ArrayList<String> end , String name){
this.start = start;
this.end = end;
this.name = name;
}
}
/*
* this methods removes complete string pointed by given startIndex and endIndex
*/
public static ArrayList<String> metaData(ArrayList<Integer> startIndex , ArrayList<Integer> endIndex , String str){
ArrayList<String> subprocess = new ArrayList<>();
for(int i = 0;i < startIndex.size();i++){
int start = startIndex.get(i) , end = endIndex.get(i);
String substring = str.substring(start , end);
subprocess.add(substring);
}
return subprocess;
}
public static ArrayList<String> subprocessMeta(String str){
/*first finding out all subprocess indexes and then one by one finding all start and end events */
// System.out.println("string received = "+str);
ArrayList<Integer> startIndex = getListIndex("<bpmn:subProcess", str) ;
ArrayList<Integer> endIndex = getListIndex("</bpmn:subProcess>", str) ;
/*parsing the subprocess from complete model all at once */
ArrayList<String> subprocess = metaData(startIndex, endIndex , str);
subprocessEvents = new HashMap<>();
for(int i = 0 ;i < subprocess.size();i++){
String ele = subprocess.get(i);
String id = printId(ele,"id=", "\"");
String name = printId(ele , "name=" , "\"");
// System.out.println("ele is = "+ele);
/*removing startEvents indexes and the complete block pointed by the given indexes but we need their
* id's for that we will remove using printId method and for that we need to call it
* recursively or iteratively
*/
ArrayList<Integer> startEle = getListIndex("<bpmn:startEvent", ele);
ArrayList<Integer> endEle = getListIndex("</bpmn:startEvent>" , ele);
ArrayList<String> startmetaIdStrings = metaData(startEle , endEle , ele);
ArrayList<String> startIds = new ArrayList<>();
for(String val : startmetaIdStrings){
startIds.add( printId(val, "id=", "\"")) ;
}
/*removing endEvents indexes */
startEle = getListIndex("<bpmn:endEvent", ele);
endEle = getListIndex("</bpmn:endEvent>" , ele);
ArrayList<String> endmetaIdStrings = metaData(startEle , endEle , ele);
ArrayList<String> endIds = new ArrayList<>();
for(String val: endmetaIdStrings){
endIds.add( printId(val, "id=", "\""));
}
/*
* adding this nodes in map so that the else if block in createGraph will have this as values
*/
Graph ob = new Graph();
for(int j = 0;j < startIds.size();j++){
ob.setIncomingNode(startIds.get(j));
ob.setIncomingEdges(1);
}
for(int j = 0;j < endIds.size();j++){
ob.setOutgoingNodes(endIds.get(j));
ob.setOutgoingEdges(1);
}
// SubEvents obj = new SubEvents(startIds, endIds, name);
// subprocessEvents.put(id , obj);
map.put(id , ob);
}
return subprocess;
}
public static void printlist(ArrayList<String> list){
for(int i = 0;i < list.size();i++){
String ele = list.get(i);
System.out.println(ele);
}
endl();
return;
}
public static ArrayList<Integer> getListIndex(String pattern , String str){
ArrayList<Integer> startIndex = new ArrayList<>();
Matcher matcher = Pattern.compile(pattern).matcher(str);
while (matcher.find()) {
startIndex.add(matcher.start());
}
return startIndex;
}
/*
NOTE:- this function will be written for single subprocess, but with
a process with multiple subprocess then we need to make individual call to this
function as this is sinle working string function
* writing two function two find start and end of subprocess
* as the link are not directly to elements to subprocess as whole
* thus two methods are needed so that we can find start as well as the
* end of subprocess
* so when we encounter subprocess in targetref then we need to replace it
* with the start events of subprovess or if we find subprocess in sourceref
* then we will replace it with the end event of subprovess
* NOTE:- we need to make sure that the links are established properly aas the start and end
* events can be various thus using it as list will be viable option
*/
// public static SubEvents findStartEventSubprocess(String str){
// }
public static int getIndex(String substr , int startIndex, String value){
int len = substr.length();
for(int i = startIndex;i + len <= value.length(); i++){
if(value.substring(i, i+len).equals(substr))return i;
}
return -1;
}
public static void printGraph(){
endl();
endl();
System.out.println("Printing Elements extracted from bpmn file printgraph() ");
for(Map.Entry<String , Graph> ele : map.entrySet()){
String key = ele.getKey() ;
Graph value = ele.getValue();
Utility.color("red");
System.out.println(key);
Utility.color("reset");
System.out.println("name = "+value.name);
System.out.println("kind of element = "+value.getKind());
System.out.println("incoming Nodes = "+value.getIncomingNodes());
System.out.println("incoming Edges = "+value.getIncomingEdges());
System.out.println("outgoing Node = "+value.getOutgoingNodes());
System.out.println("outgoing Edges = "+value.getOutgoingEdges());
System.out.println("outgoing Edges Names = "+value.getOutgoingEdgesName());
// System.out.println("Sizes of list are equal = "+(value.getOutgoingEdgesName().size() == value.getOutgoingNodes().size() && value.getOutgoingNodes().size() == value.getOutgoingEdges().size()));
System.out.println("values of list are = "+(value.getOutgoingEdgesName().size() +" "+ value.getOutgoingNodes().size()+" "+ value.getOutgoingEdges().size()));
System.out.println("Petri Net style = "+value.getPetriNetStyle());
System.out.println("type of task = "+value.getTaskType());
System.out.println("cardinality of task = "+value.getCardinality());
System.out.println("is this timer event = "+(value.getIsTimerEvent() ? "YES" : "NO"));
endl();
}
}
public static void print(Graph node){
System.out.println("name = "+node.name);
System.out.println("incoming node = "+node.getIncomingNodes());
System.out.println("outgoing node = "+node.getOutgoingNodes());
}
public static void createGraph(String[][] arr , HashMap<String,Graph> map){
// System.out.println("Printing Map \u001B[33m"+map+"\u001B[0m ");
// out.printLine("create graph");
for(int i = 0;i < arr.length;i++){
String prop = arr[i][0] , values = arr[i][1] , type = arr[i][2];
// out.printLine("\u001b[32m");
// out.printLine(prop+" "+values+" "+type+" "+map.containsKey(values));
// out.printLine("\u001b[0m");
if(prop.contains("id") && arr[2][1].length() == 0){
map.put(values , new Graph());
map.get(values).setPetriNetStyle(Integer.parseInt(type));
String str = arr[arr.length - 1][0];
if(str.length() == 0)
map.get(values).setKind("participant");
else
map.get(values).setKind(arr[arr.length-1][0]);
}
else if(prop.contains("name") && map.containsKey(arr[0][1])){
if(values.length() == 0)
(map.get(arr[0][1])).setName(arr[0][1]);
else
(map.get(arr[0][1])).setName(values);
}
//setting outgoing nodes for each element
else if(prop.contains("sourceRef") && values.length() > 0 && map.containsKey(values)){
// String strKind = map.get(arr[0][1]) == null || map.get(arr[0][1]).getKind() == null ? "" : map.get(arr[0][1]).getKind() ;
// if(strKind.length() == 0)
// map.get(arr[0][1]).setKind("participant");
// System.out.println("Setting values on basis of values "+strKind);
if(arr[i+1][1].contains("Participant")){
Graph ob = new Graph();
String name = refMapParticipant.get(arr[i+1][1])+""+participants;
ob.setName(name);
ob.setKind("participant");
ob.setIncomingNode(values);
ob.setIncomingEdges(-1);//it is message flow type
ob.setPetriNetStyle(1);//it is transition
// map.remove(arr[0][1]);
// break;
// System.out.println("name is "+name+" kind is = "+ob.getKind());
map.get(values).setOutgoingNodes(name);
map.get(values).setOutgoingEdges(-1);
map.get(values).setOutgoingEdgesName(arr[i-1][1]);
if(!map.containsKey(name)){
participantOccurence.add(name);
nameStoring.get(arr[i+1][1]).add(name);
}
map.put(name , ob);
participants++;
}
else if(values.contains("SubProcess")){
// out.printLine("\u001b[32m"); //green
// out.printLine("Subprocess is sourceRef"+map.get(values).name);
ArrayList<String> outcomingNodes = map.get(values).getOutgoingNodes();
ArrayList<Integer> outcomingEdges = map.get(values).getOutgoingEdges();
ArrayList<String> outcomingEdgesNames = map.get(values).getOutgoingEdgesName();
int ptr = 0;
for(String str : outcomingNodes){
map.get(str).setOutgoingNodes(arr[i+1][1]);
map.get(str).setOutgoingEdges(outcomingEdges.get(ptr));
/*setting incoming nodes and edges in both nodes to avoid reprocessing in main function which
* removes unnecessary Place to place or Transition to transition errors
*/
if(ptr < outcomingEdgesNames.size())
map.get(str).setOutgoingEdgesName(outcomingEdgesNames.get(ptr));
else
map.get(str).setOutgoingEdgesName("");
ptr++;
map.get(arr[i+1][1]).setIncomingNode(str);
map.get(arr[i+1][1]).setIncomingEdges(1);
}
// print(map.get(values));
// out.printLine("\u001b[0m ");
}
else {
map.get(values).setOutgoingNodes(arr[i+1][1]);
/** adding names of edges */
map.get(values).setOutgoingEdgesName(arr[i-1][1]);
}
if(arr[0][1].contains("MessageFlow")){
map.get(values).setOutgoingEdges(-1);
// map.get(values).setOutgoingEdgesName("");
// map.get(values).setMessageFlow();
}
else{
map.get(values).setOutgoingEdges(1);
// map.get(values).setOutgoingEdgesName("");
}
// endl();
// System.out.println("new node "+values);
// System.out.println("\u001B[31m"); //RED
// System.out.println("print function executed");
// print(map.get(values));
// System.out.println("\u001B[0m ");
// endl();
}
//setting incoming nodes for each element
else if(prop.contains("targetRef") && values.length() > 0 && map.containsKey(values)){
if(arr[i-1][1].contains("Participant")){
Graph ob = new Graph();
// System.out.println("name of the participant is "+arr[i-1][1]);
String name = refMapParticipant.get(arr[i-1][1])+""+participants;
ob.setName(name);
ob.setKind("participant");
ob.setOutgoingNodes(values);
ob.setOutgoingEdges(-1);//it is message flow type
map.get(values).setOutgoingEdgesName(arr[i-2][1]);
ob.setPetriNetStyle(1);//it is transition
// map.remove(arr[0][1]);
// break;
map.get(values).setIncomingNode(name);
map.get(values).setIncomingEdges(-1);
if(!map.containsKey(name)){
participantOccurence.add(name);
nameStoring.get(arr[i-1][1]).add(name);
}
map.put(name , ob);
participants++;
}
else if(values.trim().contains("SubProcess")){
// System.out.println("\u001b[32m"); //green
System.out.println("Subprocess is targetRef"+map.get(values).name);
ArrayList<String> incomingNodes= map.get(values).getIncomingNodes();
ArrayList<Integer> incomingEdges = map.get(values).getIncomingEdges();