Search in sources :

Example 21 with NodeIterable

use of org.gephi.graph.api.NodeIterable in project gephi by gephi.

the class ExporterGDF method exportData.

private void exportData(Graph graph, GraphModel graphModel) throws Exception {
    Progress.start(progressTicket);
    defaultNodeColumns(graph);
    defaultEdgeColumns(graph);
    Column[] nodeColumns = attributesNodeColumns(graphModel);
    Column[] edgeColumns = attributesEdgeColumns(graphModel);
    StringBuilder stringBuilder = new StringBuilder();
    // Node intro
    stringBuilder.append("nodedef> name VARCHAR,");
    // Default Node columns title
    for (NodeColumnsGDF c : defaultNodeColumnsGDFs) {
        if (c.isEnable()) {
            stringBuilder.append(c.getTitle());
            stringBuilder.append(" ");
            stringBuilder.append(c.getType().toString().toUpperCase());
            if (c.getDefaultValue() != null) {
                stringBuilder.append(" default ");
                stringBuilder.append(c.getDefaultValue().toString());
            }
            stringBuilder.append(",");
        }
    }
    // Attributes Node columns
    for (Column c : nodeColumns) {
        if (!c.isProperty()) {
            stringBuilder.append(c.getTitle());
            stringBuilder.append(" ");
            DataTypeGDF dataTypeGDF = getDataTypeGDF(c.getTypeClass());
            stringBuilder.append(dataTypeGDF.toString().toUpperCase());
            if (c.getDefaultValue() != null) {
                stringBuilder.append(" default ");
                stringBuilder.append(c.getDefaultValue().toString());
            }
            stringBuilder.append(",");
        }
    }
    // Remove last coma
    stringBuilder.setLength(stringBuilder.length() - 1);
    stringBuilder.append("\n");
    // Options
    if (normalize) {
        calculateMinMax(graph);
    }
    // Calculate progress units count
    int max = graph.getNodeCount() + graph.getEdgeCount();
    Progress.switchToDeterminate(progressTicket, max);
    // Node lines
    NodeIterable itr = graph.getNodes();
    for (Node node : itr) {
        if (cancel) {
            itr.doBreak();
            return;
        }
        // Id
        stringBuilder.append(node.getId());
        stringBuilder.append(",");
        // Default columns
        for (NodeColumnsGDF c : defaultNodeColumnsGDFs) {
            if (c.isEnable()) {
                c.writeData(stringBuilder, node);
                stringBuilder.append(",");
            }
        }
        // Attributes columns
        for (Column c : nodeColumns) {
            if (!c.isProperty()) {
                Object val = node.getAttribute(c, graph.getView());
                if (val != null) {
                    if (c.getTypeClass().equals(String.class) || c.getTypeClass().equals(String[].class)) {
                        String quote = !useQuotes ? "" : simpleQuotes ? "'" : "\"";
                        stringBuilder.append(quote);
                        stringBuilder.append(val.toString());
                        stringBuilder.append(quote);
                    } else {
                        stringBuilder.append(val.toString());
                    }
                }
                stringBuilder.append(",");
            }
        }
        // Remove last coma
        stringBuilder.setLength(stringBuilder.length() - 1);
        stringBuilder.append("\n");
        Progress.progress(progressTicket);
    }
    // Edge intro
    stringBuilder.append("edgedef> node1,node2,");
    // Edge settings helper
    for (Edge e : graph.getEdges()) {
        edgeColors = edgeColors || e.alpha() != 0;
        edgeLabels = edgeLabels || (e.getLabel() != null && !e.getLabel().isEmpty());
    }
    // Edge columns title
    for (EdgeColumnsGDF c : defaultEdgeColumnsGDFs) {
        if (c.isEnable()) {
            stringBuilder.append(c.getTitle());
            stringBuilder.append(" ");
            stringBuilder.append(c.getType().toString().toUpperCase());
            if (c.getDefaultValue() != null) {
                stringBuilder.append(" default ");
                stringBuilder.append(c.getDefaultValue().toString());
            }
            stringBuilder.append(",");
        }
    }
    // Attributes Edge columns
    for (Column c : edgeColumns) {
        if (!c.isProperty()) {
            stringBuilder.append(c.getTitle());
            stringBuilder.append(" ");
            DataTypeGDF dataTypeGDF = getDataTypeGDF(c.getTypeClass());
            stringBuilder.append(dataTypeGDF.toString().toUpperCase());
            if (c.getDefaultValue() != null) {
                stringBuilder.append(" default ");
                stringBuilder.append(c.getDefaultValue().toString());
            }
            stringBuilder.append(",");
        }
    }
    // Remove last coma
    stringBuilder.setLength(stringBuilder.length() - 1);
    stringBuilder.append("\n");
    // Edge lines
    EdgeIterable itrEdges = graph.getEdges();
    for (Edge edge : itrEdges) {
        if (cancel) {
            itrEdges.doBreak();
            return;
        }
        // Source & Target
        stringBuilder.append(edge.getSource().getId());
        stringBuilder.append(",");
        stringBuilder.append(edge.getTarget().getId());
        stringBuilder.append(",");
        // Default columns
        for (EdgeColumnsGDF c : defaultEdgeColumnsGDFs) {
            if (c.isEnable()) {
                c.writeData(stringBuilder, edge);
                stringBuilder.append(",");
            }
        }
        // Attributes columns
        for (Column c : edgeColumns) {
            if (!c.isProperty()) {
                Object val = edge.getAttribute(c, graph.getView());
                if (val != null) {
                    if (c.getTypeClass().equals(String.class) || c.getTypeClass().equals(String[].class)) {
                        String quote = !useQuotes ? "" : simpleQuotes ? "'" : "\"";
                        stringBuilder.append(quote);
                        stringBuilder.append(val.toString());
                        stringBuilder.append(quote);
                    } else {
                        stringBuilder.append(val.toString());
                    }
                }
                stringBuilder.append(",");
            }
        }
        // Remove last coma
        stringBuilder.setLength(stringBuilder.length() - 1);
        stringBuilder.append("\n");
        Progress.progress(progressTicket);
    }
    // Write StringBuilder
    if (!cancel) {
        writer.append(stringBuilder);
    }
    Progress.finish(progressTicket);
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node) Column(org.gephi.graph.api.Column) EdgeIterable(org.gephi.graph.api.EdgeIterable) Edge(org.gephi.graph.api.Edge)

