public Graph filter(Graph[] graphs) {
   HierarchicalGraph minHGraph = (HierarchicalGraph) graphs[0];
   int minElements = Integer.MAX_VALUE;
   for (int i = 0; i < graphs.length; i++) {
     int count = ((HierarchicalGraph) graphs[i]).getNodeCount();
     if (count < minElements) {
       minHGraph = (HierarchicalGraph) graphs[i];
       minElements = count;
     }
   }
   for (Node n : minHGraph.getNodes().toArray()) {
     for (int i = 0; i < graphs.length; i++) {
       if ((HierarchicalGraph) graphs[i] != minHGraph) {
         if (!((HierarchicalGraph) graphs[i]).contains(n)) {
           minHGraph.removeNode(n);
           break;
         }
       }
     }
   }
   for (Edge e : minHGraph.getEdges().toArray()) {
     for (int i = 0; i < graphs.length; i++) {
       if ((HierarchicalGraph) graphs[i] != minHGraph) {
         if (!((HierarchicalGraph) graphs[i]).contains(e)) {
           minHGraph.removeEdge(e);
           break;
         }
       }
     }
   }
   return minHGraph;
 }
  private void writeNodes(XMLStreamWriter xmlWriter, HierarchicalGraph graph) throws Exception {
    if (cancel) {
      return;
    }
    xmlWriter.writeStartElement(NODES);

    AttributeColumn dynamicCol =
        dynamicCol = attributeModel.getNodeTable().getColumn(DynamicModel.TIMEINTERVAL_COLUMN);

    NodeIterable nodeIterable = exportHierarchy ? graph.getNodesTree() : graph.getNodes();
    for (Node node : nodeIterable) {
      xmlWriter.writeStartElement(NODE);

      String id = node.getNodeData().getId();
      xmlWriter.writeAttribute(NODE_ID, id);
      if (node.getNodeData().getLabel() != null && !node.getNodeData().getLabel().isEmpty()) {
        xmlWriter.writeAttribute(NODE_LABEL, node.getNodeData().getLabel());
      }

      if (exportHierarchy) {
        Node parent = graph.getParent(node);
        if (parent != null) {
          xmlWriter.writeAttribute(NODE_PID, parent.getNodeData().getId());
        }
      }

      if (exportDynamic && dynamicCol != null && visibleInterval != null) {
        TimeInterval timeInterval =
            (TimeInterval) node.getNodeData().getAttributes().getValue(dynamicCol.getIndex());
        if (timeInterval != null) {
          writeTimeInterval(xmlWriter, timeInterval);
        }
      }

      if (exportAttributes && node.getNodeData().getAttributes() != null) {
        AttributeRow attributeRow = (AttributeRow) node.getNodeData().getAttributes();
        writeAttValues(xmlWriter, attributeRow, visibleInterval);
      }

      if (exportSize) {
        writeNodeSize(xmlWriter, node);
      }

      if (exportPosition) {
        writeNodePosition(xmlWriter, node);
      }

      if (exportColors) {
        writeNodeColor(xmlWriter, node);
      }

      xmlWriter.writeEndElement();
      Progress.progress(progress);
      if (cancel) {
        break;
      }
    }

    xmlWriter.writeEndElement();
  }
 public void ungroupCluster(Cluster cluster) {
   GraphModel gm = Lookup.getDefault().lookup(GraphController.class).getModel();
   if (gm != null) {
     HierarchicalGraph graph = gm.getHierarchicalGraphVisible();
     graph.ungroupNodes(cluster.getMetaNode());
     cluster.setMetaNode(null);
   }
 }
  private void updateNodes(HierarchicalGraph graph) {
    Modeler nodeInit = engine.getModelClasses()[AbstractEngine.CLASS_NODE].getCurrentModeler();

    NodeIterable nodeIterable;
    nodeIterable = graph.getNodes();

    for (Node node : nodeIterable) {

      Model obj = node.getNodeData().getModel();
      if (obj == null) {
        // Model is null, ADD
        obj = nodeInit.initModel(node.getNodeData());
        engine.addObject(AbstractEngine.CLASS_NODE, (ModelImpl) obj);
      } else if (!obj.isValid()) {
        engine.addObject(AbstractEngine.CLASS_NODE, (ModelImpl) obj);
      }
      obj.setCacheMarker(cacheMarker);

      // Modeaction
      if (modeManager.getMode().equals(ModeManager.AVAILABLE_MODES.HIGHLIGHT)) {
        ModelImpl impl = (ModelImpl) obj;
        //                if (!node.isVisible()) {
        //                    ColorLayer.layerColor(impl, 0.8f, 0.8f, 0.8f);
        //                }
      }
    }
  }
  private void updateEdges(HierarchicalGraph graph) {
    Modeler edgeInit = engine.getModelClasses()[AbstractEngine.CLASS_EDGE].getCurrentModeler();
    Modeler arrowInit = engine.getModelClasses()[AbstractEngine.CLASS_ARROW].getCurrentModeler();

    EdgeIterable edgeIterable;
    edgeIterable = graph.getEdges();

    float minWeight = Float.POSITIVE_INFINITY;
    float maxWeight = Float.NEGATIVE_INFINITY;

    TimeInterval timeInterval = DynamicUtilities.getVisibleInterval(dynamicModel);

    for (Edge edge : edgeIterable) {
      if (edge.getSource().getNodeData().getModel() == null
          || edge.getTarget().getNodeData().getModel() == null) {
        continue;
      }
      float weight = 1f;
      if (timeInterval == null) {
        weight = edge.getWeight();
      } else {
        weight = edge.getWeight(timeInterval.getLow(), timeInterval.getHigh());
      }
      minWeight = Math.min(minWeight, weight);
      maxWeight = Math.max(maxWeight, weight);
      Edge2dModel obj = (Edge2dModel) edge.getEdgeData().getModel();
      if (obj == null) {
        // Model is null, ADD
        obj = (Edge2dModel) edgeInit.initModel(edge.getEdgeData());
        engine.addObject(AbstractEngine.CLASS_EDGE, obj);
        if (!undirected && vizConfig.isShowArrows() && !edge.isSelfLoop()) {
          Arrow2dModel arrowObj = (Arrow2dModel) arrowInit.initModel(edge.getEdgeData());
          engine.addObject(AbstractEngine.CLASS_ARROW, arrowObj);
          arrowObj.setCacheMarker(cacheMarker);
          arrowObj.setWeight(weight);
          obj.setArrow(arrowObj);
        }
      } else if (!obj.isValid()) {
        engine.addObject(AbstractEngine.CLASS_EDGE, obj);
        if (!undirected && vizConfig.isShowArrows() && !edge.isSelfLoop()) {
          Arrow2dModel arrowObj = obj.getArrow();
          engine.addObject(AbstractEngine.CLASS_ARROW, arrowObj);
          arrowObj.setCacheMarker(cacheMarker);
          arrowObj.setWeight(weight);
        }
      } else {
        if (!undirected && vizConfig.isShowArrows() && !edge.isSelfLoop() && edge.isDirected()) {
          Arrow2dModel arrowObj = obj.getArrow();
          arrowObj.setCacheMarker(cacheMarker);
          arrowObj.setWeight(weight);
        }
      }
      obj.setWeight(weight);
      obj.setCacheMarker(cacheMarker);
    }

    limits.setMinWeight(minWeight);
    limits.setMaxWeight(maxWeight);
  }
  public void updatePotatoes(HierarchicalGraph graph) {

    ModelClass potatoClass = engine.getModelClasses()[AbstractEngine.CLASS_POTATO];
    if (potatoClass.isEnabled()) {
      Modeler potInit = engine.getModelClasses()[AbstractEngine.CLASS_POTATO].getCurrentModeler();

      List<ModelImpl> hulls = new ArrayList<ModelImpl>();
      Node[] nodes = graph.getNodes().toArray();
      for (Node n : nodes) {
        Node parent = graph.getParent(n);
        if (parent != null) {
          Group group = (Group) parent;
          Model hullModel = group.getGroupData().getHullModel();
          if (hullModel != null && hullModel.isCacheMatching(cacheMarker)) {
            ConvexHull hull = (ConvexHull) hullModel.getObj();
            hull.addNode(n);
            hull.setModel(hullModel);
          } else if (hullModel != null) {
            // Its not the first time the hull exist
            ConvexHullModel model = (ConvexHullModel) hullModel;
            model.setScale(1f);
            hullModel.setCacheMarker(cacheMarker);
            hulls.add((ModelImpl) hullModel);
          } else {
            ConvexHull ch = new ConvexHull();
            ch.setMetaNode(parent);
            ch.addNode(n);
            ModelImpl obj = potInit.initModel(ch);
            group.getGroupData().setHullModel(obj);
            obj.setCacheMarker(cacheMarker);
            hulls.add(obj);
          }
        }
      }
      for (ModelImpl im : hulls) {
        ConvexHull hull = (ConvexHull) im.getObj();
        hull.recompute();
        engine.addObject(AbstractEngine.CLASS_POTATO, im);
      }
    }
  }
  public void refine(HierarchicalGraph graph) {
    double r = 10;
    int count = 0;
    int refined = 0;
    for (Node node : graph.getTopNodes().toArray()) {
      count++;
      if (graph.getChildrenCount(node) == 2) {
        refined++;
        float x = node.getNodeData().x();
        float y = node.getNodeData().y();

        for (Node child : graph.getChildren(node)) {
          double t = Math.random();
          child.getNodeData().setX((float) (x + r * Math.cos(t)));
          child.getNodeData().setY((float) (y + r * Math.sin(t)));
        }
        graph.ungroupNodes(node);
      }
    }
    // System.out.println("COUNT = " + count);
    // System.out.println("REFINED = " + refined);
  }
  public void initAlgo() {
    graph = graphModel.getHierarchicalGraphVisible();
    initedView = graph.getView().getViewId();
    setConverged(false);
    level = 0;

    while (true) {
      int graphSize = graph.getTopNodes().toArray().length;
      coarseningStrategy.coarsen(graph);
      level++;
      int newGraphSize = graph.getTopNodes().toArray().length;
      if (newGraphSize < getMinSize() || newGraphSize > graphSize * getMinCoarseningRate()) {
        break;
      }
    }

    Layout random = new RandomLayout(1000);
    random.setGraphModel(graphModel);
    random.initAlgo();
    random.goAlgo();

    initYifanHu();
  }
  public void groupCluster(Cluster cluster) {
    GraphModel gm = Lookup.getDefault().lookup(GraphController.class).getModel();
    if (gm != null) {
      HierarchicalGraph graph = gm.getHierarchicalGraphVisible();
      Node[] newGroup = cluster.getNodes();
      float centroidX = 0;
      float centroidY = 0;
      int len = 0;
      Node group = graph.groupNodes(newGroup);
      cluster.setMetaNode(group);

      group.getNodeData().setLabel("Group");
      group.getNodeData().setSize(10f);
      for (Node child : newGroup) {
        centroidX += child.getNodeData().x();
        centroidY += child.getNodeData().y();
        len++;
      }
      centroidX /= len;
      centroidY /= len;
      group.getNodeData().setX(centroidX);
      group.getNodeData().setY(centroidY);
    }
  }
  public void goAlgo() {
    HierarchicalGraph newGraph = graphModel.getHierarchicalGraphVisible();
    if (newGraph.getView().getViewId() != initedView) {
      setConverged(true);
      layout.endAlgo();
      endAlgo();
      return;
    }
    this.graph = newGraph;
    if (layout.canAlgo()) {
      layout.goAlgo();
    } else {
      layout.endAlgo();
      if (level > 0) {
        coarseningStrategy.refine(graph);
        level--;

        initYifanHu();
      } else {
        setConverged(true);
        layout = null;
      }
    }
  }
  public void coarsen(HierarchicalGraph g) {
    HierarchicalGraph graph = g;
    int retract = 0;
    int count = 0;
    for (Edge e : graph.getEdgesAndMetaEdges().toArray()) {
      Node a = e.getSource();
      Node b = e.getTarget();
      count++;
      if (graph.getParent(a) == graph.getParent(b) && graph.getLevel(a) == 0) {
        float x = (a.getNodeData().x() + b.getNodeData().x()) / 2;
        float y = (a.getNodeData().y() + b.getNodeData().y()) / 2;

        Node parent = graph.groupNodes(new Node[] {a, b});
        parent.getNodeData().setX(x);
        parent.getNodeData().setY(y);
        graph.retract(parent);
        retract++;
      }
    }
  }
  @Override
  public void execute(GraphModel graphModel, AttributeModel attributeModel) {
    // Graph graph = graphModel.getGraphVisible();
    HierarchicalGraph graph = null;
    // get visible graph
    if (directed) {
      graph = graphModel.getHierarchicalDirectedGraphVisible();
    } else {
      graph = graphModel.getHierarchicalUndirectedGraphVisible();
    }

    // lock graph
    graph.readLock();
    try {
      Progress.start(progressTicket, graph.getNodeCount());
      // all coefficients
      nodeCoefficients = new double[graph.getNodeCount()];

      // attribute column
      AttributeTable nodeTable = attributeModel.getNodeTable();
      AttributeColumn clusteringColumn = nodeTable.getColumn("newClusteringCoefficient");
      if (clusteringColumn == null) {
        clusteringColumn =
            nodeTable.addColumn(
                "newClusteringCoefficient",
                "Local Clustering Coefficient",
                AttributeType.DOUBLE,
                AttributeOrigin.COMPUTED,
                0.0);
      }

      int i = 0;
      // for each node
      for (Node e : graph.getNodes()) {
        // compute coefficient
        double coeficient = 0.0;
        double denominator = (graph.getDegree(e) * (graph.getDegree(e) - 1));
        if (!directed) {
          denominator /= 2;
        }
        double numerator = 0.0;
        // get neighbors as list
        List<Node> n2 = Arrays.asList(graph.getNeighbors(e).toArray());
        List<Node> neighbors2 = new ArrayList<Node>(n2);
        for (Node neighbor1 : graph.getNeighbors(e)) {
          neighbors2.remove(neighbor1);
          // count edges betwwen neighbors
          for (Node neighbor2 : neighbors2) {
            if (graph.getEdge(neighbor1, neighbor2) != null
                || graph.getEdge(neighbor2, neighbor1) != null) {
              numerator++;
            }
          }
        }
        // compute coefficient
        if (denominator > 0) {
          coeficient = numerator / denominator;
        } else {
          coeficient = 0.0;
        }
        averageCoefficient += coeficient;
        nodeCoefficients[i] = coeficient;
        i++;
        // set attribute
        AttributeRow row = (AttributeRow) e.getNodeData().getAttributes();
        row.setValue(clusteringColumn, coeficient);
        Progress.progress(progressTicket);
        if (cancel) {
          break;
        }
      }
      if (graph.getNodeCount() > 0) {
        averageCoefficient = averageCoefficient / graph.getNodeCount();
      }

      graph.readUnlockAll();
    } catch (Exception e) {
      e.printStackTrace();
      // Unlock graph
      graph.readUnlockAll();
    }
  }
  public void process() {
    // Workspace
    ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
    if (workspace == null) {
      workspace = pc.newWorkspace(pc.getCurrentProject());
      pc.openWorkspace(workspace);
    }
    if (container.getSource() != null) {
      pc.setSource(workspace, container.getSource());
    }

    // Architecture
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(workspace);

    HierarchicalGraph graph = null;
    switch (container.getEdgeDefault()) {
      case DIRECTED:
        graph = graphModel.getHierarchicalDirectedGraph();
        break;
      case UNDIRECTED:
        graph = graphModel.getHierarchicalUndirectedGraph();
        break;
      case MIXED:
        graph = graphModel.getHierarchicalMixedGraph();
        break;
      default:
        graph = graphModel.getHierarchicalMixedGraph();
        break;
    }
    GraphFactory factory = graphModel.factory();

    // Attributes - Creates columns for properties
    attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel(workspace);
    attributeModel.mergeModel(container.getAttributeModel());

    // Dynamic
    if (container.getTimeFormat() != null) {
      DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
      if (dynamicController != null) {
        dynamicController.setTimeFormat(container.getTimeFormat());
      }
    }

    int nodeCount = 0;
    // Create all nodes
    for (NodeDraftGetter draftNode : container.getNodes()) {
      Node n = factory.newNode(draftNode.isAutoId() ? null : draftNode.getId());
      flushToNode(draftNode, n);
      draftNode.setNode(n);
      nodeCount++;
    }

    // Push nodes in data structure
    for (NodeDraftGetter draftNode : container.getNodes()) {
      Node n = draftNode.getNode();
      NodeDraftGetter[] parents = draftNode.getParents();
      if (parents != null) {
        for (int i = 0; i < parents.length; i++) {
          Node parent = parents[i].getNode();
          graph.addNode(n, parent);
        }
      } else {
        graph.addNode(n);
      }
    }

    // Create all edges and push to data structure
    int edgeCount = 0;
    for (EdgeDraftGetter edge : container.getEdges()) {
      Node source = edge.getSource().getNode();
      Node target = edge.getTarget().getNode();
      Edge e = null;
      switch (container.getEdgeDefault()) {
        case DIRECTED:
          e =
              factory.newEdge(
                  edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), true);
          break;
        case UNDIRECTED:
          e =
              factory.newEdge(
                  edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), false);
          break;
        case MIXED:
          e =
              factory.newEdge(
                  edge.isAutoId() ? null : edge.getId(),
                  source,
                  target,
                  edge.getWeight(),
                  edge.getType().equals(EdgeType.UNDIRECTED) ? false : true);
          break;
      }

      flushToEdge(edge, e);
      edgeCount++;
      graph.addEdge(e);
    }
    workspace = null;
  }
  public void updateWorld() {
    // System.out.println("update world");
    cacheMarker++;

    GraphModel graphModel = controller.getModel();
    if (graphModel == null) {
      engine.worldUpdated(cacheMarker);
      return;
    }
    if (gm != null && gm != graphModel) {
      reset();
    }
    gm = graphModel;
    HierarchicalGraph graph;
    if (graphModel.isDirected()) {
      undirected = false;
      graph = graphModel.getHierarchicalDirectedGraphVisible();
    } else if (graphModel.isUndirected()) {
      undirected = true;
      graph = graphModel.getHierarchicalUndirectedGraphVisible();
    } else if (graphModel.isMixed()) {
      undirected = false;
      graph = graphModel.getHierarchicalMixedGraphVisible();
    } else {
      undirected = false;
      graph = graphModel.getHierarchicalDirectedGraphVisible();
    }

    if (dynamicModel == null) {
      DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
      dynamicModel = dynamicController.getModel();
    }

    graphView = graph.getView().getViewId();

    ModelClass[] object3dClasses = engine.getModelClasses();

    graph.readLock();

    ModelClass nodeClass = object3dClasses[AbstractEngine.CLASS_NODE];
    if (nodeClass.isEnabled()
        && (graph.getNodeVersion() > nodeVersion || modeManager.requireModeChange())) {
      updateNodes(graph);
      nodeClass.setCacheMarker(cacheMarker);
    }

    ModelClass edgeClass = object3dClasses[AbstractEngine.CLASS_EDGE];
    if (edgeClass.isEnabled()
        && (graph.getEdgeVersion() > edgeVersion || modeManager.requireModeChange())) {
      updateEdges(graph);
      updateMetaEdges(graph);
      edgeClass.setCacheMarker(cacheMarker);
      if (!undirected && vizConfig.isShowArrows()) {
        object3dClasses[AbstractEngine.CLASS_ARROW].setCacheMarker(cacheMarker);
      }
    }

    ModelClass potatoClass = object3dClasses[AbstractEngine.CLASS_POTATO];
    if (potatoClass.isEnabled()
        && (graph.getNodeVersion() > nodeVersion || modeManager.requireModeChange())) {
      updatePotatoes(graph);
      potatoClass.setCacheMarker(cacheMarker);
    }

    nodeVersion = graph.getNodeVersion();
    edgeVersion = graph.getEdgeVersion();

    graph.readUnlock();

    engine.worldUpdated(cacheMarker);
  }
    public Graph filter(Graph graph, Filter[] filters) {
      HierarchicalGraph hgraph = (HierarchicalGraph) graph;
      List<NodeFilter> nodeFilters = new ArrayList<NodeFilter>();
      List<EdgeFilter> edgeFilters = new ArrayList<EdgeFilter>();
      for (Filter f : filters) {
        if (f instanceof NodeFilter) {
          nodeFilters.add((NodeFilter) f);
        } else if (f instanceof EdgeFilter) {
          edgeFilters.add((EdgeFilter) f);
        }
      }
      if (nodeFilters.size() > 0) {
        for (Iterator<NodeFilter> itr = nodeFilters.iterator(); itr.hasNext(); ) {
          NodeFilter nf = itr.next();
          if (!nf.init(hgraph)) {
            itr.remove();
          }
        }
        List<Node> nodesToRemove = new ArrayList<Node>();
        for (Node n : hgraph.getNodes()) {
          for (NodeFilter nf : nodeFilters) {
            if (!nf.evaluate(hgraph, n)) {
              nodesToRemove.add(n);
              break;
            }
          }
        }

        for (Node n : nodesToRemove) {
          hgraph.removeNode(n);
        }

        for (NodeFilter nf : nodeFilters) {
          nf.finish();
        }
      }
      if (edgeFilters.size() > 0) {
        for (Iterator<EdgeFilter> itr = edgeFilters.iterator(); itr.hasNext(); ) {
          EdgeFilter ef = itr.next();
          if (!ef.init(hgraph)) {
            itr.remove();
          }
        }
        List<Edge> edgesToRemove = new ArrayList<Edge>();
        for (Edge e : hgraph.getEdges()) {
          for (EdgeFilter ef : edgeFilters) {
            if (!ef.evaluate(hgraph, e)) {
              edgesToRemove.add(e);
              break;
            }
          }
        }

        for (Edge e : edgesToRemove) {
          hgraph.removeEdge(e);
        }

        for (EdgeFilter ef : edgeFilters) {
          ef.finish();
        }
      }
      return hgraph;
    }
  public boolean execute() {
    attributeModel = workspace.getLookup().lookup(AttributeModel.class);
    graphModel = workspace.getLookup().lookup(GraphModel.class);
    HierarchicalGraph graph = null;
    if (exportVisible) {
      graph = graphModel.getHierarchicalGraphVisible();
    } else {
      graph = graphModel.getHierarchicalGraph();
    }
    Progress.start(progress);
    graph.readLock();

    // Options
    if (normalize) {
      calculateMinMax(graph);
    }

    // Calculate progress units count
    int max = 0;
    if (exportHierarchy) {
      for (Node n : graph.getNodesTree()) {
        max++;
      }
      for (Edge e : graph.getEdgesTree()) {
        max++;
      }
    } else {
      max = graph.getNodeCount();
      for (Edge e : graph.getEdgesAndMetaEdges()) {
        max++;
      }
    }
    Progress.switchToDeterminate(progress, max);

    try {
      XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
      outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.FALSE);

      XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(writer);
      xmlWriter = new IndentingXMLStreamWriter(xmlWriter);

      xmlWriter.writeStartDocument("UTF-8", "1.0");
      xmlWriter.setPrefix("", GEXF_NAMESPACE);
      xmlWriter.writeStartElement(GEXF_NAMESPACE, GEXF);
      xmlWriter.writeNamespace("", GEXF_NAMESPACE);
      xmlWriter.writeAttribute(GEXF_VERSION, "1.1");

      if (exportColors || exportPosition || exportSize) {
        xmlWriter.writeNamespace(VIZ, VIZ_NAMESPACE);
      }
      xmlWriter.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      xmlWriter.writeAttribute("xsi:schemaLocation", GEXF_NAMESPACE_LOCATION);

      if (exportDynamic) {
        DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
        dynamicModel = dynamicController != null ? dynamicController.getModel(workspace) : null;
        visibleInterval =
            dynamicModel == null
                ? null
                : exportVisible
                    ? dynamicModel.getVisibleInterval()
                    : new TimeInterval(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
      }

      writeMeta(xmlWriter);
      writeGraph(xmlWriter, graph);

      xmlWriter.writeEndElement();
      xmlWriter.writeEndDocument();
      xmlWriter.close();

    } catch (Exception e) {
      graph.readUnlockAll();
      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      }
      throw new RuntimeException(e);
    }

    graph.readUnlock();

    Progress.finish(progress);
    return !cancel;
  }
  private void writeEdges(XMLStreamWriter xmlWriter, HierarchicalGraph graph) throws Exception {
    if (cancel) {
      return;
    }
    xmlWriter.writeStartElement(EDGES);

    AttributeColumn dynamicCol =
        dynamicCol = attributeModel.getEdgeTable().getColumn(DynamicModel.TIMEINTERVAL_COLUMN);

    EdgeIterable edgeIterable =
        exportHierarchy ? graph.getEdgesTree() : graph.getEdgesAndMetaEdges();
    for (Edge edge : edgeIterable) {
      xmlWriter.writeStartElement(EDGE);

      if (edge.getEdgeData().getId() != null
          && !edge.getEdgeData().getId().equals(Integer.toString(edge.getId()))) {
        xmlWriter.writeAttribute(EDGE_ID, edge.getEdgeData().getId());
      }
      xmlWriter.writeAttribute(EDGE_SOURCE, edge.getSource().getNodeData().getId());
      xmlWriter.writeAttribute(EDGE_TARGET, edge.getTarget().getNodeData().getId());

      if (edge.isDirected() && graphModel.isMixed()) {
        xmlWriter.writeAttribute(EDGE_TYPE, "directed");
      } else if (!edge.isDirected() && graphModel.isMixed()) {
        xmlWriter.writeAttribute(EDGE_TYPE, "undirected");
      }

      String label = edge.getEdgeData().getLabel();
      if (label != null && !label.isEmpty()) {
        xmlWriter.writeAttribute(EDGE_LABEL, label);
      }

      float weight = edge.getWeight();
      if (weight != 1f) {
        xmlWriter.writeAttribute(EDGE_WEIGHT, "" + weight);
      }

      if (exportDynamic && dynamicCol != null && visibleInterval != null) {
        TimeInterval timeInterval =
            (TimeInterval) edge.getEdgeData().getAttributes().getValue(dynamicCol.getIndex());
        if (timeInterval != null) {
          writeTimeInterval(xmlWriter, timeInterval);
        }
      }

      if (exportColors) {
        writeEdgeColor(xmlWriter, edge);
      }

      if (exportAttributes && edge.getEdgeData().getAttributes() != null) {
        AttributeRow attributeRow = (AttributeRow) edge.getEdgeData().getAttributes();
        writeAttValues(xmlWriter, attributeRow, visibleInterval);
      }

      xmlWriter.writeEndElement();
      Progress.progress(progress);
      if (cancel) {
        break;
      }
    }

    xmlWriter.writeEndElement();
  }