diff --git a/source/finite-state-machines/sec-extended-example.ptx b/source/finite-state-machines/sec-extended-example.ptx index a746e6c..8ff0b4a 100644 --- a/source/finite-state-machines/sec-extended-example.ptx +++ b/source/finite-state-machines/sec-extended-example.ptx @@ -1,10 +1,6 @@
Extended Example - - Controlling Traffic Lights and Pedestrian Crossing Signals

@@ -112,15 +108,10 @@ Solution - - Blueprints: States, Inputs and Outputs

- - + # BLUEPRINTS from dataclasses import dataclass @@ -229,8 +220,7 @@ State Machine Engine

- - + class StateMachine: """Generic StateMachine Engine to display and run a state-machine.""" @@ -297,7 +287,6 @@ Model Definition and Instantiation

- # DEFINE STATE-MACHINE: states, inputs/outputs, and transitions @@ -431,7 +420,6 @@ Visualize the Finite State Machine Model

- from sage.graphs.digraph import DiGraph @@ -496,7 +484,6 @@ Running the State Machine (Simulation)

- # Run the SM for a test scenario diff --git a/source/finite-state-machines/sec-modeling-finite-state-machines.ptx b/source/finite-state-machines/sec-modeling-finite-state-machines.ptx index 4460b40..45f7743 100644 --- a/source/finite-state-machines/sec-modeling-finite-state-machines.ptx +++ b/source/finite-state-machines/sec-modeling-finite-state-machines.ptx @@ -24,7 +24,7 @@ visualize the state machine, and simulate its execution in SageMath.

- 1. Define States and Transitions + Define States, Transitions and Outputs

The first step is to define the states and transitions in the state machine. We can represent these using lists and dictionaries.

@@ -54,18 +54,33 @@ 'A0': 'S0' } } + + # The machine output controls a locking mechanism that can be either LOCKED, or UNLOCKED + outputs = { + 'S0': 'LOCKED', + 'S1': 'LOCKED', + 'S2': 'LOCKED', + 'S3': 'UNLOCKED', + 'S4': 'UNLOCKED' + } + + # Display the machine configuration + import json + print('States: ', states, '\n') + print('Transitions: ', json.dumps(transitions, indent=2), '\n') + print('Outputs: ', json.dumps(outputs, indent=2), '\n')

- The states are defined as vertices in the graph, and transitions are defined as directed + The states are defined as vertices in the graph, the transitions (along with the outputs) are defined as directed edges between these vertices.

- 2. Create a State Transition Graph -

In SageMath, we can use the DiGraph class to represent the states and transitions - of the state machine as a directed graph, and use the graph structure to visualize the - state machine representation.

+ Create Graph Model of State Machine +

In SageMath, we can use the DiGraph class to represent the states, transitions + and outputs of the state machine as a directed graph, and use the graph structure to + visualize the state machine representation.

# Import DiGraph from SageMath @@ -77,16 +92,26 @@ # Add states as vertices SM.add_vertices(states) - # Add transitions as edges + # Add transitions and outputs as edges for state, transition in transitions.items(): + if outputs[state] == 'LOCKED': + output = 'L' + elif outputs[state] == 'UNLOCKED': + output = 'U' + else: + output = '?' + for action, next_state in transition.items(): - SM.add_edge(state, next_state, label=action) + edge_label = f"{action},{output}" + SM.add_edge(state, next_state, label=edge_label) + + SM.__repr__()
- 3. Display the State Machine + Display the State Machine

The SM.show() command renders a graphical representation of the state machine. Each vertex in the graph represents a state, and each directed edge represents a @@ -100,7 +125,7 @@ - 4. Run the State Machine + Run the State Machine

Next, we can simulate the state machine's behavior by creating a function that processes a list of inputs and transitions through the states accordingly.

diff --git a/source/frontmatter.ptx b/source/frontmatter.ptx index 5866a9c..303eb74 100644 --- a/source/frontmatter.ptx +++ b/source/frontmatter.ptx @@ -171,7 +171,7 @@ Allaoua Boughrira (Contrib.) - Math Department* + Mathematics Wright College a.boughrira@gmail.com