how to filter edges based on weight (or other property) with Jung and display the new network

StackOverflow https://stackoverflow.com/questions/23338098

  •  10-07-2023
  •  | 
  •  

Question

I have an SparseMultigraph< Node, Edge > g where edges have two attributes:

int weight;
ArrayList<Date> time;

I would like to filter the graph according to weight and time independently. Say I start filtering out according to weight using a JSlider called weightSlider (I am in Netbeans and Swing):

 private void weightSliderMouseReleased(java.awt.event.MouseEvent evt) {                                           
    // filter network according to weight:
    Predicate<Edge> edgeAboveWeight = new Predicate<Edge>() {
        @Override
        public boolean evaluate(Edge e) {
            return e.getWeight() >= weightSlider.getValue();
        }
    };
    EdgePredicateFilter<Node, Edge> edgePredicateFilter = new EdgePredicateFilter<>(edgeAboveWeight);
    Graph<Node, Edge> transform = edgePredicateFilter.transform(g);
}         

My question is: how do I push the new graph in the visualization, preserving the node position?

Since it is the first time I attempt to do something like that, I might have missed a simpler way to achieve my goal, so any suggestion is more than welcome!

EDIT: I succeeded to implement this with:

private void weightSliderMouseReleased(java.awt.event.MouseEvent evt) {                                           
    // filter network according to weight:
    Predicate<Edge> edgeAboveWeight = new Predicate<Edge>() {
        @Override
        public boolean evaluate(Edge e) {
            return e.getWeight() >= weightSlider.getValue();
        }
    };
    EdgePredicateFilter<Node, Edge> edgePredicateFilter = new EdgePredicateFilter<>(edgeAboveWeight);
    Graph<Node, Edge> transform = edgePredicateFilter.transform(g);
    layout.setGraph(transform);
    vv.validate();
    vv.repaint();
}        

Still don't know if it's the most efficient way, but it works...

Était-ce utile?

La solution

If you want to filter the actual data, then what you're doing is fine.

If what you want to do is just filter the edges whose weight is below a certain value from being rendered, then you can tell the PluggableRendererContext that directly:

http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/visualization/PluggableRenderContext.html#setEdgeIncludePredicate(org.apache.commons.collections15.Predicate)

This is demonstrated in PluggableRendererDemo.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top