Search in sources :

Example 1 with EdgeDraft

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

the class ImporterGraphML method readEdge.

private void readEdge(XMLStreamReader reader) throws Exception {
    String id = "";
    String source = "";
    String target = "";
    String directed = "";
    //Attributes
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attName = reader.getAttributeName(i).getLocalPart();
        if (EDGE_SOURCE.equalsIgnoreCase(attName)) {
            source = reader.getAttributeValue(i);
        } else if (EDGE_TARGET.equalsIgnoreCase(attName)) {
            target = reader.getAttributeValue(i);
        } else if (EDGE_ID.equalsIgnoreCase(attName)) {
            id = reader.getAttributeValue(i);
        } else if (EDGE_DIRECTED.equalsIgnoreCase(attName)) {
            directed = reader.getAttributeValue(i);
        }
    }
    //Edge Id
    EdgeDraft edge;
    if (!id.isEmpty()) {
        edge = container.factory().newEdgeDraft(id);
    } else {
        edge = container.factory().newEdgeDraft();
    }
    NodeDraft nodeSource = container.getNode(source);
    NodeDraft nodeTarget = container.getNode(target);
    edge.setSource(nodeSource);
    edge.setTarget(nodeTarget);
    //Type
    if (!directed.isEmpty()) {
        if (directed.equalsIgnoreCase("true")) {
            edge.setDirection(EdgeDirection.DIRECTED);
        } else if (directed.equalsIgnoreCase("false")) {
            edge.setDirection(EdgeDirection.UNDIRECTED);
        } else {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_edgetype", directed, edge), Issue.Level.SEVERE));
            edge.setDirection(edgeDefault);
        }
    } else {
        edge.setDirection(edgeDefault);
    }
    boolean end = false;
    while (reader.hasNext() && !end) {
        int type = reader.next();
        switch(type) {
            case XMLStreamReader.START_ELEMENT:
                if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
                    readEdgeAttValue(reader, edge);
                }
                break;
            case XMLStreamReader.END_ELEMENT:
                if (EDGE.equalsIgnoreCase(xmlReader.getLocalName())) {
                    end = true;
                }
                break;
        }
    }
    container.addEdge(edge);
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 2 with EdgeDraft

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

the class ImporterPajek method readArcsOrEdges.

private String readArcsOrEdges(String curLine, BufferedReader br) throws Exception {
    String nextLine = curLine;
    boolean reading_arcs = false;
    boolean reading_edges = false;
    if (nextLine.toLowerCase().startsWith("*arcs")) {
        reading_arcs = true;
    } else if (nextLine.toLowerCase().startsWith("*edges")) {
        reading_edges = true;
    }
    if (!(reading_arcs || reading_edges)) {
        return nextLine;
    }
    boolean is_list = false;
    if (nextLine.toLowerCase().endsWith("list")) {
        is_list = true;
    }
    while (br.ready()) {
        if (cancel) {
            return nextLine;
        }
        nextLine = br.readLine();
        if (nextLine == null || nextLine.startsWith("*")) {
            break;
        }
        if (nextLine.equals("")) {
            // skip blank lines
            report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat2", lineReader.getLineNumber()), Issue.Level.WARNING));
            continue;
        }
        StringTokenizer st = new StringTokenizer(nextLine.trim());
        int vid1 = Integer.parseInt(st.nextToken()) - 1;
        NodeDraft nodeFrom = verticesArray[vid1];
        if (// one source, multiple destinations
        is_list) {
            do {
                int vid2 = Integer.parseInt(st.nextToken()) - 1;
                NodeDraft nodeTo = verticesArray[vid2];
                EdgeDraft edge = container.factory().newEdgeDraft();
                edge.setSource(nodeFrom);
                edge.setTarget(nodeTo);
                container.addEdge(edge);
            } while (st.hasMoreTokens());
        } else // one source, one destination, at most one weight
        {
            int vid2 = Integer.parseInt(st.nextToken()) - 1;
            NodeDraft nodeTo = verticesArray[vid2];
            EdgeDraft edge = container.factory().newEdgeDraft();
            edge.setSource(nodeFrom);
            edge.setTarget(nodeTo);
            // get the edge weight if we care
            if (st.hasMoreTokens()) {
                double edgeWeight = 1.0;
                try {
                    edgeWeight = new Double(st.nextToken());
                } catch (Exception e) {
                    report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat7", lineReader.getLineNumber()), Issue.Level.WARNING));
                }
                edge.setWeight(edgeWeight);
            }
            container.addEdge(edge);
        }
    }
    return nextLine;
}
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 3 with EdgeDraft

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

