Sha256: e2f1f92c6bf5424ffb4ba60e8c7c3d472c60fec4b395376c7888d11f0878a5ed

Contents?: true

Size: 1.06 KB

Versions: 11

Compression:

Stored size: 1.06 KB

Contents

require 'rgl/traversal'
require 'rgl/implicit'

class GraphFilter
  def initialize(graph)
    @graph = graph
  end

  def filter(source, destination)
    return @graph if source.nil? && destination.nil?
    if !source.nil? && destination.nil?
      return reduce(@graph, source)
    end
    if !destination.nil? && source.nil?
      return reduce(@graph.reverse, destination).reverse
    end
    search(@graph, source, destination)
  end

  private
  def reduce(graph, source)
    tree = graph.bfs_search_tree_from(source)
    graph.vertices_filtered_by { |v| tree.has_vertex? v }
  end

  def search(graph, source, destination)
    visitor = RGL::DFSVisitor.new(graph)
    path = []
    paths = []
    visitor.set_examine_vertex_event_handler { |x| path << x }
    visitor.set_finish_vertex_event_handler { |x| path.pop }
    visitor.set_examine_edge_event_handler { |x, y| paths << path.dclone + [y] if y == destination }
    graph.depth_first_visit(source, visitor) { |x|}
    to_remove = graph.vertices - paths.flatten
    graph.remove_vertices(*to_remove)
    graph
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
aws_security_viz-0.1.5.pre.alpha.pre.94 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.93 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.91 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.90 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.89 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.87 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.85 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.84 lib/graph_filter.rb
aws_security_viz-0.1.5.pre.alpha.pre.83 lib/graph_filter.rb
aws_security_viz-0.1.4 lib/graph_filter.rb
aws_security_viz-0.1.4.pre.alpha.pre.82 lib/graph_filter.rb