/
TaskScenario.rb
1116 lines (993 loc) · 36.9 KB
/
TaskScenario.rb
1
2
3
4
5
6
7
8
9
10
11
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# TaskScenario.rb - TaskJuggler
#
# Copyright (c) 2006, 2007 by Chris Schlaeger <cs@kde.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# $Id$
#
require 'ScenarioData'
class TaskScenario < ScenarioData
attr_reader :isRunAway
def initialize(task, scenarioIdx, attributes)
super
end
def prepareScenario
@property['startpreds', @scenarioIdx] = []
@property['startsuccs', @scenarioIdx] =[]
@property['endpreds', @scenarioIdx] = []
@property['endsuccs', @scenarioIdx] = []
@isRunAway = false
# The following variables are only used during scheduling
@lastSlot = nil
# The 'done' variables count scheduled values in number of time slots.
@doneDuration = 0
@doneLength = 0
# Due to the 'efficiency' factor the effort slots must be a float.
@doneEffort = 0.0
@startIsDetermed = nil
@endIsDetermed = nil
# Inheriting start or end values is a bit tricky. This should really only
# happen if the task is a leaf task and scheduled away from the specified
# date. Since the default meachanism inherites all values, we have to
# delete the wrong ones again.
if a('start') && @property.inherited('start', @scenarioIdx) &&
(@property.container? || (@property.leaf? && !a('forward')) ||
!a('depends').empty?)
@property['start', @scenarioIdx] = nil
end
if a('end') && @property.inherited('end', @scenarioIdx) &&
(@property.container? || (@property.leaf? && a('forward')) ||
!a('precedes').empty?)
@property['end', @scenarioIdx] = nil
end
bookBookings
end
# The parser only stores the full task IDs for each of the dependencies. This
# function resolves them to task references and checks them. In addition to
# the 'depends' and 'precedes' property lists we also keep 4 additional lists.
# startpreds: All precedessors to the start of this task
# startsuccs: All successors to the start of this task
# endpreds: All predecessors to the end of this task
# endsuccs: All successors to the end of this task
# Each list element consists of a reference/boolean pair. The reference points
# to the dependent task and the boolean specifies whether the dependency
# originates from the end of the task or not.
def Xref
@property['depends', @scenarioIdx].each do |dependency|
depTask = checkDependency(dependency, 'depends')
a('startpreds').push([ depTask, dependency.onEnd ])
depTask[dependency.onEnd ? 'endsuccs' : 'startsuccs', @scenarioIdx].
push([ @property, false ])
end
@property['precedes', @scenarioIdx].each do |dependency|
predTask = checkDependency(dependency, 'precedes')
a('endsuccs').push([ predTask, dependency.onEnd ])
predTask[dependency.onEnd ? 'endpreds' : 'startpreds', @scenarioIdx].\
push([@property, true ])
end
end
def implicitXref
# TODO: Propagate implicit dependencies.
# Automatically detect and mark task that have no duration criteria but
# proper start or end specification.
return if !@property.leaf? || a('milestone')
hasDurationSpec = a('length') > 0 || a('duration') > 0 || a('effort') > 0
hasStartSpec = !(a('start').nil? && a('depends').empty?)
hasEndSpec = !(a('end').nil? && a('precedes').empty?)
@property['milestone', @scenarioIdx] =
!hasDurationSpec && (hasStartSpec ^ hasEndSpec)
end
def propagateInitialValues
propagateDate(a('start'), false) if a('start')
propagateDate(a('end'), true) if a('end')
end
def preScheduleCheck
# TODO: Fixme
end
def postScheduleCheck
@errors = 0
@property.children.each do |task|
@errors += 1 unless task.postScheduleCheck(@scenarioIdx)
end
# There is no point to check the parent if the child(s) have errors.
return false if @errors > 0
# Same for runaway tasks. They have already been reported.
return false if isRunAway
# Make sure the task is marked complete
unless a('scheduled')
error('not_scheduled',
"Task #{@property.fullId} has not been marked as scheduled.")
end
# If the task has a follower or predecessor that is a runaway this task
# is also incomplete.
(a('startsuccs') + a('endsuccs')).each do |task, onEnd|
return false if task.isRunAway(@scenarioIdx)
end
(a('startpreds') + a('endpreds')).each do |task, onEnd|
return false if task.isRunAway(@scenarioIdx)
end
# Check if the start time is ok
if a('start').nil?
error('task_start_undef',
"Task #{@property.fullId} has undefined start time")
end
if a('start') < @project['start'] || a('start') > @project['end']
error('task_start_range',
"The start time (#{a('start')}) of task #{@property.fullId} " +
"is outside the project interval (#{@project['start']} - " +
"#{@project['end']})")
end
if !a('minstart').nil? && a('start') < a('minstart')
warning('minstart',
"The start time (#{a('start')}) of task #{@property.fullId} " +
"is too early. Must be after #{a('minstart')}.")
end
if !a('maxstart').nil? && a('start') > a('maxstart')
warning('maxstart',
"The start time (#{a('start')}) of task #{@property.fullId} " +
"is too late. Must be before #{a('maxstart')}.")
end
# Check if the end time is ok
error('task_end_undef',
"Task #{@property.fullId} has undefined end time") if a('end').nil?
if a('end') < @project['start'] || a('end') > @project['end']
error('task_end_range',
"The end time (#{a('end')}) of task #{@property.fullId} " +
"is outside the project interval (#{@project['start']} - " +
"#{@project['end']})")
end
if !a('minend').nil? && a('end') < a('minend')
warning('minend',
"The end time (#{a('end')}) of task #{@property.fullId} " +
"is too early. Must be after #{a('minend')}.")
end
if !a('maxend').nil? && a('end') > a('maxend')
warning('maxend',
"The end time (#{a('end')}) of task #{@property.fullId} " +
"is too late. Must be before #{a('maxend')}.")
end
# Check that tasks fits into parent task.
unless (parent = @property.parent).nil? ||
parent['start', @scenarioIdx].nil? ||
parent['end', @scenarioIdx].nil?
if a('start') < parent['start', @scenarioIdx]
error('task_start_in_parent',
"The start date (#{a('start')}) of task #{@property.fullId} " +
"is before the start date of the enclosing task " +
"#{parent['start', scenarioIdx]}. ")
end
if a('end') > parent['end', @scenarioIdx]
error('task_end_in_parent',
"The end date (#{a('end')}) of task #{@property.fullId} " +
"is after the end date of the enclosing task " +
"#{parent['end', scenarioIdx]}. ")
end
end
# Check that all preceding tasks start/end before this task.
@property['depends', @scenarioIdx].each do |dependency|
task = dependency.task
limit = task[dependency.onEnd ? 'end' : 'start', @scenarioIdx]
next if limit.nil?
if limit > a('start')
error('task_pred_before',
"Task #{@property.fullId} must start after " +
"#{dependency.onEnd ? 'end' : 'start'} of task #{@task.fullId}.")
end
end
# Check that all following tasks end before this task
@property['precedes', @scenarioIdx].each do |dependency|
task = dependency.task
limit = task[dependency.onEnd ? 'end' : 'start', @scenarioIdx]
next if limit.nil?
if limit < a('end')
error('task_succ_after',
"Task #{@property.fullId} must end before " +
"#{dependency.onEnd ? 'end' : 'start'} of task #{task.fullId}.")
end
end
if a('milestone') && a('start') != a('end')
error('milestone_times_equal',
"Milestone #{@property.fullId} must have identical start and end " +
"date.")
end
@errors == 0
end
def resetLoopFlags
@deadEndFlags = Array.new(4, false)
end
def checkForLoops(path, atEnd, fromOutside)
# Check if we have been here before on this path.
if path.include?([ @property, atEnd ])
error('loop_detected', "Loop detected at #{atEnd ? 'end' : 'start'} " +
"of task #{@property.fullId}", false)
skip = true
path.each do |t, e|
if t == @property && e == atEnd
skip = false
next
end
next if skip
info("loop_at_#{e ? 'end' : 'start'}",
"Loop ctnd. at #{e ? 'end' : 'start'} of task #{t.fullId}", t)
end
error('loop_end', "Aborting")
end
# Used for debugging only
if false
pathText = ''
path.each { |t, e| pathText += "#{t.fullId}(#{e ? 'end' : 'start'}) -> " }
pathText += "#{@property.fullId}(#{atEnd ? 'end' : 'start'})"
puts pathText
end
return if @deadEndFlags[(atEnd ? 2 : 0) + (fromOutside ? 1 : 0)]
path << [ @property, atEnd ]
# To find loops we have to traverse the graph in a certain order. When we
# enter a task we can either come from outside or inside. The following
# graph explains these definitions:
#
# | / \ |
# outside v / \ v outside
# +------------------------------+
# | / Task \ |
# -->| o <--- ---> o |<--
# |/ Start End \|
# /+------------------------------+\
# / ^ ^ \
# | inside |
#
# At the top we have the parent task. At the botton the child tasks.
# The horizontal arrors are start predecessors or end successors.
# As the graph is doubly-linked, we need to becareful to only find real
# loops. When coming from outside, we only continue to the inside and vice
# versa. Horizontal moves are only made when we are in a leaf task.
unless atEnd
if fromOutside
if @property.container?
#
# |
# v
# +--------
# -->| o--+
# +----|---
# |
# V
#
@property.children.each do |child|
child.checkForLoops(@scenarioIdx, path, false, true)
end
else
# |
# v
# +--------
# -->| o---->
# +--------
#
checkForLoops(path, true, false) # if a('forward')
end
else
if a('startpreds').empty?
#
# ^
# |
# +-|------
# | o <--
# +--------
# ^
# |
#
if @property.parent
@property.parent.checkForLoops(@scenarioIdx, path, false, false)
end
else
# +--------
# <---- o <--
# +--------
# ^
# |
#
a('startpreds').each do |task, targetEnd|
task.checkForLoops(@scenarioIdx, path, targetEnd, true)
end
end
end
else
if fromOutside
if @property.container?
#
# |
# v
# --------+
# +--o |<--
# ---|----+
# |
# v
#
@property.children.each do |child|
child.checkForLoops(@scenarioIdx, path, true, true)
end
else
# |
# v
# --------+
# <----o |<--
# --------+
#
checkForLoops(path, false, false) # unless a('forward')
end
else
if a('endsuccs').empty?
#
# ^
# |
# ------|-+
# --> o |
# --------+
# ^
# |
#
if @property.parent
@property.parent.checkForLoops(@scenarioIdx, path, true, false)
end
else
# --------+
# --> o---->
# --------+
# ^
# |
#
a('endsuccs').each do |task, targetEnd|
task.checkForLoops(@scenarioIdx, path, targetEnd, true)
end
end
end
end
path.pop
@deadEndFlags[(atEnd ? 2 : 0) + (fromOutside ? 1 : 0)] = true
# puts "Finished with #{@property.fullId} #{atEnd ? 'end' : 'start'} " +
# "#{fromOutside ? 'outside' : 'inside'}"
end
def checkDetermination
[ true, false ].each do |b|
unless dateCanBeDetermined(b)
error(b ? 'start_undetermed' : 'end_undetermed',
"The #{b ? 'start' : 'end'} of task " +
"#{@property.fullId} " + "is underspecified. You must use " +
"more fixed data for this task or its dependencies to solve " +
"this problem.")
end
end
end
def countResourceAllocations
return if a('effort') <= 0
resources = []
a('allocate').each do |allocation|
allocation.candidates.each do |candidate|
candidate.all.each do |resource|
resources << resource unless resources.include?(resource)
end
end
end
avgEffort = a('effort') / resources.length
resources.each do |resource|
resource['alloctdeffort', @scenarioIdx] += avgEffort
end
end
def calcCriticalness
# We cache the value for the directional path criticalness. Every time we
# recalculate the criticalnesses, we have to clear the cached values.
@maxForwardCriticalness = nil
@maxBackwardCriticalness = nil
return if a('effort') <= 0
# Users feel that milestones are somewhat important. So we use an
# arbitrary value larger than 0 for them.
@property['criticalness', @scenarioIdx] = 1.0 if a('milestone')
resources = []
a('allocate').each do |allocation|
allocation.candidates.each do |candidate|
candidate.all.each do |resource|
resources << resource unless resources.include?(resource)
end
end
end
criticalness = 0.0
resources.each do |resource|
criticalness += resource['criticalness', @scenarioIdx]
end
@property['criticalness', @scenarioIdx] = a('effort') *
criticalness / resources.length
end
# The path criticalness is a measure for the overall criticalness of the
# task taking the dependencies into account. The fact that a task is part
# of a chain of effort-based task raises all the task in the chain to a
# higher criticalness level than the individual tasks. In fact, the path
# criticalness of this chain is equal to the sum of the individual
# criticalnesses of the tasks.
#
# Since both the forward and backward functions include the
# criticalness of this function we have to subtract it again.
def calcPathCriticalness
@property['pathcriticalness', @scenarioIdx] =
calcDirCriticalness(false) - a('criticalness') +
calcDirCriticalness(true)
end
# Return the date of the next slot this task wants to have scheduled. This
# must either be the first slot ever or it must be directly adjecent to the
# previous slot. If this task has not yet been scheduled at all, @lastSlot
# is still nil. Otherwise it contains the date of the last scheduled slot.
def nextSlot(slotDuration)
return nil if a('scheduled')
if a('forward')
@lastSlot.nil? ? a('start') : @lastSlot + slotDuration
else
@lastSlot.nil? ? a('end') - slotDuration : @lastSlot - slotDuration
end
end
# Check if the task is ready to be scheduled. For this it needs to have at
# least one specified end date and a duration criteria or the other end
# date.
def readyForScheduling?
return false if a('scheduled')
if a('forward')
if !a('start').nil? &&
(a('effort') != 0 || a('length') != 0 || a('duration') != 0 ||
a('milestone') || !a('end').nil?)
return true
end
else
if !a('end').nil? &&
(a('effort') != 0 || a('length') != 0 || a('duration') != 0 ||
a('milestone') || !a('start').nil?)
return true
end
end
false
end
def schedule(slot, slotDuration)
# Tasks must always be scheduled in a single contigous fashion. @lastSlot
# indicates the slot that was used for the previous call. Depending on the
# scheduling direction the next slot must be scheduled either right before
# or after this slot. If the current slot is not directly aligned, we'll
# wait for another call with a proper slot. The function returns true
# only if a slot could be scheduled.
if a('forward')
# On first call, the @lastSlot is not set yet. We set it to the slot
# before the start slot.
if @lastSlot.nil?
@lastSlot = a('start') - slotDuration
@tentativeEnd = slot + slotDuration
end
return false unless slot == @lastSlot + slotDuration
else
if @lastSlot.nil?
@lastSlot = a('end')
@tentativeStart = slot
end
return false unless slot == @lastSlot - slotDuration
end
@lastSlot = slot
if a('length') > 0 || a('duration') > 0
# The doneDuration counts the number of scheduled slots. It is increased
# by one with every scheduled slot. The doneLength is only increased for
# global working time slots.
@doneDuration += 1
if @project.isWorkingTime(slot, slot + slotDuration)
@doneLength += 1
end
# If we have reached the specified duration or lengths, we set the end
# or start date and propagate the value to neighbouring tasks.
if (a('length') > 0 && @doneLength >= a('length')) ||
(a('duration') > 0 && @doneDuration >= a('duration'))
@property['scheduled', @scenarioIdx] = true
if a('forward')
propagateDate(slot + slotDuration, true)
else
propagateDate(slot, false)
end
return true
end
elsif a('effort') > 0
bookResources(slot, slotDuration) if @doneEffort < a('effort')
if @doneEffort >= a('effort')
@property['scheduled', @scenarioIdx] = true
if a('forward')
propagateDate(@tentativeEnd, true)
else
propagateDate(@tentativeStart, false)
end
return true
end
elsif a('milestone')
if a('forward')
propagateDate(a('start'), true)
else
propagateDate(a('end'), false)
end
elsif a('start') && a('end')
# Task with start and end date but no duration criteria
bookResources(slot, slotDuration) unless a('allocate').empty?
# Depending on the scheduling direction we can mark the task as
# scheduled once we have reached the other end.
if (a('forward') && slot + slotDuration >= a('end')) ||
(!a('forward') && slot <= a('start'))
@property['scheduled', @scenarioIdx] = true
return true
end
end
false
end
# Set a new start or end date and propagate the value to all other
# task ends that have a direct dependency to this end of the task.
def propagateDate(date, atEnd)
thisEnd = atEnd ? 'end' : 'start'
otherEnd = atEnd ? 'start' : 'end'
# puts "Setting #{thisEnd} of #{@property.fullId} to #{date}"
@property[thisEnd, @scenarioIdx] = date
if a('milestone')
# Start and end date of a milestone are identical.
@property['scheduled', @scenarioIdx] = true
if a(otherEnd).nil?
propagateDate(a(thisEnd), !atEnd)
end
end
# Propagate date to all dependent tasks.
a(thisEnd + 'preds').each do |task, onEnd|
propagateDateToDep(task, onEnd)
end
a(thisEnd + 'succs').each do |task, onEnd|
propagateDateToDep(task, onEnd)
end
# Propagate date to sub tasks which have only an implicit
# dependency on the parent task.
@property.children.each do |task|
if !task.hasDependencies(@scenarioIdx, !atEnd) &&
!task['scheduled', @scenarioIdx]
task.propagateDate(@scenarioIdx, a(thisEnd), !atEnd)
end
end
# The date propagation might have completed the date set of the enclosing
# containter task. If so, we can schedule it as well.
if !@property.parent.nil?
@property.parent.scheduleContainer(@scenarioIdx)
end
end
# Find the smallest possible interval that encloses all child tasks. Abort
# the opration if any of the child tasks are not yet scheduled.
def scheduleContainer
return if a('scheduled') || !@property.container?
# puts "Scheduling container #{@property.fullId}"
nStart = nil
nEnd = nil
@property.children.each do |task|
# Abort if a child has not yet been scheduled.
return if task['start', @scenarioIdx].nil? ||
task['end', @scenarioIdx].nil?
if nStart.nil? || task['start', @scenarioIdx] < nStart
nStart = task['start', @scenarioIdx]
end
if nEnd.nil? || task['end', @scenarioIdx] > nEnd
nEnd = task['end', @scenarioIdx]
end
end
# Propagate the dates to other dependent tasks.
if a('start').nil? || a('start') > nStart
propagateDate(nStart, false)
end
if a('end').nil? || a('end') < nEnd
propagateDate(nEnd, true)
end
@property['scheduled', @scenarioIdx] = true
end
def hasDurationSpec
(@property['effort', @scenarioIdx] > 0 ||
@property['length', @scenarioIdx] > 0 ||
@property['duration', @scenarioIdx] > 0) &&
!@property['milestone', @scenarioIdx]
end
def hasDependencies(atEnd)
if atEnd
return true if a('end') || a('forward') ||
!a('endsuccs').empty? || !a('endpreds').empty?
else
return true if a('start') || !a('forward') ||
!a('startpreds').empty? || !a('startsuccs').empty?
end
p = @property
while (p = p.parent) do
return true if p.hasDependencies(@scenarioIdx, atEnd)
end
false
end
def earliestStart
# puts "Finding earliest start date for #{@property.fullId}"
startDate = nil
a('depends').each do |dependency|
potentialStartDate =
dependency.task[dependency.onEnd ? 'end' : 'start', @scenarioIdx]
return nil if potentialStartDate.nil?
dateAfterLengthGap = potentialStartDate
gapLength = dependency.gapLength
while gapLength > 0 && dateAfterLengthGap < @project['end'] do
if @project.isWorkingTime(dateAfterLengthGap)
gapLength -= 1
end
dateAfterLengthGap += @project['scheduleGranularity']
end
if dateAfterLengthGap > potentialStartDate + dependency.gapDuration
potentialStartDate = dateAfterLengthGap
else
potentialStartDate += dependency.gapDuration
end
startDate = potentialStartDate if startDate.nil? ||
startDate < potentialStartDate
end
# If any of the parent tasks has an explicit start date, the task must
# start at or after this date.
task = @property
while (task = task.parent) do
if task['start', @scenarioIdx] &&
(startDate.nil? || task['start', @scenarioIdx] > startDate)
return task['start', @scenarioIdx]
end
end
startDate
end
def latestEnd
# puts "Finding latest end date for #{@property.fullId}"
endDate = nil
a('precedes').each do |dependency|
potentialEndDate =
dependency.task[dependency.onEnd ? 'end' : 'start', @scenarioIdx]
return nil if potentialEndDate.nil?
dateBeforeLengthGap = potentialEndDate
gapLength = dependency.gapLength
while gapLength > 0 && dateBeforeLengthGap > @project['start'] do
if @project.isWorkingTime(dateBeforeLengthGap -
@project['scheduleGranularity'])
gapLength -= 1
end
dateBeforeLengthGap -= @project['scheduleGranularity']
end
if dateBeforeLengthGap < potentialEndDate - dependency.gapDuration
potentialEndDate = dateBeforeLengthGap
else
potentialEndDate -= dependency.gapDuration
end
endDate = potentialEndDate if endDate.nil? || endDate > potentialEndDate
end
# If any of the parent tasks has an explicit end date, the task must end
# at or before this date.
task = @property
while (task = task.parent) do
if task['end', @scenarioIdx] &&
(endDate.nil? || task['end', @scenarioIdx] < endDate)
return task['end', @scenarioIdx]
end
end
endDate
end
def bookResources(date, slotDuration)
# In projection mode we do not allow bookings prior to the current date
# for any task (in strict mode) or tasks which have user specified
# bookings (sloppy mode).
if @project.scenario(@scenarioIdx).get('projection') &&
date < @project['now'] &&
(@project.scenario(@scenarioIdx).get('strict') ||
a('bookedresources').empty?)
return
end
# If the task has shifts to limit the allocations, we check that we are
# within a shift interval. If not, abort the booking.
return if !a('shifts').nil? && !a('shifts').onShift?(date)
sbIdx = @project.dateToIdx(date)
# We first have to make sure that if there are mandatory resources
# that these are all available for the time slot.
@property['allocate', @scenarioIdx].each do |allocation|
if allocation.mandatory
return unless allocation.onShift?(date)
# For mandatory allocations with alternatives at least one of the
# alternatives must be available.
found = false
allocation.candidates(scenarioIdx).each do |candidate|
# When a resource group is marked mandatory, all members of the
# group must be available.
allAvailable = true
candidate.all.each do |resource|
if !resource.available?(@scenarioIdx, sbIdx)
allAvailable = false
break
end
end
if allAvailable
found = true
break
end
end
return unless found
end
end
iv = Interval.new(date, date + slotDuration)
@property['allocate', @scenarioIdx].each do |allocation|
# TODO: Handle shifts
# TODO: Handle limits
# For persistent resources we capture the time slot where we
# could not allocate it first. This is used during debug mode to
# report contention intervals.
if allocation.persistent && !allocation.lockedResource.nil
if !bookResource(allocation.lockedResource, iv)
# The resource could not be allocated.
if allocation.lockedResource.booked?(sbIdx) &&
allocation.conflictStart.nil?
# Store starting time slot
allocation.conflictStart = date
end
elsif !allocation.conflictStart.nil
# Reset starting time slot
allocation.conflictStart = nil
end
else
found = false
busy = false
# Create a list of candidates in the proper order and assign
# the first one available.
allocation.candidates(@scenarioIdx).each do |candidate|
if bookResource(candidate, sbIdx)
allocation.lockedResource = candidate
found = true
break
elsif candidate.booked?(@scenarioIdx, sbIdx)
busy = true
end
end
# Set of reset the conflict start time slot.
if found
allocation.conflictStart = nil
elsif busy && allocation.conflictStart.nil?
allocation.conflictStart = date
end
end
end
end
def bookResource(resource, sbIdx)
booked = false
resource.all.each do |r|
if r.book(@scenarioIdx, sbIdx, @property)
if a('assignedresources').empty?
if a('forward')
@property['start', @scenarioIdx] = @project.idxToDate(sbIdx)
else
@property['end', @scenarioIdx] = @project.idxToDate(sbIdx + 1)
end
end
@tentativeStart = @project.idxToDate(sbIdx)
@tentativeEnd = @project.idxToDate(sbIdx + 1)
@doneEffort += r['efficiency', @scenarioIdx]
unless a('assignedresources').include?(r)
@property['assignedresources', @scenarioIdx] << r
end
booked = true
end
end
booked
end
def addBooking(booking)
@property['booking', @scenarioIdx] = a('booking') + [ booking ]
end
def markAsRunaway
error('runaway', "Task #{@property.get('id')} does not fit into project " +
"time frame")
@isRunAway = true
end
def getEffectiveLoad(startIdx, endIdx, resource)
return 0.0 if a('milestone')
workLoad = 0.0
if @property.container?
@property.children.each do |task|
workLoad += task.getEffectiveLoad(@scenarioIdx, startIdx, endIdx,
resource)
end
else
if resource
workLoad += resource.getEffectiveLoad(@scenarioIdx, startIdx, endIdx,
@property)
else
a('assignedresources').each do |resource|
workLoad += resource.getEffectiveLoad(@scenarioIdx, startIdx, endIdx,
@property)
end
end
end
workLoad
end
private
def bookBookings
a('booking').each do |booking|
unless booking.resource.leaf?
error('booking_resource_not_leaf',
"Booked resources may not be group resources", true,
booking.sourceFileInfo)
end
if @project.scenario(@scenarioIdx).get('strict') &&
!a('forward')
error('booking_forward_only',
"In strict projection mode only forward scheduled tasks " +
"may have booking statements.")
end
booking.intervals.each do |interval|
startIdx = @project.dateToIdx(interval.start)
date = interval.start
endIdx = @project.dateToIdx(interval.end)
startIdx.upto(endIdx - 1) do |idx|
if booking.resource.bookBooking(@scenarioIdx, idx, booking)
@doneEffort += booking.resource['efficiency', @scenarioIdx]
# Set start and lastSlot if appropriate. The task start will be set
# to the begining of the first booked slot. The lastSlot will be set
# to the last booked slot
@lastSlot = date if @lastSlot.nil? || date > @lastSlot
tEnd = date + @project['scheduleGranularity']
if a('forward')
@tentativeEnd = tEnd if @tentativeEnd.nil? ||
@tentativeEnd < tEnd
@property['start', @scenarioIdx] = date if a('start').nil? ||
date < a('start')
else
@tentativeStart = date if @tentativeStart.nil? ||
date < @tentativeStart
@property['end', @scenarioIdx] = tEnd if a('end').nil? ||
tEnd > a('end')
end
unless a('assignedresources').include?(booking.resource)
@property['assignedresources', @scenarioIdx] << booking.resource
end
end
date += @project['scheduleGranularity']
end
end
end
end
def checkDependency(dependency, depType)
if (depTask = dependency.resolve(@project)).nil?
# Remove the broken dependency. It could cause trouble later on.
@property[depType, @scenarioIdx].delete(dependency)
error('task_depend_unknown',
"Task #{@property.fullId} has unknown #{depType} " +
"#{dependency.taskId}")
end
if depTask == @property
# Remove the broken dependency. It could cause trouble later on.
@property[depType, @scenarioIdx].delete(dependency)
error('task_depend_self', "Task #{@property.fullId} cannot " +
"depend on self")
end
if depTask.isChildOf?(@property)
# Remove the broken dependency. It could cause trouble later on.
@property[depType, @scenarioIdx].delete(dependency)
error('task_depend_child',
"Task #{@property.fullId} cannot depend on child #{depTask.fullId}")
end
if @property.isChildOf?(depTask)
# Remove the broken dependency. It could cause trouble later on.
@property[depType, @scenarioIdx].delete(dependency)
error('task_depend_parent',
"Task #{@property.fullId} cannot depend on parent " +
"#{depTask.fullId}")
end
@property[depType, @scenarioIdx].each do |dep|
if dep.task == depTask && dep != dependency
# Remove the broken dependency. It could cause trouble later on.
@property[depType, @scenarioIdx].delete(dependency)
error('task_depend_multi',
"No need to specify dependency #{depTask.fullId} multiple " +
"times for task #{@property.fullId}.")
end
end
depTask
end
def dateCanBeDetermined(checkStart)
if checkStart