Module: Alf::Predicate::Factory

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

Instance Method Summary collapse

Instance Method Details

#_factor_predicate(arg) ⇒ Object


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

def _factor_predicate(arg)
  sexpr(arg)
end

#and(left, right = nil) ⇒ Object


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

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

#between(middle, lower_bound, upper_bound) ⇒ Object


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

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


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/alf/predicate/factory.rb', line 54

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/factory.rb', line 9

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

#identifier(name) ⇒ Object


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

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

#in(identifier, values) ⇒ Object Also known as: among


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

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

#literal(literal) ⇒ Object


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

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

#native(arg) ⇒ Object


84
85
86
# File 'lib/alf/predicate/factory.rb', line 84

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

#not(operand) ⇒ Object


29
30
31
# File 'lib/alf/predicate/factory.rb', line 29

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

#or(left, right = nil) ⇒ Object


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

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

#qualified_identifier(qualifier, name) ⇒ Object


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

def qualified_identifier(qualifier, name)
  _factor_predicate([:qualified_identifier, qualifier, name])
end

#relation(r) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/alf/predicate/factory.rb', line 33

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


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/alf/predicate/factory.rb', line 88

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([:identifier, 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/factory.rb', line 5

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