{"id":16848681,"url":"https://github.com/d5/go-fsm","last_synced_at":"2025-03-22T05:31:32.546Z","repository":{"id":57497035,"uuid":"169021406","full_name":"d5/go-fsm","owner":"d5","description":"A scriptable FSM library for Go","archived":false,"fork":false,"pushed_at":"2019-12-29T21:53:14.000Z","size":29,"stargazers_count":38,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-01T13:22:54.122Z","etag":null,"topics":["fsm","go","golang","scripting-language","tengo"],"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/d5.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":"2019-02-04T03:09:16.000Z","updated_at":"2025-01-11T12:23:49.000Z","dependencies_parsed_at":"2022-09-03T23:50:54.665Z","dependency_job_id":null,"html_url":"https://github.com/d5/go-fsm","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d5%2Fgo-fsm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d5%2Fgo-fsm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d5%2Fgo-fsm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d5%2Fgo-fsm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/d5","download_url":"https://codeload.github.com/d5/go-fsm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244188025,"owners_count":20412974,"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":["fsm","go","golang","scripting-language","tengo"],"created_at":"2024-10-13T13:12:11.918Z","updated_at":"2025-03-22T05:31:31.346Z","avatar_url":"https://github.com/d5.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-fsm\n\n[![GoDoc](https://godoc.org/github.com/d5/go-fsm?status.svg)](https://godoc.org/github.com/d5/go-fsm)\n[![CircleCI](https://circleci.com/gh/d5/go-fsm.svg?style=svg)](https://circleci.com/gh/d5/go-fsm)\n\n**A scriptable [FSM](https://en.wikipedia.org/wiki/Finite-state_machine) library for Go**\n\n- [Tengo](https://github.com/d5/tengo) language: fast and secure\n- Scriptable functions: transition conditions, transition actions, state entry and exit actions\n- Immutable values\n\n## Concepts\n\n### State Machines\n\nA state machine is defined by \n\n- A set of named **states**, and,\n- An ordered list of **transitions** between states\n\n\n### States\n\nA state is defined by:\n\n- **Name**: a unique string identifier in the state machine\n- **Entry Action**: a function that's executed when entering the state\n- **Exit Action**: a function that's executed when exiting the state\n\n### Transitions\n\nA transition is defined by:\n\n- **Src**: the current state\n- **Dst**: the next state\n- **Condition**: a function that evaluates the condition \n- **Action**: a function that's executed when the condition is fulfilled\n\n### Condition Functions\n\nIf condition function name is not specified (an empty space), the transition is considered as unconditional (always evalutes to true).\n\nCondition functions in the script should take 3 arguments:\n\n```golang\nfunc(src, dst, v) {\n    /* some logic */\n    return some_value \n}\n```\n\n- `src`: the current state\n- `dst`: the next state\n- `v`: the data value (immutable)\n\nThe state machine use the returned value to determine the condition of the transition. E.g. condition is fulfilled if the value is [truthy](https://github.com/d5/tengo/blob/master/docs/runtime-types.md#objectisfalsy). In Tengo, the function that does not return anything is treated as if it returns `undefined` which is falsy. \n\n### Action Functions\n\nAction functions in the script should take 3 arguments:\n\n```golang\nfunc(src, dst, v) {\n    /* some logic */\n    return some_value \n}\n```\n\n- `src`: the current state\n- `dst`: the next state\n- `v`: the data value (immutable)\n\nThe data value passed to action functions is immutable, but, the function may return a new value to change the data value for the future condition/action functions.\n\n- If the function returns `undefined` _(or does not return anything)_, the data value remains unmodified.\n- If the function returns `error` objects (e.g. `return error(\"some error\")`), the state machine stops and returns an error from `StateMachine.Run` function call.\n- If the function returns a value of any other type, the data value of the state machine is changed to the returned value.  \n\n### Input and Output\n\nWhen running the state machine, user can pass an input data value that will be used by condition and action functions. The state machine will return the final _output_ data value when there are no more transitions available.\n\n### Execution Flow\n\n1. When the state machine starts, it's given an initial state and the input data.\n2. The state machine evaluates a list of transitions that are defined with the current state as its `src` state. The state machine evaluates the transitions in the same order they were added (defined). \n    1. If `condition` script is specified, the state machine runs the script to determines whether the condition is fulfilled or not.\n    2. If `condition` script is not specified, \n3. If one of the transition's condition is fulfilled, the state machine runs the action scripts:\n    1. It runs `exit action` of the current state if it's defined.\n    2. It runs `action` of the transition if it's defined.\n    3. It runs `entry action` of the next state if it's defined.\n4. If no transitions were fulfilled, the state machine stops and returns the final value.\n5. Repeat from the step 2.\n\n## Example\n\nHere's an example code for an FSM that tests if the input string is valid decimal numbers (e.g. `123.456`) or not: \n\n```golang\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/d5/go-fsm\"\n)\n\nvar decimalsScript = []byte(`\nfmt := import(\"fmt\")\n\nexport {\n\t// test if the first character is a digit\n\tis_digit: func(src, dst, v) {\n\t\treturn v[0] \u003e= '0' \u0026\u0026 v[0] \u003c= '9'\n\t},\n\t// test if the first character is a period\n\tis_dot: func(src, dst, v) {\n\t\treturn v[0] == '.'  \n\t},\n\t// test if there are no more characters left\n\tis_eol: func(src, dst, v) {\n\t\treturn len(v) == 0  \n\t},\n\t// prints out transition info\n\tprint_tx: func(src, dst, v) {\n\t\tfmt.printf(\"%s -\u003e %s: %q\\n\", src, dst, v)\n\t},\n\t// cut the first character\n\tenter: func(src, dst, v) {\n\t\treturn v[1:]\n\t},\n\tenter_end: func(src, dst, v) {\n\t\treturn \"valid number\"\n\t}, \n\tenter_error: func(src, dst, v) {\n\t\treturn \"invalid number: \" + v\n\t}\n}`)\n\nfunc main() {\n    // build and compile state machine\n    machine, err := fsm.New(decimalsScript).\n        State(\"S\", \"enter\", \"\").       // start\n        State(\"N\", \"enter\", \"\").       // whole numbers\n        State(\"P\", \"enter\", \"\").       // decimal point\n        State(\"F\", \"enter\", \"\").       // fractional part\n        State(\"E\", \"enter_end\", \"\").   // end\n        State(\"X\", \"enter_error\", \"\"). // error\n        Transition(\"S\", \"E\", \"is_eol\", \"print_tx\").\n        Transition(\"S\", \"N\", \"is_digit\", \"print_tx\").\n        Transition(\"S\", \"X\", \"\", \"print_tx\").\n        Transition(\"N\", \"E\", \"is_eol\", \"print_tx\").\n        Transition(\"N\", \"N\", \"is_digit\", \"print_tx\").\n        Transition(\"N\", \"P\", \"is_dot\", \"print_tx\").\n        Transition(\"N\", \"X\", \"\", \"print_tx\").\n        Transition(\"P\", \"F\", \"is_digit\", \"print_tx\").\n        Transition(\"P\", \"X\", \"\", \"print_tx\").\n        Transition(\"F\", \"E\", \"is_eol\", \"print_tx\").\n        Transition(\"F\", \"F\", \"is_digit\", \"print_tx\").\n        Transition(\"F\", \"X\", \"\", \"print_tx\").\n        Compile()\n    if err != nil {\n        panic(err)\n    }\n\n    // test case 1: \"123.456\"\n    res, err := machine.Run(\"S\", \"123.456\")\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(res)\n\n    // test case 2: \"12.34.65\"\n    res, err = machine.Run(\"S\", \"12.34.56\")\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(res)\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd5%2Fgo-fsm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fd5%2Fgo-fsm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd5%2Fgo-fsm/lists"}