Method: Pacer::Core::Graph::PathRoute#subgraph

Defined in:
lib/pacer/core/graph/path_route.rb

#subgraph(target_graph = nil, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pacer/core/graph/path_route.rb', line 31

def subgraph(target_graph = nil, opts = {})
  raise "Can't create a subgraph within itself." if target_graph == graph
  target_graph ||= Pacer.tg
  target_graph.vertex_name ||= graph.vertex_name
  missing_edges = Set[]
  bulk_job(nil, target_graph) do |path|
    path.select { |e| e.is_a? Pacer::Vertex }.each do |vertex|
      vertex.clone_into target_graph
    end
    path.select { |e| e.is_a? Pacer::Edge }.each do |edge|
      unless edge.clone_into target_graph, ignore_missing_vertices: true
        missing_edges << edge
      end
    end
  end
  if missing_edges.any?
    missing_edges.to_route(graph: graph, element_type: :edge).bulk_job nil, target_graph do |edge|
      edge.clone_into target_graph,
        ignore_missing_vertices: opts[:ignore_missing_vertices],
        show_missing_vertices: opts[:show_missing_vertices]
    end
  end
  target_graph
end