-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (83 loc) · 2.59 KB
/
main.go
File metadata and controls
95 lines (83 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Package main demonstrates conditional routing in gographgo.
//
// This example models a simple review pipeline:
//
// Start → review → approve OR reject → notify → End
//
// The "review" node sets a Score. A conditional edge then routes to
// "approve" or "reject" based on that score. Both paths converge at
// "notify" before the graph exits.
package main
import (
"context"
"fmt"
"log"
"github.com/SkinnyPeteTheGiraffe/gographgo/pkg/graph"
)
// State holds review pipeline data.
type State struct {
Item string
Verdict string
Notice string
Score int
}
func main() {
builder := graph.NewStateGraph[State]()
// review assigns a score.
builder.AddNode("review", func(ctx context.Context, s State) (graph.NodeResult, error) {
score := len(s.Item) * 10 // simple deterministic score for demonstration
return graph.NodeWrites(map[string]graph.Dynamic{
"Score": graph.Dyn(score),
}), nil
})
// approve handles high-scoring items.
builder.AddNode("approve", func(ctx context.Context, s State) (graph.NodeResult, error) {
return graph.NodeWrites(map[string]graph.Dynamic{
"Verdict": graph.Dyn("approved"),
}), nil
})
// reject handles low-scoring items.
builder.AddNode("reject", func(ctx context.Context, s State) (graph.NodeResult, error) {
return graph.NodeWrites(map[string]graph.Dynamic{
"Verdict": graph.Dyn("rejected"),
}), nil
})
// notify sends a summary regardless of verdict.
builder.AddNode("notify", func(ctx context.Context, s State) (graph.NodeResult, error) {
msg := fmt.Sprintf("Item %q scored %d and was %s.", s.Item, s.Score, s.Verdict)
return graph.NodeWrites(map[string]graph.Dynamic{
"Notice": graph.Dyn(msg),
}), nil
})
// Wire static edges.
builder.AddEdge(graph.Start, "review")
builder.AddEdge("approve", "notify")
builder.AddEdge("reject", "notify")
builder.AddEdge("notify", graph.End)
// Conditional edge: after "review", route based on score.
builder.AddConditionalEdges("review",
func(ctx context.Context, s State) (graph.Route, error) {
if s.Score >= 50 {
return graph.RouteTo("approve"), nil
}
return graph.RouteTo("reject"), nil
},
// PathMap lists all possible destinations so the graph can validate them.
map[string]string{
"approve": "approve",
"reject": "reject",
},
)
compiled, err := builder.Compile()
if err != nil {
log.Fatalf("compile: %v", err)
}
for _, item := range []string{"hi", "gographgo"} {
result, err := compiled.Invoke(context.Background(), State{Item: item})
if err != nil {
log.Fatalf("invoke: %v", err)
}
final := result.Value.(State)
fmt.Println(final.Notice)
}
}