Search in sources :

Example 1 with ElementIdType

use of org.gephi.io.importer.api.ElementIdType in project gephi by gephi.

the class DefaultProcessor method process.

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);
        boolean newNode = false;
        if (node == null) {
            node = factory.newNode(id);
            addedNodes++;
            newNode = true;
        }
        flushToNode(container, draftNode, node);
        if (newNode) {
            graph.addNode(node);
        }
        Progress.progress(progressTicket);
    }
    final EdgeMergeStrategy edgesMergeStrategy = containers[0].getEdgesMergeStrategy();
    // 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);
        boolean createDirected = true;
        switch(container.getEdgeDefault()) {
            case DIRECTED:
                createDirected = true;
                break;
            case UNDIRECTED:
                createDirected = false;
                break;
            case MIXED:
                createDirected = draftEdge.getDirection() != EdgeDirection.UNDIRECTED;
                draftEdge.setDirection(createDirected ? EdgeDirection.DIRECTED : EdgeDirection.UNDIRECTED);
                break;
        }
        Edge edge = graph.getEdge(source, target, edgeType);
        if (edge != null && edgesMergeStrategy == EdgeMergeStrategy.NO_MERGE) {
            // Undirected and directed edges are incompatible, check for them or we could get an exception:
            final Edge incompatibleEdge = findIncompatibleEdge(graph, source, target, createDirected, edgeType);
            if (incompatibleEdge == null) {
                // Force create, no merge
                edge = null;
            } else {
                String message = NbBundle.getMessage(DefaultProcessor.class, "DefaultProcessor.error.incompatibleEdges", String.format("[%s -> %s; %s, type %s]", sourceId, targetId, createDirected ? "Directed" : "Undirected", type), String.format("[%s -> %s; %s; type: %s; id: %s]", incompatibleEdge.getSource().getId(), incompatibleEdge.getTarget().getId(), incompatibleEdge.isDirected() ? "Directed" : "Undirected", incompatibleEdge.getTypeLabel(), incompatibleEdge.getId()));
                report.logIssue(new Issue(message, Issue.Level.WARNING));
                Progress.progress(progressTicket);
                continue;
            }
        }
        boolean newEdge = edge == null;
        if (newEdge) {
            if (!graph.hasEdge(id)) {
                edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), createDirected);
            } else {
                // The id is already in use by a different edge, generate a new id:
                edge = factory.newEdge(source, target, edgeType, draftEdge.getWeight(), createDirected);
            }
            addedEdges++;
        }
        flushToEdge(container, draftEdge, edge, newEdge);
        if (newEdge) {
            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);
}
Also used : ElementIdType(org.gephi.io.importer.api.ElementIdType) GraphFactory(org.gephi.graph.api.GraphFactory) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft) Node(org.gephi.graph.api.Node) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Graph(org.gephi.graph.api.Graph) EdgeMergeStrategy(org.gephi.io.importer.api.EdgeMergeStrategy) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Aggregations

Edge (org.gephi.graph.api.Edge)1 Graph (org.gephi.graph.api.Graph)1 GraphController (org.gephi.graph.api.GraphController)1 GraphFactory (org.gephi.graph.api.GraphFactory)1 Node (org.gephi.graph.api.Node)1 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)1 EdgeMergeStrategy (org.gephi.io.importer.api.EdgeMergeStrategy)1 ElementIdType (org.gephi.io.importer.api.ElementIdType)1 Issue (org.gephi.io.importer.api.Issue)1 NodeDraft (org.gephi.io.importer.api.NodeDraft)1