Skip to content

Commit

Permalink
Fix #1251 Multiple edges won't be merged
Browse files Browse the repository at this point in the history
  • Loading branch information
eduramiba committed Dec 27, 2016
1 parent b322d1f commit 6b041a4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ public void importCSVToEdgesTable(Graph graph, File file, Character separator, C

int recordNumber = 0;
while (reader.readRecord()) {
String id;
String id = null;
Edge edge = null;
String sourceId, targetId;
Node source, target;
Expand Down Expand Up @@ -815,20 +815,19 @@ public void importCSVToEdgesTable(Graph graph, File file, Character separator, C
//Prepare the correct edge to assign the attributes:
if (idColumnHeader != null) {
id = reader.get(idColumnHeader);
if (id == null || id.isEmpty()) {
if (id == null || id.trim().isEmpty()) {
edge = gec.createEdge(source, target, directed);//id null or empty, assign one
} else {
Edge edgeById = graph.getEdge(id);

if (edgeById == null) {
edge = gec.createEdge(id, source, target, directed);
}
if (edge == null) {//Edge with that id already in graph
edge = gec.createEdge(source, target, directed);
edge = gec.createEdge(id, source, target, directed);//Create edge because no edge with that id exists
}
}
} else {
edge = gec.createEdge(source, target, directed);
if (findEdge(graph, null, source, target, directed) == null) {//Only create if it does not exist
edge = gec.createEdge(source, target, directed);
}
}

if (edge != null) {//Edge could be created because it does not already exist:
Expand All @@ -837,15 +836,7 @@ public void importCSVToEdgesTable(Graph graph, File file, Character separator, C
setAttributeValue(reader.get(columnHeaders.get(column)), edge, column);
}
} else {
edge = graph.getEdge(source, target);
if (edge == null && !directed) {
//Not from source to target but undirected and reverse?
edge = graph.getEdge(target, source);
}

if (edge != null && edge.isDirected() != directed) {
edge = null;//Cannot use it since directedness is different
}
edge = findEdge(graph, id, source, target, directed);

if (edge != null) {
//Increase non dynamic edge weight with specified weight (if specified), else increase by 1:
Expand Down Expand Up @@ -873,8 +864,8 @@ public void importCSVToEdgesTable(Graph graph, File file, Character separator, C
} else {
Logger.getLogger("").log(
Level.WARNING,
"Could not add edge [source = {0}, target = {1}, directed = {2}] to the graph and could not find the existing edge to add its weight",
new Object[]{source.getId(), target.getId(), directed}
"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}
);
}
}
Expand All @@ -893,6 +884,79 @@ public void importCSVToEdgesTable(Graph graph, File file, Character separator, C
}
}

/**
* Finds the same edge (same source, target, and directedness) in the graph. If directed = false (undirected), it finds the reversed undirected edge too.
*
* @param graph Graph
* @param id Optional id, to enforce the edge id to match too
* @param source Source node
* @param target Target node
* @param directed Directedness of the edge to find
* @return The found edge or null if not found
*/
private Edge findEdge(Graph graph, String id, Node source, Node target, boolean directed) {
Edge edge = null;
if (id != null) {
//Try to find same edge with same id, if the id is provided:
edge = graph.getEdge(id);

boolean sameEdgeDefinition = true;

if (edge.isDirected() != directed) {
sameEdgeDefinition = false;
} else {
if (directed) {
if (edge.getSource() != source || edge.getTarget() != target) {
sameEdgeDefinition = false;
}
} else {
if (edge.getSource() == source) {
if (edge.getTarget() != target) {
sameEdgeDefinition = false;
}
} else if (edge.getTarget() == source) {
if (edge.getSource() != target) {
sameEdgeDefinition = false;
}
} else {
//Edge data is different even when the id coincides:
sameEdgeDefinition = false;
}
}
}

if (!sameEdgeDefinition) {
Logger.getLogger("").log(
Level.WARNING,
"Found edge with correct id = {0} but different definition (wanted = [source = {1}, target = {2}, directed = {3}]; found = [source = {4}, target = {5}, directed = {6}]). Cannot use this edge",
new Object[]{
id,
source.getId(), target.getId(), directed,
edge.getSource().getId(), edge.getTarget().getId(), edge.isDirected()
}
);
//Edge data is different even when the id coincides:
edge = null;
}
} else {
//Find a similar edge with any id:
if (edge == null) {
edge = graph.getEdge(source, target);
}

if (edge == null && !directed) {
//Not from source to target but undirected and reverse?
edge = graph.getEdge(target, source);
}

if (edge != null && edge.isDirected() != directed) {
edge = null;//Cannot use it since directedness is different
}
}

return edge;
}

@Override
public void mergeRowsValues(Column[] columns, AttributeRowsMergeStrategy[] mergeStrategies, Element[] rows, Element selectedRow, Element resultRow) {
if (columns.length != mergeStrategies.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public Edge createEdge(String id, Node source, Node target, boolean directed, Ob
} catch (Exception e) {
Logger.getLogger("").log(
Level.SEVERE,
"Error when adding edge [source = {0}, target = {1}, directed = {2}, typeLabel = {3}] to the graph. Exception message: {4}",
new Object[]{source.getId(), target.getId(), directed, typeLabel, e.getMessage()}
"Error when adding edge [id = {0}, source = {1}, target = {2}, directed = {3}, typeLabel = {4}] to the graph. Exception message: {5}",
new Object[]{id, source.getId(), target.getId(), directed, typeLabel, e.getMessage()}
);
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion modules/DataLaboratoryAPI/src/main/nbm/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
AutoUpdate-Essential-Module: true
OpenIDE-Module-Localizing-Bundle: org/gephi/datalab/api/Bundle.properties
OpenIDE-Module-Specification-Version: 0.9.1.2
OpenIDE-Module-Specification-Version: 0.9.1.3
OpenIDE-Module-Display-Category: Gephi Core
OpenIDE-Module-Name: Data Laboratory API

0 comments on commit 6b041a4

Please sign in to comment.