lib/jumoku/builders/shared.rb in jumoku-0.2.2 vs lib/jumoku/builders/shared.rb in jumoku-0.2.3

- old
+ new

@@ -19,17 +19,17 @@ # @overload add_node!(n) # @param [node] n # @overload add_node!(b) # @param [Branch] b Branch[node i, node j, label l = nil]; if i (j) already exists, then j (i) must not exist # @return [RawTree] self - def add_node! u, v = nil + def add_node! u, v = nil, l = nil if nodes.empty? - add_vertex! u + add_vertex! u, l elsif u.is_a? _branch_type - add_branch! u + add_branch! u, nil, l elsif not v.nil? - add_branch! u, v + add_branch! u, v, l else # Ensure the connected constraint. raise RawTreeError, "In order to add a node to the tree, you must specify another node to attach to." end end @@ -151,25 +151,23 @@ terminals << node if terminal?(node) terminals end end alias boundaries terminal_nodes - alias leaves terminal_nodes # The branches of the tree in a 1D array. # # @return [Array(Branch)] def branches edges end # Tree helpers. - # Checks whether the tree is *really* a valid tree, that is if the - # following conditions are fulfilled: + # Checks whether the tree is a valid tree (directed or undirected), that is + # if the following conditions are fulfilled: # - # * undirected # * acyclic # * connected # # @return [Boolean] def valid? @@ -178,16 +176,13 @@ # Is the node a terminal node? # # @return [Boolean] def terminal? u - if has_node? u - # root is always terminal, otherwise check whether degree is unitary - nodes == [u] ? true : (degree(u) == 1) - else - raise UndefinedNode, "Not a node of this tree." - end + raise UndefinedNode, "Not a node of this tree." unless has_node? u + return true if (1..2).include? nodes.size + degree(u) == 1 end alias has_terminal_node? terminal? alias leaf? terminal? # Is the tree empty? @@ -208,8 +203,19 @@ # # @return [Tree] cloned tree # def _clone self.class.new(self) + end + + def _extract_strategies(options) + options.inject([]) do |strategies, (k,v)| + begin + strategies << Jumoku.const_get(k.to_s.constantize).const_get(v.to_s.constantize) + strategies + rescue NameError # silently ignored + strategies + end + end end end end