columnsList.add(addAttributeColumn(edges, columnNames[i], columnTypes[i]));
}
}
//Create edges:
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
Graph graph = Lookup.getDefault().lookup(GraphController.class).getModel().getGraph();
String id = null;
Edge edge;
String sourceId, targetId;
Node source, target;
String type;
boolean directed;
Attributes edgeAttributes;
reader = new CsvReader(new FileInputStream(file), separator, charset);
reader.readHeaders();
while (reader.readRecord()) {
sourceId = reader.get(sourceColumn);
targetId = reader.get(targetColumn);
if (sourceId == null || sourceId.isEmpty() || targetId == null || targetId.isEmpty()) {
continue;//No correct source and target ids were provided, ignore row
}
graph.readLock();
source = graph.getNode(sourceId);
graph.readUnlock();
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);
}
} else {
continue;//Ignore this edge row, since no new nodes should be created.
}
}
graph.readLock();
target = graph.getNode(targetId);
graph.readUnlock();
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);
}
} else {
continue;//Ignore this edge row, since no new nodes should be created.
}
}
if (typeColumn != null) {
type = reader.get(typeColumn);
//Undirected if indicated correctly, otherwise always directed:
if (type != null) {
directed = !type.equalsIgnoreCase("undirected");
} else {
directed = true;
}
} else {
directed = true;//Directed by default when not indicated
}
//Prepare the correct edge to assign the attributes:
if (idColumn != null) {
id = reader.get(idColumn);
if (id == null || id.isEmpty()) {
edge = gec.createEdge(source, target, directed);//id null or empty, assign one
} else {
edge = gec.createEdge(id, source, target, directed);
if (edge == null) {//Edge with that id already in graph
edge = gec.createEdge(source, target, directed);
}
}
} else {
edge = gec.createEdge(source, target, directed);
}
if (edge != null) {//Edge could be created because it does not already exist:
//Assign attributes to the current edge:
edgeAttributes = edge.getEdgeData().getAttributes();