Class: Alf::Predicate

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

Defined Under Namespace

Modules: And, Contradiction, DyadicComp, Eq, Expr, Factory, Grammar, Gt, Gte, In, Literal, Lt, Lte, NadicBool, Native, Neq, Not, Or, Tautology, VarRef Classes: Parser, Renamer, ToProc, ToRubyCode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Factory

_factor_predicate, and, between, comp, contradiction, in, literal, native, not, or, relation, sexpr, tautology, var_ref

Constructor Details

#initialize(expr) ⇒ Predicate

Returns a new instance of Predicate.



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

def initialize(expr)
  @expr = expr
end

Instance Attribute Details

#exprObject (readonly)

Returns the value of attribute expr.



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

def expr
  @expr
end

Class Method Details

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/alf-predicate/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 var_ref(arg)
  when Proc        then native(arg)
  when Hash, Tuple then eq(arg)
  when String      then Predicate.new(Grammar.parse(arg))
  when Relation    then relation(arg)
  else
    raise ArgumentError, "Unable to coerce `#{arg}` to a predicate"
  end
end

Instance Method Details

#!Object



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

def !
  Predicate.new(!expr)
end

#&(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#and_split(attr_list) ⇒ Object



89
90
91
# File 'lib/alf-predicate/alf/predicate.rb', line 89

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

#constant_variablesObject



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

def constant_variables
  expr.constant_variables
end

#contradiction?Boolean

Returns:

  • (Boolean)


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

def contradiction?
  expr.contradiction?
end

#evaluate(scope, connection = nil) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/alf-predicate/alf/predicate.rb', line 80

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

#free_variablesObject



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

def free_variables
  expr.free_variables
end

#hashObject



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

def hash
  expr.hash
end

#native?Boolean

Returns:

  • (Boolean)


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

def native?
  Native===expr
end

#rename(renaming) ⇒ Object



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

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

#tautology?Boolean

Returns:

  • (Boolean)


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

def tautology?
  expr.tautology?
end

#to_lispyObject



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

def to_lispy
  "->{ #{to_ruby_code} }"
end

#to_procObject



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

def to_proc
  @proc ||= expr.to_proc(:scope => "self")
end

#to_ruby_codeObject Also known as: to_s



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

def to_ruby_code
  @ruby_code ||= expr.to_ruby_code(:scope => "self")
end

#to_ruby_literalObject



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

def to_ruby_literal
  to_proc.to_ruby_literal
end

#|(other) ⇒ Object



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

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