{"id":26601373,"url":"https://github.com/calebwin/go-sm","last_synced_at":"2025-04-09T16:34:10.798Z","repository":{"id":57502679,"uuid":"148851058","full_name":"calebwin/go-sm","owner":"calebwin","description":"A finite-state machine library for the Go programming language","archived":false,"fork":false,"pushed_at":"2018-09-18T06:18:56.000Z","size":16,"stargazers_count":14,"open_issues_count":2,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-23T18:50:53.131Z","etag":null,"topics":["finite-state-machine","golang","library","state-machine"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/calebwin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-14T23:24:39.000Z","updated_at":"2024-02-26T10:47:38.000Z","dependencies_parsed_at":"2022-09-13T07:02:21.019Z","dependency_job_id":null,"html_url":"https://github.com/calebwin/go-sm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fgo-sm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fgo-sm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fgo-sm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fgo-sm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calebwin","download_url":"https://codeload.github.com/calebwin/go-sm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248067773,"owners_count":21042353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["finite-state-machine","golang","library","state-machine"],"created_at":"2025-03-23T18:39:36.550Z","updated_at":"2025-04-09T16:34:10.782Z","avatar_url":"https://github.com/calebwin.png","language":"Go","funding_links":[],"categories":["Libraries"],"sub_categories":["Go"],"readme":"## What it is\ngo-sm is a library for generating persistent finite-state machines in the Go programming language. go-sm currently supports lifecycle callbacks, state history storage, generated .dot graph visualizations.\n\n## How to use it\nA basic finite-state machine with two states and two transitions can be created as follows with a simple 3-step process.\n```golang\nimport \"github.com/calebwin/go-sm/fsm\"\n\nmyFSM := fsm.Generate(\"locked\") // 1) generate a new finite-state machine with an inital state of \"locked\"\n\nmyTransitions := []fsm.Transition{\n  fsm.Transition{\n  \t\"coin\", \n\t[]fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, \n\tfsm.State{\"un-locked\"}\n  },\n  fsm.Transition{\n  \t\"push\", \n\t[]fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, \n\tfsm.State{\"locked\"}\n  },\n}\nmyFSM = fsm.SetTransitions(myFSM, myTransitions) // 2) define possible transitions within the finite-state machine\n\nmyFSM = fsm.Execute(myFSM, \"coin\") // 3) execute the transition named \"coin\"\n\nmyFSM.state // \"un-locked\"\n```\n\n### Callback Functions\nCallback functions can be defined for the following 4 lifecycle events.\n- `onBeforeTransition`\n- `onAfterTransition`\n- `onEnterState`\n- `onLeaveState`\nThey can be defined with go-sm as follows.\n```golang\nmyFSM := fsm.Generate(\"locked\")\n\nmyTransitions := []fsm.Transition{\n  fsm.Transition{\"coin\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"un-locked\"}},\n  fsm.Transition{\"push\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"locked\"}},\n}\nmyFSM = fsm.SetTransitions(myFSM, myTransitions) \n\ncoins := 0\nmyFSM = fsm.SetCallbacks(myFSM,\n  func(transition string) {}, // onBeforeTransition\n  func(transition string) { // onAfterTransition\n    if transition == \"coin\" {\n      money += 1\n    }\n  },\n  func(state string) {}, // onEnterState\n  func(state string) {}, // onLeaveState\n)\n\nmyFSM = fsm.Execute(myFSM, \"coin\") \n```\n\n### State History\nState history can be maintained with go-sm as follows.\n```golang\nmyFSM := fsm.Generate(\"locked\", true) // first flag set to true to indicate history should be maintained\n\nmyTransitions := []fsm.Transition{\n  fsm.Transition{\"coin\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"un-locked\"}},\n  fsm.Transition{\"push\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"locked\"}},\n}\nmyFSM = fsm.SetTransitions(myFSM, myTransitions) \n\nmyFSM = fsm.Execute(myFSM, \"coin\") \n\nmyFSM.history // {\"locked\", \"un-locked\",}\n```\nState history can be used to undo/redo state transitions.\n```golang\nmyFSM := fsm.Generate(\"locked\", true) // first flag set to true to indicate history should be maintained\n\nmyTransitions := []fsm.Transition{\n  fsm.Transition{\"coin\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"un-locked\"}},\n  fsm.Transition{\"push\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"locked\"}},\n}\nmyFSM = fsm.SetTransitions(myFSM, myTransitions) \n\nmyFSM = fsm.Execute(myFSM, \"coin\")\nmyFSM = fsm.Execute(myFSM, \"coin\")\n\nmyFSM = fsm.HistoryBack(myFSM, 2) // undo 2 state transitions\n\nmyFSM.state // \"locked\"\n```\nState history can also be cleared.\n```golang\nmyFSM := fsm.Generate(\"locked\", true) // first flag set to true to indicate history should be maintained\n\nmyTransitions := []fsm.Transition{\n  fsm.Transition{\"coin\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"un-locked\"}},\n  fsm.Transition{\"push\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"locked\"}},\n}\nmyFSM = fsm.SetTransitions(myFSM, myTransitions) \n\nmyFSM = fsm.Execute(myFSM, \"coin\")\nmyFSM = fsm.Execute(myFSM, \"coin\")\n\nmyFSM = fsm.ClearHistory(myFSM)\n\nmyFSM.history // {\"un-locked\",}\n```\n\n### Visualizations\nVisualizations of finite-state machines can be generated as .dot files as follows.\n```golang\nmyFSM := fsm.Generate(\"locked\", true) // first flag set to true to indicate history should be maintained\n\nmyTransitions := []fsm.Transition{\n  fsm.Transition{\"coin\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"un-locked\"}},\n  fsm.Transition{\"push\", []fsm.State{fsm.State{\"locked\"}, fsm.State{\"un-locked\"},}, fsm.State{\"locked\"}},\n}\nmyFSM = fsm.SetTransitions(myFSM, myTransitions) \n\nfsm.GenerateVisualization(myFSM, \"myVisualization.dot\")\n```\nThe above call to `GenerateVisualization` will result in the following file created.\n```\ndigraph {\n\tlocked;\n\tun-locked;\n\n\t\"locked\" -\u003e \"un-locked\" [ label=\" coin \" ];\n\t\"un-locked\" -\u003e \"un-locked\" [ label=\" coin \" ];\n\t\"locked\" -\u003e \"locked\" [ label=\" push \" ];\n\t\"un-locked\" -\u003e \"locked\" [ label=\" push \" ];\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebwin%2Fgo-sm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalebwin%2Fgo-sm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebwin%2Fgo-sm/lists"}