Search in sources :

Example 1 with EdgeMergeStrategy

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

the class AbstractProcessor method flushToEdge.

protected void flushToEdge(ContainerUnloader container, EdgeDraft edgeDraft, Edge edge, boolean newEdge) {
    // Edge weight
    flushEdgeWeight(container, edgeDraft, edge, newEdge);
    // Replace data when a new edge is created or the merge strategy is not to keep the first edge data:
    EdgeMergeStrategy edgesMergeStrategy = containers[0].getEdgesMergeStrategy();
    if (newEdge || edgesMergeStrategy != EdgeMergeStrategy.FIRST) {
        if (edgeDraft.getColor() != null) {
            edge.setColor(edgeDraft.getColor());
        } else {
            edge.setR(0f);
            edge.setG(0f);
            edge.setB(0f);
            edge.setAlpha(0f);
        }
        if (edgeDraft.getLabel() != null) {
            edge.setLabel(edgeDraft.getLabel());
        }
        if (edge.getTextProperties() != null) {
            edge.getTextProperties().setVisible(edgeDraft.isLabelVisible());
        }
        if (edgeDraft.getLabelSize() != -1f && edge.getTextProperties() != null) {
            edge.getTextProperties().setSize(edgeDraft.getLabelSize());
        }
        if (edgeDraft.getLabelColor() != null && edge.getTextProperties() != null) {
            Color labelColor = edgeDraft.getLabelColor();
            edge.getTextProperties().setColor(labelColor);
        } else {
            edge.getTextProperties().setColor(new Color(0, 0, 0, 0));
        }
        // Attributes
        flushToElementAttributes(container, edgeDraft, edge);
    }
    // Timeset
    if (edgeDraft.getTimeSet() != null) {
        flushTimeSet(edgeDraft.getTimeSet(), edge);
    }
    // Graph timeset
    if (edgeDraft.getGraphTimestamp() != null) {
        edge.addTimestamp(edgeDraft.getGraphTimestamp());
    } else if (edgeDraft.getGraphInterval() != null) {
        edge.addInterval(edgeDraft.getGraphInterval());
    }
}
Also used : EdgeMergeStrategy(org.gephi.io.importer.api.EdgeMergeStrategy) Color(java.awt.Color)

Example 2 with EdgeMergeStrategy

use of org.gephi.io.importer.api.EdgeMergeStrategy 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)

Example 3 with EdgeMergeStrategy

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

the class ImportContainerImpl method mergeDirectedEdges.

protected void mergeDirectedEdges(EdgeDraftImpl source, EdgeDraftImpl dest) {
    EdgeMergeStrategy mergeStrategy = parameters.getEdgesMergeStrategy();
    double result = dest.getWeight();
    switch(mergeStrategy) {
        case AVG:
            result = (source.getWeight() + dest.getWeight()) / 2.0;
            break;
        case MAX:
            result = Math.max(source.getWeight(), dest.getWeight());
            break;
        case MIN:
            result = Math.min(source.getWeight(), dest.getWeight());
            break;
        case SUM:
            result = source.getWeight() + dest.getWeight();
            break;
        case FIRST:
            result = dest.getWeight();
            break;
        case LAST:
            result = source.getWeight();
            break;
        default:
            break;
    }
    dest.setWeight(result);
}
Also used : EdgeMergeStrategy(org.gephi.io.importer.api.EdgeMergeStrategy)

Aggregations

EdgeMergeStrategy (org.gephi.io.importer.api.EdgeMergeStrategy)3 Color (java.awt.Color)1 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 ElementIdType (org.gephi.io.importer.api.ElementIdType)1 Issue (org.gephi.io.importer.api.Issue)1 NodeDraft (org.gephi.io.importer.api.NodeDraft)1