class Logica::Predicates::Compounds::AtLeast

Attributes

amount[R]

Public Class Methods

new(amount, predicates) click to toggle source
Calls superclass method
# File lib/logica/predicates/compounds/at_least.rb, line 7
def initialize(amount, predicates)
  super(predicates)
  @amount = amount
end

Public Instance Methods

and(other) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 23
def and(other)
  other.and_with_at_least(self)
end
and_with_other(other, options = {}) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 27
def and_with_other(other, options = {})
  default_options = {
    other_first: true
  }
  options = default_options.merge(options)

  subsumed = predicates.select { |pred| pred.generalization_of?(other) }
  new_amount = amount - subsumed.size
  new_predicates = predicates - subsumed
  new_at_least = predicate_factory.at_least(new_amount, new_predicates)

  pair = options[:other_first] ? [other, new_at_least] : [new_at_least, other]
  predicate_factory.conjunction_from_pair(*pair)
end
name_and_attributes() click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 74
def name_and_attributes
  "#{name}(#{amount}, [#{attributes_string}])"
end
portion_satisfied_by(*arguments) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 42
def portion_satisfied_by(*arguments)
  if satisfied_by?(*arguments)
    self
  else
    to_disjunction.portion_satisfied_by(*arguments)
  end
end
remainder_unsatisfied_by(*arguments) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 50
def remainder_unsatisfied_by(*arguments)
  if satisfied_by?(*arguments)
    predicate_factory.contradiction
  else
    unsatisfied_predicates = predicates_unsatisfied_by(*arguments)
    satisfied_count = predicates.size - unsatisfied_predicates.size
    new_amount = amount - satisfied_count

    predicate_factory.at_least(new_amount, unsatisfied_predicates)
  end
end
satisfied_by?(*arguments) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 12
def satisfied_by?(*arguments)
  true_count = 0
  predicates_count = predicates.size
  predicates.each_with_index do |predicate, index|
    true_count += 1 if predicate.satisfied_by?(*arguments)
    return true if true_count >= amount
    remaining_predicates_count = predicates_count - (index + 1)
    return false if true_count + remaining_predicates_count < amount
  end
end
to_disjunction() click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 62
def to_disjunction
  @to_disjunction ||= begin
    combinations = predicates.combination(amount)

    conjunctions = combinations.map do |amount_preds|
      predicate_factory.conjunction(amount_preds)
    end

    predicate_factory.disjunction(conjunctions)
  end
end

Private Instance Methods

do_method_missing(name, *args, &block) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 84
def do_method_missing(name, *args, &block)
  if respond_to?(name)
    to_disjunction.public_send(name, *args, &block)
  else
    super
  end
end
do_respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 92
def do_respond_to_missing?(method_name, include_private = false)
  to_disjunction.respond_to?(method_name) || super
end
new_from_list(preds) click to toggle source
# File lib/logica/predicates/compounds/at_least.rb, line 80
def new_from_list(preds)
  self.class.new(amount, preds)
end