Example 22 with NodeIterable

use of org.gephi.graph.api.NodeIterable in project gephi by gephi.

the class ExporterVNA method exportNodeProperties.

/*
     * prints node properties as "id (x)? (y)? (size)? (color)? (shortlabel)?"
     */
private void exportNodeProperties(Graph graph) throws IOException {
    // header
    writer.append("*Node properties\n");
    writer.append("ID");
    if (exportCoords) {
        writer.append(" x y");
    }
    if (exportSize) {
        writer.append(" size");
    }
    if (exportColor) {
        writer.append(" color");
    }
    if (exportShortLabel) {
        writer.append(" shortlabel");
    }
    writer.append("\n");
    // body
    NodeIterable nodeIterable = graph.getNodes();
    for (Node node : nodeIterable) {
        Progress.progress(progressTicket);
        if (cancel) {
            nodeIterable.doBreak();
            return;
        }
        writer.append(node.getId().toString());
        if (exportCoords) {
            if (!normalize) {
                writer.append(" ").append(Float.toString(node.x())).append(" ").append(Float.toString(node.y()));
            } else {
                writer.append(" ").append(Double.toString((node.x() - minX) / (maxX - minX))).append(" ").append(Double.toString((node.y() - minY) / (maxY - minY)));
            }
        }
        if (exportSize) {
            if (!normalize) {
                writer.append(" ").append(Float.toString(node.size()));
            } else {
                writer.append(" ").append(Double.toString((node.size() - minSize) / (maxSize - minSize)));
            }
        }
        if (exportColor) {
            // [0..1] to [0..255]
            writer.append(" ").append(Integer.toString((int) (node.r() * 255f)));
        }
        if (exportShortLabel) {
            if (node.getLabel() != null) {
                writer.append(" ").append(printParameter(node.getLabel()));
            } else {
                writer.append(" ").append(printParameter(node.getId()));
            }
        }
        writer.append("\n");
    }
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node)

Example 23 with NodeIterable

use of org.gephi.graph.api.NodeIterable in project gephi by gephi.

the class NoverlapLayout method goAlgo.

