Module: Alf::Predicate::Factory

Extended by:
Factory
Included in:
Alf::Predicate, Expr, Factory
Defined in:
lib/alf-predicate/alf/predicate/factory.rb

Instance Method Summary collapse

Instance Method Details

#_factor_predicate(arg) ⇒ Object



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

def _factor_predicate(arg)
  sexpr(arg)
end

#and(left, right = nil) ⇒ Object



17
18
19
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 17

def and(left, right = nil)
  _factor_predicate([:and, sexpr(left), sexpr(right)])
end

#between(middle, lower_bound, upper_bound) ⇒ Object



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

def between(middle, lower_bound, upper_bound)
  _factor_predicate [:and, [:gte, sexpr(middle), sexpr(lower_bound)],
                           [:lte, sexpr(middle), sexpr(upper_bound)]]
end

#comp(op, h) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 49

def comp(op, h)
  h = h.to_hash
  if h.empty?
    return tautology
  elsif h.size==1
    _factor_predicate [op, sexpr(h.keys.first), sexpr(h.values.last)]
  else
    terms = h.to_a.inject([:and]) do |anded,pair|
      anded << ([op] << sexpr(pair.first) << sexpr(pair.last))
    end
    _factor_predicate terms
  end
end

#contradictionObject



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

def contradiction
  _factor_predicate([:contradiction, false])
end

#in(var_ref, values) ⇒ Object



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

def in(var_ref, values)
  var_ref = sexpr(var_ref) if var_ref.is_a?(Symbol)
  _factor_predicate([:in, var_ref, values])
end

#literal(literal) ⇒ Object



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

def literal(literal)
  _factor_predicate([:literal, literal])
end

#native(proc) ⇒ Object



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

def native(proc)
  _factor_predicate([:native, proc])
end

#not(operand) ⇒ Object



25
26
27
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 25

def not(operand)
  _factor_predicate([:not, sexpr(operand)])
end

#or(left, right = nil) ⇒ Object



21
22
23
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 21

def or(left, right = nil)
  _factor_predicate([:or, sexpr(left), sexpr(right)])
end

#relation(r) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 29

def relation(r)
  tuples = r.to_a
  case tuples.size
  when 0 then contradiction
  when 1 then eq(tuples.first)
  else
    if tuples.first.size==1
      k = tuples.first.keys.first
      self.in(k, tuples.map{|t| t[k]})
    else
      tuples.inject(contradiction){|p,t| p | eq(t) }
    end
  end
end

#sexpr(expr) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 83

def sexpr(expr)
  case expr
  when Expr       then expr
  when Predicate  then expr.expr
  when TrueClass  then Grammar.sexpr([:tautology, true])
  when FalseClass then Grammar.sexpr([:contradiction, false])
  when Symbol     then Grammar.sexpr([:var_ref, expr])
  when Proc       then Grammar.sexpr([:native, expr])
  when Array      then Grammar.sexpr(expr)
  else
    Grammar.sexpr([:literal, expr])
  end
end

#tautologyObject



5
6
7
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 5

def tautology
  _factor_predicate([:tautology, true])
end

#var_ref(name) ⇒ Object



13
14
15
# File 'lib/alf-predicate/alf/predicate/factory.rb', line 13

def var_ref(name)
  _factor_predicate([:var_ref, name])
end