Module: Predicate::And

Includes:
NadicBool
Defined in:
lib/predicate/nodes/and.rb

Constant Summary

Constants included from Expr

Expr::OP_NEGATIONS

Instance Method Summary collapse

Methods included from NadicBool

#free_variables, #priority

Methods included from Expr

#!, #bind, #contradiction?, #dyadic_priority, #identifier?, #literal?, #opaque?, #qualify, #rename, #sexpr, #tautology?, #to_postgres, #to_s, #to_sequel, #unqualify, #|

Methods included from Factory

#_factor_predicate, #and, #comp, #contradiction, #empty, #from_hash, #h, #has_size, #identifier, #in, #literal, #match, #native, #not, #opaque, #or, #pg_array_empty, #pg_array_literal, #pg_array_overlaps, #placeholder, #qualified_identifier, #sexpr, #tautology, #var, #vars

Instance Method Details

#&(other) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/predicate/nodes/and.rb', line 9

def &(other)
  case other
  when Tautology     then self
  when Contradiction then other
  when And           then sexpr(dup + other[1..-1])
  else               dup << other
  end
end

#and_split(attr_list) ⇒ Object



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

def and_split(attr_list)
  # Say we are X = X1 & X2 & X3
  # We will split each term: X = (X1 & Y1) & (X2 & Y2) & (X3 & Y3)
  # ... such that Y1, Y2, and Y2 makes no reference to attr_list
  # ... which is equivalent to (X1 & X2 & X3) & (Y1 & Y2 & Y3)
  # ... hence P1 & P2, that we return
  sexpr_body.inject([tautology, tautology]) do |(top,down),term|
    p1, p2 = term.and_split(attr_list)
    [top & p1, down & p2]
  end
end

#attr_splitObject



30
31
32
33
34
35
36
37
38
# File 'lib/predicate/nodes/and.rb', line 30

def attr_split
  # Similar to and_split above, but the reasonning applies on
  # attribute names this time.
  sexpr_body.each_with_object({}) do |term, h|
    term.attr_split.each_pair do |a,p|
      h[a] = (h[a] || tautology) & p
    end
  end
end

#constant_variablesObject



40
41
42
43
44
# File 'lib/predicate/nodes/and.rb', line 40

def constant_variables
  sexpr_body.inject([]) do |cvars,expr|
    cvars | expr.constant_variables
  end
end

#constantsObject



46
47
48
49
50
# File 'lib/predicate/nodes/and.rb', line 46

def constants
  sexpr_body.each_with_object({}) do |op, cs|
    cs.merge!(op.constants){|k,v1,v2| v1 }
  end
end

#evaluate(tuple) ⇒ Object



52
53
54
# File 'lib/predicate/nodes/and.rb', line 52

def evaluate(tuple)
  sexpr_body.all?{|operand| operand.evaluate(tuple) }
end

#operator_symbolObject



5
6
7
# File 'lib/predicate/nodes/and.rb', line 5

def operator_symbol
  :'&&'
end

#to_hashObject



56
57
58
59
60
61
62
63
# File 'lib/predicate/nodes/and.rb', line 56

def to_hash
  sexpr_body.inject({}) do |p,term|
    p.merge(term.to_hash){|k,v1,v2|
      super unless v1 == v2
      v1
    }
  end
end