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();
}
}
}
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;
}
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;
}
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);
}
}
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]));
}
Aggregations