Module: Gecode::FixnumEnum::FixnumEnumOperand

Includes:
Operand
Included in:
Dummy, Gecode::FixnumEnumMethods
Defined in:
lib/gecoder/interface/constraints/fixnum_enum_constraints.rb,
lib/gecoder/interface/constraints/fixnum_enum/element.rb,
lib/gecoder/interface/constraints/fixnum_enum/operation.rb

Overview

A FixnumEnumOperand is a enumeration of Fixnum on which the constraints defined in FixnumEnumConstraintReceiver can be placed. They typically service as constant arrays or constant sets.

The fixnum enumeration operands are created by wrapping an enumeration of fixnum Gecode::Mixin#wrap_enum. The enumerations created that way all respond to the properties defined by FixnumEnumOperand.

Examples

Uses Gecode::Mixin#wrap_enum inside a problem formulation to create a FixnumEnumOperand from an existing enumeration of Fixnum:

fixnum_enum = wrap_enum([3, 5, 7])

– Classes that mix in FixnumEnumOperand must define #model.

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:



24
25
26
27
28
29
30
31
# File 'lib/gecoder/interface/constraints/fixnum_enum_constraints.rb', line 24

def method_missing(method, *args) #:nodoc:
  if Gecode::FixnumEnum::Dummy.instance_methods.include? method.to_s
    # Delegate to the fixnum enum.
    to_fixnum_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/fixnum_enum/element.rb', line 6

def self.included(enum_mod) #:nodoc:
  enum_mod.module_eval do
    # Now we enter the module FixnumEnumOperands 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 constant integer 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 price of +selected_item+ as described by +prices+ .
          #   prices = wrap_enum([500, 24, 4711, 412, 24])
          #   prices[selected_item]
          # 
          def [](*vars)
            if vars.first.respond_to? :to_int_var
              return Element::ElementIntOperand.new(
                self, vars.first, model)
            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

#disjoint_union(set_operand) ⇒ Object

Produces a new SetOperand representing the disjoint union between this operand, interpreted as a constant set, and set_operand. The disjoint union is the union of the disjoint parts of the sets.

Examples

# The disjoint union between +fixnum_enum+ and +set+.
fixnum_enum.disjoint_union set


23
24
25
# File 'lib/gecoder/interface/constraints/fixnum_enum/operation.rb', line 23

def disjoint_union(set_operand)
  set_operation(:disjoint_union, set_operand)
end

#intersection(set_operand) ⇒ Object

Produces a new SetOperand representing the intersection between this operand, interpreted as a constant set, and set_operand.

Examples

# The intersection between +fixnum_enum+ and +set+.
fixnum_enum.intersection set


35
36
37
# File 'lib/gecoder/interface/constraints/fixnum_enum/operation.rb', line 35

def intersection(set_operand)
  set_operation(:intersection, set_operand)
end

#minus(set_operand) ⇒ Object

Produces a new SetOperand representing this operand, interpreted as a constant set, minus set_operand.

Examples

# +fixnum_enum+ minus +set+.
fixnum_enum.minus set


46
47
48
# File 'lib/gecoder/interface/constraints/fixnum_enum/operation.rb', line 46

def minus(set_operand)
  set_operation(:minus, set_operand)
end

#union(set_operand) ⇒ Object

Produces a new SetOperand representing the union between this operand, interpreted as a constant set, and set_operand.

Examples

# The union between +fixnum_enum+ and +set+.
fixnum_enum.union set


10
11
12
# File 'lib/gecoder/interface/constraints/fixnum_enum/operation.rb', line 10

def union(set_operand)
  set_operation(:union, set_operand)
end