Module: Gecode::IntEnum::IntEnumOperand

Includes:
Operand
Included in:
Gecode::IntEnumMethods, IntEnum::Dummy
Defined in:
lib/gecoder/interface/constraints/int_enum_constraints.rb,
lib/gecoder/interface/constraints/int_enum/count.rb,
lib/gecoder/interface/constraints/int_enum/element.rb,
lib/gecoder/interface/constraints/int_enum/arithmetic.rb

Overview

A IntEnumOperand is a enumeration of IntOperand on which the constraints defined in IntEnumConstraintReceiver can be placed.

Enumerations of integer operands can be created either by using Gecode::Mixin#int_var_array and Gecode::Mixin#int_var_matrix, or by wrapping an existing enumeration containing IntOperand using Gecode::Mixin#wrap_enum. The enumerations, no matter how they were created, all respond to the properties defined by IntEnumOperand.

Examples

Produces an array of five int operands with domain 0..9 inside a problem formulation using Gecode::Mixin#int_var_array:

int_enum = int_var_array(5, 0..9)

Uses Gecode::Mixin#wrap_enum inside a problem formulation to create a IntEnumOperand from an existing enumeration containing the integer operands int_operand1 and int_operand2:

int_enum = wrap_enum([int_operand1, int_operand2])

– Classes that mix in IntEnumOperand must define #model and #to_int_enum .

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operand

#model, #must, #must_not

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
39
# File 'lib/gecoder/interface/constraints/int_enum_constraints.rb', line 32

def method_missing(method, *args) #:nodoc:
  if Gecode::IntEnum::Dummy.instance_methods.include? method.to_s
    # Delegate to the int enum.
    to_int_enum.method(method).call(*args)
  else
    super
  end
end

Class Method Details

.included(enum_mod) ⇒ Object

This adds the adder for the methods in the modules including it. The reason for doing it so indirect is that the first #[] won’t be defined before the module that this is mixed into is mixed into an enum.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gecoder/interface/constraints/int_enum/element.rb', line 6

def self.included(enum_mod) #:nodoc:
  enum_mod.module_eval do
    # Now we enter the module IntEnumOperands is mixed into.
    class << self
      alias_method :pre_element_included, :included
      def included(mod) #:nodoc:
        mod.module_eval do
          if instance_methods.include? '[]'
            alias_method :pre_element_access, :[]
          end
      
          # Produces an IntOperand representing the
          # i:th integer operand in the enumeration, where i is the
          # value of the integer operand used as index. Think of it
          # as array access in the world of constraint programming.
          # 
          # ==== Examples 
          # 
          #   # The operand at the +x+:th position in +int_enum+,
          #   # where +x+ is an integer operand.  
          #   int_enum[x]
          # 
          def [](*vars)
            if vars.first.respond_to? :to_int_var
              return Element::ElementIntOperand.new(
                model, self, vars.first)
            else
              pre_element_access(*vars) if respond_to? :pre_element_access
            end
          end
        end
        pre_element_included(mod)
      end
    end
  end
end

Instance Method Details

#count(int_operand_or_fixnum) ⇒ Object

Produces a new IntOperand representing the number of times int_operand_or_fixnum is present in this enumeration.

Examples

# The number of times 17 occurs in +int_enum+.
int_enum.count(17)

# The number of times +int_operand+ occurs in +int_enum+.
int_enum.count(int_operand)


13
14
15
16
17
18
19
20
# File 'lib/gecoder/interface/constraints/int_enum/count.rb', line 13

def count(int_operand_or_fixnum)
  unless int_operand_or_fixnum.respond_to? :to_int_var or 
      int_operand_or_fixnum.kind_of?(Fixnum)
    raise TypeError, 'Expected integer operand of fixnum, got ' + 
      "#{int_operand_or_fixnum.class}."
  end
  Count::IntEnumCountOperand.new(@model, self, int_operand_or_fixnum)
end

#maxObject

Produces an IntOperand representing the maximum value of the integer operands in this enumeration.

Examples

# The maximum of +int_enum+.
int_enum.max


10
11
12
# File 'lib/gecoder/interface/constraints/int_enum/arithmetic.rb', line 10

def max
  Arithmetic::IntEnumMaxOperand.new(@model, self)
end

#minObject

Produces an IntOperand representing the minimum value of the integer operands in this enumeration.

Examples

# The minimum of +int_enum+.
int_enum.min


21
22
23
# File 'lib/gecoder/interface/constraints/int_enum/arithmetic.rb', line 21

def min
  Arithmetic::IntEnumMinOperand.new(@model, self)
end