Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/gecoder/interface/constraints/bool/linear.rb | 200 | 126 | 98.00%
|
96.83%
|
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::Bool |
2 module BoolLinearOperations #:nodoc: |
3 # Produces an IntOperand representing the value of this boolean |
4 # operand (0 or 1) plus +op2+. |
5 # |
6 # ==== Examples |
7 # |
8 # # +bool1+ plus +bool2+ |
9 # bool1 + bool2 |
10 def +(op2) |
11 bool_linear_expression_operation(:+, op2) |
12 end |
13 |
14 # Produces an IntOperand representing the value of this boolean |
15 # operand (0 or 1) times a constant. |
16 # |
17 # ==== Examples |
18 # |
19 # # +bool+ times 17 |
20 # bool * 17 |
21 def *(fixnum) |
22 if fixnum.kind_of? Fixnum |
23 bool_linear_expression_operation(:*, fixnum) |
24 else |
25 raise TypeError, "Expected fixnum, got #{fixnum.class}." |
26 end |
27 end |
28 |
29 # Produces an IntOperand representing the value of this boolean |
30 # operand (0 or 1) minus +op2+. |
31 # |
32 # ==== Examples |
33 # |
34 # # +bool1+ minus +bool2+ |
35 # bool1 - bool2 |
36 def -(op2) |
37 bool_linear_expression_operation(:-, op2) |
38 end |
39 |
40 private |
41 |
42 # Performs the bool linear expression operation +operator+ on self |
43 # and +operand2+. |
44 def bool_linear_expression_operation(operator, operand2) |
45 linear_module = Linear |
46 unless operand2.respond_to? :to_minimodel_lin_exp |
47 operand2 = linear_module::ExpressionNode.new operand2 |
48 end |
49 operand1 = self |
50 unless operand1.respond_to? :to_minimodel_lin_exp |
51 operand1 = linear_module::ExpressionNode.new(self, @model) |
52 end |
53 linear_module::ExpressionTree.new(operand1, operand2, operator) |
54 end |
55 end |
56 |
57 module BoolOperand |
58 # We include the operations and then redefine them so that they show |
59 # up in the documentation. |
60 include BoolLinearOperations |
61 |
62 # Produces an IntOperand representing the value of this boolean |
63 # operand (0 or 1) plus +op2+. |
64 # |
65 # ==== Examples |
66 # |
67 # # +bool1+ plus +bool2+ |
68 # bool1 + bool2 |
69 def +(op2) |
70 bool_linear_expression_operation(:+, op2) |
71 end |
72 |
73 # Produces an IntOperand representing the value of this boolean |
74 # operand (0 or 1) times a constant. |
75 # |
76 # ==== Examples |
77 # |
78 # # +bool+ times 17 |
79 # bool * 17 |
80 def *(fixnum) |
81 if fixnum.kind_of? Fixnum |
82 bool_linear_expression_operation(:*, fixnum) |
83 else |
84 raise TypeError, "Expected fixnum, got #{fixnum.class}." |
85 end |
86 end |
87 |
88 # Produces an IntOperand representing the value of this boolean |
89 # operand (0 or 1) minus +op2+. |
90 # |
91 # ==== Examples |
92 # |
93 # # +bool1+ minus +bool2+ |
94 # bool1 - bool2 |
95 def -(op2) |
96 bool_linear_expression_operation(:-, op2) |
97 end |
98 end |
99 |
100 # A module that gathers the classes and modules used in linear constraints. |
101 module Linear #:nodoc: |
102 class LinearRelationConstraint < Gecode::ReifiableConstraint #:nodoc: |
103 def post |
104 lhs, rhs, relation_type, reif_var = |
105 @params.values_at(:lhs, :rhs, :relation_type, :reif) |
106 reif_var = reif_var.to_bool_var.bind if reif_var.respond_to? :to_bool_var |
107 final_exp = (lhs.to_minimodel_lin_exp - rhs.to_minimodel_lin_exp) |
108 if reif_var.nil? |
109 final_exp.post(@model.active_space, relation_type, |
110 *propagation_options) |
111 else |
112 final_exp.post(@model.active_space, relation_type, reif_var, |
113 *propagation_options) |
114 end |
115 end |
116 end |
117 |
118 # Describes a binary tree of expression nodes which together form a linear |
119 # expression. |
120 class ExpressionTree < Gecode::Int::ShortCircuitRelationsOperand #:nodoc: |
121 include Gecode::Bool::BoolLinearOperations |
122 attr :model |
123 |
124 # Constructs a new expression with the specified operands. |
125 def initialize(left_node, right_node, operation) |
126 super(left_node.model || right_node.model) |
127 @left = left_node |
128 @right = right_node |
129 @operation = operation |
130 @model = @left.model || @right.model |
131 end |
132 |
133 # Converts the linear expression to an instance of |
134 # Gecode::Raw::MiniModel::LinExpr |
135 def to_minimodel_lin_exp |
136 @left.to_minimodel_lin_exp.send(@operation, @right.to_minimodel_lin_exp) |
137 end |
138 |
139 alias_method :pre_bool_construct_receiver, :construct_receiver |
140 def construct_receiver(params) |
141 receiver = pre_bool_construct_receiver(params) |
142 lhs = self |
143 receiver.instance_eval{ @lhs = lhs } |
144 class <<receiver |
145 alias_method :pre_bool_equality, :== |
146 def ==(op, options = {}) |
147 if op.respond_to? :to_bool_var |
148 (@lhs - op).must.equal(0, options) |
149 else |
150 pre_bool_equality(op, options) |
151 end |
152 end |
153 alias_comparison_methods |
154 end |
155 return receiver |
156 end |
157 |
158 def relation_constraint(relation, bool_operand_or_fix, params) |
159 unless params[:negate] |
160 relation_type = |
161 Gecode::Util::RELATION_TYPES[relation] |
162 else |
163 relation_type = |
164 Gecode::Util::NEGATED_RELATION_TYPES[relation] |
165 end |
166 |
167 unless bool_operand_or_fix.respond_to? :to_minimodel_lin_exp |
168 bool_operand_or_fix = Linear::ExpressionNode.new(bool_operand_or_fix); |
169 end |
170 |
171 params.update(:rhs => bool_operand_or_fix, :relation_type => relation_type) |
172 LinearRelationConstraint.new(model, params) |
173 end |
174 end |
175 |
176 # Describes a single node in a linear expression. |
177 class ExpressionNode #:nodoc: |
178 attr :model |
179 |
180 def initialize(value, model = nil) |
181 unless value.respond_to?(:to_bool_var) or value.kind_of?(Fixnum) |
182 raise TypeError, 'Expected bool operand or ' + |
183 "fixnum, got #{value.class}." |
184 end |
185 @value = value |
186 @model = model |
187 end |
188 |
189 # Converts the linear expression to an instance of |
190 # Gecode::Raw::MiniModel::LinExpr |
191 def to_minimodel_lin_exp |
192 expression = @value |
193 if expression.respond_to? :to_bool_var |
194 expression = expression.to_bool_var.bind * 1 |
195 end |
196 expression |
197 end |
198 end |
199 end |
200 end |
Generated on Thu Jan 08 13:27:03 +0100 2015 with rcov 1.0.0