if (columnTypes == null || columnNames.length != columnTypes.length) {
throw new IllegalArgumentException("Column names length must be the same as column types lenght");
}
CsvReader reader = null;
try {
//Prepare attribute columns for the column names, creating the not already existing columns:
AttributeTable edges = Lookup.getDefault().lookup(AttributeController.class).getModel().getEdgeTable();
String idColumn = null;
String sourceColumn = null;
String targetColumn = null;
String typeColumn = null;
ArrayList<AttributeColumn> columnsList = new ArrayList<AttributeColumn>();
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 (columnNames[i].equalsIgnoreCase("source") && sourceColumn == null) {//Separate first source column found from the list to use as source node id
sourceColumn = columnNames[i];
} else if (columnNames[i].equalsIgnoreCase("target") && targetColumn == null) {//Separate first target column found from the list to use as target node id
targetColumn = columnNames[i];
} else if (columnNames[i].equalsIgnoreCase("type") && typeColumn == null) {//Separate first type column found from the list to use as edge type (directed/undirected)
typeColumn = columnNames[i];
} else if (edges.hasColumn(columnNames[i])) {
columnsList.add(edges.getColumn(columnNames[i]));
} else {
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();
for (AttributeColumn column : columnsList) {
setAttributeValue(reader.get(column.getTitle()), edgeAttributes, column);
}
}
}
} catch (FileNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
reader.close();
}
}