Class: Gecode::Constraints::CompositeExpression

Inherits:
Expression
  • Object
show all
Defined in:
lib/gecoder/interface/constraints.rb

Overview

A composite expression which is a expression with a left hand side resulting from a previous constraint.

Instance Method Summary collapse

Constructor Details

#initialize(expression_class, variable_class, new_var_proc, model, params, &block) ⇒ CompositeExpression

The expression class should be the class of the expression delegated to, the variable class the kind of single variable used in the expression. The new var proc should produce a new variable (of the appropriate type) which has an unconstricted domain. The block given should take three parameters. The first is the variable that should be the left hand side. The second is the hash of parameters. The third is a boolean, it it’s true then the block should try to constrain the first variable’s domain as much as possible.



247
248
249
250
251
252
253
254
# File 'lib/gecoder/interface/constraints.rb', line 247

def initialize(expression_class, variable_class, new_var_proc, model, 
    params, &block)
  super(model, params)
  @expression_class = expression_class
  @variable_class = variable_class
  @new_var_proc = new_var_proc
  @constrain_equal_proc = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Delegate to an instance of the expression class when we get something that we can’t handle.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/gecoder/interface/constraints.rb', line 258

def method_missing(name, *args)
  if @expression_class.instance_methods.include? name.to_s
    options = {}
    if args.size >= 2 and args[1].kind_of? Hash
      options = args[1]
    end
    
    # Link a variable to the composite constraint.
    @params.update Gecode::Constraints::Util.decode_options(options.clone)
    variable = @new_var_proc.call
    @model.add_interaction do
      @constrain_equal_proc.call(variable, @params, true)
    end
    
    # Perform the operation on the linked variable.
    int_var_params = @params.clone.update(:lhs => variable)
    @expression_class.new(@model, int_var_params).send(name, *args)
  else
    super
  end
end

Instance Method Details

#==(expression, options = {}) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/gecoder/interface/constraints.rb', line 280

def ==(expression, options = {})
  if !@params[:negate] and options[:reify].nil? and 
      expression.kind_of? @variable_class
    # We don't need any additional constraints.
    @params.update Gecode::Constraints::Util.decode_options(options)
    @model.add_interaction do
      @constrain_equal_proc.call(expression, @params, false)
    end
  else
    method_missing(:==, expression, options)
  end
end