Class: Ruleby::Ferrari::RulesContainer

Inherits:
Array
  • Object
show all
Defined in:
lib/dsl/ferrari.rb

Instance Method Summary collapse

Instance Method Details

#build(name, options, engine, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dsl/ferrari.rb', line 120

def build(name,options,engine,&block)
  rules = []        
  self.each do |x|          
    x.process_tree do |c|
      transform_or(c)     
    end
  end
  handle_branching(self).each do |a|
    rules << build_rule(name, a, options, &block)
  end
  return rules
end

#build_rule(name, container, options, &block) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/dsl/ferrari.rb', line 133

def build_rule(name, container, options, &block)
  r = RuleBuilder.new name
  container.build r
  r.then(&block)
  r.priority = options[:priority] if options[:priority]
  r.build_rule
end

#handle_branching(container) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dsl/ferrari.rb', line 102

def handle_branching(container)
  ands = []
  container.each do |x|
    if x.or?
      x.each do |branch|
        ands << branch
      end
    elsif x.and?
      ands << x
    else
      new_and = Container.new(:and)
      new_and << x
      ands << new_and
    end   
  end
  return ands
end

#transform_or(parent) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dsl/ferrari.rb', line 56

def transform_or(parent)
  ors = []
  others = []
  permutations = 1
  index = 0
  parent.each do |child|
    if(child.or?)
      permutations *= child.size
      ors << child
    else
      others[index] = child
    end
    index = index + 1
  end
  # set parent type to or and clear
  parent.kind = :or
  parent.clear
  indexes = []
  # initialize indexes
  ors.each do |o|
    indexes << 0
  end
  # create children
  (1.upto(permutations)).each do |i|
    and_container = Container.new(:and)
    
    mod = 1
    (ors.size - 1).downto(0) do |j|
      and_container.insert(0,ors[j][indexes[j]])
      if((i % mod) == 0)
        indexes[j] = (indexes[j] + 1) % ors[j].size
      end
      mod *= ors[j].size
    end
    
    others.each_with_index do |other, k|
      if others[k] != nil
        and_container.insert(k, others[k])
      end
    end  
    # add child to parent        
    parent.push(and_container)         
  end
  parent.uniq!
end