Class: TG::TimeGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/time_graph.rb

Class Method Summary collapse

Class Method Details

.populate(years = (1900 .. 2050), delete: true) ⇒ Object

we populate the graph with a 1:n-Layer (year –>) n –> n ( –> n ]) thus creating edges is providing a static :from-vertex to numerous :to-vertices the to:vertices are first created and fenced in an array. Then all edges are created at once. In Rest-Mode this is much quicker.



12
13
14
15
16
17
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/time_graph.rb', line 12

def populate years = (1900 .. 2050), delete: true

  count_gridspace = -> do
    [TG::Jahr,TG::Monat,TG::Tag,TG::Stunde].map{|x|  "#{x.ref_name} -> #{x.count}" }
  end

  if delete
    puts count_gridspace[].join('; ')
    puts "deleting content"
    [TG::Jahr,TG::Monat,TG::Tag,TG::Stunde].each{|x| x.delete all: true}
    puts "checking .."
    puts count_gridspace[].join('; ')
  end

  kind_of_grid = if years.is_a? Range
                   'daily'
                 else
                   years = years.is_a?(Fixnum) ? [years]: years
                   'hourly'
                 end

  ### switch logger level
    previous_looger_level = V.db.logger.level
  V.db.logger.level = 2
  ### NOW WHERE THE DATABASE IS CLEAN, POPULATE IT WITH A DAILY GRID
  print "Grid: " 
  year_grid, month_grid, day_grid, hour_grid  =  nil
  years.each do | the_year |
    year_vertex = TG::Jahr.create value: the_year
    # puts "YEAR_GRID: #{year_grid.inspect}"
    TG::GRID_OF.create( from: year_grid , to: year_vertex ) if year_grid.present?
    year_grid =  year_vertex
    month_vertices = ( 1 .. 12 ).map do | the_month |
      month_vertex= TG::Monat.create value: the_month
      TG::GRID_OF.create( from: month_grid , to: month_vertex ) if month_grid.present?
      month_grid =  month_vertex
      last_month_day =  (Date.new( the_year, the_month+1, 1)-1).day rescue 31  # rescue covers month > 12
      day_vertices = ( 1 .. last_month_day ).map do | the_day | 
        day_vertex = TG::Tag.create value: the_day  
        TG::GRID_OF.create( from: day_grid , to: day_vertex ) if day_grid.present?
        day_grid =  day_vertex
        if kind_of_grid == 'hourly'
          hour_vertices = (0 .. 23).map do |h| 
            hour_vertex =  Stunde.create( value: h)

            TG::GRID_OF.create( from: hour_grid , to: hour_vertex ) if hour_grid.present?
            hour_grid =  hour_vertex
            hour_vertex # return_value
          end
          TG::TIME_OF.create from: day_vertex, to: hour_vertices
        end 
        day_vertex # return_value
      end
      TG::DAY_OF.create from: month_vertex, to: day_vertices
      month_vertex # return_value
    end
    print "#{the_year} "
    TG::MONTH_OF.create from: year_vertex, to: month_vertices
  end
  print "\n"
  V.db.logger.level =  previous_looger_level 
end