Repository navigation
Expand file tree
/
Copy pathGenerateBPMN.java
More file actions
149 lines (129 loc) · 6.22 KB
/
Copy pathGenerateBPMN.java
File metadata and controls
149 lines (129 loc) · 6.22 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
import java.io.FileWriter; // Import the FileWriter class
import java.io.IOException;
import java.util.*;
class GenerateBPMN{
/**
* Generates BPMN file from graph. It is assumed that graph is acyclic. This method is called by generate
*
* @param map - Map of nodes to
*/
public void generate(HashMap<String,Graph> map){
try {
FileWriter myWriter = new FileWriter("bpmn.dot");
// myWriter.write("Files in Java might be tricky, but it is fun enough!");
// myWriter.write("digraph D {");
myWriter.write("digraph D { \n rankdir=LR;\n size=\"19,12\" ; \n ratio=\"fill\";\n fontsize=\"15\" \n;");
metaData(map , myWriter);
traverseGraph(map , myWriter);
myWriter.write("}");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
/**
* Traverses the graph and writes to the file. This method is used for debugging purposes as a part of the GraphTrainer.
*
* @param map - The map to traverse. The key is the name of the graph the value is the Graph.
* @param writer - The FileWriter to write the graph to. The file is written to
*/
public void traverseGraph(HashMap<String,Graph> map , FileWriter writer){
for(Map.Entry<String , Graph> ele : map.entrySet()){
try{
String key = ele.getKey() ;
Graph value = ele.getValue();
// If the value is empty continue.
if(value.getOutgoingNodes().size() == 0)continue;
for(int i = 0;i < value.getOutgoingNodes().size();i++){
String o_node = value.getOutgoingNodes().get(i);
if(!map.containsKey(o_node) || convert(value.getName()).startsWith("_") ||
convert(map.get(o_node).getName()).startsWith("_") || convert(value.getName()).length() < 1)continue;
writer.write(convert(value.getName())+"-> { " +convert(map.get(o_node).getName())+"}"+
( value.getOutgoingEdges().get(i) == -1 ? "[style = dashed]" : "" ));
if(value.getOutgoingEdgesName().size() > i && value.getOutgoingEdgesName().get(i).length() >= 1){
writer.write("[label = \""+convert( value.getOutgoingEdgesName().get(i))+"\"];\n");
}
else writer.write("\n");
}
}
catch(IOException e){
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
/**
* Writes meta data to file. This method is used to write meta data for each graph in the graph.
*
* @param map - Map of graph names to graphs that should be written
* @param writer - File to which we are writing
*/
public void metaData(HashMap<String ,Graph> map , FileWriter writer){
for(Map.Entry<String , Graph> ele : map.entrySet()){
try{
String key = ele.getKey() ;
Graph value = ele.getValue();
String name = value.getName();
if(convert(name).length() < 1 || convert(name).startsWith("_") || printName(name).startsWith("_"))continue;
// Write out the shape of the box.
if(key.contains("Task") || value.getKind().contains("participant") || key.contains("Activity")){
writer.write(convert(name)+" [ shape = box ,style=filled,height= 0.4,label= \"\", xlabel=\""+ printName(name)+" \" ] ;");
}
// Write the shape of the event.
else if(key.contains("Gateway")){
writer.write(convert(name)+" [shape = diamond ,label= \"\", xlabel=\""+ printName(name)+" \" ] ;");
}
// Draw a circle circle style filled and filled label.
else if(key.contains("StartEvent")){
writer.write(convert(name)+" [shape = circle ,label= \"\" , xlabel=\""+ printName(name)+" \" ] ;");
}
else if(key.contains("Intermediate")){
writer.write(convert(name)+" [shape = doublecircle,style=filled ,label= \"\", xlabel=\""+ printName(name)+" \" ] ;");
}
else{
writer.write(convert(name)+" [shape = doublecircle,style=filled ,label= \"\", xlabel=\""+ printName(name)+" \" , fillcolor=black] ;");
}
writer.write("\n");
}
catch(IOException e){
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
/**
* Converts a string to a space separated list. This is used to make it easier to read the results of the user's program
*
* @param str - The string to be converted
*
* @return A space separated list of the user's program output as a single string with spaces in between each
*/
public String convert(String str){
String[] arr = str.split(" ");
StringBuilder sb = new StringBuilder("");
for(String val : arr){
// Append the value to the buffer.
if(!val.contains("?"))sb.append(val+"_");
}
return sb.toString();
}
/**
* Prints a name to a new line. This is useful for debugging the program.
If you want to print the name of a class or class in a way that does not contain spaces use printClass ( name )
* It is different from above method as convert uses "?" for adding "_" character to avoid clumsyness in output we use :"/n"
* @param str - The name to print.
*
* @return The name of the class or class in a new line separated by spaces ( " Class " ). For example : " Class_A_B
*/
public static String printName(String str){
String[] arr = str.split(" ");
StringBuilder sb = new StringBuilder("");
for(String val : arr){
// Append a _n to the buffer
if(!val.contains("?"))sb.append(val+"_\n");
}
return sb.toString();
}
}