Hi,
I have a graph that contains duplicate links (please don't ask me why....) and JUNG gave me this error when I try to add the edge:
Exception in thread "main" java.lang.IllegalArgumentException: Predicate org.apache.commons.collections.functors.NotPredicate rejected E81(V8,V60): org.apache.commons.collections.functors.NotPredicate@a97b0b
at edu.uci.ics.jung.graph.impl.AbstractArchetypeGraph.validate(AbstractArchetypeGraph.java:188)
at edu.uci.ics.jung.graph.impl.AbstractSparseGraph.addEdge(AbstractSparseGraph.java:136)
Is there a way in JUNG to detect the duplicate links and prevent this exception from happening? Of course, I could write my own code to detect that but I wonder if there is already such thing in JUNG. Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you Fisher. Actually, I just thought of another answer myself. I used a try Iand catch for IllegalArgumentException so the program doesn't quit. But I guess that your solution would be cleaner. Mine is not clean but maybe somewhat faster :-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Actually, I suspect that Danyel's solution is faster. If the "no parallel edges" constraint is in place, the addEdge() call has to do the equivalent of the findEdge() check, and also does some other things (e.g., checks other constraints). Just FYI. :)
Joshua
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
By the way, it wasn't clear from this original message whether (1) your data really included parallel edges, or (2) the data was noisy such that you occasionally accidentally created a duplicate edge.
If (1), then you can create graphs in JUNG with parallel edges; just use SparseGraph (which has no edge directionality or parallelism constraints), or remove the "no parallel edges" constraint from the graph implementation you're currently using. Let us know if you need help with this.
If (2), then the solutions already mentioned here will work fine.
Regards,
Joshua
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A final note on this: if you already have references to the vertex endpoints, rather than using findEdge(), isPredecessorOf is cleaner for this purpose:
if (! v1.isPredecessorOf(v2))
g.addEdge(new DirectedSparseEdge(v1,v2));
Or, if for whatever reason, you've already created the edge e (but not added it to the graph, because you're not sure whether it has a parallel), you could simply use the built-in parallel edge predicate:
if (Graph.NOT_PARALLEL_EDGE.evaluate(e))
g.addEdge(e);
Joshua
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have a graph that contains duplicate links (please don't ask me why....) and JUNG gave me this error when I try to add the edge:
Exception in thread "main" java.lang.IllegalArgumentException: Predicate org.apache.commons.collections.functors.NotPredicate rejected E81(V8,V60): org.apache.commons.collections.functors.NotPredicate@a97b0b
at edu.uci.ics.jung.graph.impl.AbstractArchetypeGraph.validate(AbstractArchetypeGraph.java:188)
at edu.uci.ics.jung.graph.impl.AbstractSparseGraph.addEdge(AbstractSparseGraph.java:136)
Is there a way in JUNG to detect the duplicate links and prevent this exception from happening? Of course, I could write my own code to detect that but I wonder if there is already such thing in JUNG. Thanks.
Yes! Consider code like this:
Vertex v1 = calculate_front( hypothetical_edge );
Vertex v2 = calculate_back( hypothetical_edge );
if( v1.findEdge( v2 )) != null ) {
g.addEdge( hypothetical_edge );
}
Thank you Fisher. Actually, I just thought of another answer myself. I used a try Iand catch for IllegalArgumentException so the program doesn't quit. But I guess that your solution would be cleaner. Mine is not clean but maybe somewhat faster :-)
Actually, I suspect that Danyel's solution is faster. If the "no parallel edges" constraint is in place, the addEdge() call has to do the equivalent of the findEdge() check, and also does some other things (e.g., checks other constraints). Just FYI. :)
Joshua
By the way, it wasn't clear from this original message whether (1) your data really included parallel edges, or (2) the data was noisy such that you occasionally accidentally created a duplicate edge.
If (1), then you can create graphs in JUNG with parallel edges; just use SparseGraph (which has no edge directionality or parallelism constraints), or remove the "no parallel edges" constraint from the graph implementation you're currently using. Let us know if you need help with this.
If (2), then the solutions already mentioned here will work fine.
Regards,
Joshua
A final note on this: if you already have references to the vertex endpoints, rather than using findEdge(), isPredecessorOf is cleaner for this purpose:
if (! v1.isPredecessorOf(v2))
g.addEdge(new DirectedSparseEdge(v1,v2));
Or, if for whatever reason, you've already created the edge e (but not added it to the graph, because you're not sure whether it has a parallel), you could simply use the built-in parallel edge predicate:
if (Graph.NOT_PARALLEL_EDGE.evaluate(e))
g.addEdge(e);
Joshua