Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/gecoder/interface/enum_wrapper.rb | 205 | 150 | 100.00%
|
100.00%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 module Gecode |
2 module Mixin |
3 # Wraps a custom enumerable so that constraints can be specified using it. |
4 # The argument is altered and returned. |
5 def wrap_enum(enum) |
6 unless enum.kind_of? Enumerable |
7 raise TypeError, 'Only enumerables can be wrapped.' |
8 end |
9 if enum.kind_of? Gecode::EnumMethods |
10 raise ArgumentError, 'The enumration is already wrapped.' |
11 end |
12 elements = enum.to_a |
13 if elements.empty? |
14 raise ArgumentError, 'Enumerable must not be empty.' |
15 end |
16 |
17 if elements.all?{ |var| var.respond_to? :to_int_var } |
18 elements.map!{ |var| var.to_int_var } |
19 class <<enum |
20 include Gecode::IntEnumMethods |
21 end |
22 elsif elements.all?{ |var| var.respond_to? :to_bool_var } |
23 elements.map!{ |var| var.to_bool_var } |
24 class <<enum |
25 include Gecode::BoolEnumMethods |
26 end |
27 elsif elements.all?{ |var| var.respond_to? :to_set_var } |
28 elements.map!{ |var| var.to_set_var } |
29 class <<enum |
30 include Gecode::SetEnumMethods |
31 end |
32 elsif elements.all?{ |var| var.kind_of? Fixnum } |
33 class <<enum |
34 include Gecode::FixnumEnumMethods |
35 end |
36 else |
37 raise TypeError, "Enumerable doesn't contain operands #{enum.inspect}." |
38 end |
39 |
40 enum.model = self |
41 return enum |
42 end |
43 end |
44 |
45 # A module containing the methods needed by enumerations containing |
46 # operands. |
47 module EnumMethods #:nodoc: |
48 attr_accessor :model |
49 # Gets the current space of the model the array is connected to. |
50 def active_space |
51 @model.active_space |
52 end |
53 end |
54 |
55 module VariableEnumMethods #:nodoc: |
56 include EnumMethods |
57 |
58 # Gets the values of all the operands in the enum. |
59 def values |
60 map{ |var| var.value } |
61 end |
62 end |
63 |
64 # A module containing the methods needed by enumerations containing int |
65 # operands. Requires that it's included in an enumerable. |
66 module IntEnumMethods #:nodoc: |
67 include IntEnum::IntEnumOperand |
68 include VariableEnumMethods |
69 |
70 # Returns an int variable array with all the bound variables. |
71 def bind_array |
72 space = @model.active_space |
73 unless @bound_space == space |
74 elements = to_a |
75 @bound_arr = Gecode::Raw::IntVarArray.new(active_space, elements.size) |
76 elements.each_with_index{ |var, index| @bound_arr[index] = var.bind } |
77 @bound_space = space |
78 end |
79 return @bound_arr |
80 end |
81 |
82 # Returns the receiver. |
83 def to_int_enum |
84 self |
85 end |
86 |
87 # Returns the smallest range that contains the domains of all integer |
88 # variables involved. |
89 def domain_range |
90 inject(nil) do |range, var| |
91 min = var.min |
92 max = var.max |
93 next min..max if range.nil? |
94 |
95 range = min..range.last if min < range.first |
96 range = range.first..max if max > range.last |
97 range |
98 end |
99 end |
100 end |
101 |
102 # A dummy class that just shows what methods an int enum responds to. |
103 class IntEnum::Dummy < Array #:nodoc: |
104 include IntEnum::IntEnumOperand |
105 include VariableEnumMethods |
106 end |
107 |
108 # A module containing the methods needed by enumerations containing boolean |
109 # operands. Requires that it's included in an enumerable. |
110 module BoolEnumMethods #:nodoc: |
111 include BoolEnum::BoolEnumOperand |
112 include VariableEnumMethods |
113 |
114 # Returns a bool variable array with all the bound variables. |
115 def bind_array |
116 space = @model.active_space |
117 unless @bound_space == space |
118 elements = to_a |
119 @bound_arr = Gecode::Raw::BoolVarArray.new(active_space, elements.size) |
120 elements.each_with_index{ |var, index| @bound_arr[index] = var.bind } |
121 @bound_space = space |
122 end |
123 return @bound_arr |
124 end |
125 |
126 # Returns the receiver. |
127 def to_bool_enum |
128 self |
129 end |
130 end |
131 |
132 # A dummy class that just shows what methods a bool enum responds to. |
133 class BoolEnum::Dummy < Array #:nodoc: |
134 include BoolEnum::BoolEnumOperand |
135 include VariableEnumMethods |
136 end |
137 |
138 # A module containing the methods needed by enumerations containing set |
139 # operands. Requires that it's included in an enumerable. |
140 module SetEnumMethods #:nodoc: |
141 include SetEnum::SetEnumOperand |
142 include VariableEnumMethods |
143 |
144 # Returns a set variable array with all the bound variables. |
145 def bind_array |
146 space = @model.active_space |
147 unless @bound_space == space |
148 elements = to_a |
149 @bound_arr = Gecode::Raw::SetVarArray.new(active_space, elements.size) |
150 elements.each_with_index{ |var, index| @bound_arr[index] = var.bind } |
151 @bound_space = space |
152 end |
153 return @bound_arr |
154 end |
155 |
156 # Returns the receiver. |
157 def to_set_enum |
158 self |
159 end |
160 |
161 # Returns the range of the union of the contained sets' upper bounds. |
162 def upper_bound_range |
163 inject(nil) do |range, var| |
164 upper_bound = var.upper_bound |
165 min = upper_bound.min |
166 max = upper_bound.max |
167 next min..max if range.nil? |
168 |
169 range = min..range.last if min < range.first |
170 range = range.first..max if max > range.last |
171 range |
172 end |
173 end |
174 end |
175 |
176 # A dummy class that just shows what methods a set enum responds to. |
177 class SetEnum::Dummy < Array #:nodoc: |
178 include SetEnum::SetEnumOperand |
179 include VariableEnumMethods |
180 end |
181 |
182 # A module containing the methods needed by enumerations containing fixnums. |
183 # Requires that it's included in an enumerable. |
184 module FixnumEnumMethods #:nodoc: |
185 include FixnumEnum::FixnumEnumOperand |
186 include EnumMethods |
187 |
188 # Returns the receiver. |
189 def to_fixnum_enum |
190 self |
191 end |
192 |
193 # Returns the smallest range that contains the domains of all integer |
194 # variables involved. |
195 def domain_range |
196 min..max |
197 end |
198 end |
199 |
200 # A dummy class that just shows what methods a fixnum enum responds to. |
201 class FixnumEnum::Dummy < Array #:nodoc: |
202 include FixnumEnum::FixnumEnumOperand |
203 include VariableEnumMethods |
204 end |
205 end |
Generated on Thu Jan 08 13:27:03 +0100 2015 with rcov 1.0.0