Class: Logica::Predicates::Base

Inherits:
Object
  • Object
show all
Includes:
ComparableByState, OpenHouse::Acceptor
Defined in:
lib/logica/predicates/base.rb

Constant Summary collapse

ACCEPTOR_TYPE_ID =
:predicate

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ComparableByState

#==, #hash, #state

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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

Class Method Details

.predicate_factoryObject



9
10
11
# File 'lib/logica/predicates/base.rb', line 9

def self.predicate_factory
  Logica.predicate_factory
end

Instance Method Details

#and(other) ⇒ Object



17
18
19
# File 'lib/logica/predicates/base.rb', line 17

def and(other)
  other.and_with_other(self)
end

#and_not(other) ⇒ Object



29
30
31
# File 'lib/logica/predicates/base.rb', line 29

def and_not(other)
  self.and(other.negated)
end

#arityObject



92
93
94
# File 'lib/logica/predicates/base.rb', line 92

def arity
  to_method.arity
end

#disjoint_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/logica/predicates/base.rb', line 66

def disjoint_with?(other)
  other.disjoint_with_other?(self)
end

#exhaustive_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/logica/predicates/base.rb', line 70

def exhaustive_with?(other)
  other.exhaustive_with_other?(self)
end

#generalization_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/logica/predicates/base.rb', line 54

def generalization_of?(other)
  other.specialization_of?(self)
end

#generalization_of_negation_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/logica/predicates/base.rb', line 62

def generalization_of_negation_of?(other)
  false
end

#generalization_of_other?(other) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/logica/predicates/base.rb', line 58

def generalization_of_other?(other)
  other == self
end

#iff(other) ⇒ Object



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



41
42
43
# File 'lib/logica/predicates/base.rb', line 41

def implies(other)
  negated.or(other)
end

#name_and_attributesObject



104
105
106
# File 'lib/logica/predicates/base.rb', line 104

def name_and_attributes
  "#{name}(#{attributes.values.join(', ')})"
end

#negatedObject



25
26
27
# File 'lib/logica/predicates/base.rb', line 25

def negated
  predicate_factory.negation(self)
end

#or(other) ⇒ Object



21
22
23
# File 'lib/logica/predicates/base.rb', line 21

def or(other)
  other.or_with_other(self)
end

#or_not(other) ⇒ Object



33
34
35
# File 'lib/logica/predicates/base.rb', line 33

def or_not(other)
  self.or(other.negated)
end

#partially_applied_with(*first_arguments) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object



74
75
76
# 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) ⇒ Object



78
79
80
# 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) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
# 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) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/logica/predicates/base.rb', line 50

def specialization_of?(other)
  other.generalization_of_other?(self)
end

#to_methodObject



88
89
90
# File 'lib/logica/predicates/base.rb', line 88

def to_method
  method(:satisfied_by?)
end

#to_procObject



96
97
98
# File 'lib/logica/predicates/base.rb', line 96

def to_proc
  to_method.to_proc
end

#to_sObject



100
101
102
# File 'lib/logica/predicates/base.rb', line 100

def to_s
  "#{name_and_attributes}#{to_s_suffix}"
end

#unsatisfied_by?(*arguments) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/logica/predicates/base.rb', line 13

def unsatisfied_by?(*arguments)
  !satisfied_by?(*arguments)
end

#xor(other) ⇒ Object



37
38
39
# File 'lib/logica/predicates/base.rb', line 37

def xor(other)
  self.or(other).and(self.and(other).negated)
end