Module: Gecode::Util::Extensional

Defined in:
lib/gecoder/interface/constraints.rb,
lib/gecoder/interface/constraints/extensional_regexp.rb

Overview

A module that contains utility-methods for extensional constraints.

Class Method Summary collapse

Class Method Details

.parse_regexp(regexp) ⇒ Object

Parses a regular expression over the integer domain, returning an instance of Gecode::REG .

Pseudo-BNF of the integer regexp representation: regexp ::= <Fixnum> | <TrueClass> | <FalseClass> | <Gecode::Raw::REG>

| [<regexp>, ...]


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gecoder/interface/constraints/extensional_regexp.rb', line 65

def parse_regexp(regexp)
  # Check the involved types.
  unless regexp.kind_of? Enumerable
    regexp = [regexp]
  end
  regexp.to_a.flatten.each do |element|
    unless element.kind_of?(Fixnum) or element.kind_of?(Gecode::Raw::REG) or
        element.kind_of?(TrueClass) or element.kind_of?(FalseClass)
      raise TypeError, 
        "Can't translate #{element.class} into integer or boolean regexp."
    end
  end

  # Convert it into a regexp.
  internal_parse_regexp(regexp)
end

.perform_tuple_checks(tuples, expected_size, &additional_test) ⇒ Object

Checks that the specified enumeration is an enumeration containing one or more tuples of the specified size. It also allows the caller to define additional tests by providing a block, which is given each tuple. If a test fails then an appropriate error is raised.



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/gecoder/interface/constraints.rb', line 442

def perform_tuple_checks(tuples, expected_size, &additional_test)
  unless tuples.respond_to?(:each)
    raise TypeError, 'Expected an enumeration with tuples, got ' + 
      "#{tuples.class}."
  end
  
  if tuples.empty?
    raise ArgumentError, 'One or more tuples must be specified.'
  end
  
  tuples.each do |tuple|
    unless tuple.respond_to?(:each)
      raise TypeError, 'Expected an enumeration containing enumeraions, ' +
        "got #{tuple.class}."
    end
    
    unless tuple.size == expected_size
      raise ArgumentError, 'All tuples must be of the same size as the ' + 
        'number of operands in the array.'
    end
    
    yield tuple
  end
end