Module: Predicate::In

Includes:
Expr
Defined in:
lib/predicate/nodes/in.rb

Constant Summary

Constants included from Expr

Expr::OP_NEGATIONS

Instance Method Summary collapse

Methods included from Expr

#!, #and_split, #attr_split, #bind, #contradiction?, #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, #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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/predicate/nodes/in.rb', line 16

def &(other)
  case other
  when Eq
    other & self
  when In
    # we only optimize is same free variables
    fv = free_variables
    return super unless fv.size == 1 && fv == other.free_variables

    # we only optimize if both right terms are literals
    return super unless right.literal? and other.right.literal?
    return super if right.has_placeholder? or other.right.has_placeholder?

    intersection = right.value & other.right.value
    if intersection.empty?
      Factory.contradiction
    elsif intersection.size == 1
      Factory.eq(fv.first, [:literal, intersection.first])
    else
      Factory.in(fv.first, intersection)
    end
  else
    super
  end
end

#constant_variablesObject



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

def constant_variables
  if right.literal? and right.singleton_value?
    free_variables
  else
    []
  end
end

#constantsObject



54
55
56
57
58
59
60
# File 'lib/predicate/nodes/in.rb', line 54

def constants
  if right.literal? and right.singleton_value?
    { identifier.name => right.value.first }
  else
    {}
  end
end

#dyadic_priorityObject



62
# File 'lib/predicate/nodes/in.rb', line 62

def dyadic_priority; 800; end

#evaluate(tuple) ⇒ Object

Raises:



64
65
66
67
68
# File 'lib/predicate/nodes/in.rb', line 64

def evaluate(tuple)
  values = right.evaluate(tuple)
  raise UnboundError if values.is_a?(Placeholder)
  values.include?(identifier.evaluate(tuple))
end

#free_variablesObject



42
43
44
# File 'lib/predicate/nodes/in.rb', line 42

def free_variables
  @free_variables ||= identifier.free_variables
end

#leftObject Also known as: identifier



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

def left
  self[1]
end

#priorityObject



5
# File 'lib/predicate/nodes/in.rb', line 5

def priority; 80; end

#rightObject



12
13
14
# File 'lib/predicate/nodes/in.rb', line 12

def right
  self[2]
end

#to_hashObject



74
75
76
77
# File 'lib/predicate/nodes/in.rb', line 74

def to_hash
  return super unless var_against_literal_value?
  { identifier.name => right.value }
end

#var_against_literal_value?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/predicate/nodes/in.rb', line 70

def var_against_literal_value?
  left.identifier? && right.literal? && !right.has_placeholder?
end