module Predicate::In

Public Instance Methods

&(other) click to toggle source
Calls superclass method Predicate::Expr#&
# 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_variables() click to toggle source
# File lib/predicate/nodes/in.rb, line 46
def constant_variables
  if right.literal? and right.singleton_value?
    free_variables
  else
    []
  end
end
constants() click to toggle source
# 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_priority() click to toggle source
# File lib/predicate/nodes/in.rb, line 62
def dyadic_priority; 800; end
evaluate(tuple) click to toggle source
# 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_variables() click to toggle source
# File lib/predicate/nodes/in.rb, line 42
def free_variables
  @free_variables ||= identifier.free_variables
end
identifier()
Alias for: left
left() click to toggle source
# File lib/predicate/nodes/in.rb, line 7
def left
  self[1]
end
Also aliased as: identifier
priority() click to toggle source
# File lib/predicate/nodes/in.rb, line 5
def priority; 80; end
right() click to toggle source
# File lib/predicate/nodes/in.rb, line 12
def right
  self[2]
end
to_hash() click to toggle source
Calls superclass method Predicate::Expr#to_hash
# 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?() click to toggle source
# File lib/predicate/nodes/in.rb, line 70
def var_against_literal_value?
  left.identifier? && right.literal? && !right.has_placeholder?
end