Search in sources :

Example 16 with Table

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

the class AttributeColumnsControllerImpl method importCSVToEdgesTable.

@Override
public void importCSVToEdgesTable(Graph graph, File file, Character separator, Charset charset, String[] columnNames, Class[] columnTypes, boolean createNewNodes) {
    if (columnNames == null || columnNames.length == 0) {
        return;
    }
    if (columnTypes == null || columnNames.length != columnTypes.length) {
        throw new IllegalArgumentException("Column names length must be the same as column types length");
    }
    CsvReader reader = null;
    graph.writeLock();
    try {
        //Prepare attribute columns for the column names, creating the not already existing columns:
        Table edgesTable = graph.getModel().getEdgeTable();
        Column weightColumn = edgesTable.getColumn("Weight");
        boolean isDynamicWeight = weightColumn.isDynamic();
        String idColumnHeader = null;
        String sourceColumnHeader = null;
        String targetColumnHeader = null;
        String typeColumnHeader = null;
        String weightColumnHeader = null;
        //Necessary because of column name case insensitivity, to map columns to its corresponding csv header.
        HashMap<Column, String> columnHeaders = new HashMap<>();
        for (int i = 0; i < columnNames.length; i++) {
            //Separate first id column found from the list to use as id. If more are found later, the will not be in the list and be ignored.
            if (columnNames[i].equalsIgnoreCase("id")) {
                if (idColumnHeader == null) {
                    idColumnHeader = columnNames[i];
                }
            } else if (columnNames[i].equalsIgnoreCase("source") && sourceColumnHeader == null) {
                //Separate first source column found from the list to use as source node id
                sourceColumnHeader = columnNames[i];
            } else if (columnNames[i].equalsIgnoreCase("target") && targetColumnHeader == null) {
                //Separate first target column found from the list to use as target node id
                targetColumnHeader = columnNames[i];
            } else if (columnNames[i].equalsIgnoreCase("type") && typeColumnHeader == null) {
                //Separate first type column found from the list to use as edge type (directed/undirected)
                typeColumnHeader = columnNames[i];
            } else if (edgesTable.hasColumn(columnNames[i])) {
                //Any other existing column:
                Column column = edgesTable.getColumn(columnNames[i]);
                columnHeaders.put(column, columnNames[i]);
                if (column.equals(weightColumn)) {
                    weightColumnHeader = columnNames[i];
                }
            } else {
                //New column:
                Column column = addAttributeColumn(edgesTable, columnNames[i], columnTypes[i]);
                if (column != null) {
                    columnHeaders.put(column, columnNames[i]);
                }
            }
        }
        Set<Column> columnList = columnHeaders.keySet();
        //Create edges:
        GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
        reader = new CsvReader(new FileInputStream(file), separator, charset);
        reader.setTrimWhitespace(false);
        reader.readHeaders();
        int recordNumber = 0;
        while (reader.readRecord()) {
            String id = null;
            Edge edge = null;
            String sourceId, targetId;
            Node source, target;
            String type;
            boolean directed;
            recordNumber++;
            sourceId = reader.get(sourceColumnHeader);
            targetId = reader.get(targetColumnHeader);
            if (sourceId == null || sourceId.trim().isEmpty() || targetId == null || targetId.trim().isEmpty()) {
                Logger.getLogger("").log(Level.WARNING, "Ignoring record number {0} due to empty source and/or target node ids", recordNumber);
                //No correct source and target ids were provided, ignore row
                continue;
            }
            source = graph.getNode(sourceId);
            if (source == null) {
                if (createNewNodes) {
                    //Create new nodes when they don't exist already and option is enabled
                    if (source == null) {
                        source = gec.createNode(null, sourceId, graph);
                    }
                } else {
                    //Ignore this edge row, since no new nodes should be created.
                    continue;
                }
            }
            target = graph.getNode(targetId);
            if (target == null) {
                if (createNewNodes) {
                    //Create new nodes when they don't exist already and option is enabled
                    if (target == null) {
                        target = gec.createNode(null, targetId, graph);
                    }
                } else {
                    //Ignore this edge row, since no new nodes should be created.
                    continue;
                }
            }
            if (typeColumnHeader != null) {
                type = reader.get(typeColumnHeader);
                //Undirected if indicated correctly, otherwise always directed:
                if (type != null) {
                    directed = !type.equalsIgnoreCase("undirected");
                } else {
                    directed = true;
                }
            } else {
                //Directed by default when not indicated
                directed = true;
            }
            //Prepare the correct edge to assign the attributes:
            if (idColumnHeader != null) {
                id = reader.get(idColumnHeader);
                if (id == null || id.trim().isEmpty()) {
                    //id null or empty, assign one
                    edge = gec.createEdge(source, target, directed);
                } else {
                    Edge edgeById = graph.getEdge(id);
                    if (edgeById == null) {
                        //Create edge because no edge with that id exists
                        edge = gec.createEdge(id, source, target, directed);
                    }
                }
            } else {
                if (findEdge(graph, null, source, target, directed) == null) {
                    //Only create if it does not exist
                    edge = gec.createEdge(source, target, directed);
                }
            }
            if (edge != null) {
                //Assign all attributes to the new edge:
                for (Column column : columnList) {
                    setAttributeValue(reader.get(columnHeaders.get(column)), edge, column);
                }
            } else {
                edge = findEdge(graph, id, source, target, directed);
                if (edge != null) {
                    //Increase non dynamic edge weight with specified weight (if specified), else increase by 1:
                    if (!isDynamicWeight) {
                        if (weightColumnHeader != null) {
                            String weight = reader.get(weightColumnHeader);
                            try {
                                Float weightFloat = Float.parseFloat(weight);
                                edge.setWeight(edge.getWeight() + weightFloat);
                            } catch (NumberFormatException numberFormatException) {
                                //Not valid weight, add 1
                                edge.setWeight(edge.getWeight() + 1);
                                Logger.getLogger("").log(Level.WARNING, "Could not parse weight {0}, adding 1", weight);
                            }
                        } else {
                            //Add 1 (weight not specified)
                            edge.setWeight(edge.getWeight() + 1);
                        }
                    }
                } else {
                    Logger.getLogger("").log(Level.WARNING, "Could not add edge [id = {0}, source = {1}, target = {2}, directed = {3}] to the graph and could not find the existing edge to add its weight. Skipping edge", new Object[] { id, source.getId(), target.getId(), directed });
                }
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger("").log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger("").log(Level.SEVERE, null, ex);
    } finally {
        graph.readUnlockAll();
        graph.writeUnlock();
        if (reader != null) {
            reader.close();
        }
    }
}
Also used : Table(org.gephi.graph.api.Table) HashMap(java.util.HashMap) Node(org.gephi.graph.api.Node) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CsvReader(com.csvreader.CsvReader) Column(org.gephi.graph.api.Column) GraphElementsController(org.gephi.datalab.api.GraphElementsController) Edge(org.gephi.graph.api.Edge)

Example 17 with Table

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

the class AttributeColumnsControllerImpl method importCSVToNodesTable.

@Override
public void importCSVToNodesTable(Graph graph, File file, Character separator, Charset charset, String[] columnNames, Class[] columnTypes, boolean assignNewNodeIds) {
    if (columnNames == null || columnNames.length == 0) {
        return;
    }
    if (columnTypes == null || columnNames.length != columnTypes.length) {
        throw new IllegalArgumentException("Column names length must be the same as column types length");
    }
    CsvReader reader = null;
    graph.writeLock();
    try {
        //Prepare attribute columns for the column names, creating the not already existing columns:
        Table nodesTable = graph.getModel().getNodeTable();
        String idColumn = null;
        //Necessary because of column name case insensitivity, to map columns to its corresponding csv header.
        HashMap<Column, String> columnHeaders = new HashMap<>();
        for (int i = 0; i < columnNames.length; i++) {
            //Separate first id column found from the list to use as id. If more are found later, the will not be in the list and be ignored.
            if (columnNames[i].equalsIgnoreCase("id")) {
                if (idColumn == null) {
                    idColumn = columnNames[i];
                }
            } else if (nodesTable.hasColumn(columnNames[i])) {
                Column column = nodesTable.getColumn(columnNames[i]);
                columnHeaders.put(column, columnNames[i]);
            } else {
                Column column = addAttributeColumn(nodesTable, columnNames[i], columnTypes[i]);
                if (column != null) {
                    columnHeaders.put(column, columnNames[i]);
                }
            }
        }
        Set<Column> columnList = columnHeaders.keySet();
        //Create nodes:
        GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
        String id;
        Node node;
        reader = new CsvReader(new FileInputStream(file), separator, charset);
        reader.setTrimWhitespace(false);
        reader.readHeaders();
        while (reader.readRecord()) {
            //Prepare the correct node to assign the attributes:
            if (idColumn != null) {
                id = reader.get(idColumn);
                if (id == null || id.isEmpty()) {
                    //id null or empty, assign one
                    node = gec.createNode(null, graph);
                } else {
                    node = graph.getNode(id);
                    if (node != null) {
                        //Node with that id already in graph
                        if (assignNewNodeIds) {
                            node = gec.createNode(null, graph);
                        }
                    } else {
                        //New id in the graph
                        node = gec.createNode(null, id, graph);
                    }
                }
            } else {
                node = gec.createNode(null);
            }
            //Assign attributes to the current node:
            for (Column column : columnList) {
                setAttributeValue(reader.get(columnHeaders.get(column)), node, column);
            }
        }
    } catch (FileNotFoundException ex) {
        Exceptions.printStackTrace(ex);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        graph.readUnlockAll();
        graph.writeUnlock();
        if (reader != null) {
            reader.close();
        }
    }
}
Also used : Table(org.gephi.graph.api.Table) HashMap(java.util.HashMap) Node(org.gephi.graph.api.Node) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CsvReader(com.csvreader.CsvReader) Column(org.gephi.graph.api.Column) GraphElementsController(org.gephi.datalab.api.GraphElementsController)

Example 18 with Table

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

the class GraphElementsControllerImpl method copyNode.

private Node copyNode(Node node, Graph graph) {
    Node copy = buildNode(graph, node.getLabel());
    //Copy properties (position, size and color):
    copy.setX(node.x());
    copy.setY(node.y());
    copy.setZ(node.z());
    copy.setSize(node.size());
    copy.setR(node.r());
    copy.setG(node.g());
    copy.setB(node.b());
    copy.setAlpha(node.alpha());
    Table nodeTable = graph.getModel().getNodeTable();
    //Copy attributes:
    for (Column column : nodeTable) {
        if (!column.isReadOnly()) {
            copy.setAttribute(column, node.getAttribute(column));
        }
    }
    graph.addNode(copy);
    return copy;
}
Also used : Table(org.gephi.graph.api.Table) Column(org.gephi.graph.api.Column) Node(org.gephi.graph.api.Node)

Example 19 with Table

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

the class SearchReplaceControllerImpl method findOnNodes.

private SearchResult findOnNodes(SearchOptions searchOptions, int rowIndex, int columnIndex) {
    GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
    SearchResult result = null;
    Set<Integer> columnsToSearch = searchOptions.getColumnsToSearch();
    boolean searchAllColumns = columnsToSearch.isEmpty();
    Table table = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getNodeTable();
    Node[] nodes = searchOptions.getNodesToSearch();
    Node row;
    Column column;
    Object value;
    TimeFormat timeFormat = table.getGraph().getModel().getTimeFormat();
    DateTimeZone timeZone = table.getGraph().getModel().getTimeZone();
    for (; rowIndex < nodes.length; rowIndex++) {
        if (!gec.isNodeInGraph(nodes[rowIndex])) {
            //Make sure node is still in graph when continuing a search
            continue;
        }
        row = nodes[rowIndex];
        for (; columnIndex < table.countColumns(); columnIndex++) {
            if (searchAllColumns || columnsToSearch.contains(columnIndex)) {
                column = table.getColumn(columnIndex);
                value = row.getAttribute(column);
                result = matchRegex(value, searchOptions, rowIndex, columnIndex, timeFormat, timeZone);
                if (result != null) {
                    result.setFoundNode(nodes[rowIndex]);
                    return result;
                }
            }
            //Start at the beginning for the next value
            searchOptions.setRegionStart(0);
        }
        //Start at the beginning for the next value
        searchOptions.setRegionStart(0);
        //Start at the first column for the next row
        columnIndex = 0;
    }
    return result;
}
Also used : TimeFormat(org.gephi.graph.api.TimeFormat) Table(org.gephi.graph.api.Table) Column(org.gephi.graph.api.Column) GraphElementsController(org.gephi.datalab.api.GraphElementsController) Node(org.gephi.graph.api.Node) SearchResult(org.gephi.datalab.api.SearchReplaceController.SearchResult) DateTimeZone(org.joda.time.DateTimeZone)

Example 20 with Table

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

the class SearchReplaceControllerImpl method findOnEdges.

private SearchResult findOnEdges(SearchOptions searchOptions, int rowIndex, int columnIndex) {
    GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
    SearchResult result = null;
    Set<Integer> columnsToSearch = searchOptions.getColumnsToSearch();
    boolean searchAllColumns = columnsToSearch.isEmpty();
    Table table = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getEdgeTable();
    Edge[] edges = searchOptions.getEdgesToSearch();
    Edge row;
    Column column;
    Object value;
    TimeFormat timeFormat = table.getGraph().getModel().getTimeFormat();
    DateTimeZone timeZone = table.getGraph().getModel().getTimeZone();
    for (; rowIndex < edges.length; rowIndex++) {
        if (!gec.isEdgeInGraph(edges[rowIndex])) {
            //Make sure edge is still in graph when continuing a search
            continue;
        }
        row = edges[rowIndex];
        for (; columnIndex < table.countColumns(); columnIndex++) {
            if (searchAllColumns || columnsToSearch.contains(columnIndex)) {
                column = table.getColumn(columnIndex);
                value = row.getAttribute(column);
                result = matchRegex(value, searchOptions, rowIndex, columnIndex, timeFormat, timeZone);
                if (result != null) {
                    result.setFoundEdge(edges[rowIndex]);
                    return result;
                }
            }
            //Start at the beginning for the next value
            searchOptions.setRegionStart(0);
        }
        //Start at the beginning for the next value
        searchOptions.setRegionStart(0);
        //Start at the first column for the next row
        columnIndex = 0;
    }
    return result;
}
Also used : TimeFormat(org.gephi.graph.api.TimeFormat) Table(org.gephi.graph.api.Table) Column(org.gephi.graph.api.Column) GraphElementsController(org.gephi.datalab.api.GraphElementsController) SearchResult(org.gephi.datalab.api.SearchReplaceController.SearchResult) Edge(org.gephi.graph.api.Edge) DateTimeZone(org.joda.time.DateTimeZone)

Aggregations

Table (org.gephi.graph.api.Table)28 Column (org.gephi.graph.api.Column)22 Node (org.gephi.graph.api.Node)10 GraphController (org.gephi.graph.api.GraphController)7 Edge (org.gephi.graph.api.Edge)5 GraphElementsController (org.gephi.datalab.api.GraphElementsController)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 EdgesDataTable (org.gephi.desktop.datalab.tables.EdgesDataTable)3 NodesDataTable (org.gephi.desktop.datalab.tables.NodesDataTable)3 TimeFormat (org.gephi.graph.api.TimeFormat)3 TimeRepresentation (org.gephi.graph.api.TimeRepresentation)3 DateTimeZone (org.joda.time.DateTimeZone)3 CsvReader (com.csvreader.CsvReader)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 AttributeColumnsController (org.gephi.datalab.api.AttributeColumnsController)2 SearchResult (org.gephi.datalab.api.SearchReplaceController.SearchResult)2 DataTablesController (org.gephi.datalab.api.datatables.DataTablesController)2