{"id":18586893,"url":"https://github.com/brice-morin/luasm","last_synced_at":"2025-11-01T23:30:28.760Z","repository":{"id":150433909,"uuid":"46118296","full_name":"brice-morin/LuaSM","owner":"brice-morin","description":"UML-like State Machine (including composites, concurrent regions, etc) for Lua","archived":false,"fork":false,"pushed_at":"2016-05-23T11:32:43.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-26T18:25:43.250Z","etag":null,"topics":["lua","state-machine"],"latest_commit_sha":null,"homepage":"","language":"Lua","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/brice-morin.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-13T11:31:36.000Z","updated_at":"2016-05-20T13:29:01.000Z","dependencies_parsed_at":"2023-07-14T21:15:10.343Z","dependency_job_id":null,"html_url":"https://github.com/brice-morin/LuaSM","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/brice-morin%2FLuaSM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brice-morin%2FLuaSM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brice-morin%2FLuaSM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brice-morin%2FLuaSM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brice-morin","download_url":"https://codeload.github.com/brice-morin/LuaSM/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239340504,"owners_count":19622704,"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":["lua","state-machine"],"created_at":"2024-11-07T00:38:43.388Z","updated_at":"2025-11-01T23:30:28.717Z","avatar_url":"https://github.com/brice-morin.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LuaSM\nUML-like State Machine (including composites, concurrent regions, etc) for Lua\n\n## Hello World\n\n```lua\nrequire \"luasm\"\n------Test------\n-- Create a State A\nlocal A = luasm.AtomicState:new{name = \"A\"}\nfunction A:executeOnEntry() --on entry/exit actions are defined like this\n  print(\"hello\")\nend\nfunction A:executeOnExit()\n\tprint(\"bye\")\nend\n\n-- Create a State B\nlocal B = luasm.AtomicState:new{name = \"B\"} -- on entry/exit are optional\n\n-- Create a Transition between A and B\nlocal T = luasm.Transition:new{name = \"T\", source = A, target = B, eventType = luasm.NullEvent}:init()\nfunction T:execute(event) --actions on transitions are defined like this\n\t\tprint(\"executing transition\") \nend\n\nlocal R = luasm.Region:new{name = \"R\", initial = A, states = {A, B}}\nlocal CS = luasm.CompositeState:new{name = \"CS\", regions = {R}} --root state machine\n\nlocal Comp = luasm.Component:new{name = \"Cpt\", behavior = CS}:init():start()\n```\n\n## Define some properties in the component\n\nLet's say you want your component to define a property `count`:\n\n```lua\nlocal Comp = luasm.Component:new{name = \"Cpt\", behavior = CS, count = 0}:init():start()\n```\n\nYou can then access this property from a state:\n```lua\nfunction A:executeOnEntry()\n  local component = self.component\n  print(\"hello \" .. component.count)\n  component.count = component.count + 1\nend\n```\n\nOr from a transition:\n```lua\nfunction T:execute(event)\n    local component = self.source.component\n\t\tprint(\"executing transition \" .. component.count) \nend\n```\n\n## Communication among components\n\nState machines are wrapped into lightweight components. Component can communicate through message passing. \n\nFirst, define a message (or event) type:\n\n```lua\nlocal E1 = luasm.Event:new{name = \"t\"} --Message type (basically just a name)\n```\n\nTo create/instantiate a message:\n```lua\nlocal e1 = E1:create({p1 = \"zzz\", p2 = true, p3 = 42}) -- parameters can be passed in table (with arbitrary names)\n```\n\nTo programmatically send a message to a component (e.g. in your \"main\", for testing purpose):\n```lua\nComp:receive(\"p\", e1)\n```\n\nFor a component to send a message to another one, we should first add a connector/callback:\n```lua\n---Comp2 will be notified whenever Comp emits/sends a message on port p\nComp.connectors = {\n\tp = { \n\t\tComp2 = function(event) if not Comp2.terminated then Comp2:receive(\"p\", event) else Comp.connectors.p.Comp2 = nil end end\n\t}\n}\n---note: it is a good idea to check that Comp2 is not \"terminated\" before sending something to it\n```\n\nNow in the implementation of Comp (in a state or transition):\n\n```lua\n...\ncomponent:send(\"p\", e1) --sends message e1 (of type E1, see above) on port p\n...\n```\n\nNow in the implementation of Comp2, we can define a transition that will react to that event\n```lua\nlocal T = luasm.Transition:new{name = \"T\", source = B, target = A, eventType = E1, port = \"p\"}:init()\nfunction T:execute(event) \n\tprint \"received!\" \nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrice-morin%2Fluasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrice-morin%2Fluasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrice-morin%2Fluasm/lists"}