Skip to content

Commit

Permalink
feat: detect PERT undefined dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 29, 2019
1 parent 49e30a0 commit 77ea9f9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
6 changes: 6 additions & 0 deletions attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func (a Attrs) getOrCreatePert() *PertAttrs {
return a["pert"].(*PertAttrs)
}

func (a Attrs) SetPertUndefinedDependency() Attrs {
pert := a.getOrCreatePert()
pert.IsUndefinedDependency = true
return a
}

func (a Attrs) SetPertEstimates(opt, real, pess float64) Attrs {
pert := a.getOrCreatePert()
pert.Optimistic = opt
Expand Down
2 changes: 1 addition & 1 deletion cmd/pertify/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/pkg/errors v0.8.1
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22
moul.io/graphman v1.3.0
moul.io/graphman v1.4.0
moul.io/graphman/viz v0.0.0
)

Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module moul.io/graphman/examples

go 1.12

require moul.io/graphman v1.3.0
require moul.io/graphman v1.4.0

require moul.io/graphman/viz v0.0.0

Expand Down
2 changes: 1 addition & 1 deletion lambda/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/aws/aws-lambda-go v1.11.1
github.com/pkg/errors v0.8.1
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22
moul.io/graphman v1.3.0
moul.io/graphman v1.4.0
moul.io/graphman/viz v1.2.0
)

Expand Down
1 change: 1 addition & 0 deletions pert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type PertAttrs struct {
IsAction, IsState bool
IsNonStandardGraph bool
IsStart, IsFinish bool
IsUndefinedDependency bool
}

func (pa PertAttrs) WeightedEstimate() float64 {
Expand Down
29 changes: 18 additions & 11 deletions pert_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func FromPertConfig(config PertConfig) *Graph {
dependency := action.DependsOn[0]

graph.AddEdge(
graph.pertGetDependencyEnd(dependency),
graph.pertGetDependencyEnd(dependency, config),
pertPreID(action.ID),
attrs,
)
Expand All @@ -116,7 +116,7 @@ func FromPertConfig(config PertConfig) *Graph {
)
for _, dependency := range action.DependsOn {
graph.AddEdge(
graph.pertGetDependencyEnd(dependency),
graph.pertGetDependencyEnd(dependency, config),
pertPreID(action.ID),
Attrs{}.SetPertZeroTimeActivity(),
)
Expand All @@ -128,7 +128,7 @@ func FromPertConfig(config PertConfig) *Graph {
for _, state := range config.States {
for _, dependency := range state.DependsOn {
graph.AddEdge(
graph.pertGetDependencyEnd(dependency),
graph.pertGetDependencyEnd(dependency, config),
state.ID,
Attrs{}.SetPertZeroTimeActivity(),
)
Expand All @@ -149,24 +149,27 @@ func FromPertConfig(config PertConfig) *Graph {

if !config.Opts.NoSimplify { // simplify the graph
for {
pertRemoveDummySteps(graph)
pertMergeDummyActionGroups(graph)
pertRemoveDummySteps(graph, config)
pertMergeDummyActionGroups(graph, config)
if removed := graph.gc(); removed == 0 {
break
}
}
}

for _, edge := range graph.Vertices() {
for _, vertex := range graph.Vertices() {
if pert := vertex.GetPert(); pert == nil {
vertex.Attrs.SetPertUndefinedDependency()
}
if !config.Opts.StandardPert {
edge.SetPertNonStandardGraph()
vertex.SetPertNonStandardGraph()
}
}

return graph
}

func pertRemoveDummySteps(graph *Graph) {
func pertRemoveDummySteps(graph *Graph, config PertConfig) {
// remove dummy states with only one dummy successor
for _, vertex := range graph.Vertices() {
if vertex.deleted || !pertIsUntitled(vertex) || vertex.OutDegree() != 1 || vertex.InDegree() == 0 || pertHasEstimates(vertex) {
Expand All @@ -183,7 +186,7 @@ func pertRemoveDummySteps(graph *Graph) {
}
}

func pertMergeDummyActionGroups(graph *Graph) {
func pertMergeDummyActionGroups(graph *Graph, config PertConfig) {
// merge dummy action groups
for _, vertex := range graph.Vertices() {
if vertex.deleted || vertex.OutDegree() < 2 {
Expand Down Expand Up @@ -241,7 +244,7 @@ func pertMergeDummyActionGroups(graph *Graph) {
}
metaVertex := graph.AddVertex(metaID, attrs)
for _, predecessor := range predecessors {
depID := graph.pertGetDependencyEnd(predecessor.id)
depID := graph.pertGetDependencyEnd(predecessor.id, config)
graph.AddEdge(depID, metaID, Attrs{}.SetPertZeroTimeActivity())
}
for _, successor := range successors {
Expand All @@ -261,7 +264,11 @@ func pertMergeDummyActionGroups(graph *Graph) {
func pertPreID(id string) string { return fmt.Sprintf("pre_%s", id) }
func pertPostID(id string) string { return fmt.Sprintf("post_%s", id) }

func (g *Graph) pertGetDependencyEnd(dependency string) string {
func (g *Graph) pertGetDependencyEnd(dependency string, config PertConfig) string {
if !config.Opts.StandardPert {
return dependency
}

// if dependency is a vertex, the end is the vertex itself
if vertex := g.GetVertex(dependency); vertex != nil {
return vertex.id
Expand Down
2 changes: 1 addition & 1 deletion viz/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/awalterschulze/gographviz v0.0.0-20190522210029-fa59802746ab
moul.io/graphman v1.3.0
moul.io/graphman v1.4.0
)

replace moul.io/graphman => ../
3 changes: 3 additions & 0 deletions viz/graphviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func attrsFromVertex(vertex *graphman.Vertex, opts *Opts) map[string]string {
attrs[string(graphviz.Label)] = " "
attrs[string(graphviz.Shape)] = "circle"
// attrs[string(graphviz.Style)] = "dashed"
case pert.IsUndefinedDependency:
attrs[string(graphviz.Label)] = fmt.Sprintf("undefined dependency: %q", vertex.ID())
attrs[string(graphviz.Shape)] = "octagon"
default:
log.Printf("cannot determine pert style: %v", pert)
attrs[string(graphviz.Label)] = fmt.Sprintf("error: %q", vertex.ID())
Expand Down

0 comments on commit 77ea9f9

Please sign in to comment.