public static void main(String[] args) throws Exception {

    System.out.println("This is the stand alone version of the BPMNMiner algorithm proposed in:");
    System.out.println();
    System.out.println("R. Conforti, M. Dumas, L. García-Bañuelos, and M. La Rosa.");
    System.out.println(
        "BPMN Miner: Automated discovery of BPMN process models with hierarchical structure.");
    System.out.println("Information Systems, 56, pp 284-303, 2016.");
    System.out.println();
    System.out.println("For more info contact me at [email protected]");
    System.out.println("or visit my website www.raffaeleconforti.com");

    Scanner console = new Scanner(System.in);
    System.out.println("Input file:");
    String name = console.nextLine();

    while (name.endsWith(" ")) {
      name = name.substring(0, name.length() - 1);
    }

    XFactory factory = new XFactoryMemoryImpl();
    XLog log = LogImporter.importFromFile(factory, name);

    UIPluginContext fakeContext = new FakePluginContext();
    BPMNMinerCommandLine plugin = new BPMNMinerCommandLine();

    BPMNDiagram diagram = plugin.mineBPMNModel(fakeContext, log);

    System.out.println("Do you want to structure the diagram? (y/n)");
    String token = null;
    boolean structure = true;
    while (token == null) {
      token = console.nextLine();
      if (token.equalsIgnoreCase("y")) {
        structure = true;
      } else if (token.equalsIgnoreCase("n")) {
        structure = false;
      } else {
        token = null;
        System.out.println("Accepted input Y or N");
        System.out.println("Do you want to structure the diagram? (y/n)");
      }
    }

    for (Activity activity : diagram.getActivities()) {
      if (activity.getLabel().endsWith("+complete")) {
        activity
            .getAttributeMap()
            .put(
                "ProM_Vis_attr_label",
                activity.getLabel().substring(0, activity.getLabel().indexOf("+complete")));
      }
    }

    if (structure) {
      StructuringService ss = new StructuringService();
      diagram = ss.structureDiagram(diagram);
    }

    if (name.endsWith(".xes")) name = name.substring(0, name.length() - 4);
    else if (name.endsWith(".xes.gz")) name = name.substring(0, name.length() - 7);
    else if (name.endsWith(".mxml")) name = name.substring(0, name.length() - 5);
    else if (name.endsWith(".mxml.gz")) name = name.substring(0, name.length() - 8);

    System.out.println("Output file: " + name + ".bpmn");
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    UIContext context = new UIContext();
    UIPluginContext uiPluginContext = context.getMainPluginContext();
    BpmnDefinitions.BpmnDefinitionsBuilder definitionsBuilder =
        new BpmnDefinitions.BpmnDefinitionsBuilder(uiPluginContext, diagram);
    BpmnDefinitions definitions = new BpmnDefinitions("definitions", definitionsBuilder);

    StringBuilder sb = new StringBuilder();
    sb.append(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\"\n "
            + "xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\"\n "
            + "xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\"\n "
            + "xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\"\n "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n "
            + "targetNamespace=\"http://www.omg.org/bpmn20\"\n "
            + "xsi:schemaLocation=\"http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd\">");

    sb.append(definitions.exportElements());
    sb.append("</definitions>");
    FileWriter fileWriter = new FileWriter(new File(name + ".bpmn"));
    fileWriter.write(sb.toString());
    fileWriter.flush();
    fileWriter.close();
  }