public Graph filter(Graph[] graphs) { HierarchicalGraph minHGraph = (HierarchicalGraph) graphs[0]; int minElements = Integer.MAX_VALUE; for (int i = 0; i < graphs.length; i++) { int count = ((HierarchicalGraph) graphs[i]).getNodeCount(); if (count < minElements) { minHGraph = (HierarchicalGraph) graphs[i]; minElements = count; } } for (Node n : minHGraph.getNodes().toArray()) { for (int i = 0; i < graphs.length; i++) { if ((HierarchicalGraph) graphs[i] != minHGraph) { if (!((HierarchicalGraph) graphs[i]).contains(n)) { minHGraph.removeNode(n); break; } } } } for (Edge e : minHGraph.getEdges().toArray()) { for (int i = 0; i < graphs.length; i++) { if ((HierarchicalGraph) graphs[i] != minHGraph) { if (!((HierarchicalGraph) graphs[i]).contains(e)) { minHGraph.removeEdge(e); break; } } } } return minHGraph; }
public Graph filter(Graph graph, Filter[] filters) { HierarchicalGraph hgraph = (HierarchicalGraph) graph; List<NodeFilter> nodeFilters = new ArrayList<NodeFilter>(); List<EdgeFilter> edgeFilters = new ArrayList<EdgeFilter>(); for (Filter f : filters) { if (f instanceof NodeFilter) { nodeFilters.add((NodeFilter) f); } else if (f instanceof EdgeFilter) { edgeFilters.add((EdgeFilter) f); } } if (nodeFilters.size() > 0) { for (Iterator<NodeFilter> itr = nodeFilters.iterator(); itr.hasNext(); ) { NodeFilter nf = itr.next(); if (!nf.init(hgraph)) { itr.remove(); } } List<Node> nodesToRemove = new ArrayList<Node>(); for (Node n : hgraph.getNodes()) { for (NodeFilter nf : nodeFilters) { if (!nf.evaluate(hgraph, n)) { nodesToRemove.add(n); break; } } } for (Node n : nodesToRemove) { hgraph.removeNode(n); } for (NodeFilter nf : nodeFilters) { nf.finish(); } } if (edgeFilters.size() > 0) { for (Iterator<EdgeFilter> itr = edgeFilters.iterator(); itr.hasNext(); ) { EdgeFilter ef = itr.next(); if (!ef.init(hgraph)) { itr.remove(); } } List<Edge> edgesToRemove = new ArrayList<Edge>(); for (Edge e : hgraph.getEdges()) { for (EdgeFilter ef : edgeFilters) { if (!ef.evaluate(hgraph, e)) { edgesToRemove.add(e); break; } } } for (Edge e : edgesToRemove) { hgraph.removeEdge(e); } for (EdgeFilter ef : edgeFilters) { ef.finish(); } } return hgraph; }