Search in sources :

Example 6 with NodeDraft

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

the class ImporterGDF method importData.

private void importData(LineNumberReader reader) throws Exception {
    //Progress
    Progress.start(progressTicket);
    //Verify a node line exists and puts nodes and edges lines in arrays
    walkFile(reader);
    //Progress
    Progress.switchToDeterminate(progressTicket, nodeLines.size() + edgeLines.size());
    //Magix regex
    Pattern pattern = Pattern.compile("(?<=(?:,|^)\")(.*?)(?=(?<=(?:[^\\\\]))\",|\"$)|(?<=(?:,|^)')(.*?)(?=(?<=(?:[^\\\\]))',|'$)|(?<=(?:,|^))(?=[^'\"])(.*?)(?=(?:,|$))|(?<=,)($)");
    //Nodes
    for (String nodeLine : nodeLines) {
        if (cancel) {
            return;
        }
        //Create Node
        NodeDraft node = null;
        Matcher m = pattern.matcher(nodeLine);
        int count = 0;
        String id = "";
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            if (start != end) {
                String data = nodeLine.substring(start, end);
                data = data.trim();
                if (!data.isEmpty() && !data.toLowerCase().equals("null")) {
                    if (count == 0) {
                        //Id
                        id = data;
                        if (node == null) {
                            node = container.factory().newNodeDraft(id);
                        }
                    } else if (count - 1 < nodeColumns.length) {
                        if (nodeColumns[count - 1] != null) {
                            setNodeData(node, nodeColumns[count - 1], data);
                        }
                    } else {
                        report.logIssue(new Issue(NbBundle.getMessage(ImporterGDF.class, "importerGDF_error_dataformat7", id), Issue.Level.SEVERE));
                    }
                }
            }
            count++;
        }
        container.addNode(node);
        //Progress
        Progress.progress(progressTicket);
    }
    //Edges
    for (String edgeLine : edgeLines) {
        if (cancel) {
            return;
        }
        //Create Edge
        EdgeDraft edge = container.factory().newEdgeDraft();
        //Default to undirected unless stated
        edge.setDirection(EdgeDirection.UNDIRECTED);
        Matcher m = pattern.matcher(edgeLine);
        int count = 0;
        String id = "";
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            if (start != end) {
                String data = edgeLine.substring(start, end);
                data = data.trim();
                if (!data.isEmpty() && !data.toLowerCase().equals("null")) {
                    if (count == 0) {
                        NodeDraft nodeSource = container.getNode(data);
                        edge.setSource(nodeSource);
                        id = data;
                    } else if (count == 1) {
                        NodeDraft nodeTarget = container.getNode(data);
                        edge.setTarget(nodeTarget);
                        id += "," + data;
                    } else if (count - 2 < edgeColumns.length) {
                        if (edgeColumns[count - 2] != null) {
                            setEdgeData(edge, edgeColumns[count - 2], data);
                        }
                    } else {
                        report.logIssue(new Issue(NbBundle.getMessage(ImporterGDF.class, "importerGDF_error_dataformat7", id), Issue.Level.SEVERE));
                    }
                }
            }
            count++;
        }
        container.addEdge(edge);
        //Progress
        Progress.progress(progressTicket);
    }
}
Also used : Pattern(java.util.regex.Pattern) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Issue(org.gephi.io.importer.api.Issue) Matcher(java.util.regex.Matcher) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 7 with NodeDraft

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

the class ImporterCSV method addNode.

private void addNode(String id, String label) {
    NodeDraft node;
    if (!container.nodeExists(id)) {
        node = container.factory().newNodeDraft(id);
        node.setLabel(label);
        container.addNode(node);
    }
}
Also used : NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 8 with NodeDraft

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

the class ImporterCSV method addEdge.

private void addEdge(String source, String target, float weight) {
    NodeDraft sourceNode;
    if (!container.nodeExists(source)) {
        sourceNode = container.factory().newNodeDraft(source);
        container.addNode(sourceNode);
    } else {
        sourceNode = container.getNode(source);
    }
    NodeDraft targetNode;
    if (!container.nodeExists(target)) {
        targetNode = container.factory().newNodeDraft(target);
        container.addNode(targetNode);
    } else {
        targetNode = container.getNode(target);
    }
    EdgeDraft edge = container.factory().newEdgeDraft();
    edge.setSource(sourceNode);
    edge.setTarget(targetNode);
    edge.setWeight(weight);
    container.addEdge(edge);
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 9 with NodeDraft

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

the class ImporterDL method readEdgelistRow.

private void readEdgelistRow(String row, int pointer, double startTime, double endTime) {
    StringTokenizer rowkonizer = new StringTokenizer(row);
    if (!rowkonizer.hasMoreTokens()) {
        return;
    }
    // should have three entries, int from, int to, weight
    String from = rowkonizer.nextToken();
    if (!rowkonizer.hasMoreTokens()) {
        return;
    }
    String to = rowkonizer.nextToken();
    double weight = 1.0;
    if (rowkonizer.hasMoreTokens()) {
        String weightParse = rowkonizer.nextToken();
        try {
            weight = Double.parseDouble(weightParse);
        } catch (Exception e) {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterDL.class, "importerDL_error_edgeparseweight", weightParse, getLineNumber(pointer)), Issue.Level.WARNING));
        }
    }
    NodeDraft sourceNode = container.getNode(from);
    NodeDraft targetNode = container.getNode(to);
    EdgeDraft edgeDraft = container.factory().newEdgeDraft();
    edgeDraft.setSource(sourceNode);
    edgeDraft.setTarget(targetNode);
    edgeDraft.setWeight((float) weight);
    container.addEdge(edgeDraft);
}
Also used : StringTokenizer(java.util.StringTokenizer) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft) IOException(java.io.IOException)

Example 10 with NodeDraft

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

the class ImporterDL method readMatrixRow.

private void readMatrixRow(String line, int pointer, int row, int startTime, int endTime) {
    StringTokenizer rowkonizer = new StringTokenizer(line, " ");
    int from = row + 1;
    int to = 1;
    double weight = 0;
    while (rowkonizer.hasMoreTokens()) {
        String toParse = (String) rowkonizer.nextToken();
        if (to > numNodes) {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterDL.class, "importerDL_error_matrixentriescount", row, startTime, getLineNumber(pointer)), Issue.Level.SEVERE));
        }
        try {
            weight = Double.parseDouble(toParse);
        } catch (Exception e) {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterDL.class, "importerDL_error_weightparseerror", toParse, startTime, getLineNumber(pointer)), Issue.Level.SEVERE));
        }
        if (weight != 0) {
            NodeDraft sourceNode = container.getNode("" + from);
            NodeDraft targetNode = container.getNode("" + to);
            EdgeDraft edgeDraft = container.factory().newEdgeDraft();
            edgeDraft.setSource(sourceNode);
            edgeDraft.setTarget(targetNode);
            edgeDraft.setWeight((float) weight);
            container.addEdge(edgeDraft);
        }
        to++;
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft) IOException(java.io.IOException)

Aggregations

NodeDraft (org.gephi.io.importer.api.NodeDraft)31 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)15 Issue (org.gephi.io.importer.api.Issue)14 IOException (java.io.IOException)5 StringTokenizer (java.util.StringTokenizer)5 Random (java.util.Random)4 SQLException (java.sql.SQLException)2 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)2 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)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 ContainerUnloader (org.gephi.io.importer.api.ContainerUnloader)1 ElementDraft (org.gephi.io.importer.api.ElementDraft)1