add comments

This commit is contained in:
filifa 2024-05-25 23:32:29 -05:00
parent b1f0d60184
commit 82befabe64
1 changed files with 14 additions and 0 deletions

View File

@ -27,18 +27,22 @@ import (
"gonum.org/v1/gonum/graph/topo" "gonum.org/v1/gonum/graph/topo"
) )
// projectNetwork is a directed graph of activity nodes
type projectNetwork struct { type projectNetwork struct {
*simple.DirectedGraph *simple.DirectedGraph
} }
// newProjectNetwork is a constructor for a projectNetwork
func newProjectNetwork() *projectNetwork { func newProjectNetwork() *projectNetwork {
return &projectNetwork{DirectedGraph: simple.NewDirectedGraph()} return &projectNetwork{DirectedGraph: simple.NewDirectedGraph()}
} }
// NewNode returns an activity node
func (g *projectNetwork) NewNode() graph.Node { func (g *projectNetwork) NewNode() graph.Node {
return &activity{Node: g.DirectedGraph.NewNode()} return &activity{Node: g.DirectedGraph.NewNode()}
} }
// calculateTimes computes the times for each activity in g
func (g *projectNetwork) calculateTimes() error { func (g *projectNetwork) calculateTimes() error {
sorted, err := topo.Sort(g) sorted, err := topo.Sort(g)
if err != nil { if err != nil {
@ -56,12 +60,15 @@ func (g *projectNetwork) calculateTimes() error {
return nil return nil
} }
// forwardPass computes the early times for each activity
func (g *projectNetwork) forwardPass(toposort []*activity) { func (g *projectNetwork) forwardPass(toposort []*activity) {
for _, a := range toposort { for _, a := range toposort {
g.setEarlyTimes(a) g.setEarlyTimes(a)
} }
} }
// backwardPass computes the late times and slack for each activity using the
// early times
func (g *projectNetwork) backwardPass(toposort []*activity) { func (g *projectNetwork) backwardPass(toposort []*activity) {
n := len(toposort) n := len(toposort)
@ -75,6 +82,7 @@ func (g *projectNetwork) backwardPass(toposort []*activity) {
} }
} }
// setEarlyTimes sets the early times for an activity
func (g *projectNetwork) setEarlyTimes(a *activity) { func (g *projectNetwork) setEarlyTimes(a *activity) {
es := 0.0 es := 0.0
predecessors := g.To(a.ID()) predecessors := g.To(a.ID())
@ -86,6 +94,7 @@ func (g *projectNetwork) setEarlyTimes(a *activity) {
a.earlyFinish = a.earlyStart + a.duration a.earlyFinish = a.earlyStart + a.duration
} }
// setLateTimes sets the late times for an activity
func (g *projectNetwork) setLateTimes(a *activity) { func (g *projectNetwork) setLateTimes(a *activity) {
lf := math.Inf(1) lf := math.Inf(1)
successors := g.From(a.ID()) successors := g.From(a.ID())
@ -98,6 +107,7 @@ func (g *projectNetwork) setLateTimes(a *activity) {
a.slack = a.lateFinish - a.earlyFinish a.slack = a.lateFinish - a.earlyFinish
} }
// an activity represents a task in the project network
type activity struct { type activity struct {
graph.Node graph.Node
dotID string dotID string
@ -109,6 +119,7 @@ type activity struct {
slack float64 slack float64
} }
// Attributes returns all the activity attributes to include in the DOT file
func (a *activity) Attributes() []encoding.Attribute { func (a *activity) Attributes() []encoding.Attribute {
return []encoding.Attribute{ return []encoding.Attribute{
{"DUR", strconv.FormatFloat(a.duration, 'f', 2, 64)}, {"DUR", strconv.FormatFloat(a.duration, 'f', 2, 64)},
@ -120,6 +131,7 @@ func (a *activity) Attributes() []encoding.Attribute {
} }
} }
// SetAttribute permits reading the duration from the DOT file
func (a *activity) SetAttribute(attr encoding.Attribute) error { func (a *activity) SetAttribute(attr encoding.Attribute) error {
var err error var err error
switch attr.Key { switch attr.Key {
@ -131,10 +143,12 @@ func (a *activity) SetAttribute(attr encoding.Attribute) error {
return err return err
} }
// DOTID returns the DOTID of the activity
func (a *activity) DOTID() string { func (a *activity) DOTID() string {
return a.dotID return a.dotID
} }
// SetDOTID sets the DOT ID of the activity
func (a *activity) SetDOTID(id string) { func (a *activity) SetDOTID(id string) {
a.dotID = id a.dotID = id
} }