Class: Gecode::Constraints::IntEnum::Expression

Inherits:
Expression
  • Object
show all
Defined in:
lib/gecoder/interface/constraints/int_enum_constraints.rb,
lib/gecoder/interface/constraints/int_enum/sort.rb,
lib/gecoder/interface/constraints/int_enum/channel.rb,
lib/gecoder/interface/constraints/int_enum/distinct.rb,
lib/gecoder/interface/constraints/int_enum/equality.rb

Overview

Expressions with int enums as left hand sides.

Direct Known Subclasses

Count::Expression

Instance Method Summary collapse

Constructor Details

#initialize(model, params) ⇒ Expression

Raises TypeError unless the left hand side is an int enum.



20
21
22
23
24
25
26
# File 'lib/gecoder/interface/constraints/int_enum_constraints.rb', line 20

def initialize(model, params)
  super
  
  unless params[:lhs].respond_to? :to_int_var_array
    raise TypeError, 'Must have int enum as left hand side.'
  end
end

Instance Method Details

#channel(enum, options = {}) ⇒ Object

Adds a channel constraint on the variables in the enum with the specified other set or int enum.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gecoder/interface/constraints/int_enum/channel.rb', line 5

def channel(enum, options = {})
  if @params[:negate]
    raise Gecode::MissingConstraintError, 'A negated channel constraint ' + 
      'is not implemented.'
  end
  unless enum.respond_to?(:to_int_var_array) or 
      enum.respond_to?(:to_set_var_array)
    raise TypeError, "Expected int or set enum, got #{enum.class}."
  end
  
  @params.update(Gecode::Constraints::Util.decode_options(options))
  @params.update(:rhs => enum)
  @model.add_constraint Channel::ChannelConstraint.new(@model, @params)
end

#distinct(options = {}) ⇒ Object

Posts a distinct constraint on the variables in the enum.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gecoder/interface/constraints/int_enum/distinct.rb', line 21

def distinct(options = {})
  if @params[:negate]
    # The best we could implement it as from here would be a bunch of 
    # reified pairwise equality constraints. 
    raise Gecode::MissingConstraintError, 'A negated distinct is not ' + 
      'implemented.'
  end

  @model.add_constraint Distinct::DistinctConstraint.new(@model, 
    @params.update(Gecode::Constraints::Util.decode_options(options)))
end

#equal(options = {}) ⇒ Object

Posts an equality constraint on the variables in the enum.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/gecoder/interface/constraints/int_enum/equality.rb', line 4

def equal(options = {})
  if @params[:negate]
    # The best we could implement it as from here would be a bunch of 
    # reified pairwise inequality constraints.
    raise Gecode::MissingConstraintError, 'A negated equality is not ' + 
      'implemented.'
  end

  @model.add_constraint Equality::EqualityConstraint.new(@model, 
    @params.update(Gecode::Constraints::Util.decode_options(options)))
end

#sorted(options = {}) ⇒ Object

Initiates a sort constraint. Beyond the common options the sort constraint can also take the following options:

:as

Defines a target (must be an int variable enumerable) that will hold the sorted version of the original enumerable. The original enumerable will not be affected (i.e. will not necessarily be sorted)

:order

Sets an int variable enumerable that should be used to store the order of the original enum’s variables when sorted. The original enumerable will not be affected (i.e. will not necessarily be sorted)

If neither of those options are specified then the original enumerable will be constrained to be sorted (otherwise not). Sort constraints with options do not allow negation.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gecoder/interface/constraints/int_enum/sort.rb', line 18

def sorted(options = {})
  # Extract and check options.
  target = options.delete(:as)
  order = options.delete(:order)
  unless target.nil? or target.respond_to? :to_int_var_array
    raise TypeError, 'Expected int var enum as :as, got ' + 
      "#{target.class}."
  end
  unless order.nil? or order.respond_to? :to_int_var_array
    raise TypeError, 'Expected int var enum as :order, got ' + 
      "#{order.class}."
  end
  
  # Extract standard options and convert to constraint.
  @params.update(Gecode::Constraints::Util.decode_options(options))
  if target.nil? and order.nil?
    @model.add_constraint Sort::SortConstraint.new(@model, @params)
  else
    # Do not allow negation.
    if @params[:negate]
      raise Gecode::MissingConstraintError, 'A negated sort with options ' +
        'is not implemented.'
    end
  
    @params.update(:target => target, :order => order)
    @model.add_constraint Sort::SortConstraintWithOptions.new(@model, 
      @params)
  end
end