Skip to content

Feature/documentation #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 36 additions & 42 deletions examples/complete_statemachine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"""
![alt text](/assets/complete_statemachine.svg "Title")
"""
import stateforward as sf

e1 = sf.event("e1")
Expand All @@ -13,7 +10,7 @@ class e2(sf.Event):
e3 = sf.event("e3")


class SampleMachine(sf.AsyncStateMachine):
class Submachine(sf.AsyncStateMachine):
class s2(sf.State):
class r1(sf.Region):
class s1(sf.State):
Expand All @@ -34,55 +31,52 @@ class s2(sf.State):
initial = sf.initial(s2)

initial = sf.initial(s2)
#
# entry_point_to_r2_s2 = sf.entry_point(sf.transition(target=s2.r2.s2))
# entry_point_to_r1_s2_and_r2_s1 = sf.entry_point(
# sf.transition(target=s2.r1.s2), sf.transition(target=s2.r2.s1)
# )
# exit_point_from_r2_s2 = sf.exit_point()
# r2_s2_to_exit_point = sf.transition(source=s2.r2.s2, target=exit_point_from_r2_s2)
entry_point_to_r2_s2 = sf.entry_point(sf.transition(target=s2.r2.s2))
entry_point_to_r1_s2_and_r2_s1 = sf.entry_point(
sf.transition(target=s2.r1.s2), sf.transition(target=s2.r2.s1)
)
exit_point_from_r2_s2 = sf.exit_point()
r2_s2_to_exit_point = sf.transition(source=s2.r2.s2, target=exit_point_from_r2_s2)


class CompleteSM(sf.AsyncStateMachine):
a1: int = 0

s0 = sf.simple_state("s0")
#
# class s1(sf.State):
# class r1(sf.Region):
# class s1(sf.State):
# pass
#
# class s2(sf.State):
# pass
#
# r1_s1_to_r1_s2 = sf.transition(e1, source=r1.s1, target=r1.s2)
# r1_s2_to_r1_s1 = sf.transition(sf.after(seconds=1), source=r1.s2, target=r1.s1)
#
# class r2(sf.Region):
# class s1(sf.State):
# pass
#
# class s2(sf.State):
# pass
#
# r2_s2_to_r2_s1 = sf.transition(e2, source=r2.s2, target=r2.s1)
# r2_s1_to_r2_s2 = sf.transition(
# sf.when(lambda self: self.model.a1), source=r2.s1, target=r2.s2
# )

s2 = sf.submachine_state(SampleMachine)

class s1(sf.State):
class r1(sf.Region):
class s1(sf.State):
pass

class s2(sf.State):
pass

r1_s1_to_r1_s2 = sf.transition(e1, source=r1.s1, target=r1.s2)
r1_s2_to_r1_s1 = sf.transition(sf.after(seconds=1), source=r1.s2, target=r1.s1)

class r2(sf.Region):
class s1(sf.State):
pass

class s2(sf.State):
pass

r2_s2_to_r2_s1 = sf.transition(e2, source=r2.s2, target=r2.s1)
r2_s1_to_r2_s2 = sf.transition(
sf.when(lambda self: self.model.a1), source=r2.s1, target=r2.s2
)

s2 = sf.submachine_state(Submachine)

initial = sf.initial(s0)
# s0_fork = sf.fork(sf.transition(target=s1.r1.s1), sf.transition(target=s1.r2.s2))
s0_fork = sf.fork(sf.transition(target=s1.r1.s1), sf.transition(target=s1.r2.s2))
# s1_join = sf.join(s2)
#

# s1_r1_s1_transition_to_s1_join = sf.transition(source=s1.r1.s1, target=s1_join)
# s1_r2_s2_transition_to_s1_join = sf.transition(source=s1.r2.s1, target=s1_join)
#
s1_transtion_to_s2_s1 = sf.transition(e3, source=s0, target=s2.s2)
#
# fork_transition = sf.transition(e2, source=s0, target=s0_fork)
# s1_transtion_to_s2_s1 = sf.transition(e3, source=s0, target=s2.s2)
fork_transition = sf.transition(e2, source=s0, target=s0_fork)


if __name__ == "__main__":
Expand Down
5 changes: 0 additions & 5 deletions examples/initial_state_example.py

This file was deleted.

39 changes: 39 additions & 0 deletions examples/inspector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import aiohttp.web
import socketio

# Create a Socket.IO server
sio = socketio.AsyncServer(async_mode="aiohttp", cors_allowed_origins="*")
app = aiohttp.web.Application()
sio.attach(app)


# Event handler for new connections
@sio.event
async def connect(sid, environ):
print("connect ", sid)


# Event handler for messages
@sio.event
async def message(sid, data):
print("message ", data)
await sio.emit("reply", room=sid, data="Received your message!")


# Event handler for disconnections
@sio.event
async def disconnect(sid):
print("disconnect ", sid)


# Add a simple HTTP route for demonstration
async def index(request):
return aiohttp.web.Response(
text="Hello, this is an aiohttp server!", content_type="text/html"
)


app.router.add_get("/", index)

if __name__ == "__main__":
aiohttp.web.run_app(app, host="127.0.0.1", port=5000)
21 changes: 8 additions & 13 deletions examples/light_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,9 @@ class Broken(sf.State):
class Flashing(sf.State):
pass

async def throw_behavior(self, *args, **kwargs):
raise Exception("Behavior exception")

initial = sf.initial(Off)
transitions = sf.collection(
sf.transition(OnEvent, source=Off, target=On, effect=throw_behavior),
sf.transition(OnEvent, source=Off, target=On),
sf.transition(OffEvent, source=On, target=Off),
sf.transition(FlashEvent, source=Off, target=Flashing),
)
Expand All @@ -192,8 +189,6 @@ class ThreeWay(LightSwitch):
if __name__ == "__main__":

async def light_switch_main():
import pickle

# instantiate a light switch
light_switch = LightSwitch()
# start the interpreter and wait for it to be settled
Expand All @@ -203,12 +198,12 @@ async def light_switch_main():
# dispatch a OnEvent to the state machine
task = await sf.send(OnEvent(), light_switch)
print("->", task)
# print(light_switch.state)
# await sf.dispatch(OffEvent(), light_switch)
# print(light_switch.state)
# light_switch.flashing = True
# await asyncio.sleep(2)
# print(light_switch.state)
# print(light_switch, light_switch.flashing)
print(light_switch.state)
await sf.send(OffEvent(), light_switch)
print(light_switch.state)
light_switch.flashing = True
await asyncio.sleep(2)
print(light_switch.state)
print(light_switch, light_switch.flashing)

asyncio.run(light_switch_main())
14 changes: 14 additions & 0 deletions examples/microwave.puml
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
@startuml
skinparam linetype ortho
skinparam arrowColor white
skinparam backgroundColor #000000
skinparam ActivityBarColor white
<style>
circle {
backgroundColor white
}
</style>
skinparam State {
backgroundColor black
FontColor white
borderColor white
}
state "Microwave" as Microwave {
[*] --> Microwave_door
state "door" as Microwave_door {
Expand Down
Loading