Class: Schedsolver2::Schedule

Inherits:
Object
  • Object
show all
Includes:
Schedsolver2
Defined in:
lib/schedsolver2/schedule.rb

Constant Summary

Constants included from Schedsolver2

LIBPATH, PATH, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schedsolver2

libpath, #log, log, #log=, log=, path, require_all_libs_relative_to

Constructor Details

#initialize(consts) ⇒ Schedule

Returns a new instance of Schedule.



6
7
8
9
# File 'lib/schedsolver2/schedule.rb', line 6

def initialize consts    
  @@consts = consts
  @sched = init_sched
end

Instance Attribute Details

#schedObject

Returns the value of attribute sched.



4
5
6
# File 'lib/schedsolver2/schedule.rb', line 4

def sched
  @sched
end

Class Method Details

.parse_getter_setter_args(day, time, et_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/schedsolver2/schedule.rb', line 18

def self.parse_getter_setter_args day, time, et_id
  if day == nil 
    raise ArgumentError, "Day is nil, cannot proceed"
  end

  i = @@consts[:days].index(day) 
  j = @@consts[:times].index(time.to_f) 
  k = @@consts[:et_ids].index(et_id) 

  if (day != nil and i == nil) then raise ArgumentError, "day #{day} isn't is days" end
  if (time != nil and j == nil) then raise ArgumentError, "time #{time} isn't is times" end
  if (et_id != nil and k == nil) then raise ArgumentError, "et_id #{et_id} isn't is ed_ids" end

  return [i,j,k]
end

Instance Method Details

#[](day = nil, time = nil, et_id = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/schedsolver2/schedule.rb', line 48

def [](day=nil, time=nil, et_id=nil)
  ijk = Schedule.parse_getter_setter_args(day, time, et_id)
  i, j, k = ijk

  if j == nil 
    @sched[i]
  elsif k == nil
    @sched[i][j]
  else
    @sched[i][j][k]
  end
end

#[]=(day = nil, time = nil, et_id = nil, entry = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/schedsolver2/schedule.rb', line 34

def []=(day=nil, time=nil, et_id=nil, entry=nil)  
  ijk = Schedule.parse_getter_setter_args(day, time, et_id)
  i, j, k = ijk
  
  if j == nil 
    @sched[i] = entry
  elsif k == nil
    @sched[i][j] = entry
  else
    sanity_check ijk, entry
    @sched[i][j][k] = entry
  end
end

#init_schedObject



11
12
13
14
15
16
# File 'lib/schedsolver2/schedule.rb', line 11

def init_sched
  n_days = @@consts[:days].size  
  n_ts = @@consts[:times].size  
  n_ets = @@consts[:et_ids].size  
  Array.new(n_days){ Array.new(n_ts){ Array.new(n_ets, nil)}}
end

#sanity_check(ijk, entry) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/schedsolver2/schedule.rb', line 61

def sanity_check ijk, entry
  i, j, k = ijk 
  #check for simultaneous ECs assigned to HC 
  num_ets = @@consts[:et_ids].size
  (0..num_ets-1).each do |n|
    if n == k then next end
    if @sched[i][j][n] == entry 
      raise(InsanityError,"Can't assign HT #{entry} to sched[#{i}, #{j}, #{k}] because already assigned to sched[#{i}, #{j}, #{n}]")
    end
  end
  # Check for simultaneous HCs assigned to EC?
  # Should this be carried out by algorithm instead?
end

#to_sObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/schedsolver2/schedule.rb', line 76

def to_s
  head = [nil] + (@@consts[:et_ids] + [nil]) * 5
  rows = []
  #create col1
  @@consts[:times].each { |t| rows << [t] }
  #add body
  @@consts[:days].each_index do |d| 
    @@consts[:times].each_index do |t|
      rows[t] += (@sched[d][t] << "^")
    end
  end
  return Text::Table.new(:head => head, :rows => rows).to_s
end