Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/gecoder/interface/constraints/int/linear.rb | 143 | 91 | 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::Int |
2 module IntOperand |
3 # Produces a new IntOperand representing this operand plus |
4 # +int_operand_or_fixnum+. |
5 # |
6 # ==== Examples |
7 # |
8 # # +int1+ plus +int2+ |
9 # int1 + int2 |
10 # |
11 # # +int+ plus 17 |
12 # int + 17 |
13 def +(int_operand_or_fixnum) |
14 int_linear_expression_operation(:+, int_operand_or_fixnum) |
15 end |
16 |
17 # Produces a new IntOperand representing this operand minus |
18 # +int_operand_or_fixnum+. |
19 # |
20 # ==== Examples |
21 # |
22 # # +int1+ minus +int2+ |
23 # int1 - int2 |
24 # |
25 # # +int+ minus 17 |
26 # int - 17 |
27 def -(int_operand_or_fixnum) |
28 int_linear_expression_operation(:-, int_operand_or_fixnum) |
29 end |
30 |
31 # Produces a new IntOperand representing this operand times a |
32 # constant. |
33 # |
34 # ==== Examples |
35 # |
36 # # +int+ times 17 |
37 # int * 17 |
38 def *(fixnum) |
39 if fixnum.kind_of? Fixnum |
40 int_linear_expression_operation(:*, fixnum) |
41 else |
42 raise TypeError, "Expected fixnum, got #{fixnum.class}." |
43 end |
44 end |
45 |
46 private |
47 |
48 # Performs the int linear expression operation +operator+ on self |
49 # and +operand2+. |
50 def int_linear_expression_operation(operator, operand2) |
51 unless operand2.respond_to? :to_minimodel_lin_exp |
52 operand2 = Linear::ExpressionNode.new operand2 |
53 end |
54 operand1 = self |
55 unless operand1.respond_to? :to_minimodel_lin_exp |
56 operand1 = Linear::ExpressionNode.new(self, @model) |
57 end |
58 Linear::ExpressionTree.new(operand1, operand2, operator) |
59 end |
60 end |
61 |
62 # A module that gathers the classes and modules used in linear constraints. |
63 module Linear #:nodoc: |
64 class LinearRelationConstraint < Gecode::ReifiableConstraint #:nodoc: |
65 def post |
66 lhs, rhs, relation_type, reif_var = |
67 @params.values_at(:lhs, :rhs, :relation_type, :reif) |
68 reif_var = reif_var.to_bool_var.bind if reif_var.respond_to? :to_bool_var |
69 final_exp = (lhs.to_minimodel_lin_exp - rhs.to_minimodel_lin_exp) |
70 if reif_var.nil? |
71 final_exp.post(@model.active_space, relation_type, |
72 *propagation_options) |
73 else |
74 final_exp.post(@model.active_space, relation_type, reif_var, |
75 *propagation_options) |
76 end |
77 end |
78 end |
79 |
80 # Describes a binary tree of expression nodes which together form a linear |
81 # expression. |
82 class ExpressionTree < Gecode::Int::ShortCircuitRelationsOperand #:nodoc: |
83 attr :model |
84 |
85 # Constructs a new expression with the specified operands. |
86 def initialize(left_node, right_node, operation) |
87 super(left_node.model || right_node.model) |
88 @left = left_node |
89 @right = right_node |
90 @operation = operation |
91 @model = @left.model || @right.model |
92 end |
93 |
94 # Converts the linear expression to an instance of |
95 # Gecode::Raw::MiniModel::LinExpr |
96 def to_minimodel_lin_exp |
97 @left.to_minimodel_lin_exp.send(@operation, @right.to_minimodel_lin_exp) |
98 end |
99 |
100 def relation_constraint(relation, int_operand_or_fix, params) |
101 unless params[:negate] |
102 relation_type = |
103 Gecode::Util::RELATION_TYPES[relation] |
104 else |
105 relation_type = |
106 Gecode::Util::NEGATED_RELATION_TYPES[relation] |
107 end |
108 |
109 unless int_operand_or_fix.respond_to? :to_minimodel_lin_exp |
110 int_operand_or_fix = Linear::ExpressionNode.new(int_operand_or_fix); |
111 end |
112 |
113 params.update(:lhs => self, :rhs => int_operand_or_fix, |
114 :relation_type => relation_type) |
115 LinearRelationConstraint.new(model, params) |
116 end |
117 end |
118 |
119 # Describes a single node in a linear expression. |
120 class ExpressionNode #:nodoc: |
121 attr :model |
122 |
123 def initialize(value, model = nil) |
124 unless value.respond_to?(:to_int_var) or value.kind_of?(Fixnum) |
125 raise TypeError, 'Expected int operand or fixnum, ' + |
126 "got #{value.class}." |
127 end |
128 @value = value |
129 @model = model |
130 end |
131 |
132 # Converts the linear expression to an instance of |
133 # Gecode::Raw::MiniModel::LinExpr |
134 def to_minimodel_lin_exp |
135 expression = @value |
136 if expression.respond_to? :to_int_var |
137 expression = expression.to_int_var.bind * 1 |
138 end |
139 expression |
140 end |
141 end |
142 end |
143 end |
Generated on Thu Jan 08 13:27:03 +0100 2015 with rcov 1.0.0