gocgraph

package module
v0.0.0-...-52ce737 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: MIT Imports: 6 Imported by: 0

README

go_cgraph_lite

A lightweight DAG (Directed Acyclic Graph) execution framework for Go, ported from C++ CGraph-lite library.

Features

  • 🚀 Lightweight DAG execution framework
  • 🔄 Thread-safe parameter sharing between nodes
  • Concurrent execution with worker pool
  • 🏗️ Easy-to-use builder pattern for pipeline construction
  • 🔧 Compatible with original C++ CGraph-lite interface

Quick Start

Basic Example
package main

import "fmt"

// Define your custom node
type MyNode struct {
    BaseGElement
}

func (n *MyNode) Run() *CStatus {
    fmt.Printf("%s: Hello World!\n", n.GetName())
    return NewCStatus()
}

func main() {
    // Create pipeline
    pipeline := Factory.Create()
    
    // Register nodes
    node := &MyNode{}
    pipeline.RegisterGElement(node, []GElement{}, "MyNode")
    
    // Execute
    pipeline.Process(1)
    
    // Cleanup
    Factory.Remove(pipeline)
}
Parameter Sharing Example

See T02_Param.go for a complete example of sharing parameters between nodes.

Examples

  • T01_Simple.go - Basic node execution example
  • T02_Param.go - Parameter sharing between nodes example

Installation

go get github.com/AsunaU2/GoCGraph

Building

go build .

Running Examples

# Run simple example
go run T01_Simple.go go_cgraph_lite.go

# Run parameter example  
go run T02_Param.go go_cgraph_lite.go

Core Concepts

GElement (Node)

Basic execution unit in the DAG. Implement the Run() method to define your logic.

GPipeline

Manages the entire DAG execution flow, handles dependencies and concurrent execution.

GParam

Thread-safe parameter sharing mechanism between nodes.

Acknowledgments

This project is inspired by and ported from the excellent CGraph-lite C++ library by ChunelFeng. We extend our heartfelt gratitude to the original author for creating such an elegant and efficient DAG execution framework.

License

MIT License

Contributing

Pull requests are welcome! For major changes, please open an issue first.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Factory = &GPipelineFactory{}

Global factory instance for convenience

Functions

This section is empty.

Types

type BaseGElement

type BaseGElement struct {
	// contains filtered or unexported fields
}

BaseGElement provides a basic implementation that other nodes can embed

func (*BaseGElement) CreateGParam

func (e *BaseGElement) CreateGParam(key string, param GParam) *CStatus

CreateGParam creates a parameter with the specified key and type

func (*BaseGElement) Destroy

func (e *BaseGElement) Destroy() *CStatus

Destroy provides default destruction

func (*BaseGElement) GetGParam

func (e *BaseGElement) GetGParam(key string) GParam

GetGParam retrieves a parameter by key

func (*BaseGElement) GetName

func (e *BaseGElement) GetName() string

GetName returns the node name

func (*BaseGElement) Init

func (e *BaseGElement) Init() *CStatus

Init provides default initialization

func (*BaseGElement) SetName

func (e *BaseGElement) SetName(name string)

SetName sets the node name

func (*BaseGElement) SetParamManager

func (e *BaseGElement) SetParamManager(pm *GParamManager)

SetParamManager sets the parameter manager

type BaseGParam

type BaseGParam struct {
	// contains filtered or unexported fields
}

BaseGParam provides a default implementation of GParam

func (*BaseGParam) GetLock

func (p *BaseGParam) GetLock() *sync.RWMutex

GetLock returns the shared lock for thread safety

func (*BaseGParam) Reset

func (p *BaseGParam) Reset(curStatus *CStatus)

Reset provides default parameter reset

func (*BaseGParam) Setup

func (p *BaseGParam) Setup() *CStatus

Setup provides default parameter initialization

type CStatus

type CStatus struct {
	// contains filtered or unexported fields
}

CStatus represents the execution result status

func NewCStatus

func NewCStatus() *CStatus

NewCStatus creates a new successful status

func NewCStatusWithError

func NewCStatusWithError(errorInfo string) *CStatus

NewCStatusWithError creates a new error status

func (*CStatus) Combine

func (s *CStatus) Combine(other *CStatus) *CStatus

Combine combines two status objects

func (*CStatus) GetCode

func (s *CStatus) GetCode() int

GetCode returns the error code

func (*CStatus) GetInfo

func (s *CStatus) GetInfo() string

GetInfo returns the error information

func (*CStatus) IsOK

func (s *CStatus) IsOK() bool

IsOK checks if the status is successful

type GElement

type GElement interface {
	Init() *CStatus
	Run() *CStatus
	Destroy() *CStatus
	GetName() string

	// Parameter management methods
	CreateGParam(key string, param GParam) *CStatus
	GetGParam(key string) GParam
}

GElement represents a node in the DAG

type GNode

type GNode = GElement

GNode is an alias for GElement

type GParam

type GParam interface {
	Setup() *CStatus
	Reset(curStatus *CStatus)
}

GParam is the base interface for shared parameters between nodes

type GParamManager

type GParamManager struct {
	// contains filtered or unexported fields
}

GParamManager manages the lifecycle of all shared parameters

func NewGParamManager

func NewGParamManager() *GParamManager

NewGParamManager creates a new parameter manager

func (*GParamManager) Create

func (pm *GParamManager) Create(key string, param GParam) *CStatus

Create creates a parameter with the specified key and type

func (*GParamManager) Get

func (pm *GParamManager) Get(key string) GParam

Get retrieves a parameter by key

func (*GParamManager) Reset

func (pm *GParamManager) Reset(curStatus *CStatus)

Reset resets all parameters

func (*GParamManager) Setup

func (pm *GParamManager) Setup() *CStatus

Setup initializes all parameters

type GPipeline

type GPipeline struct {
	// contains filtered or unexported fields
}

GPipeline manages the entire DAG execution flow

func NewGPipeline

func NewGPipeline() *GPipeline

NewGPipeline creates a new pipeline

func (*GPipeline) Process

func (p *GPipeline) Process(times int) *CStatus

Process executes the pipeline for the specified number of times

func (*GPipeline) RegisterGElement

func (p *GPipeline) RegisterGElement(element GElement, depends []GElement, name string) *CStatus

RegisterGElement registers a graph element with dependencies

type GPipelineFactory

type GPipelineFactory struct{}

GPipelineFactory provides factory methods for pipeline creation and destruction

func (*GPipelineFactory) Create

func (f *GPipelineFactory) Create() *GPipeline

Create creates a new pipeline instance

func (*GPipelineFactory) Remove

func (f *GPipelineFactory) Remove(pipeline *GPipeline) *CStatus

Remove destroys a pipeline instance

type Schedule

type Schedule struct {
	// contains filtered or unexported fields
}

Schedule implements a thread pool and task queue for concurrent execution

func NewSchedule

func NewSchedule(numWorkers int) *Schedule

NewSchedule creates a new scheduler with specified number of workers

func (*Schedule) Commit

func (s *Schedule) Commit(task func())

Commit submits a task to the task queue

func (*Schedule) Stop

func (s *Schedule) Stop()

Stop stops all worker goroutines

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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