lib/dr/base/graph.rb in drain-0.3.0 vs lib/dr/base/graph.rb in drain-0.4
- old
+ new
@@ -5,11 +5,11 @@
class Node
include Enumerable
attr_reader :graph
attr_accessor :name, :attributes, :parents, :children
def initialize(name, attributes: {}, graph: nil)
- @name = name.to_s
+ @name = name
@children = []
@parents = []
@attributes = attributes
@graph=graph
graph.nodes << self if @graph
@@ -76,11 +76,11 @@
end
end
STEP = 4
def to_s(show_attr: true)
- @name + (show_attr && ! attributes.empty? ? " #{attributes}" : "")
+ @name.to_s + (show_attr && ! attributes.empty? ? " #{attributes}" : "")
end
def inspect
"#{self.class}: #{to_s(show_attr: true)}"+(graph.nil? ? "" : " (#{graph})")
end
# output like a graph
@@ -105,14 +105,16 @@
end
class Graph
attr_accessor :nodes
include Enumerable
+ # note: passing a graph won't work
def initialize(*nodes, attributes: {}, infos: nil)
@nodes=[]
# a node can be a Hash or a Node
- build(*nodes, attributes: {}, infos: infos)
+ # so nodes really is a list of subgraphs
+ build(*nodes, attributes: attributes, infos: infos)
end
def each(&b)
@nodes.each(&b)
end
@@ -121,10 +123,14 @@
end
def to_a
return @nodes
end
+ def names
+ @nodes.map(&:name)
+ end
+
def to_hash(methods: [:children,:parents,:attributes], compact: true, recursive: true)
require 'dr/base/converter'
Converter.to_hash(@nodes, methods: methods, recursive: recursive, compact: compact)
end
@@ -140,19 +146,22 @@
end
def to_h
h=to_hash(methods: [:children])
Hash[h.map {|k,v| [k.name, v.map(&:name)]}]
end
- alias to_children to_h
+ #alias to_children to_h
def to_children
require 'dr/base/converter'
Converter.to_hash(@nodes, methods:[:children], recursive: true, compact: true).map { |k,v| [k.name, v.map(&:name)]}.to_h
end
def inspect
- "#{self.class}: #{map {|x| x.to_s}}"
+ "#{self.class}: #{map {|x| x.to_s(show_attr: false)}}"
end
+ # def to_s
+ # "#{self.class}: #{map {|x| x.to_s(show_attr: false)}}"
+ # end
#construct a node (without edges)
def new_node(node,**attributes)
n=case node
when Node