Class: Alf::Engine::Hierarchize

Inherits:
Object
  • Object
show all
Includes:
Cog
Defined in:
lib/alf/engine/hierarchize.rb

Constant Summary

Constants included from Cog

Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS

Instance Attribute Summary collapse

Attributes included from Compiler::Cog

#compiler, #expr

Instance Method Summary collapse

Methods included from Cog

#each, #options, #to_s

Methods included from Compiler::Cog

#cog_orders, #orderedby?, #to_ascii_tree, #to_cog

Constructor Details

#initialize(operand, id, parent, children, expr = nil, compiler = nil) ⇒ Hierarchize

Creates a Hierarchize instance


25
26
27
28
29
30
31
# File 'lib/alf/engine/hierarchize.rb', line 25

def initialize(operand, id, parent, children, expr = nil, compiler = nil)
  super(expr, compiler)
  @operand = operand
  @id = id
  @parent = parent
  @children = children
end

Instance Attribute Details

#childrenAttrName (readonly)


16
17
18
# File 'lib/alf/engine/hierarchize.rb', line 16

def children
  @children
end

#idAttrList (readonly)


10
11
12
# File 'lib/alf/engine/hierarchize.rb', line 10

def id
  @id
end

#operandEnumerable (readonly)


7
8
9
# File 'lib/alf/engine/hierarchize.rb', line 7

def operand
  @operand
end

#parentAttrList (readonly)


13
14
15
# File 'lib/alf/engine/hierarchize.rb', line 13

def parent
  @parent
end

#relation_typeClass (readonly)


19
20
21
# File 'lib/alf/engine/hierarchize.rb', line 19

def relation_type
  @relation_type
end

#tuple_typeClass (readonly)


22
23
24
# File 'lib/alf/engine/hierarchize.rb', line 22

def tuple_type
  @tuple_type
end

Instance Method Details

#_each(&block) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/alf/engine/hierarchize.rb', line 34

def _each(&block)
  by_id = Hash.new{|h,k| h[k] = {children => nil} }
  operand.each do |tuple|
    tuple = tuple.to_hash
    infer_types(tuple) unless @relation_type

    # extract my key and my parent's key
    tuple_key, parent_key = keys_of(tuple)

    # this will be my tuple
    by_id[tuple_key] = tuple.merge(by_id[tuple_key])

    # add me to my parent unless same tuple
    unless tuple_key==parent_key
      my_tuple = tuple_type.new(by_id[tuple_key])
      by_id[parent_key][children] ||= relation_type.new(Set.new)
      by_id[parent_key][children].send(:reused_instance) << my_tuple
    end
  end

  # Now output tuples that are their own parent
  by_id.each_pair do |k,tuple|
    tuple_key, parent_key = keys_of(tuple)
    tuple[children] ||= relation_type.empty
    yield(tuple) if tuple_key==parent_key
  end
end

#argumentsObject


62
63
64
# File 'lib/alf/engine/hierarchize.rb', line 62

def arguments
  [ id, parent, children ]
end

#to_relationObject


66
67
68
69
# File 'lib/alf/engine/hierarchize.rb', line 66

def to_relation
  tuples = to_set
  relation_type.new(tuples)
end