mirror of
https://github.com/fazo96/pbs.git
synced 2025-01-27 14:34:19 +01:00
changed api to something better
This commit is contained in:
parent
d27c9b9cb3
commit
bd1f993373
@ -1,3 +1,2 @@
|
||||
src/
|
||||
test.json
|
||||
build.sh
|
||||
test/
|
||||
|
@ -2,7 +2,7 @@
|
||||
chalk = require 'chalk'
|
||||
cli = require 'commander'
|
||||
fs = require 'fs'
|
||||
pert = require '../lib/pert.js'
|
||||
Pert = require '../lib/pert.js'
|
||||
|
||||
ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
|
||||
|
||||
@ -17,9 +17,10 @@ cli
|
||||
.command 'example'
|
||||
.description 'show an example of the JSON data format'
|
||||
.action ->
|
||||
pert = new Pert ex, cli.verbose
|
||||
didSomething = yes
|
||||
console.log chalk.bold.green('Before:'), ex
|
||||
console.log chalk.bold.green('After calculations:'), pert.calculate ex
|
||||
console.log chalk.bold.green('After calculations:'), pert.calculate()
|
||||
console.log chalk.green 'Tip:',chalk.bold 'optional fields can be freely included in the input data'
|
||||
|
||||
cli
|
||||
@ -32,8 +33,8 @@ cli
|
||||
fs.readFile file, (error,content) ->
|
||||
if error then err error
|
||||
else
|
||||
if options.json then console.log JSON.stringify (pert.calculate JSON.parse(content))
|
||||
else console.log pert.calculate JSON.parse(content)
|
||||
pert = new Pert JSON.parse(content), cli.verbose
|
||||
console.log pert.calculate options
|
||||
|
||||
cli.parse process.argv
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
chalk = require 'chalk'
|
||||
fs = require 'fs'
|
||||
|
||||
pert =
|
||||
module.exports = class Pert
|
||||
constructor: (@list, @verbose) ->
|
||||
|
||||
log: (x...) -> if @verbose then console.log chalk.bold("Pert:"), x...
|
||||
err: (x...) -> console.log chalk.bold (chalk.red "Pert:"), x...
|
||||
|
||||
@ -9,44 +11,41 @@ pert =
|
||||
maxa: (l) -> return Math.max.apply null, l
|
||||
|
||||
# Find the activity with given id
|
||||
toActivity: (id,list) ->
|
||||
if !list? then pert.err "list is",list
|
||||
toActivity: (id) =>
|
||||
if !@list? then @err "list is", @list
|
||||
item = {}
|
||||
list.forEach (x) -> if x.id is id then item = x
|
||||
@list.forEach (x) -> if x.id is id then item = x
|
||||
return item
|
||||
|
||||
# Find the item
|
||||
calculateEndDay: (item,list) ->
|
||||
# Compute the item's end day
|
||||
calculateEndDay: (item) =>
|
||||
if !item.startDay?
|
||||
pert.log "calculating start day of",item.id
|
||||
item.startDay = pert.calculateStartDay item, list
|
||||
pert.log "start day of",item.id,"is",item.startDay
|
||||
@log "calculating start day of",item.id
|
||||
item.startDay = @calculateStartDay item
|
||||
@log "start day of",item.id,"is",item.startDay
|
||||
item.endDay = item.startDay + item.duration
|
||||
pert.log "end day of",item.id,"is",item.endDay
|
||||
@log "end day of",item.id,"is",item.endDay
|
||||
return item.endDay
|
||||
|
||||
# Find out which day the activity starts
|
||||
calculateStartDay: (item,list) ->
|
||||
calculateStartDay: (item) =>
|
||||
if !item.depends? or item.depends.length is 0 then return item.startDay = 0
|
||||
item.startDay = pert.maxa item.depends.map((x) -> pert.toActivity x,list).map((x) -> pert.calculateEndDay x, list)
|
||||
pert.log "start day of",item.id,"is",item.startDay
|
||||
item.startDay = @maxa item.depends.map(@toActivity).map @calculateEndDay
|
||||
@log "start day of",item.id,"is",item.startDay
|
||||
# write max delay time to each depend
|
||||
item.depends.forEach (x) ->
|
||||
pert.log "checking permittedDelay to dependency", x, "of", item
|
||||
i = pert.toActivity x, list
|
||||
item.depends.forEach (x) =>
|
||||
@log "checking permittedDelay to dependency", x, "of", item
|
||||
i = @toActivity x
|
||||
if !i.permittedDelay?
|
||||
i.permittedDelay = item.startDay - pert.calculateEndDay i, list
|
||||
pert.log "written permittedDelay to dependency", x, "of", item, "as", i.permittedDelay
|
||||
else pert.log "aborting permittedDelay: already calculated"
|
||||
pert.log "permitted delay of",x,"is",i.permittedDelay
|
||||
i.permittedDelay = item.startDay - @calculateEndDay i
|
||||
@log "written permittedDelay to dependency", x, "of", item, "as", i.permittedDelay
|
||||
else @log "aborting permittedDelay: already calculated"
|
||||
@log "permitted delay of",x,"is",i.permittedDelay
|
||||
return item.startDay
|
||||
|
||||
# Find out which activity has the highest id
|
||||
highestID: (list) -> return pert.maxa(list.map (x) -> x.id)
|
||||
highestID: => return @maxa(@list.map (x) -> x.id)
|
||||
|
||||
calculate: (list,verbose) ->
|
||||
pert.verbose = verbose
|
||||
pert.calculateEndDay (pert.toActivity pert.highestID(list), list), list
|
||||
return list
|
||||
|
||||
module.exports = pert
|
||||
calculate: (options) ->
|
||||
@calculateEndDay @toActivity @highestID()
|
||||
if options?.json then JSON.stringify @list else @list
|
||||
|
Loading…
Reference in New Issue
Block a user