Class: Alf::Relation

Inherits:
Object
  • Object
show all
Includes:
Algebra::Operand, Lang::ObjectOriented, Enumerable
Defined in:
lib/alf-relation/alf/relation.rb

Overview

Defines an in-memory relation data structure.

A relation is a set of tuples; a tuple is a set of attribute (name, value) pairs. The class implements such a data structure with full relational algebra installed as instance methods.

Relation values can be obtained in various ways, for example by invoking a relational operator on an existing relation. Relation literals are simply constructed as follows:

Alf::Relation([
  # ... a comma list of ruby hashes ...
])

See main Alf documentation about relational operators.

Constant Summary collapse

DUM_TYPE =
DUM =
DUM_TYPE.new([])
DEE =
DEE_TYPE.new([{}])

Instance Attribute Summary

Attributes included from Support::Bindable

#connection

Instance Method Summary collapse

Methods included from Lang::ObjectOriented

new

Methods included from Lang::ObjectOriented::RenderingMethods

def_renderer_method, #to_a, #to_array

Methods included from Lang::ObjectOriented::AlgebraMethods

#!~, #&, #*, #+, #-, #=~, #allbut, def_operator_method, #tuple_extract

Methods included from Lang::ObjectOriented::AggregationMethods

def_aggregator_method

Methods included from Algebra::Operand

#attr_list, coerce, #keys, #to_dot, #to_relation

Methods included from Support::Bindable

#bind, #bound?, #connection!

Constructor Details

#initialize(tuples) ⇒ Relation

Returns a new instance of Relation.



26
27
28
# File 'lib/alf-relation/alf/relation.rb', line 26

def initialize(tuples)
  super(tuples.map{|x| Tuple.coerce(x)}.to_set)
end

Instance Method Details

#[](*args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/alf-relation/alf/relation.rb', line 61

def [](*args)
  attrs = args.map{|arg| arg.is_a?(Hash) ? arg.keys : arg }.flatten
  handler = ->(v){ v.is_a?(Symbol) ? ->{ __send__(v) } : v }
  extension = args.each_with_object({}) do |arg, ext|
    case arg
    when Symbol
      ext[arg] = handler[arg]
    when Hash
      arg.each_pair do |k,v|
        ext[k] = handler[v]
      end
    end
  end
  self.extend(extension).project(attrs)
end

#check_internal_representation!Object



50
51
52
53
54
55
# File 'lib/alf-relation/alf/relation.rb', line 50

def check_internal_representation!
  error = lambda{|msg| raise TypeError, msg }
  error["Set expected"]        unless reused_instance.is_a?(Set)
  error["Superclass mismatch"] unless self.class.superclass == Relation
  self
end

#headingObject

Returns the relation heading



78
79
80
# File 'lib/alf-relation/alf/relation.rb', line 78

def heading
  self.class.heading
end

#to_attr_listObject

Returns the attribute list.



83
84
85
# File 'lib/alf-relation/alf/relation.rb', line 83

def to_attr_list
  heading.to_attr_list
end

#to_cogObject

Returns an engine Cog



93
94
95
# File 'lib/alf-relation/alf/relation.rb', line 93

def to_cog
  Engine::Leaf.new(self)
end

#to_relvarObject

Returns a ReadOnly relvar



88
89
90
# File 'lib/alf-relation/alf/relation.rb', line 88

def to_relvar
  Relvar::ReadOnly.new(self)
end

#to_ruby_literalObject Also known as: inspect

Returns a literal representation of this relation



103
104
105
# File 'lib/alf-relation/alf/relation.rb', line 103

def to_ruby_literal
  "Alf::Relation([" + tuples.map{|t| Support.to_ruby_literal(t) }.join(', ') + "])"
end

#to_sObject

Returns a textual representation of this relation



98
99
100
# File 'lib/alf-relation/alf/relation.rb', line 98

def to_s
  to_text
end