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_s, #to_sequel, #unqualify, #|

Methods included from Factory

#and, #between, #comp, #contradiction, #from_hash, #in, #intersect, #literal, #match, #native, #not, #opaque, #or, #placeholder, #qualified_identifier, #tautology

Instance Method Details

#&(other) ⇒ Object



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

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



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

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

#constantsObject



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

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

#dyadic_priorityObject



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

def dyadic_priority
  800
end

#evaluate(tuple) ⇒ Object

Raises:



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

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

#free_variablesObject



44
45
46
# File 'lib/predicate/nodes/in.rb', line 44

def free_variables
  @free_variables ||= identifier.free_variables
end

#leftObject Also known as: identifier



9
10
11
# File 'lib/predicate/nodes/in.rb', line 9

def left
  self[1]
end

#priorityObject



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

def priority
  80
end

#rightObject



14
15
16
# File 'lib/predicate/nodes/in.rb', line 14

def right
  self[2]
end

#to_hashObject



78
79
80
81
# File 'lib/predicate/nodes/in.rb', line 78

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

#var_against_literal_value?Boolean

Returns:

  • (Boolean)


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

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