@Override
public void goAlgo() {
    setConverged(true);
    this.graph = graphModel.getGraphVisible();
    graph.readLock();
    try {
        // Reset Layout Data
        for (Node n : graph.getNodes()) {
            if (n.getLayoutData() == null || !(n.getLayoutData() instanceof NoverlapLayoutData)) {
                n.setLayoutData(new NoverlapLayoutData());
            }
            NoverlapLayoutData layoutData = n.getLayoutData();
            layoutData.neighbours.clear();
            layoutData.dx = 0;
            layoutData.dy = 0;
        }
        // Get xmin, xmax, ymin, ymax
        this.xmin = Double.MAX_VALUE;
        this.xmax = Double.MIN_VALUE;
        this.ymin = Double.MAX_VALUE;
        this.ymax = Double.MIN_VALUE;
        for (Node n : graph.getNodes()) {
            float x = n.x();
            float y = n.y();
            float radius = n.size();
            // Get the rectangle occupied by the node
            double nxmin = x - (radius * ratio + margin);
            double nxmax = x + (radius * ratio + margin);
            double nymin = y - (radius * ratio + margin);
            double nymax = y + (radius * ratio + margin);
            // Update global boundaries
            this.xmin = Math.min(this.xmin, nxmin);
            this.xmax = Math.max(this.xmax, nxmax);
            this.ymin = Math.min(this.ymin, nymin);
            this.ymax = Math.max(this.ymax, nymax);
        }
        // Secure the bounds
        double xwidth = this.xmax - this.xmin;
        double yheight = this.ymax - this.ymin;
        double xcenter = (this.xmin + this.xmax) / 2;
        double ycenter = (this.ymin + this.ymax) / 2;
        double securityRatio = 1.1;
        this.xmin = xcenter - securityRatio * xwidth / 2;
        this.xmax = xcenter + securityRatio * xwidth / 2;
        this.ymin = ycenter - securityRatio * yheight / 2;
        this.ymax = ycenter + securityRatio * yheight / 2;
        SpatialGrid grid = new SpatialGrid();
        // Put nodes in their boxes
        for (Node n : graph.getNodes()) {
            grid.add(n);
        }
        // Build proximities
        for (int row = 0; row < grid.countRows() && !cancel; row++) {
            for (int col = 0; col < grid.countColumns() && !cancel; col++) {
                for (Node n : grid.getContent(row, col)) {
                    NoverlapLayoutData lald = n.getLayoutData();
                    // We search nodes that are in the boxes that are adjacent or the same.
                    for (int row2 = Math.max(0, row - 1); row2 <= Math.min(row + 1, grid.countRows() - 1); row2++) {
                        for (int col2 = Math.max(0, col - 1); col2 <= Math.min(col + 1, grid.countColumns() - 1); col2++) {
                            for (Node n2 : grid.getContent(row2, col2)) {
                                if (n2 != n && !lald.neighbours.contains(n2)) {
                                    lald.neighbours.add(n2);
                                }
                            }
                        }
                    }
                }
            }
        }
        // Proximities are built !
        // Apply repulsion force - along proximities...
        NodeIterable nodesIterable = graph.getNodes();
        for (Node n1 : nodesIterable) {
            NoverlapLayoutData lald = n1.getLayoutData();
            for (Node n2 : lald.neighbours) {
                float n1x = n1.x();
                float n1y = n1.y();
                float n2x = n2.x();
                float n2y = n2.y();
                float n1radius = n1.size();
                float n2radius = n2.size();
                // Check sizes (spheric)
                double xDist = n2x - n1x;
                double yDist = n2y - n1y;
                double dist = Math.sqrt(xDist * xDist + yDist * yDist);
                boolean collision = dist < (n1radius * ratio + margin) + (n2radius * ratio + margin);
                if (collision) {
                    setConverged(false);
                    // n1 repulses n2, as strongly as it is big
                    NoverlapLayoutData layoutData = n2.getLayoutData();
                    double f = 1. + n1.size();
                    if (dist > 0) {
                        layoutData.dx += xDist / dist * f;
                        layoutData.dy += yDist / dist * f;
                    } else {
                        // Same exact position, divide by zero impossible: jitter
                        layoutData.dx += 0.01 * (0.5 - Math.random());
                        layoutData.dy += 0.01 * (0.5 - Math.random());
                    }
                }
                if (cancel) {
                    break;
                }
            }
            if (cancel) {
                nodesIterable.doBreak();
                break;
            }
        }
        // apply forces
        for (Node n : graph.getNodes()) {
            NoverlapLayoutData layoutData = n.getLayoutData();
            if (!n.isFixed()) {
                layoutData.dx *= 0.1 * speed;
                layoutData.dy *= 0.1 * speed;
                float x = n.x() + layoutData.dx;
                float y = n.y() + layoutData.dy;
                n.setX(x);
                n.setY(y);
            }
        }
    } finally {
        graph.readUnlockAll();
    }
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) Node(org.gephi.graph.api.Node)

Aggregations

Node (org.gephi.graph.api.Node)23 NodeIterable (org.gephi.graph.api.NodeIterable)23 Edge (org.gephi.graph.api.Edge)9 EdgeIterable (org.gephi.graph.api.EdgeIterable)8 LinkedList (java.util.LinkedList)3 Column (org.gephi.graph.api.Column)3 Object2DoubleOpenHashMap (it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap)2 Object2ObjectOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)2 ObjectOpenHashSet (it.unimi.dsi.fastutil.objects.ObjectOpenHashSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 DirectedGraph (org.gephi.graph.api.DirectedGraph)2 ArrayList (java.util.ArrayList)1 Graph (org.gephi.graph.api.Graph)1 Element (org.w3c.dom.Element)1