class Logica::Predicates::Base

Constants

ACCEPTOR_TYPE_ID

Public Class Methods

predicate_factory() click to toggle source
# File lib/logica/predicates/base.rb, line 9
def self.predicate_factory
  Logica.predicate_factory
end

Public Instance Methods

and(other) click to toggle source
# File lib/logica/predicates/base.rb, line 17
def and(other)
  other.and_with_other(self)
end
and_not(other) click to toggle source
# File lib/logica/predicates/base.rb, line 29
def and_not(other)
  self.and(other.negated)
end
arity() click to toggle source
# File lib/logica/predicates/base.rb, line 92
def arity
  to_method.arity
end
disjoint_with?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 66
def disjoint_with?(other)
  other.disjoint_with_other?(self)
end
exhaustive_with?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 70
def exhaustive_with?(other)
  other.exhaustive_with_other?(self)
end
generalization_of?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 54
def generalization_of?(other)
  other.specialization_of?(self)
end
generalization_of_negation_of?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 62
def generalization_of_negation_of?(other)
  false
end
generalization_of_other?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 58
def generalization_of_other?(other)
  other == self
end
iff(other) click to toggle source
# File lib/logica/predicates/base.rb, line 45
def iff(other)
  # this is equivalent to xor(other).negated
  self.and(other).or(self.or(other).negated)
end
implies(other) click to toggle source
# File lib/logica/predicates/base.rb, line 41
def implies(other)
  negated.or(other)
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/logica/predicates/base.rb, line 108
def method_missing(name, *args, &block)
  if name.to_s.start_with?('and_with_')
    and_with_other(*args)
  elsif name.to_s.start_with?('or_with_')
    or_with_other(*args)
  elsif name.to_s.start_with?('generalization_of_')
    generalization_of_other?(*args)
  elsif name.to_s.start_with?('disjoint_with_')
    disjoint_with_other?(*args)
  elsif name.to_s.start_with?('exhaustive_with_')
    exhaustive_with_other?(*args)
  else
    ret = do_method_missing(name, *args, &block)
    ret == :__super__ ? super : ret
  end
end
name_and_attributes() click to toggle source
# File lib/logica/predicates/base.rb, line 104
def name_and_attributes
  "#{name}(#{attributes.values.join(', ')})"
end
negated() click to toggle source
# File lib/logica/predicates/base.rb, line 25
def negated
  predicate_factory.negation(self)
end
or(other) click to toggle source
# File lib/logica/predicates/base.rb, line 21
def or(other)
  other.or_with_other(self)
end
or_not(other) click to toggle source
# File lib/logica/predicates/base.rb, line 33
def or_not(other)
  self.or(other.negated)
end
partially_applied_with(*first_arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 82
def partially_applied_with(*first_arguments)
  return self if first_arguments.empty?
  validate_partial_application(first_arguments)
  do_partially_applied_with(first_arguments)
end
portion_satisfied_by(*arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 74
def portion_satisfied_by(*arguments)
  satisfied_by?(*arguments) ? self : predicate_factory.tautology
end
remainder_unsatisfied_by(*arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 78
def remainder_unsatisfied_by(*arguments)
  satisfied_by?(*arguments) ? predicate_factory.contradiction : self
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/logica/predicates/base.rb, line 125
def respond_to_missing?(method_name, include_private = false)
  prefixes = %w(and_with or_with generalization_of_ disjoint_with_ exhaustive_with_)

  prefixes.any? { |prefix| method_name.to_s.start_with?(prefix) } ||
    begin
      ret = do_respond_to_missing?(method_name, include_private)
      ret == :__super__ ? super : ret
    end
end
specialization_of?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 50
def specialization_of?(other)
  other.generalization_of_other?(self)
end
to_method() click to toggle source
# File lib/logica/predicates/base.rb, line 88
def to_method
  method(:satisfied_by?)
end
to_proc() click to toggle source
# File lib/logica/predicates/base.rb, line 96
def to_proc
  to_method.to_proc
end
to_s() click to toggle source
# File lib/logica/predicates/base.rb, line 100
def to_s
  "#{name_and_attributes}#{to_s_suffix}"
end
unsatisfied_by?(*arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 13
def unsatisfied_by?(*arguments)
  !satisfied_by?(*arguments)
end
xor(other) click to toggle source
# File lib/logica/predicates/base.rb, line 37
def xor(other)
  self.or(other).and(self.and(other).negated)
end

Protected Instance Methods

and_with_at_least(at_least) click to toggle source
# File lib/logica/predicates/base.rb, line 141
def and_with_at_least(at_least)
  at_least.and_with_other(self, other_first: false)
end
and_with_conjunction(conjunction) click to toggle source
# File lib/logica/predicates/base.rb, line 137
def and_with_conjunction(conjunction)
  conjunction.with_extra_predicate_last(self)
end
and_with_other(other) click to toggle source
# File lib/logica/predicates/base.rb, line 145
def and_with_other(other)
  predicate_factory.conjunction_from_pair(other, self)
end
disjoint_with_other?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 161
def disjoint_with_other?(other)
  specialization_of?(other.negated)
end
exhaustive_with_other?(other) click to toggle source
# File lib/logica/predicates/base.rb, line 165
def exhaustive_with_other?(other)
  negated.specialization_of?(other)
end
generalization_of_conjunction?(conjunction) click to toggle source
# File lib/logica/predicates/base.rb, line 157
def generalization_of_conjunction?(conjunction)
  conjunction.specialization_of_other?(self)
end
or_with_disjunction(disjunction) click to toggle source
# File lib/logica/predicates/base.rb, line 149
def or_with_disjunction(disjunction)
  disjunction.with_extra_predicate_last(self)
end
or_with_other(other) click to toggle source
# File lib/logica/predicates/base.rb, line 153
def or_with_other(other)
  predicate_factory.disjunction_from_pair(other, self)
end
to_s_suffix() click to toggle source
# File lib/logica/predicates/base.rb, line 173
def to_s_suffix
  " |#{arity}|"
end
without_predicates(preds, options = {}) click to toggle source
# File lib/logica/predicates/base.rb, line 169
def without_predicates(preds, options = {})
  preds.include?(self) ? [] : [self]
end

Private Instance Methods

do_method_missing(name, *args, &block) click to toggle source
# File lib/logica/predicates/base.rb, line 201
def do_method_missing(name, *args, &block)
  :__super__
end
do_partially_applied_with(first_arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 189
def do_partially_applied_with(first_arguments)
  predicate_factory.partial_application(self, first_arguments)
end
do_respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/logica/predicates/base.rb, line 205
def do_respond_to_missing?(method_name, include_private = false)
  :__super__
end
name() click to toggle source
# File lib/logica/predicates/base.rb, line 193
def name
  self.class.name.split('::').last
end
partial_application_error_message(first_arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 185
def partial_application_error_message(first_arguments)
  "cannot partially apply #{self} with #{first_arguments} (too many arguments)"
end
predicate_factory() click to toggle source
# File lib/logica/predicates/base.rb, line 197
def predicate_factory
  self.class.predicate_factory
end
validate_partial_application(first_arguments) click to toggle source
# File lib/logica/predicates/base.rb, line 179
def validate_partial_application(first_arguments)
  if first_arguments.size > arity
    raise ArgumentError, partial_application_error_message(first_arguments)
  end
end