Class: Logica::Predicates::Compounds::AtLeast

Inherits:
Base
  • Object
show all
Defined in:
lib/logica/predicates/compounds/at_least.rb

Constant Summary

Constants inherited from Base

Base::ACCEPTOR_TYPE_ID

Instance Attribute Summary collapse

Attributes inherited from Base

#predicates

Instance Method Summary collapse

Methods inherited from Base

#arity, new_from_list, new_from_pair, #with_extra_predicate_last, #with_extra_predicates, #without

Methods inherited from Base

#and_not, #arity, #disjoint_with?, #exhaustive_with?, #generalization_of?, #generalization_of_negation_of?, #generalization_of_other?, #iff, #implies, #method_missing, #negated, #or, #or_not, #partially_applied_with, predicate_factory, #respond_to_missing?, #specialization_of?, #to_method, #to_proc, #to_s, #unsatisfied_by?, #xor

Methods included from ComparableByState

#==, #hash, #state

Constructor Details

#initialize(amount, predicates) ⇒ AtLeast

Returns a new instance of AtLeast.



7
8
9
10
# File 'lib/logica/predicates/compounds/at_least.rb', line 7

def initialize(amount, predicates)
  super(predicates)
  @amount = amount
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Logica::Predicates::Base

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



5
6
7
# File 'lib/logica/predicates/compounds/at_least.rb', line 5

def amount
  @amount
end

Instance Method Details

#and(other) ⇒ Object



23
24
25
# 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 = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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_attributesObject



74
75
76
# File 'lib/logica/predicates/compounds/at_least.rb', line 74

def name_and_attributes
  "#{name}(#{amount}, [#{attributes_string}])"
end

#portion_satisfied_by(*arguments) ⇒ Object



42
43
44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
# 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_disjunctionObject



62
63
64
65
66
67
68
69
70
71
72
# 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