Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/gecoder/interface/constraints/int_enum/channel.rb | 86 | 30 | 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::IntEnum |
2 class IntEnumConstraintReceiver |
3 # Constrains this enumeration to "channel" +int_enum+. Channel |
4 # constraints are used to give access to multiple viewpoints when |
5 # modelling. |
6 # |
7 # The channel constraint can be thought of as constraining the arrays to |
8 # be each other's inverses. I.e. if the i:th value in the first enumeration |
9 # is j, then the j:th value in the second enumeration is constrained to be |
10 # i. |
11 # |
12 # Neither reification nor negation is supported. |
13 # |
14 # ==== Examples |
15 # |
16 # Lets say that we're modelling a sequence of numbers that must be distinct |
17 # and that we want access to the following two view simultaneously. |
18 # |
19 # === First view |
20 # |
21 # The sequence is modelled as an array of integer variables where the first |
22 # variable holds the value of the first position in the sequence, the |
23 # second the value of the second position and so on. |
24 # |
25 # # n variables with values from 0 to n-1. |
26 # elements = int_var_array(n, 0...n) |
27 # elements.must_be.distinct |
28 # |
29 # That way +elements+ will contain the actual sequence when the problem has |
30 # been solved. |
31 # |
32 # === Second view |
33 # |
34 # The sequence is modelled as the positions of each value in 0..(n-1) in |
35 # the sequence. That way the first variable would hold the positions of 0 |
36 # in the sequence, the second variable would hold the positions of 1 in the |
37 # sequence and so on. |
38 # |
39 # positions = int_var_array(n, 0...n) |
40 # positions.must_be.distinct |
41 # |
42 # === Connecting the views |
43 # |
44 # In essence the relationship between the two arrays +elements+ and |
45 # +positions+ is that |
46 # |
47 # elements.map{ |e| e.val }[i] == positions.map{ |p| p.val }.index(i) |
48 # |
49 # for all i in 0..(n-1). This relationship is enforced by the channel |
50 # constraint as follows. |
51 # |
52 # elements.must.channel positions |
53 # |
54 def channel(int_enum, options = {}) |
55 if @params[:negate] |
56 raise Gecode::MissingConstraintError, 'A negated channel constraint ' + |
57 'is not implemented.' |
58 end |
59 unless int_enum.respond_to? :to_int_enum |
60 raise TypeError, "Expected int enum, got #{int_enum.class}." |
61 end |
62 if options.has_key? :reify |
63 raise ArgumentError, 'The channel constraints does not support the ' + |
64 'reification option.' |
65 end |
66 |
67 @params.update(Gecode::Util.decode_options(options)) |
68 @params.update(:rhs => int_enum) |
69 @model.add_constraint Channel::ChannelConstraint.new(@model, @params) |
70 end |
71 |
72 # Provides commutativity with SetEnumConstraintReceiver#channel . |
73 provide_commutativity(:channel){ |rhs, _| rhs.respond_to? :to_set_enum } |
74 end |
75 |
76 # A module that gathers the classes and modules used in channel constraints. |
77 module Channel #:nodoc: |
78 class ChannelConstraint < Gecode::Constraint #:nodoc: |
79 def post |
80 lhs, rhs = @params.values_at(:lhs, :rhs) |
81 Gecode::Raw::channel(@model.active_space, lhs.to_int_enum.bind_array, |
82 rhs.to_int_enum.bind_array, *propagation_options) |
83 end |
84 end |
85 end |
86 end |
Generated on Thu Jan 08 13:27:03 +0100 2015 with rcov 1.0.0