Skip to content

Commit

Permalink
TestParsingChoiceDagWithDataTestExpr: all choice branches works!
Browse files Browse the repository at this point in the history
Fixed bug in ChoiceNode.GetNodesToSkip
Improved CompositionExecutionReport.String()
Now DagNodeStatus is a String
  • Loading branch information
redjack96 committed Aug 30, 2024
1 parent abd60b1 commit ef6e486
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 39 deletions.
19 changes: 18 additions & 1 deletion internal/fc/choice_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,33 @@ func (c *ChoiceNode) GetChoiceBranch(dag *Dag, branch int) []DagNode {
return VisitDag(dag, node, branchNodes, true)
}

// GetNodesToSkip skips all node that are in a branch that will not be executed.
// If a skipped branch contains one or more node that is used by the current branch, the node,
// should NOT be skipped (Tested in TestParsingChoiceDagWithDataTestExpr)
func (c *ChoiceNode) GetNodesToSkip(dag *Dag) []DagNode {
nodesToSkip := make([]DagNode, 0)
if c.FirstMatch == -1 || c.FirstMatch >= len(c.Alternatives) {
return nodesToSkip
}

nodesToNotSkip := c.GetChoiceBranch(dag, c.FirstMatch)
for i := 0; i < len(c.Alternatives); i++ {
if i == c.FirstMatch {
continue
}
nodesToSkip = append(nodesToSkip, c.GetChoiceBranch(dag, i)...)
branchNodes := c.GetChoiceBranch(dag, i)
for _, node := range branchNodes {
shouldBeSkipped := true
for _, nodeToNotSkip := range nodesToNotSkip {
if node.Equals(nodeToNotSkip) {
shouldBeSkipped = false
break
}
}
if shouldBeSkipped {
nodesToSkip = append(nodesToSkip, node)
}
}
}
return nodesToSkip
}
Expand Down
17 changes: 14 additions & 3 deletions internal/fc/function_composition.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,20 @@ func (cer *CompositionExecutionReport) String() string {
str += fmt.Sprintf("\n\tResponseTime: %f", cer.ResponseTime)
str += fmt.Sprintf("\n\tReports (len): %d", cer.Reports.Len())
str += "\n\tResult: {"
for s, i := range cer.Result {
str += fmt.Sprintf("\n\t\t%s: %v,", s, i)
i := 0
lll := len(cer.Result)
for s, v := range cer.Result {
if i == 0 {
str += "\n"
}
str += fmt.Sprintf("\t\t%s: %v,", s, v)
if i < lll-1 {
str += ",\n"
} else if i == lll-1 {
str += "\n"
}
i++
}
str += "}\n}\n"
str += "\t}\n}\n"
return str
}
10 changes: 5 additions & 5 deletions internal/fc/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func (ni *DagNodeInfo) Equals(ni2 *DagNodeInfo) bool {
return ni.Id == ni2.Id && ni.Type == ni2.Type && ni.Status == ni2.Status && ni.Group == ni2.Group && ni.Branch == ni2.Branch
}

type DagNodeStatus int
type DagNodeStatus string

const (
Pending = iota
Executed
Skipped // if a node is skipped, all its children nodes should also be skipped
Failed
Pending = "Pending"
Executed = "Executed"
Skipped = "Skipped" // if a node is skipped, all its children nodes should also be skipped
Failed = "Failed"
)

func printStatus(s DagNodeStatus) string {
Expand Down
59 changes: 29 additions & 30 deletions internal/test/aslparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,42 +186,41 @@ func TestParsingChoiceDagWithDataTestExpr(t *testing.T) {
incFn := funcs[0]
// helloFn := funcs[len(funcs)-1]

// runs the workflow (1st choice branch) // TODO: first branch should simply use inc
//params := make(map[string]interface{})
//params[incFn.Signature.GetInputs()[0].Name] = 0
//request := fc.NewCompositionRequest(shortuuid.New(), comp, params)
//_, err2 := comp.Invoke(request) // TODO: Default state fails the nextState is the same SimpleNode, but has the same name in the state machine
//utils.AssertNil(t, err2)

// checks the result // TODO: check that output is 1+1+1=3
//output := resultMap.Result[helloFn.Signature.GetOutputs()[0].Name]
//utils.AssertEquals(t, "expectedResult", output.(string))
//fmt.Println("Result: ", output)

// runs the workflow (2nd choice branch) // TODO: second branch should use double and then inc
//params := make(map[string]interface{})
//params[incFn.Signature.GetInputs()[0].Name] = 0
//request := fc.NewCompositionRequest(shortuuid.New(), comp, params)
//resultMap, err2 := comp.Invoke(request) // TODO: Default state fails the nextState is the same SimpleNode, but has the same name in the state machine
//utils.AssertNil(t, err2)
// runs the workflow (1st choice branch) test: (input == 1)
fmt.Println("1st branch invocation (if input == 1)... ")
params1 := make(map[string]interface{})
params1[incFn.Signature.GetInputs()[0].Name] = 1
request1 := fc.NewCompositionRequest(shortuuid.New(), comp, params1)
resultMap1, err1 := comp.Invoke(request1) // TODO: Default state fails the nextState is the same SimpleNode, but has the same name in the state machine
utils.AssertNil(t, err1)

// checks that output is 1+1+1=3
output := resultMap1.Result[incFn.Signature.GetOutputs()[0].Name]
utils.AssertEquals(t, 3, output.(int))
fmt.Println(resultMap1.String())
fmt.Println("=============================================")
// runs the workflow (2nd choice branch) test: (input == 2)
fmt.Println("2nd branch invocation (else if input == 2)...")
params2 := make(map[string]interface{})
params2[incFn.Signature.GetInputs()[0].Name] = 2
request2 := fc.NewCompositionRequest(shortuuid.New(), comp, params2)
resultMap, err2 := comp.Invoke(request2)
utils.AssertNil(t, err2)

// checks the result // TODO: check that output is 2*2+1 = 5
//output := resultMap.Result[helloFn.Signature.GetOutputs()[0].Name]
//utils.AssertEquals(t, "expectedResult", output.(string))
//fmt.Println("Result: ", output)
// check that output is 2*2+1 = 5
output2 := resultMap.Result[incFn.Signature.GetOutputs()[0].Name]
utils.AssertEquals(t, 5, output2.(int))
fmt.Println("Result: ", output2)

// runs the workflow (default choice branch) // TODO: should only print hello
// runs the workflow (default choice branch)
fmt.Println("=============================================")
fmt.Println("Default branch invocation...")
paramsDefault := make(map[string]interface{})
paramsDefault[incFn.Signature.GetInputs()[0].Name] = "Giacomo"
request := fc.NewCompositionRequest(shortuuid.New(), comp, paramsDefault)
resultMap, errDef := comp.Invoke(request) // TODO: Default state fails the nextState is the same SimpleNode, but has the same name in the state machine
requestDefault := fc.NewCompositionRequest(shortuuid.New(), comp, paramsDefault)
resultMap, errDef := comp.Invoke(requestDefault)
utils.AssertNil(t, errDef)
fmt.Printf("Composition Execution Report: %s\n", resultMap.String())

// checks the result // TODO: should check that contains the printed result.
//output := resultMap.Result[helloFn.Signature.GetOutputs()[0].Name]
//utils.AssertEquals(t, "expectedResult", output.(string))
//fmt.Println("Result: ", output)
}

func TestParsingChoiceDagWithBoolExpr(t *testing.T) {
Expand Down

0 comments on commit ef6e486

Please sign in to comment.