lib/ruote/util/tree.rb in ruote-2.3.0.1 vs lib/ruote/util/tree.rb in ruote-2.3.0.2

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2013, 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 @@ -36,11 +36,14 @@ # bravo # end # end # # p pdef - # # => ["define", {"name"=>"def0"}, [["sequence", {}, [["alpha", {}, []], ["bravo", {}, []]]]]] + # # => ["define", {"name"=>"def0"}, [ + # # ["sequence", {}, [ + # # ["alpha", {}, []], + # # ["bravo", {}, []]]]]] # # puts Ruote.tree_to_s(pdef) # # => # # 0 define {"name"=>"def0"} # # 0_0 sequence {} @@ -53,12 +56,48 @@ 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. + # Compacts # + # [ 'participant', { 'ref' => 'sam' }, [] ] # and + # [ 'subprocess', { 'ref' => 'compute_prime' }, [] ] + # + # into + # + # [ 'sam', {}, [] ] # and + # [ 'compute_prime', {}, [] ] + # + # to simplify tree comparisons. + # + def self.compact_tree(tree) + + tree = tree.dup + + if %w[ participant subprocess ].include?(tree[0]) + + ref = + tree[1].delete('ref') || + begin + kv = tree[1].find { |k, v| v == nil } + tree[1].delete(kv[0]) + kv[0] + end + + tree[0] = ref + + else + + tree[2] = tree[2].collect { |t| compact_tree(t) } + end + + tree + end + + # Used by some projects, used to be called from Ruote::ProcessStatus. + # # Given a tree # # [ 'define', { 'name' => 'nada' }, [ # [ 'sequence', {}, [ [ 'alpha', {}, [] ], [ 'bravo', {}, [] ] ] ] # ] ] @@ -79,10 +118,10 @@ h[pos] = t[0, 2] t[2].each_with_index { |c, i| decompose_tree(c, "#{pos}_#{i}", h) } h end - # Used by Ruote::ProcessStatus. + # Used by some projects, used to be called from Ruote::ProcessStatus. # # Given a decomposed tree like # # { '0' => [ 'define', { 'name' => 'nada' } ], # '0_0' => [ 'sequence', {} ],