Class: Gecode::Constraints::BoolEnum::Expression

Inherits:
Expression
  • Object
show all
Defined in:
lib/gecoder/interface/constraints/bool_enum_constraints.rb,
lib/gecoder/interface/constraints/bool_enum/channel.rb,
lib/gecoder/interface/constraints/bool_enum/extensional.rb

Overview

Expressions with bool enums as left hand sides.

Instance Method Summary collapse

Constructor Details

#initialize(model, params) ⇒ Expression

Raises TypeError unless the left hand side is a bool enum.



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

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

Instance Method Details

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

Adds a channel constraint on the variables in the enum with the specified integer variable. Beyond the common options the channel constraint can also take the following option:

:offset

Specifies an offset for the integer variable. If the offset is set to k then the integer variable takes value i+k exactly when the variable at index i in the boolean enumeration is true and the rest are false.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gecoder/interface/constraints/bool_enum/channel.rb', line 11

def channel(int_var, options = {})
  if @params[:negate]
    raise Gecode::MissingConstraintError, 'A negated channel constraint ' + 
      'is not implemented.'
  end
  if options.has_key? :reify
    raise ArgumentError, 'The channel constraint does not support the ' + 
      'reification option.'
  end
  unless int_var.kind_of? Gecode::FreeIntVar
    raise TypeError, "Expected an integer variable, got #{int_var.class}."
  end
  
  @params.update(:rhs => int_var, :offset => options.delete(:offset) || 0)
  @params.update(Gecode::Constraints::Util.decode_options(options))
  @model.add_constraint Channel::ChannelConstraint.new(@model, @params)
end

#in(tuples, options = {}) ⇒ Object

Posts an equality constraint on the variables in the enum.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gecoder/interface/constraints/bool_enum/extensional.rb', line 4

def in(tuples, options = {})
  if @params[:negate]
    raise Gecode::MissingConstraintError, 'A negated tuple constraint is ' +
      'not implemented.'
  end
  unless options[:reify].nil?
    raise ArgumentError, 'Reification is not supported by the tuple ' + 
      'constraint.'
  end
  
  util = Gecode::Constraints::Util
  
  # Check that the tuples are correct.
  expected_size = @params[:lhs].size
  util::Extensional.perform_tuple_checks(tuples, expected_size) do |tuple|
    unless tuple.all?{ |x| x.kind_of?(TrueClass) or x.kind_of?(FalseClass) }
      raise TypeError, 'All tuples must contain booleans.'
    end
  end
  
  @params[:tuples] = tuples
  @model.add_constraint Extensional::TupleConstraint.new(@model, 
    @params.update(Gecode::Constraints::Util.decode_options(options)))
end

#match(regexp, options = {}) ⇒ Object

Adds a constraint that forces the enumeration to match the specified regular expression over the boolean domain. The regular expression is expressed using arrays and boolean values (or integers). See BoolEnum::Extensional::RegexpConstraint for more information and examples of such regexps.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gecoder/interface/constraints/bool_enum/extensional.rb', line 34

def match(regexp, options = {})
  if @params[:negate]
    raise Gecode::MissingConstraintError, 'A negated regexp constraint ' +
      'is not implemented.'
  end
  unless options[:reify].nil?
    raise ArgumentError, 'Reification is not supported by the regexp ' + 
      'constraint.'
  end

  @params[:regexp] = 
    Gecode::Constraints::Util::Extensional.parse_regexp regexp
  @params.update Gecode::Constraints::Util.decode_options(options)
  @model.add_constraint Extensional::RegexpConstraint.new(@model, @params)
end