Class: SwitchInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/whatnot/switch_interpreter.rb

Defined Under Namespace

Classes: NameCollisionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSwitchInterpreter

Returns a new instance of SwitchInterpreter.



10
11
12
13
14
15
16
# File 'lib/whatnot/switch_interpreter.rb', line 10

def initialize
  Switch.class_variable_set(:@@all, {})
  Switch.class_variable_set(:@@next_number, 1)
  @slot_groups = {}
  @set_groups  = {}
  @non_interpreted_groups = {}
end

Instance Attribute Details

#non_interpreted_groupsObject

Returns the value of attribute non_interpreted_groups.



8
9
10
# File 'lib/whatnot/switch_interpreter.rb', line 8

def non_interpreted_groups
  @non_interpreted_groups
end

#set_groupsObject

Returns the value of attribute set_groups.



8
9
10
# File 'lib/whatnot/switch_interpreter.rb', line 8

def set_groups
  @set_groups
end

#slot_groupsObject

Returns the value of attribute slot_groups.



8
9
10
# File 'lib/whatnot/switch_interpreter.rb', line 8

def slot_groups
  @slot_groups
end

Instance Method Details

#create_constraint(*slotnames) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/whatnot/switch_interpreter.rb', line 146

def create_constraint(*slotnames)
  solutions_to_try = nil

  slotnames.each do |groupname|
    group = interpreted_groups[groupname]

    if group.nil?
      group = @non_interpreted_groups[groupname]
    end

    raise "can't find group #{groupname}" if group.nil?

    # get possible solutions for each group
    group_solutions = group.possibilities

    merge_possibilities = -> (p1, p2) do
      Hash[p1.to_a.product(p2.to_a).map { |prod| [prod.map(&:first).inject(&:merge), prod.map(&:last).inject("") { |memo, d| memo + d[0..-2] } + "0"] }]
    end

    if solutions_to_try
      solutions_to_try = merge_possibilities.call(solutions_to_try, group_solutions)
    else
      solutions_to_try = group_solutions
    end
  end

  # get source of constraint in user code
  trace = Thread.current.backtrace
  interpreter_lines = []
  trace.each.with_index do |line, ix|
    if line.match(/switch_interpreter.rb/)
      interpreter_lines << ix
    end
  end
  line_of_caller = trace[interpreter_lines.max + 1].split("/").last
  failed_solution_strings = []

  solutions_to_try.each do |solution, solution_str|
    result = yield(**solution)

    if !result
      failed_solution_strings << solution_str
    end
  end

  if constraint_group = @non_interpreted_groups[line_of_caller]
    constraint_group.failed_solutions += failed_solution_strings
  else
    @non_interpreted_groups[line_of_caller] = FailedSolutionSwitchGroup.new(failed_solution_strings)
  end
end

#create_mutually_exclusive_sets(slotnames, slotvalues, allow_empty: true, require_complete: true, max_values: 2) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/whatnot/switch_interpreter.rb', line 126

def create_mutually_exclusive_sets(slotnames, slotvalues, allow_empty: true, require_complete: true, max_values: 2)
  begin
    slotnames.each do |slotname|
      create_set(slotname, slotvalues, allow_empty: allow_empty, max_values: max_values)
    end
  rescue NameCollisionError
  end

  slotvalues.each do |slotvalue|
    min_on = require_complete ? 1 : 0

    mutual_exclusion = SwitchGroup.new(nil, min_on: min_on, max_on: 1) do |payload|
      k, v = *payload.to_a[0]
      slotnames.include?(k) && slotvalue == v
    end

    @non_interpreted_groups[slotvalue] = mutual_exclusion
  end
end

#create_mutually_exclusive_slots(slotnames, slotvalues, allow_empty: false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/whatnot/switch_interpreter.rb', line 108

def create_mutually_exclusive_slots(slotnames, slotvalues, allow_empty: false)
  begin
    slotnames.each do |slotname|
      create_slot(slotname, slotvalues, allow_empty: allow_empty)
    end
  rescue NameCollisionError
  end

  slotvalues.each do |slotvalue|
    mutual_exclusion = SwitchGroup.new(nil, min_on: 0, max_on: 1) do |payload|
      k, v = *payload.to_a[0]
      slotnames.include?(k) && slotvalue == v
    end

    @non_interpreted_groups[slotvalue] = mutual_exclusion
  end
end

#create_set(setname, setvalues, allow_empty: true, max_values: 2) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/whatnot/switch_interpreter.rb', line 91

def create_set(setname, setvalues, allow_empty: true, max_values: 2)
  if interpreted_groups[setname]
    raise NameCollisionError.new(setname)
  end

  numbers = []

  [setname].product(setvalues).each do |name, value|
    s = Switch.new({name => value})
    numbers << s.number
  end

  min_on = allow_empty ? 0 : 1

  @set_groups[setname] = SwitchGroup.new(numbers, min_on: min_on, max_on: max_values)
end

#create_slot(slotname, slotvalues, allow_empty: true) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/whatnot/switch_interpreter.rb', line 74

def create_slot(slotname, slotvalues, allow_empty: true)
  if interpreted_groups[slotname]
    raise NameCollisionError.new(slotname)
  end

  numbers = []

  [slotname].product(slotvalues).each do |name, value|
    s = Switch.new({name => value})
    numbers << s.number
  end

  min_on = allow_empty ? 0 : 1

  @slot_groups[slotname] = SwitchGroup.new(numbers, min_on: min_on, max_on: 1)
end

#dimacsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/whatnot/switch_interpreter.rb', line 25

def dimacs
  d = "c SwitchInterpreter\np cnf 1 1\n"
  d << "\n"

  interpreted_groups.to_a.each do |key, group|
    d << "c ---------------------------\n"
    d << "c INTERPRETED\n"
    d << "c #{group.class} => \n"
    d << "c   #{key}\n"
    d << "c \n"
    d << group.dimacs
  end

  @non_interpreted_groups.to_a.each do |key, group|
    d << "c ---------------------------\n"
    d << "c NON-INTERPRETED\n"
    d << "c #{group.class} => \n"
    d << "c   #{key}\n"
    d << "c \n"
    d << group.dimacs
  end

  d
end

#interpret(solution) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/whatnot/switch_interpreter.rb', line 198

def interpret(solution)
  out = {}

  solution.split(" ").each do |num|
    num = num.to_i

    if num > 0
      merge_payload!(for_num: num, to_hash: out)
    end
  end

  out
end

#interpreted_groupsObject



18
19
20
21
22
23
# File 'lib/whatnot/switch_interpreter.rb', line 18

def interpreted_groups
  groups = {}
  groups.merge! @slot_groups
  groups.merge! @set_groups
  groups
end

#merge_payload!(for_num: nil, to_hash: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/whatnot/switch_interpreter.rb', line 50

def merge_payload!(for_num: nil, to_hash: nil)
  if for_num.nil? || to_hash.nil?
    raise "left off required kwargs #{"for_num, " unless for_num}#{"to_hash" unless to_hash}" 
  end

  payload = Switch.find(for_num).payload
  payload_key, payload_val = *payload.to_a[0]

  # manage sets vs. slots
  if @set_groups[payload_key]
    payload_val = [payload_val] unless payload_val.is_a?(Array)

    if to_hash[payload_key]
      to_hash[payload_key] += payload_val
    else
      to_hash[payload_key] = payload_val
    end

    return to_hash
  end

  to_hash.merge!(payload)
end