the class ImporterDOT method edgeStructure.

protected void edgeStructure(StreamTokenizer streamTokenizer, final NodeDraft nodeDraft) throws Exception {
    streamTokenizer.nextToken();
    EdgeDraft edge = null;
    if (streamTokenizer.ttype == '>' || streamTokenizer.ttype == '-') {
        streamTokenizer.nextToken();
        if (streamTokenizer.ttype == '{') {
            while (true) {
                streamTokenizer.nextToken();
                if (streamTokenizer.ttype == '}') {
                    break;
                } else {
                    nodeID(streamTokenizer);
                    edge = container.factory().newEdgeDraft();
                    edge.setSource(nodeDraft);
                    edge.setTarget(getOrCreateNode("" + streamTokenizer.sval));
                    container.addEdge(edge);
                }
            }
        } else {
            nodeID(streamTokenizer);
            edge = container.factory().newEdgeDraft();
            edge.setSource(nodeDraft);
            edge.setTarget(getOrCreateNode("" + streamTokenizer.sval));
            container.addEdge(edge);
        }
    } else {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterDOT.class, "importerDOT_error_edgeparsing", streamTokenizer.lineno()), Issue.Level.SEVERE));
        if (streamTokenizer.ttype == StreamTokenizer.TT_WORD) {
            streamTokenizer.pushBack();
        }
        return;
    }
    streamTokenizer.nextToken();
    if (streamTokenizer.ttype == '[') {
        edgeAttributes(streamTokenizer, edge);
    } else {
        streamTokenizer.pushBack();
    }
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Issue(org.gephi.io.importer.api.Issue)

Example 4 with EdgeDraft

use of org.gephi.io.importer.api.EdgeDraft 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 5 with EdgeDraft

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

the class ImporterEdgeList method getEdges.

private void getEdges(Connection connection) throws SQLException {
    //Factory
    ElementDraft.Factory factory = container.factory();
    //Properties
    PropertiesAssociations properties = database.getPropertiesAssociations();
    Statement s = connection.createStatement();
    ResultSet rs = null;
    try {
        rs = s.executeQuery(database.getEdgeQuery());
    } catch (SQLException ex) {
        report.logIssue(new Issue("Failed to execute Edge query", Issue.Level.SEVERE, ex));
        return;
    }
    findEdgeAttributesColumns(rs);
    ResultSetMetaData metaData = rs.getMetaData();
    int columnsCount = metaData.getColumnCount();
    while (rs.next()) {
        String id = null;
        for (int i = 0; i < columnsCount; i++) {
            String columnName = metaData.getColumnLabel(i + 1);
            EdgeProperties p = properties.getEdgeProperty(columnName);
            if (p != null && p.equals(EdgeProperties.ID)) {
                String ide = rs.getString(i + 1);
                if (ide != null) {
                    id = ide;
                }
            }
        }
        EdgeDraft edge;
        if (id != null) {
            edge = factory.newEdgeDraft(id);
        } else {
            edge = factory.newEdgeDraft();
        }
        for (int i = 0; i < columnsCount; i++) {
            String columnName = metaData.getColumnLabel(i + 1);
            EdgeProperties p = properties.getEdgeProperty(columnName);
            if (p != null) {
                injectEdgeProperty(p, rs, i + 1, edge);
            } else {
                //Inject edge attributes
                ColumnDraft col = container.getEdgeColumn(columnName);
                injectElementAttribute(rs, i + 1, col, edge);
            }
        }
        injectTimeIntervalProperty(edge);
        container.addEdge(edge);
    }
    rs.close();
    s.close();
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) SQLException(java.sql.SQLException) ElementDraft(org.gephi.io.importer.api.ElementDraft) PropertiesAssociations(org.gephi.io.importer.api.PropertiesAssociations) Statement(java.sql.Statement) ResultSetMetaData(java.sql.ResultSetMetaData) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) ResultSet(java.sql.ResultSet) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties)

Aggregations

EdgeDraft (org.gephi.io.importer.api.EdgeDraft)18 NodeDraft (org.gephi.io.importer.api.NodeDraft)15 Issue (org.gephi.io.importer.api.Issue)9 IOException (java.io.IOException)3 Random (java.util.Random)3 StringTokenizer (java.util.StringTokenizer)3 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)3 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)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 TimeMap (org.gephi.graph.api.types.TimeMap)1 ElementDraft (org.gephi.io.importer.api.ElementDraft)1