Class: Schedsolver2::Constraint

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

Constant Summary collapse

@@days =
[:mon, :tue, :wed, :thu, :fri]

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(hsh) ⇒ Constraint

Returns a new instance of Constraint.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/schedsolver2/constraint.rb', line 8

def initialize hsh
  raise ArgumentError, "No type specified" unless hsh.has_key?(:type)
  raise ArgumentError, "No slot descriptors given" unless hsh.has_key?(:s_ds)

  if hsh.has_key?(:name) 
    @name = hsh[:name] 
  else 
    @name = 'Unnamed constraint'
  end

  @descriptors = []
  hsh[:s_ds].each do |s_d|
    @descriptors << Constraint.make_slots_ary(s_d)
  end

  unless hsh[:type] == :custom
    raise ArgumentError, "No et specified"  unless hsh.has_key?(:et_ids)
    @test = get_test_for(hsh)
    @assess = get_assess_for(hsh)
  else
    @test = hsh[:test]  
    @assess = hsh[:assess]
  end
end

Instance Attribute Details

#assessObject (readonly)

Returns the value of attribute assess.



5
6
7
# File 'lib/schedsolver2/constraint.rb', line 5

def assess
  @assess
end

#descriptorsObject (readonly)

Returns the value of attribute descriptors.



5
6
7
# File 'lib/schedsolver2/constraint.rb', line 5

def descriptors
  @descriptors
end

#testObject (readonly)

Returns the value of attribute test.



5
6
7
# File 'lib/schedsolver2/constraint.rb', line 5

def test
  @test
end

Class Method Details

.day_descriptor(day, times, et_ids) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
# File 'lib/schedsolver2/constraint.rb', line 62

def self.day_descriptor day, times, et_ids
  raise ArgumentError, "et_ids not an array" unless (et_ids.class == Array)
  h = {}
  h[day.to_sym] = {}
  times.each do |time|
    h[day.to_sym][time] = et_ids
  end
  return h
end

.days_descriptor(days, times, et_ids) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/schedsolver2/constraint.rb', line 72

def self.days_descriptor days, times, et_ids
  s_d = {}
  days.each do |day|
    s_d.merge! Constraint.day_descriptor(day, times, et_ids)
  end
  return s_d
end

.hard_assert(schedule, constraint) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/schedsolver2/constraint.rb', line 106

def self.hard_assert schedule, constraint

  unless schedule.class == Schedule 
    raise ArgumentError, "Schedule is of wrong class #{schedule.class}" 
  end

  unless constraint.class == Constraint
    raise ArgumentError, "Constraint is of wrong class #{constraint.class}" 
  end

  constraint.descriptors.each do |descriptor|
    count = 0
    descriptor.each do |slot|
      d,t,id = slot 
      value = schedule[d,t,id]
      if (constraint.test.call(slot, value) == true)
        count += 1 
        #Schedsolver2.log.info("Constraint#hard_assert #{d},#{t},#{id} gives true value: #{value}")
      else
        #Schedsolver2.log.info("#Constraint#hard_assert #{d},#{t},#{id} gives false value: #{value}")
      end
    end
    if constraint.assess.call(count) != true then return false end
  end
  return true
end

.make_slots_ary(s_d) ⇒ Object

In the constructor, we get one or more descriptors which are converted into an array of slots_arys. Confusing variable names are everywhere. I suck.

Raises:

  • (ArgumentError)


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

def self.make_slots_ary s_d
  raise ArgumentError, "No slots given in slot descriptor" if s_d == nil
  ary = []
  s_d.each do |day, hour_hash| 
    hour_hash.each do |hour, et_ids| 
      et_ids.each do |et_id| 
        ary << [day, hour, et_id]
      end
    end
  end
  return ary
end

.week_descriptor(times, et_ids) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/schedsolver2/constraint.rb', line 80

def self.week_descriptor times, et_ids
  s_d = {} 
  @@days.each do |day|
    s_d.merge! Constraint.day_descriptor(day, times, et_ids)
  end
  return s_d
end

Instance Method Details

#get_assess_for(hsh) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/schedsolver2/constraint.rb', line 97

def get_assess_for(hsh)
  case hsh[:type]
  when :part_time
    p = Proc.new {|count| if count > 0 then false else true end } 
    return p
  end
end

#get_test_for(hsh) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/schedsolver2/constraint.rb', line 88

def get_test_for(hsh)
  case hsh[:type]
  when :part_time
    raise ArgumentError, "et_ids.size > 1 for part_time type" unless(hsh[:et_ids].size <= 1)
    p = Proc.new { |slot, value| if value != nil then true else false end}
    return p
  end
end

#to_sObject



33
34
35
# File 'lib/schedsolver2/constraint.rb', line 33

def to_s
  @name
end