pbs/pert.coffee

51 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2015-03-13 11:01:18 +01:00
#!/usr/bin/env coffee
2015-03-13 11:44:12 +01:00
chalk = require 'chalk'
2015-03-13 11:01:18 +01:00
2015-03-13 11:44:12 +01:00
verbose = no
2015-03-13 11:01:18 +01:00
2015-03-13 11:44:12 +01:00
log = (x...) -> if verbose then console.log chalk.bold("Log:"), x...
err = (x...) -> console.log chalk.bold (chalk.red "Error:"), x...
2015-03-13 11:01:18 +01:00
2015-03-13 11:44:12 +01:00
# Returns the highest number in an array of numbers
2015-03-13 11:01:18 +01:00
maxa = (l) -> return Math.max.apply null, l
# Find the activity with given id
2015-03-13 11:44:12 +01:00
toActivity = (id,list) ->
if !list? then err "list is",list
2015-03-13 11:01:18 +01:00
item = {}
list.forEach (x) -> if x.id is id then item = x
return item
# Find the item
2015-03-13 11:44:12 +01:00
calculateEndDay = (item,list) ->
2015-03-13 11:01:18 +01:00
if !item.startDay?
2015-03-13 11:44:12 +01:00
log "calculating start day of",item.id
item.startDay = calculateStartDay item, list
log "start day of",item.id,"is",item.startDay
2015-03-13 11:01:18 +01:00
item.endDay = item.startDay + item.duration
2015-03-13 11:44:12 +01:00
log "end day of",item.id,"is",item.endDay
2015-03-13 11:01:18 +01:00
return item.endDay
# Find out which day the activity starts
2015-03-13 11:44:12 +01:00
calculateStartDay = (item,list) ->
2015-03-13 11:01:18 +01:00
if item.depends.length is 0 then return item.startDay = 0
2015-03-13 11:44:12 +01:00
max = maxa item.depends.map((x) -> toActivity x,list).map((x) -> calculateEndDay x, list)
log "start day of",item.id,"is",max
2015-03-13 11:01:18 +01:00
# write max delay time to each depend
item.depends.forEach (x) ->
2015-03-13 11:44:12 +01:00
log "writing permittedDelay to dependency", x, "of", item
i = toActivity x, list
i.permittedDelay = max - calculateEndDay i, list
log "permitted delay of",x,"is",i.permittedDelay
2015-03-13 11:01:18 +01:00
return item.startDay = max
# Find out which activity has the highest id
2015-03-13 11:44:12 +01:00
highestID = (list) -> return maxa(list.map (x) -> x.id)
2015-03-13 11:01:18 +01:00
2015-03-13 11:44:12 +01:00
calculate = (list) ->
calculateEndDay (toActivity highestID(list), list), list
2015-03-13 11:01:18 +01:00
return list
2015-03-13 11:44:12 +01:00
ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
console.log calculate ex