Class: Tree
- Inherits:
-
Object
- Object
- Tree
- Defined in:
- lib/rdtree.rb,
lib/rdtree2rd.rb,
lib/rdtree2dot.rb,
lib/generic/tree.rb
Instance Method Summary collapse
- #add_edge(from, to) ⇒ Object
- #add_node(id, value) ⇒ Object
- #branch?(id) ⇒ Boolean
- #children(id) ⇒ Object
- #depth(id) ⇒ Object
-
#initialize ⇒ Tree
constructor
A new instance of Tree.
- #leaf?(id) ⇒ Boolean
- #list_depth_traverse(under) ⇒ Object
-
#not_found_link2text! ⇒ Object
NOTE: About Design.
- #parent(id) ⇒ Object
-
#rationalize! ⇒ Object
NOTE: Crazy Design Akira Hayakawa, Aug 23, 2011 This method must be called wherever it needs the rationalized state.
- #root_id ⇒ Object
- #size ⇒ Object
- #to_csv ⇒ Object
- #to_dot ⇒ Object
- #to_rd ⇒ Object
- #update_value(id, value) ⇒ Object
- #value(id) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Tree
Returns a new instance of Tree.
2 3 4 5 6 |
# File 'lib/generic/tree.rb', line 2 def initialize @values = [] @edges = [] @parents = [] end |
Instance Method Details
#add_edge(from, to) ⇒ Object
59 60 61 62 |
# File 'lib/generic/tree.rb', line 59 def add_edge(from, to) register_edge(from, to) register_parent(to, from) end |
#add_node(id, value) ⇒ Object
48 49 50 |
# File 'lib/generic/tree.rb', line 48 def add_node(id, value) @values[id] = value end |
#branch?(id) ⇒ Boolean
68 69 70 |
# File 'lib/generic/tree.rb', line 68 def branch?(id) ! leaf?(id) end |
#children(id) ⇒ Object
44 45 46 |
# File 'lib/generic/tree.rb', line 44 def children(id) @edges[id] end |
#depth(id) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/generic/tree.rb', line 26 def depth(id) d = 0 i = id until( i==root_id ) i = parent(i) d += 1 end d end |
#leaf?(id) ⇒ Boolean
64 65 66 |
# File 'lib/generic/tree.rb', line 64 def leaf?(id) @edges[id] == nil end |
#list_depth_traverse(under) ⇒ Object
8 9 10 11 12 |
# File 'lib/generic/tree.rb', line 8 def list_depth_traverse(under) list = [] do_list_depth_traverse(under, list) return list end |
#not_found_link2text! ⇒ Object
NOTE: About Design. In current design, a link node that can not find the tag node it points to is treated as text node.
10 11 12 13 14 15 16 |
# File 'lib/rdtree.rb', line 10 def not_found_link2text! list_link_nodes.each do |id| if can_not_find_tag(id) modify_link2text(id) end end end |
#parent(id) ⇒ Object
22 23 24 |
# File 'lib/generic/tree.rb', line 22 def parent(id) @parents[id] end |
#rationalize! ⇒ Object
NOTE: Crazy Design Akira Hayakawa, Aug 23, 2011 This method must be called wherever it needs the rationalized state. For exmaple, a Link node that can not find the tag in the tree should be altered to Text node. But, this is innately a bad design. Because of my incapable of not coming up with a good idea to solve this issue elegantly. The version 2.0.2 scattered this method to to_csv and to_dot. But I do not know if this is sufficient. To solve this issue theoritically, embed this method into every single mutate methods of the instance mattered.
If you embed this method, please leave comment “# CRAZY” there so that you can remove after I find a better solution.
A solution is to simplify the design that lets every paths through RDTree structure. Then call rationalize only one time before it is needed, case of conversion for example.
33 34 35 |
# File 'lib/rdtree.rb', line 33 def rationalize! not_found_link2text! end |
#root_id ⇒ Object
36 37 38 |
# File 'lib/generic/tree.rb', line 36 def root_id 0 end |
#size ⇒ Object
18 19 20 |
# File 'lib/generic/tree.rb', line 18 def size @values.size end |
#to_csv ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rdtree.rb', line 37 def to_csv rationalize! # CRAZY m = tree2matrix(self) range = [ 0...m.m_size, 1...m.n_size] c = matrix2csv(m, range) do |n| s = nil if n == nil s = "" else s = n.to_csv end s end c end |
#to_dot ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rdtree2dot.rb', line 5 def to_dot rationalize! # CRAZY elems = [] for i in 0...size unless link_node?(i) elems << dot_node_desc(i) end end linkmap = mk_id2id for i in 0...size if link_node?(i) to = linkmap[i] from = parent(i) elems << dot_edge_desc(from, to) next end if leaf?(i) next end children(i).each do |child| elems << dot_edge_desc(i, child) unless link_node?(child) end end # NOTE: The ratio of the output figure is 1 at default. # I/F to change the ratio is the future work. """ digraph graphname { graph [ratio = 1] #{elems.join("\n ")} } """ end |
#to_rd ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/rdtree2rd.rb', line 8 def to_rd nodes = list_depth_traverse(root_id) # NOTE: Eliminate the DummyRoot from rd file. nodes.delete(root_id) nodes.map do |node| value(node).to_rd end.join("\n") end |
#update_value(id, value) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/generic/tree.rb', line 52 def update_value(id, value) # In current version, # update_value is just a wrapper of add_node. # but in future version, this may need modification. add_node(id, value) end |
#value(id) ⇒ Object
40 41 42 |
# File 'lib/generic/tree.rb', line 40 def value(id) @values[id] end |
#values ⇒ Object
14 15 16 |
# File 'lib/generic/tree.rb', line 14 def values @values end |