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;
}
}