private static BPMNDiagram removeConnectedGateways(BPMNDiagram diagram) {
    List<Gateway> XOR = getXORGateways(diagram);
    Iterator<Flow> iterator = diagram.getFlows().iterator();

    while (iterator.hasNext()) {
      Flow flow = iterator.next();
      if (XOR.contains(flow.getSource())
          && XOR.contains(flow.getTarget())
          && isSplit(diagram, flow.getSource())
          && isSplit(diagram, flow.getTarget())) {
        if (!flow.getSource().equals(flow.getTarget())) {
          List<Flow> flows = getOutgoingFlows(diagram, flow.getTarget());
          for (Flow flow1 : flows) {
            diagram.addFlow(flow.getSource(), flow1.getTarget(), "");
          }
          diagram.removeGateway((Gateway) flow.getTarget());
          XOR = getXORGateways(diagram);
        } else {
          diagram.removeEdge(flow);
        }
        iterator = diagram.getFlows().iterator();
      }
    }

    return diagram;
  }
 private static List<Activity> getIncomingActivities(BPMNDiagram diagram, BPMNNode bpmnNode) {
   List<Activity> flows = new ArrayList<Activity>();
   for (Flow flow : diagram.getFlows()) {
     if (flow.getTarget().equals(bpmnNode) && flow.getSource() instanceof Activity) {
       flows.add((Activity) flow.getSource());
     }
   }
   return flows;
 }
 private static List<Flow> getOutgoingFlows(BPMNDiagram diagram, BPMNNode bpmnNode) {
   List<Flow> flows = new ArrayList<Flow>();
   for (Flow flow : diagram.getFlows()) {
     if (flow.getSource().equals(bpmnNode)) {
       flows.add(flow);
     }
   }
   return flows;
 }
 private static List<BPMNNode> getIncomingNodes(BPMNDiagram diagram, BPMNNode bpmnNode) {
   List<BPMNNode> flows = new ArrayList<BPMNNode>();
   for (Flow flow : diagram.getFlows()) {
     if (flow.getTarget().equals(bpmnNode)) {
       flows.add(flow.getSource());
     }
   }
   return flows;
 }
  private static BPMNDiagram removeXORtoAND(BPMNDiagram diagram) {
    List<Gateway> XOR = getXORGateways(diagram);
    List<Gateway> AND = getANDGateways(diagram);
    for (Gateway gateway1 : XOR) {
      for (Gateway gateway2 : AND) {
        List<Flow> flows = new ArrayList<Flow>();
        for (Flow flow : diagram.getFlows()) {
          if (flow.getSource().equals(gateway1) && flow.getTarget().equals(gateway2)) {
            flows.add(flow);
          }
        }
        if (flows.size() > 1) {
          for (int i = 1; i < flows.size(); i++) {
            diagram.removeEdge(flows.get(i));
          }
        }
      }
    }

    return diagram;
  }
  private static BPMNDiagram mergeActivitiesAfterXOR(
      BPMNDiagram diagram, Gateway gateway, Activity activity1, Activity activity2) {
    List<Flow> outgoingFlows1 = getOutgoingFlows(diagram, activity1);
    List<Flow> outgoingFlows2 = getOutgoingFlows(diagram, activity2);
    List<Flow> outgoingFlows3 = getOutgoingFlows(diagram, gateway);
    List<Flow> incomingFlows = getIncomingFlows(diagram, gateway);

    String label = activity1.getLabel();
    Activity activity = diagram.addActivity(label, false, false, false, false, false);

    Set<BPMNNode> outgoingNodes1 = new UnifiedSet<BPMNNode>();
    for (Flow flow : outgoingFlows1) {
      outgoingNodes1.add(flow.getTarget());
    }
    for (Flow flow : outgoingFlows2) {
      outgoingNodes1.add(flow.getTarget());
    }

    if (outgoingNodes1.size() > 1) {
      Gateway gateway1 = diagram.addGateway("", Gateway.GatewayType.DATABASED);

      Set<BPMNNode> outgoingNodes = new UnifiedSet<BPMNNode>();
      for (Flow flow : outgoingFlows1) {
        if (!outgoingNodes.contains(flow.getTarget())) {
          outgoingNodes.add(flow.getTarget());
          diagram.addFlow(gateway1, flow.getTarget(), "");
        }
      }

      for (Flow flow : outgoingFlows2) {
        if (!outgoingNodes.contains(flow.getTarget())) {
          outgoingNodes.add(flow.getTarget());
          diagram.addFlow(gateway1, flow.getTarget(), "");
        }
      }

      diagram.addFlow(activity, gateway1, "");
    } else if (outgoingNodes1.size() > 0) {
      diagram.addFlow(activity, outgoingNodes1.toArray(new BPMNNode[1])[0], "");
    }

    diagram.removeActivity(activity1);
    diagram.removeActivity(activity2);

    if (outgoingFlows3.size() == 2) {
      for (Flow flow : incomingFlows) {
        diagram.addFlow(flow.getSource(), activity, "");
      }
      diagram.removeGateway(gateway);
    } else {
      diagram.addFlow(gateway, activity, "");
    }
    //            if(outgoingNodes.size() == 1) {
    //                BPMNNode node = outgoingNodes.toArray(new BPMNNode[1])[0];
    //                if(node instanceof Gateway) {
    //                    Gateway gateway2 = (Gateway) node;
    //                    if(gateway2.getGatewayType().equals(Gateway.GatewayType.DATABASED)) {
    //                        List<BPMNNode> list = new ArrayList<BPMNNode>();
    //                        for(Flow flow: diagram.getFlows()) {
    //                            if(flow.getSource().equals(gateway2)) {
    //                                list.add(flow.getTarget());
    //                            }
    //                        }
    //                        for(BPMNNode node1 : list) {
    //                            diagram.addFlow(activity, node1, "");
    //                        }
    //                        diagram.removeGateway(gateway1);
    //                        diagram.removeGateway(gateway2);
    //                    }
    //                }
    //            }
    //        }

    return diagram;
  }