use of org.gephi.datalab.api.GraphElementsController in project gephi by gephi.
the class Free method execute.
@Override
public void execute() {
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
gec.setNodesFixed(nodes, false);
Lookup.getDefault().lookup(DataTablesController.class).refreshCurrentTable();
}
use of org.gephi.datalab.api.GraphElementsController in project gephi by gephi.
the class MergeNodes method execute.
@Override
public void execute() {
Graph graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraph();
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
Node newNode = gec.mergeNodes(graph, nodes, selectedNode, columns, mergeStrategies, deleteMergedNodes);
Lookup.getDefault().lookup(DataTablesController.class).setNodeTableSelection(new Node[] { newNode });
NbPreferences.forModule(MergeNodes.class).putBoolean(DELETE_MERGED_NODES_SAVED_PREFERENCES, deleteMergedNodes);
}
use of org.gephi.datalab.api.GraphElementsController in project gephi by gephi.
the class DeleteEdges method execute.
@Override
public void execute() {
if (JOptionPane.showConfirmDialog(null, NbBundle.getMessage(DeleteEdges.class, "DeleteEdges.confirmation.message"), getName(), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
gec.deleteEdges(edges);
}
}
use of org.gephi.datalab.api.GraphElementsController in project gephi by gephi.
the class DeleteEdgesWithNodes method execute.
@Override
public void execute() {
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
gec.deleteEdgesWithNodes(edges, deleteSource, deleteTarget);
}
use of org.gephi.datalab.api.GraphElementsController 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();
}
}
}
Aggregations