lib/gecoder/interface/constraints/int/linear.rb in gecoder-0.3.0 vs lib/gecoder/interface/constraints/int/linear.rb in gecoder-0.4.0

- old
+ new

@@ -6,13 +6,18 @@ @model) + var end # Creates a linear expression where the int variable is multiplied with # a constant integer. + alias_method :pre_linear_mult, :* if instance_methods.include? '*' def *(int) - Gecode::Constraints::Int::Linear::ExpressionNode.new(self, - @model) * int + if int.kind_of? Fixnum + Gecode::Constraints::Int::Linear::ExpressionNode.new(self, + @model) * int + else + pre_linear_mult(int) if respond_to? :pre_linear_mult + end end # Creates a linear expression where the specified variable is subtracted # from this one. def -(var) @@ -21,74 +26,37 @@ end end module Constraints::Int class Expression - private - - # Maps the names of the methods to the corresponding integer relation - # type in Gecode. - RELATION_TYPES = { - :== => Gecode::Raw::IRT_EQ, - :<= => Gecode::Raw::IRT_LQ, - :< => Gecode::Raw::IRT_LE, - :>= => Gecode::Raw::IRT_GQ, - :> => Gecode::Raw::IRT_GR } - # The same as above, but negated. - NEGATED_RELATION_TYPES = { - :== => Gecode::Raw::IRT_NQ, - :<= => Gecode::Raw::IRT_GR, - :< => Gecode::Raw::IRT_GQ, - :>= => Gecode::Raw::IRT_LE, - :> => Gecode::Raw::IRT_LQ - } - - # Various method aliases for the class. Maps the original name to an - # array of aliases. - METHOD_ALIASES = { - :== => [:equal, :equal_to], - :> => [:greater, :greater_than], - :>= => [:greater_or_equal, :greater_than_or_equal_to], - :< => [:less, :less_than], - :<= => [:less_or_equal, :less_than_or_equal_to] - } - - public - # Add some relation selection based on whether the expression is negated. alias_method :pre_linear_initialize, :initialize def initialize(model, params) pre_linear_initialize(model, params) unless params[:negate] - @method_relations = RELATION_TYPES + @method_relations = Constraints::Util::RELATION_TYPES else - @method_relations = NEGATED_RELATION_TYPES + @method_relations = Constraints::Util::NEGATED_RELATION_TYPES end end # Define the relation methods. - RELATION_TYPES.each_key do |name| + Constraints::Util::RELATION_TYPES.each_key do |name| module_eval <<-"end_code" def #{name}(expression, options = {}) relation = @method_relations[:#{name}] @params.update( - Gecode::Constraints::OptionUtil.decode_options(options)) + Gecode::Constraints::Util.decode_options(options)) if self.simple_expression? and simple_expression?(expression) # A relation constraint is enough. add_relation_constraint(relation, expression) else add_linear_constraint(relation, expression) end end end_code end - - # Various aliases. - METHOD_ALIASES.each_pair do |orig, alias_names| - alias_names.each do |name| - alias_method name, orig - end - end + alias_comparison_methods protected # Checks whether the given expression is simple enough to be used in a # simple relation constraint. Returns true if it is, false otherwise. If \ No newline at end of file