Class: Axiom::Relation

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat, Visitable, Enumerable
Defined in:
lib/axiom/relation.rb,
lib/axiom/relation/base.rb,
lib/axiom/relation/keys.rb,
lib/axiom/relation/empty.rb,
lib/axiom/relation/index.rb,
lib/axiom/relation/proxy.rb,
lib/axiom/relation/header.rb,
lib/axiom/relation/variable.rb,
lib/axiom/relation/materialized.rb,
lib/axiom/relation/operation/set.rb,
lib/axiom/relation/operation/wrap.rb,
lib/axiom/relation/operation/group.rb,
lib/axiom/relation/operation/limit.rb,
lib/axiom/relation/operation/unary.rb,
lib/axiom/relation/operation/binary.rb,
lib/axiom/relation/operation/offset.rb,
lib/axiom/relation/operation/sorted.rb,
lib/axiom/relation/operation/unwrap.rb,
lib/axiom/relation/operation/reverse.rb,
lib/axiom/relation/operation/ungroup.rb,
lib/axiom/relation/operation/deletion.rb,
lib/axiom/relation/operation/insertion.rb,
lib/axiom/relation/operation/combination.rb,
lib/axiom/relation/operation/sorted/direction.rb,
lib/axiom/relation/operation/sorted/direction_set.rb

Overview

Abstract base class for Relation operations

Defined Under Namespace

Modules: Operation, Proxy Classes: Base, Empty, Header, Index, Keys, Materialized, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Visitable

#accept

Constructor Details

#initialize(header, tuples) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Relation

Parameters:

  • header (Header, #to_ary)

    the relation header

  • tuples (Enumerable)

    the relation tuples



86
87
88
89
# File 'lib/axiom/relation.rb', line 86

def initialize(header, tuples)
  @header = Header.coerce(header)
  @tuples = tuples
end

Instance Attribute Details

#headerHeader (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The relation header

Returns:



15
16
17
# File 'lib/axiom/relation.rb', line 15

def header
  @header
end

Class Method Details

.new(*args) ⇒ Relation

Instantiate a new Relation

Examples:

of a materialized Array based relation

array    = [[1], [2], [3]]
relation = Relation.new([[:id, Integer]], array)

of a materialized Set based relation

set      = Set[[1], [2], [3]]
relation = Relation.new([[:id, Integer]], set)

of a non-materialized Enumerator based relation

enumerator = [[1], [2], [3]].each
relation   = Relation.new([[:id, Integer]], enumerator)

Parameters:

  • args (Array(Header, Enumerable))

Returns:



53
54
55
56
57
58
59
# File 'lib/axiom/relation.rb', line 53

def self.new(*args)
  if equal?(Relation) && materialized?(args[1])
    Materialized.new(*args)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compare the relation with other relation for equivalency

Examples:

relation == other  # => true or false

Parameters:

  • other (Relation)

    the other relation to compare with

Returns:

  • (Boolean)


197
198
199
200
201
202
# File 'lib/axiom/relation.rb', line 197

def ==(other)
  other = coerce(other)
  other.kind_of?(Relation) &&
  header == other.header   &&
  to_set == other.to_set
end

#[](name) ⇒ Attribute

Lookup an Attribute in the header given an attribute name

Examples:

attribute = relation[name]

Parameters:

  • name (#to_sym)

    the attribute name

Returns:



102
103
104
# File 'lib/axiom/relation.rb', line 102

def [](name)
  header[name]
end

#directionsOperation::Sorted::DirectionSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The relation sort order



30
31
32
# File 'lib/axiom/relation.rb', line 30

def directions
  Operation::Sorted::DirectionSet::EMPTY
end

#each {|tuple| ... } ⇒ self

Iterate over each tuple in the set

Examples:

relation = Relation.new(header, tuples)
relation.each { |tuple| ... }

Yields:

  • (tuple)

Yield Parameters:

  • tuple (Tuple)

    each tuple in the set

Returns:

  • (self)


120
121
122
123
124
125
126
127
128
# File 'lib/axiom/relation.rb', line 120

def each
  return to_enum unless block_given?
  seen = {}
  tuples.each do |tuple|
    tuple = Tuple.coerce(header, tuple)
    yield seen[tuple] = tuple unless seen.key?(tuple)
  end
  self
end

#empty?Boolean

Test if there are no tuples

Examples:

relation.empty?  # => true or false

Returns:

  • (Boolean)


212
213
214
# File 'lib/axiom/relation.rb', line 212

def empty?
  none?
end

#include?(tuple) ⇒ Boolean

Test if the tuple exists in the relation

Examples:

relation.include?(tuple)  # => true or false

Parameters:

Returns:

  • (Boolean)


182
183
184
# File 'lib/axiom/relation.rb', line 182

def include?(tuple)
  to_set.include?(tuple)
end

#materializeMaterialized

Return a relation with each tuple materialized

Examples:

materialized = relation.materialize

Returns:



156
157
158
# File 'lib/axiom/relation.rb', line 156

def materialize
  Materialized.new(header, to_a, directions)
end

#materialized?false

Return false for a non-Materialized relation

Examples:

relation.materialized?  # => false

Returns:

  • (false)


168
169
170
# File 'lib/axiom/relation.rb', line 168

def materialized?
  false
end

#replace(other) ⇒ Relation::Operation::Insertion

Return a relation that represents a replacement of a relation

Delete the tuples from the relation that are not in the other relation, then insert only new tuples.

Examples:

replacement = relation.delete(other)

Parameters:

  • other (Enumerable)

Returns:



143
144
145
146
# File 'lib/axiom/relation.rb', line 143

def replace(other)
  other = coerce(other)
  delete(difference(other)).insert(other.difference(self))
end