Parent: [0181a5] (diff)

Child: [275565] (diff)

Download this file

Main.java    203 lines (175 with data), 7.4 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
package converter;
import converter.utils.StringPair;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static String path;
public static String pathToSave;
private static boolean infer;
public static void main(String[] args) throws IOException, JSONException, ParseException {
JSONObject jsonObject;
// create Options object
Options options = new Options();
// add t option
options.addOption("help", false, "Show all options");
options.addOption("h", false, "Show all options");
options.addOption("config", true, "Choose config file");
options.addOption("s", true, "Choose source file");
options.addOption("d", true, "Choose destination file");
options.addOption("i", false, "Infer start and end events");
CommandLine cmd = new BasicParser().parse(options, args);
if(cmd.hasOption("h") || cmd.hasOption("help") || cmd.getArgs().length == 0) {
System.out.println("-config\tChoose config file\n-s\tChoose source file - Mandatory\n-d\tChoose destination file\n-i\tInfer start and end events");
System.exit(0);
}
if (cmd.hasOption("i"))
infer = true;
else
infer = false;
if (cmd.hasOption("config")) {
jsonObject = getJSONFromFile(cmd.getOptionValue("config"));
// System.out.println(path);
} else
jsonObject = new JSONObject("{\n" +
"\tActivity:[\"BusinessActivity\",\"BusinessProcess\"],\n" +
"\tStart:[\"BusinessEvent\"],\n" +
"\tEnd:[\"BusinessEvent\"],\n" +
"}");
if (cmd.hasOption("s"))
path = cmd.getOptionValue("s");
else
throw new IllegalArgumentException("No source file is set");
if (cmd.hasOption("d"))
pathToSave = cmd.getOptionValue("d");
else
pathToSave = "output.bpmn";
// System.out.println(jsonObject);
OwlManager owl = new OwlManager(path);
BPMNManager bpm = new BPMNManager();
for (int i = 0; i < jsonObject.getJSONArray("Activity").length(); i++) {
HashMap<String, String> ba = owl.getElementsFromClass(jsonObject.getJSONArray("Activity").getString(i));
for (String id : ba.keySet()) {
bpm.addActivity(id, ba.get(id));
}
}
HashMap<String, String> junc = owl.getJunctions();
ArrayList<String> mergeCandidates = owl.getElementWithNumberOfInFlows(2, true);
for (String id : junc.keySet()) {
if (mergeCandidates.contains(id))
bpm.addMerge(id, junc.get(id));
}
ArrayList<String> decisionCandidates = owl.getElementWithNumberOfOutFlows(2, true);
for (String id : junc.keySet()) {
if (decisionCandidates.contains(id))
bpm.addDecision(id, junc.get(id));
}
HashMap<String, String> andJunc = owl.getAndJunctions();
ArrayList<String> possibleJoins = owl.getElementWithNumberOfInFlows(2, true);
for (String id : andJunc.keySet()) {
if (possibleJoins.contains(id))
bpm.addJoin(id, andJunc.get(id));
else
bpm.addFork(id, andJunc.get(id));
}
HashMap<String, String> candidateStartEnd = owl.getElementsFromClass("BusinessEvent");
for (String a : candidateStartEnd.keySet()) {
if (owl.getElementWithNumberOfInFlows(0, false).contains(a) && owl.getElementWithNumberOfOutFlows(1, false).contains(a))
bpm.addStart(a, candidateStartEnd.get(a));
if (owl.getElementWithNumberOfInFlows(1, false).contains(a)) {
int n = 0;
for (StringPair sp : owl.getFlows())
if (sp.getFirst().equals(a)) {
n = 1;
}
if (n == 0)
bpm.addFinish(a, candidateStartEnd.get(a));
}
}
if (infer) {
ArrayList<String> possibleStartEvents = owl.getElementWithNumberOfInFlows(0, false);
// System.out.println(possibleStartEvents);
ArrayList<String> possibleEndEvents = new ArrayList<String>();
ArrayList<StringPair> flows = owl.getFlows();
for (String a : bpm.getElements().keySet()) {
boolean exists = false;
for (StringPair aa : flows) {
if (aa.getFirst().equals(a)) {
exists = true;
break;
}
}
if (!exists)
possibleEndEvents.add(a);
}
for (String a : possibleEndEvents) {
// if(!possibleStartEvents.contains(a))
bpm.addArtificialFinish(a);
}
for (String a : possibleStartEvents) {
// if(oneOuters.contains(a) || twoOuters.contains(a))
bpm.addArtificialStart(a);
}
}
ArrayList<StringPair> fl = owl.getFlows();
for (StringPair id : fl)
bpm.addFlow(id.getFirst(), id.getSecond());
bpm.saveModel(pathToSave);
/*
HashMap<String, String> ba = owl.getBusinessActivities();
for (String id : ba.keySet()) {
bpm.addActivity(id, ba.get(id));
}
HashMap<String, String> bp = owl.getBusinessProcesses();
for (String id : bp.keySet()) {
bpm.addActivity(id, bp.get(id));
}*/
}
private static JSONObject getJSONFromFile(String path) {
/*
BufferedReader br;
JSONObject jsonObject = null;
try {
br = new BufferedReader(new FileReader(new File(path)));
StringBuilder sb = new StringBuilder();
String line;
line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
br.close();
jsonObject = new JSONObject(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
*/
try {
return new JSONObject(IOUtils.toString(new FileInputStream(fileHandler(path))));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static File fileHandler(String path){
File file;
if (path.startsWith("/") || path.startsWith("~"))
file = new File(path);
else
file = new File(new File(System.getProperty("user.dir")), path);
return file;
}
}