Search in sources :

Example 6 with GraphElementsController

use of org.gephi.datalab.api.GraphElementsController 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 7 with GraphElementsController

use of org.gephi.datalab.api.GraphElementsController 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 8 with GraphElementsController

use of org.gephi.datalab.api.GraphElementsController 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)

Example 9 with GraphElementsController

use of org.gephi.datalab.api.GraphElementsController in project gephi by gephi.

the class DeleteNodes method execute.

@Override
public void execute() {
    if (JOptionPane.showConfirmDialog(null, NbBundle.getMessage(DeleteNodes.class, "DeleteNodes.confirmation.message"), getName(), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
        GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
        gec.deleteNodes(nodes);
    }
}
Also used : GraphElementsController(org.gephi.datalab.api.GraphElementsController)

Example 10 with GraphElementsController

use of org.gephi.datalab.api.GraphElementsController in project gephi by gephi.

the class ClearEdges method execute.

@Override
public void execute() {
    GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
    Graph graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraph();
    List<Edge> edges = new ArrayList<>();
    for (Edge edge : graph.getEdges().toArray()) {
        if (edge.isDirected()) {
            if (deleteDirected) {
                edges.add(edge);
            }
        } else if (deleteUndirected) {
            edges.add(edge);
        }
    }
    gec.deleteEdges(edges.toArray(new Edge[0]));
}
Also used : Graph(org.gephi.graph.api.Graph) GraphElementsController(org.gephi.datalab.api.GraphElementsController) ArrayList(java.util.ArrayList) Edge(org.gephi.graph.api.Edge)

Aggregations

GraphElementsController (org.gephi.datalab.api.GraphElementsController)16 Node (org.gephi.graph.api.Node)5 Column (org.gephi.graph.api.Column)4 Table (org.gephi.graph.api.Table)4 DataTablesController (org.gephi.datalab.api.datatables.DataTablesController)3 Edge (org.gephi.graph.api.Edge)3 Graph (org.gephi.graph.api.Graph)3 CsvReader (com.csvreader.CsvReader)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 SearchResult (org.gephi.datalab.api.SearchReplaceController.SearchResult)2 TimeFormat (org.gephi.graph.api.TimeFormat)2 DateTimeZone (org.joda.time.DateTimeZone)2 ArrayList (java.util.ArrayList)1 AddEdgeToGraph (org.gephi.datalab.plugin.manipulators.general.AddEdgeToGraph)1 NotifyDescriptor (org.openide.NotifyDescriptor)1