Class: Alf::Predicate

Inherits:
Object
  • Object
show all
Extended by:
Factory
Defined in:
lib/alf/predicate.rb,
lib/alf/predicate/factory.rb,
lib/alf/predicate/grammar.rb,
lib/alf/predicate/nodes/in.rb,
lib/alf/predicate/nodes/eq.rb,
lib/alf/predicate/nodes/gt.rb,
lib/alf/predicate/nodes/or.rb,
lib/alf/predicate/nodes/lt.rb,
lib/alf/predicate/nodes/not.rb,
lib/alf/predicate/nodes/gte.rb,
lib/alf/predicate/nodes/lte.rb,
lib/alf/predicate/nodes/and.rb,
lib/alf/predicate/nodes/neq.rb,
lib/alf/predicate/nodes/expr.rb,
lib/alf/predicate/nodes/native.rb,
lib/alf/predicate/nodes/literal.rb,
lib/alf/predicate/nodes/tautology.rb,
lib/alf/predicate/nodes/identifier.rb,
lib/alf/predicate/nodes/nadic_bool.rb,
lib/alf/predicate/nodes/dyadic_comp.rb,
lib/alf/predicate/processors/renamer.rb,
lib/alf/predicate/nodes/contradiction.rb,
lib/alf/predicate/processors/qualifier.rb,
lib/alf/predicate/processors/to_ruby_code.rb,
lib/alf/predicate/nodes/qualified_identifier.rb

Defined Under Namespace

Modules: And, Contradiction, DyadicComp, Eq, Expr, Factory, Gt, Gte, Identifier, In, Literal, Lt, Lte, NadicBool, Native, Neq, Not, Or, QualifiedIdentifier, Tautology Classes: Qualifier, Renamer, ToRubyCode

Constant Summary collapse

Grammar =
Sexpr.load Path.relative('grammar.sexp.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Factory

_factor_predicate, and, between, comp, contradiction, identifier, in, literal, native, not, or, qualified_identifier, relation, tautology

Constructor Details

#initialize(sexpr) ⇒ Predicate

Returns a new instance of Predicate


7
8
9
# File 'lib/alf/predicate.rb', line 7

def initialize(sexpr)
  @sexpr = sexpr
end

Instance Attribute Details

#sexprObject (readonly) Also known as: expr

Returns the value of attribute sexpr


10
11
12
# File 'lib/alf/predicate.rb', line 10

def sexpr
  @sexpr
end

Class Method Details

.coerce(arg) ⇒ Object Also known as: parse


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/alf/predicate.rb', line 16

def coerce(arg)
  case arg
  when Predicate   then arg
  when TrueClass   then tautology
  when FalseClass  then contradiction
  when Symbol      then identifier(arg)
  when Proc        then native(arg)
  when Hash, Tuple then eq(arg)
  when Relation    then relation(arg)
  else
    raise ArgumentError, "Unable to coerce `#{arg}` to a predicate"
  end
end

Instance Method Details

#!Object


71
72
73
# File 'lib/alf/predicate.rb', line 71

def !
  Predicate.new(!expr)
end

#&(other) ⇒ Object


59
60
61
62
63
# File 'lib/alf/predicate.rb', line 59

def &(other)
  return self  if other.tautology? or other==self
  return other if tautology?
  Predicate.new(expr & other.expr)
end

#==(other) ⇒ Object Also known as: eql?


96
97
98
# File 'lib/alf/predicate.rb', line 96

def ==(other)
  other.is_a?(Predicate) && (other.expr==expr)
end

#and_split(attr_list) ⇒ Object


92
93
94
# File 'lib/alf/predicate.rb', line 92

def and_split(attr_list)
  expr.and_split(attr_list).map{|e| Predicate.new(e)}
end

#constant_variablesObject


55
56
57
# File 'lib/alf/predicate.rb', line 55

def constant_variables
  expr.constant_variables
end

#contradiction?Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/alf/predicate.rb', line 47

def contradiction?
  expr.contradiction?
end

#evaluate(scope) ⇒ Object


83
84
85
86
87
88
89
90
# File 'lib/alf/predicate.rb', line 83

def evaluate(scope)
  proc = to_proc
  if proc.arity == 1
    proc.call(scope)
  else
    scope.instance_exec(&to_proc)
  end
end

#free_variablesObject


51
52
53
# File 'lib/alf/predicate.rb', line 51

def free_variables
  expr.free_variables
end

#hashObject


101
102
103
# File 'lib/alf/predicate.rb', line 101

def hash
  expr.hash
end

#native?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/alf/predicate.rb', line 39

def native?
  Native===expr
end

#qualify(qualifier) ⇒ Object


75
76
77
# File 'lib/alf/predicate.rb', line 75

def qualify(qualifier)
  Predicate.new(expr.qualify(qualifier))
end

#rename(renaming) ⇒ Object


79
80
81
# File 'lib/alf/predicate.rb', line 79

def rename(renaming)
  Predicate.new(expr.rename(renaming))
end

#tautology?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'lib/alf/predicate.rb', line 43

def tautology?
  expr.tautology?
end

#to_lispyObject


114
115
116
# File 'lib/alf/predicate.rb', line 114

def to_lispy
  to_ruby_code
end

#to_procObject


110
111
112
# File 'lib/alf/predicate.rb', line 110

def to_proc
  @proc ||= expr.to_proc("t")
end

#to_ruby_code(scope = "t") ⇒ Object Also known as: to_s


105
106
107
# File 'lib/alf/predicate.rb', line 105

def to_ruby_code(scope = "t")
  expr.to_ruby_code(scope)
end

#to_ruby_literalObject


118
119
120
# File 'lib/alf/predicate.rb', line 118

def to_ruby_literal
  to_lispy
end

#|(other) ⇒ Object


65
66
67
68
69
# File 'lib/alf/predicate.rb', line 65

def |(other)
  return self  if other.contradiction? or other==self
  return other if contradiction?
  Predicate.new(expr | other.expr)
end