{"id":26749571,"url":"https://github.com/digitalpetri/strict-machine","last_synced_at":"2025-04-14T23:27:20.143Z","repository":{"id":57724668,"uuid":"159901587","full_name":"digitalpetri/strict-machine","owner":"digitalpetri","description":"A declarative DSL for building asynchronously evaluated Finite State Machines on the JVM","archived":false,"fork":false,"pushed_at":"2024-11-08T23:50:10.000Z","size":128,"stargazers_count":30,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T11:35:03.516Z","etag":null,"topics":["finite-state-machine","fsm","java","state-machine"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/digitalpetri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-01T02:34:01.000Z","updated_at":"2025-03-10T09:12:02.000Z","dependencies_parsed_at":"2025-03-28T11:42:29.412Z","dependency_job_id":null,"html_url":"https://github.com/digitalpetri/strict-machine","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalpetri%2Fstrict-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalpetri%2Fstrict-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalpetri%2Fstrict-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalpetri%2Fstrict-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/digitalpetri","download_url":"https://codeload.github.com/digitalpetri/strict-machine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248977018,"owners_count":21192513,"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","fsm","java","state-machine"],"created_at":"2025-03-28T11:28:49.815Z","updated_at":"2025-04-14T23:27:20.123Z","avatar_url":"https://github.com/digitalpetri.png","language":"Java","funding_links":[],"categories":["\u003ca name=\"Java\"\u003e\u003c/a\u003eJava"],"sub_categories":[],"readme":"# Strict Machine\n![Maven Central](https://img.shields.io/maven-central/v/com.digitalpetri.fsm/strict-machine)\n\nStrict Machine is a declarative DSL for building asynchronously evaluated Finite State Machines.\n\nRelease builds are available from [Maven Central](https://repo.maven.apache.org/maven2/) and SNAPSHOT builds are available from the [Sonatype OSS Snapshot Repository](https://oss.sonatype.org/content/repositories/snapshots/).\n\n## Gradle\n```\ndependencies {\n    compile(\"com.digitalpetri.fsm:strict-machine:1.0.0\")\n}\n```\n\n## Maven\n```\n\u003cdependencies\u003e\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.digitalpetri.fsm\u003c/groupId\u003e\n      \u003cartifactId\u003estrict-machine\u003c/artifactId\u003e\n      \u003cversion\u003e1.0.0\u003c/version\u003e\n    \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\n\n# What does it look like?\n\nGiven this simple ATM example:\n\n![atmfsm.png](https://github.com/digitalpetri/strict-machine/blob/master/src/test/java/com/digitalpetri/fsm/dsl/atm/atmfsm.png)\n\n\nDefine the State and Event types:\n```java\nenum State {\n    Idle,\n    Loading,\n    OutOfService,\n    InService,\n    Disconnected\n}\n\nenum Event {\n    Connected,\n    ConnectionClosed,\n    ConnectionLost,\n    ConnectionRestored,\n    LoadFail,\n    LoadSuccess,\n    Shutdown,\n    Startup\n}\n```\n\nDefine the transitions:\n```java\nFsmBuilder\u003cState, Event\u003e fb = new FsmBuilder\u003c\u003e();\n\n/* Idle */\nfb.when(State.Idle)\n  .on(Event.Connected)\n  .transitionTo(State.Loading);\n\n/* Loading */\nfb.when(State.Loading)\n  .on(Event.LoadSuccess)\n  .transitionTo(State.InService);\n\nfb.when(State.Loading)\n  .on(Event.LoadFail)\n  .transitionTo(State.OutOfService);\n\nfb.when(State.Loading)\n  .on(Event.ConnectionClosed)\n  .transitionTo(State.Disconnected);\n\n/* OutOfService */\nfb.when(State.OutOfService)\n  .on(Event.Startup)\n  .transitionTo(State.InService);\n\nfb.when(State.OutOfService)\n  .on(Event.ConnectionLost)\n  .transitionTo(State.Disconnected);\n\n/* InService */\nfb.when(State.InService)\n  .on(Event.ConnectionLost)\n  .transitionTo(State.Disconnected);\n\nfb.when(State.InService)\n  .on(Event.Shutdown)\n  .transitionTo(State.OutOfService);\n\n/* Disconnected */\nfb.when(State.Disconnected)\n  .on(Event.ConnectionRestored)\n  .transitionTo(State.InService);\n\nFsm\u003cState, Event\u003e fsm = fb.build(State.Idle);\n```\n\nFire events to be evaluated asynchronously:\n```java\nfsm.fireEvent(Event.Connected);\n```\nor block waiting for the state that resulted from evaluating the event:\n```java\nState state = fsm.fireEventBlocking(Event.Connected); \n```\n\n\n# Transition Guards\nGuard conditions that prevent a transition from occurring unless they're met can be defined using `guardedBy`:\n\n```java\nfb.when(State.Idle)\n    .on(Event.Connected)\n    .transitionTo(State.Loading)\n    .guardedBy(ctx -\u003e ...guard condition here...);\n```\n\nWhere the guard provided to `guardedBy` is a `Predicate` that receives the `FsmContext`.\n\n\n# Transition Actions\n\nOf course, in order for your state machine to actually *do* something, you need to define actions that execute when events are evaluated and transitions occur.\n\nThis is accomplished using the `onTransitionTo`, `onTransitionFrom`, and `onInternalTransition` methods on `FsmBuilder`. \n\nFor example, logging a message on the transition from `Idle` to `Loading` might be defined in either of two ways:\n```java\n// define the action in the context of a transition away from Idle\nfb.onTransitionFrom(State.Idle)\n    .to(State.Loading)\n    .via(Event.Connected)\n    .execute(ctx -\u003e System.out.println(\"S(Idle) x E(Connected) = S'(Loading)\"));\n        \n// define the action in the context of a transition to Loading\nfb.onTransitionTo(State.Loading)\n    .from(State.Idle)\n    .via(Event.Connected)\n    .execute(ctx -\u003e System.out.println(\"S(Idle) x E(Connected) = S'(Loading)\"));\n```\n\n\n# More Examples\n\nSee the full [AtmFsm](https://github.com/kevinherron/strict-machine/blob/master/src/test/java/com/digitalpetri/strictmachine/dsl/atm/AtmFsm.java) defined in the test suite.\n\nAdvanced examples from production code:\n- [ChannelFsmFactory](https://github.com/digitalpetri/netty-channel-fsm/blob/master/src/main/java/com/digitalpetri/netty/fsm/ChannelFsmFactory.java) from [netty-channel-fsm](https://github.com/digitalpetri/netty-channel-fsm).\n- [SessionFsmFactory](https://github.com/eclipse/milo/blob/master/opc-ua-sdk/sdk-client/src/main/java/org/eclipse/milo/opcua/sdk/client/session/SessionFsmFactory.java) from [Eclipse Milo](https://github.com/eclipse/milo).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalpetri%2Fstrict-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigitalpetri%2Fstrict-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalpetri%2Fstrict-machine/lists"}