Class: DirectedGraph::Graph
- Inherits:
-
Object
- Object
- DirectedGraph::Graph
- Defined in:
- lib/directed_graph/graph.rb,
lib/directed_graph/graphml.rb
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
Instance Method Summary collapse
- #children(vertex) ⇒ Object
-
#compute_key(external_identifier) ⇒ Object
Generate a string key from an array of identifiers.
-
#initialize(edges = []) ⇒ Graph
constructor
A new instance of Graph.
- #longest_path(origin_vertex_name, destination_vertex, result = []) ⇒ Object
- #ordered_edges ⇒ Object
- #parents(vertex) ⇒ Object
- #shortest_path(origin_vertex, destination_vertex) ⇒ Object
- #sorted_vertices ⇒ Object
-
#to_graphml ⇒ Object
Return graph as graphml text.
- #vertices ⇒ Object
Constructor Details
#initialize(edges = []) ⇒ Graph
Returns a new instance of Graph.
7 8 9 |
# File 'lib/directed_graph/graph.rb', line 7 def initialize(edges = []) @edges = edges end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
5 6 7 |
# File 'lib/directed_graph/graph.rb', line 5 def edges @edges end |
Instance Method Details
#children(vertex) ⇒ Object
40 41 42 |
# File 'lib/directed_graph/graph.rb', line 40 def children(vertex) edges.select {|e| e.origin_vertex.name == vertex.name}.map{|e| e.destination_vertex} end |
#compute_key(external_identifier) ⇒ Object
Generate a string key from an array of identifiers
7 8 9 10 11 |
# File 'lib/directed_graph/graphml.rb', line 7 def compute_key(external_identifier) x = [external_identifier].flatten x.map! {|xx| xx.to_s} x.join("_") end |
#longest_path(origin_vertex_name, destination_vertex, result = []) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/directed_graph/graph.rb', line 33 def longest_path(origin_vertex_name, destination_vertex, result = []) return [destination_vertex] + result if origin_vertex_name == destination_vertex.name parents(destination_vertex).map do |v| longest_path(origin_vertex_name, v, [destination_vertex] + result) end.inject([]) {|m, arr| m = arr if arr.length > m.length; m} end |
#ordered_edges ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/directed_graph/graph.rb', line 17 def ordered_edges sorted_vertices.inject([]) do |memo, v| edge = edges.select {|e| e.destination_vertex == v} memo.push(*edge) if edge memo end end |
#parents(vertex) ⇒ Object
44 45 46 |
# File 'lib/directed_graph/graph.rb', line 44 def parents(vertex) edges.select {|e| e.destination_vertex.name == vertex.name}.map{|e| e.origin_vertex} end |
#shortest_path(origin_vertex, destination_vertex) ⇒ Object
29 30 31 |
# File 'lib/directed_graph/graph.rb', line 29 def shortest_path(origin_vertex, destination_vertex) simple_graph.shortest_path(origin_vertex, destination_vertex) end |
#sorted_vertices ⇒ Object
25 26 27 |
# File 'lib/directed_graph/graph.rb', line 25 def sorted_vertices JobRunner.sorted_vertices(vertices_and_children) end |
#to_graphml ⇒ Object
Return graph as graphml text
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/directed_graph/graphml.rb', line 14 def to_graphml() builder = Builder::XmlMarkup.new(:indent => 1) builder.instruct! :xml, :version => "1.0" graphml_attribs = { "xmlns" => "http://graphml.graphdrawing.org/xmlns", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:y" => "http://www.yworks.com/xml/graphml", "xmlns:yed" => "http://www.yworks.com/xml/yed/3", "xsi:schemaLocation" => "http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd", :directed => "1", :label => "test" } builder.graphml(graphml_attribs) do # Define key id's at top of graphml file builder.key({:for=>"node", :id=>"d3", "yfiles.type"=>"nodegraphics"}) # Build Graph # builder.graph({:id=>"G"}) do vertices.each do |vertex| builder.node(:id => compute_key([vertex.name, vertex.object_id])) do builder.data({:key=>"d3"}) do builder.tag!("y:ShapeNode") do graphics = vertex.data.fetch(:graphics, {}) graphics.fetch(:fill, []).each {|f| builder.tag! "y:Fill", {:color=>f, :transparent=>"false"}} graphics.fetch(:shape,[]).each {|s| builder.tag! "y:Shape", {:type=>s}} graphics.fetch(:geometry,[]).each {|s| builder.tag! "y:Geometry", s} graphics.fetch(:label,[]).each {|l| builder.tag! "y:NodeLabel", l} end end end end edges.each do |edge| source = edge.origin_vertex target = edge.destination_vertex = edge.data[:options] label = "" builder.edge( :source => s = compute_key([source.name, source.object_id]), :target => t = compute_key([target.name, target.object_id]), :id => compute_key([source.name, target.name, edge.object_id]), :label => "#{label}" ) do #edge[:attributes].each_pair { |k, v| # id_str = compute_key([k,:edge_attr]) # builder.data(v.to_s, {:key=>@guid[id_str]}) #} end end end end builder.target! end |
#vertices ⇒ Object
11 12 13 14 15 |
# File 'lib/directed_graph/graph.rb', line 11 def vertices r = [] edges.each {|e| r.push(e.origin_vertex, e.destination_vertex)} r.uniq {|e| e.object_id} end |