private int depth = 1;
public Graph filter(Graph graph) {
GraphView graphView = graph.getView();
HierarchicalGraph mainGraph = graphView.getGraphModel().getHierarchicalGraph();
List<Node> nodes = new ArrayList<Node>();
for (Node n : graph.getNodes()) {
nodes.add(n.getNodeData().getNode(mainGraph.getView().getViewId()));
}
Set<Node> result = new HashSet<Node>();
Set<Node> neighbours = new HashSet<Node>();
neighbours.addAll(nodes);
//Put all neighbors into result
for (int i = 0; i < depth; i++) {
Node[] nei = neighbours.toArray(new Node[0]);
neighbours.clear();
for (Node n : nei) {
//Extract all neighbors of n
for (Node neighbor : mainGraph.getNeighbors(n)) {
if (!result.contains(neighbor)) {
neighbours.add(neighbor);
result.add(neighbor);
}
}
}
if (neighbours.isEmpty()) {
break;
}
}
if (self) {
result.addAll(nodes);
} else {
result.removeAll(nodes);
}
//Update nodes
for (Node node : mainGraph.getNodes().toArray()) {
if (result.contains(node)) {
graph.addNode(node);
} else if(graph.contains(node)) {
graph.removeNode(node);
}
}
//Update edges
for (Node n : graph.getNodes().toArray()) {
Node mainNode = n.getNodeData().getNode(mainGraph.getView().getViewId());
Edge[] edges = mainGraph.getEdges(mainNode).toArray();
for (Edge e : edges) {
if (e.getSource().getNodeData().getNode(graphView.getViewId()) != null
&& e.getTarget().getNodeData().getNode(graphView.getViewId()) != null) {
graph.addEdge(e);
}