Class: CML::LogicTree::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/cml/logic_tree/solver.rb

Overview

A utility to solve the logic structures provided by the logic tree.

Class Method Summary collapse

Class Method Details

.recurse(dependencies, dependent_name, &block) ⇒ Object

Traverse the tree in logic order, until a solution is found.

A block is required to process each node, returning nil/false or a solution.

Returns one value for a single-OR solution or an array for multiple-AND solutions.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cml/logic_tree/solver.rb', line 14

def self.recurse(dependencies, dependent_name, &block)
  solution = nil
  dependencies.each do |k,v|
    s = case k
    when '||'
      ss = nil
      v.detect {|vv| ss = recurse(vv, dependent_name, &block) }
      ss if ss
    when '&&'
      catch(:unfullfilled_and) do
        v.collect do |vv| 
          rr = recurse(vv, dependent_name, &block)
          throw(:unfullfilled_and) unless rr 
          rr
        end
      end
    else
      yield(dependent_name, k, v[0]) # for a non-boolean node, use the first & only value
    end
    if s
      solution = s
      break
    end
  end
  solution
end