graph

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 3 Imported by: 0

README

Library for graph based programming in go

Install

go get github.com/rizvn/graph

Usage

import (
	"fmt"
	g "github.com/rizvn/graph"
)

// Create a mock graph struct for testing
type ExampleGraph struct {
	g.Graph
	counter int
}

// Define nodes and edges of the graph
func (r *ExampleGraph) Init() {

	// Initialize the graph
	r.InitGraph()

	// on start run Step1
	r.StepNamed(g.START, r.Step1).
		// then goto step 2
		When(g.DEFAULT, r.Step2, "Proceed to Step 2")

	// define Step2
	r.Step(r.Step2).
		// after step 2, if Step2RepeatCondition is true goto Step2
		When(r.Step2RepeatCondition, r.Step2, "").

		// after step 2, if Step2ContinueCondition is true goto Step3
		When(r.Step2ContinueCondition, r.Step3, "Repeat step 3 until counter is  3")

	// define Step3
	r.Step(r.Step3)

}

// Step1 logic
func (r *ExampleGraph) Step1() {
	fmt.Println("Counter initialised to 0")
	r.counter = 0
}

// Step2 logic
func (r *ExampleGraph) Step2() {
	fmt.Println("Increment counter")
	r.counter++
	fmt.Printf("Counter: %d\n", r.counter)
}

// Condition to continue to from Step2
func (r *ExampleGraph) Step2ContinueCondition() bool {
	return r.counter >= 3
}

// Condition to repeat Step2
func (r *ExampleGraph) Step2RepeatCondition() bool {
	return r.counter < 3
}

// Step3 logic
func (r *ExampleGraph) Step3() {
	fmt.Println("Reached Step 3, Graph execution complete.")
}

To run the graph:

exampleGraph := &ExampleGraph{}
exampleGraph.Init()
exampleGraph.RunGraph()	

More examples

see Graph_test.go for more examples of graph usage.

Documentation

Index

Constants

View Source
const START = "start"

Variables

View Source
var DEFAULT = func() bool {
	return true
}

Functions

func GetMethodName

func GetMethodName(f any) string

GetMethodName returns the name of the function, stripping any suffixes this is used to derive node names from function names

Types

type Edge

type Edge struct {
	Description string
	To          string
	Condition   func() bool
}

type Graph

type Graph struct {
	Nodes       map[string]*Node
	CurrentNode string

	// Hooks for before and after node and edge execution
	// These can be set to custom functions for logging or other purposes
	HookBeforeStep       func(stepName string)
	HookAfterStep        func(stepName string)
	HookBeforeTransition func(fromStep string, toStep string, description string)
}

func (*Graph) InitGraph

func (r *Graph) InitGraph()

InitGraph initializes the graph

func (*Graph) RunGraph

func (r *Graph) RunGraph()

RunGraph executes the graph starting from the current node

func (*Graph) SetStepToRun

func (r *Graph) SetStepToRun(stepName string)

SetStepToRun Override the next node in the graph

func (*Graph) Step

func (r *Graph) Step(fn func()) *Node

Step adds a node to the graph, name is derived from the function name

func (*Graph) StepNamed

func (r *Graph) StepNamed(name string, fn func()) *Node

StepNamed adds a node to the graph with a specific name

func (*Graph) String

func (r *Graph) String() string

String traverses all nodes and generates a Mermaid JS graph string

type Node

type Node struct {
	Func  func()
	Edges []*Edge
}

func (*Node) When

func (r *Node) When(condition func() bool, fn func(), descriptions ...string) *Node

When adds a conditional edge to the node name is derived from the function name

func (*Node) WhenNamed

func (r *Node) WhenNamed(condition func() bool, to string, descriptions ...string) *Node

WhenNamed adds a conditional edge to the node

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL