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;
  }
Esempio n. 2
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);
  }