private void exportData(Graph graph) throws Exception {
    int max = graph.getNodeCount();

    Progress.start(progressTicket, max);

    if (!list) {
      if (header) {
        writer.append(SEPARATOR);
        Node[] nodes = graph.getNodes().toArray();
        for (int i = 0; i < nodes.length; i++) {
          writeMatrixNode(nodes[i], i < nodes.length - 1);
        }
        writer.append(EOL);
      }
    }

    if (list) {
      Node[] nodes = graph.getNodes().toArray();
      for (int i = 0; i < nodes.length; i++) {
        Node n = nodes[i];
        List<Node> neighbours = new ArrayList<Node>();
        for (Edge e : graph.getEdges(n)) {
          if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
            Node m = graph.getOpposite(n, e);
            neighbours.add(m);
          }
        }

        for (Edge e : ((HierarchicalGraph) graph).getMetaEdges(n)) {
          if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
            Node m = graph.getOpposite(n, e);
            neighbours.add(m);
          }
        }

        writeListNode(n, !neighbours.isEmpty());
        for (int j = 0; j < neighbours.size(); j++) {
          writeListNode(neighbours.get(j), j < neighbours.size() - 1);
        }
        writer.append(EOL);
      }
    } else {
      if (graph instanceof DirectedGraph) {
        DirectedGraph directedGraph = (DirectedGraph) graph;
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : nodes) {
          if (cancel) {
            break;
          }
          writeMatrixNode(n, true);
          for (int j = 0; j < nodes.length; j++) {
            Node m = nodes[j];
            Edge e = directedGraph.getEdge(n, m);
            e = e == null ? ((HierarchicalDirectedGraph) directedGraph).getMetaEdge(n, m) : e;
            writeEdge(e, j < nodes.length - 1);
          }
          Progress.progress(progressTicket);
          writer.append(EOL);
        }
      } else if (graph instanceof UndirectedGraph) {
        UndirectedGraph undirectedGraph = (UndirectedGraph) graph;
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : nodes) {
          if (cancel) {
            break;
          }
          writeMatrixNode(n, true);
          for (int j = 0; j < nodes.length; j++) {
            Node m = nodes[j];
            Edge e = undirectedGraph.getEdge(n, m);
            e = e == null ? ((HierarchicalUndirectedGraph) undirectedGraph).getMetaEdge(n, m) : e;
            writeEdge(e, j < nodes.length - 1);
          }
          Progress.progress(progressTicket);
          writer.append(EOL);
        }
      } else {
        MixedGraph mixedGraph = (MixedGraph) graph;
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : graph.getNodes()) {
          if (cancel) {
            break;
          }
          writeMatrixNode(n, true);
          for (int j = 0; j < nodes.length; j++) {
            Node m = nodes[j];
            Edge e = mixedGraph.getEdge(n, m);
            e = e == null ? ((HierarchicalMixedGraph) mixedGraph).getMetaEdge(n, m) : e;
            writeEdge(e, j < nodes.length - 1);
          }
          Progress.progress(progressTicket);
          writer.append(EOL);
        }
      }
    }

    graph.readUnlockAll();

    Progress.finish(progressTicket);
  }
  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;
  }
Esempio n. 3
0
  protected void process(ContainerUnloader container, Workspace workspace) {
    // Architecture
    GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
    graphModel = graphController.getGraphModel(workspace);

    // Get graph
    Graph graph = graphModel.getGraph();
    GraphFactory factory = graphModel.factory();

    // Time Format & Time zone
    graphModel.setTimeFormat(container.getTimeFormat());
    graphModel.setTimeZone(container.getTimeZone());

    // Progress
    Progress.start(progressTicket, container.getNodeCount() + container.getEdgeCount());

    // Attributes - Creates columns for properties
    flushColumns(container);

    // Counters
    int addedNodes = 0, addedEdges = 0;

    // Create all nodes
    ElementIdType elementIdType = container.getElementIdType();
    for (NodeDraft draftNode : container.getNodes()) {
      String idString = draftNode.getId();
      Object id = toElementId(elementIdType, idString);
      Node node = graph.getNode(id);
      if (node == null) {
        node = factory.newNode(id);
        addedNodes++;
      }
      flushToNode(draftNode, node);

      graph.addNode(node);

      Progress.progress(progressTicket);
    }

    // Create all edges and push to data structure
    for (EdgeDraft draftEdge : container.getEdges()) {
      String idString = draftEdge.getId();
      Object id = toElementId(elementIdType, idString);
      String sourceId = draftEdge.getSource().getId();
      String targetId = draftEdge.getTarget().getId();
      Node source = graph.getNode(toElementId(elementIdType, sourceId));
      Node target = graph.getNode(toElementId(elementIdType, targetId));
      Object type = draftEdge.getType();
      int edgeType = graphModel.addEdgeType(type);

      Edge edge = graph.getEdge(source, target, edgeType);
      if (edge == null) {
        switch (container.getEdgeDefault()) {
          case DIRECTED:
            edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), true);
            break;
          case UNDIRECTED:
            edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), false);
            break;
          case MIXED:
            boolean directed =
                draftEdge.getDirection() == null
                    || !draftEdge.getDirection().equals(EdgeDirection.UNDIRECTED);
            edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), directed);
            break;
        }
        addedEdges++;
      }
      flushToEdge(draftEdge, edge);

      graph.addEdge(edge);

      Progress.progress(progressTicket);
    }

    // Report
    int touchedNodes = container.getNodeCount();
    int touchedEdges = container.getEdgeCount();
    if (touchedNodes != addedNodes || touchedEdges != addedEdges) {
      Logger.getLogger(getClass().getSimpleName())
          .log(
              Level.INFO,
              "# Nodes loaded: {0} ({1} added)",
              new Object[] {touchedNodes, addedNodes});
      Logger.getLogger(getClass().getSimpleName())
          .log(
              Level.INFO,
              "# Edges loaded: {0} ({1} added)",
              new Object[] {touchedEdges, addedEdges});
    } else {
      Logger.getLogger(getClass().getSimpleName())
          .log(Level.INFO, "# Nodes loaded: {0}", new Object[] {touchedNodes});
      Logger.getLogger(getClass().getSimpleName())
          .log(Level.INFO, "# Edges loaded: {0}", new Object[] {touchedEdges});
    }

    Progress.finish(progressTicket);
  }