lib/ruote/util/tree.rb in ruote-2.2.0 vs lib/ruote/util/tree.rb in ruote-2.3.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -45,14 +45,76 @@
# # 0 define {"name"=>"def0"}
# # 0_0 sequence {}
# # 0_0_0 alpha {}
# # 0_0_1 bravo {}
#
- def Ruote.tree_to_s(tree, expid='0')
+ def self.tree_to_s(tree, expid='0')
d = expid.split('_').size - 1
s = "#{' ' * d * 2}#{expid} #{tree[0]} #{tree[1].inspect}\n"
tree[2].each_with_index { |t, i| s << tree_to_s(t, "#{expid}_#{i}") }
s
+ end
+
+ # Used by Ruote::ProcessStatus.
+ #
+ # Given a tree
+ #
+ # [ 'define', { 'name' => 'nada' }, [
+ # [ 'sequence', {}, [ [ 'alpha', {}, [] ], [ 'bravo', {}, [] ] ] ]
+ # ] ]
+ #
+ # will output something like
+ #
+ # { '0' => [ 'define', { 'name' => 'nada' } ],
+ # '0_0' => [ 'sequence', {} ],
+ # '0_0_0' => [ 'alpha', {} ],
+ # '0_0_1' => [ 'bravo', {} ] },
+ #
+ # An initial offset can be specifid with the 'pos' argument.
+ #
+ # Don't touch 'h', it's an accumulator.
+ #
+ def self.decompose_tree(t, pos='0', h={})
+
+ h[pos] = t[0, 2]
+ t[2].each_with_index { |c, i| decompose_tree(c, "#{pos}_#{i}", h) }
+ h
+ end
+
+ # Used by Ruote::ProcessStatus.
+ #
+ # Given a decomposed tree like
+ #
+ # { '0' => [ 'define', { 'name' => 'nada' } ],
+ # '0_0' => [ 'sequence', {} ],
+ # '0_0_0' => [ 'alpha', {} ],
+ # '0_0_1' => [ 'bravo', {} ] },
+ #
+ # will recompose it to
+ #
+ # [ 'define', { 'name' => 'nada' }, [
+ # [ 'sequence', {}, [ [ 'alpha', {}, [] ], [ 'bravo', {}, [] ] ] ]
+ # ] ]
+ #
+ # A starting point in the recomposition can be given with the 'pos' argument.
+ #
+ def self.recompose_tree(h, pos='0')
+
+ t = h[pos]
+
+ return nil unless t
+
+ t << []
+ i = 0
+
+ loop do
+ tt = recompose_tree(h, "#{pos}_#{i}")
+ break unless tt
+ t.last << tt
+ i = i + 1
+ end
+
+ t
end
end