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