Search in sources :

Example 6 with EdgeIterable

use of org.gephi.graph.api.EdgeIterable 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 7 with EdgeIterable

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

the class ExporterGML method exportData.

private void exportData(Graph graph) throws IOException {
    printOpen("graph");
    printTag("Creator \"Gephi\"");
    if (graph.isDirected() || graph.isMixed()) {
        printTag("directed 1");
    } else if (graph.isUndirected()) {
        printTag("directed 0");
    }
    NodeIterable nodeIterable = graph.getNodes();
    for (Node node : nodeIterable) {
        if (cancel) {
            nodeIterable.doBreak();
            return;
        }
        printNode(node, graph);
    }
    EdgeIterable edgeIterable = graph.getEdges();
    for (Edge edge : edgeIterable) {
        if (cancel) {
            edgeIterable.doBreak();
            return;
        }
        printEdge(edge, graph);
    }
    printClose();
}
Also used : NodeIterable(org.gephi.graph.api.NodeIterable) EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 8 with EdgeIterable

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

the class ExporterVNA method exportEdgeData.

/*
     * prints edge data as "from to (strength)? (attributes)*"
     */
private void exportEdgeData(Graph graph) throws IOException {
    writer.append("*Tie data\n");
    writer.append("from to");
    if (exportEdgeWeight) {
        writer.append(" strength");
    }
    if (exportAttributes) {
        for (Column col : graph.getModel().getEdgeTable()) {
            if (!col.isProperty()) {
                writer.append(" ").append(printParameter(col.getTitle()).replace(' ', '_'));
            //replace spaces because importer can't read attributes titles in quotes
            }
        }
    }
    writer.append("\n");
    EdgeIterable edgeIterable = graph.getEdges();
    for (Edge edge : edgeIterable) {
        //all edges in vna are directed, so make clone
        printEdgeData(edge, edge.getSource(), edge.getTarget(), graph);
        if (!edge.isDirected() && !edge.isSelfLoop()) {
            printEdgeData(edge, edge.getTarget(), edge.getSource(), graph);
        }
        Progress.progress(progressTicket);
        if (cancel) {
            edgeIterable.doBreak();
            return;
        }
    }
}
Also used : Column(org.gephi.graph.api.Column) EdgeIterable(org.gephi.graph.api.EdgeIterable) Edge(org.gephi.graph.api.Edge)

Example 9 with EdgeIterable

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

the class ExporterGraphML method createEdges.

private void createEdges(Document document, Element edgesE, Graph graph) throws Exception {
    EdgeIterable itr = graph.getEdges();
    for (Edge e : itr) {
        if (cancel) {
            itr.doBreak();
            break;
        }
        Element edgeE = createEdge(document, e, graph);
        edgesE.appendChild(edgeE);
    }
}
Also used : EdgeIterable(org.gephi.graph.api.EdgeIterable) Element(org.w3c.dom.Element) Edge(org.gephi.graph.api.Edge)

Example 10 with EdgeIterable

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

the class ConnectedComponents method tarjans.

private LinkedList<LinkedList<Node>> tarjans(LinkedList<LinkedList<Node>> components, LinkedList<Node> S, DirectedGraph graph, Node f, int[] index, int[] low_index, HashMap<Node, Integer> indicies) {
    int id = indicies.get(f);
    index[id] = count;
    low_index[id] = count;
    count++;
    S.addFirst(f);
    EdgeIterable edgeIter = graph.getOutEdges(f);
    for (Edge e : edgeIter) {
        Node u = graph.getOpposite(f, e);
        int x = indicies.get(u);
        if (index[x] == 0) {
            tarjans(components, S, graph, u, index, low_index, indicies);
            low_index[id] = Math.min(low_index[x], low_index[id]);
        } else if (S.contains(u)) {
            low_index[id] = Math.min(low_index[id], index[x]);
        }
    }
    LinkedList<Node> currentComponent = new LinkedList<>();
    if (low_index[id] == index[id]) {
        Node v = null;
        while (v != f) {
            v = S.removeFirst();
            currentComponent.add(v);
        }
        components.add(currentComponent);
    }
    return components;
}
Also used : EdgeIterable(org.gephi.graph.api.EdgeIterable) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) LinkedList(java.util.LinkedList)

Aggregations

Edge (org.gephi.graph.api.Edge)12 EdgeIterable (org.gephi.graph.api.EdgeIterable)12 Node (org.gephi.graph.api.Node)10 NodeIterable (org.gephi.graph.api.NodeIterable)4 LinkedList (java.util.LinkedList)2 Column (org.gephi.graph.api.Column)2 HashMap (java.util.HashMap)1 Element (org.w3c.dom.Element)1