Skip to content

Commit

Permalink
Merge pull request #128 from SageMathOER-CCC/fsm-minor-clean-up
Browse files Browse the repository at this point in the history
FSM Clean up and fix the meeting remarks & feedback
  • Loading branch information
Samuel-Lubliner authored Sep 15, 2024
2 parents 5ad5c0d + ac1a990 commit 58436da
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
17 changes: 2 additions & 15 deletions source/finite-state-machines/sec-extended-example.ptx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<section xml:id="sec-extended-example" xmlns:xi="http://www.w3.org/2001/XInclude" xml:lang="en">
<title>Extended Example</title>
<!-- TODO. clean up commented code that is no longer needed -->
<!-- <idx>
<h>state machines</h>
</idx> -->
<introduction>
<title>Controlling Traffic Lights and Pedestrian Crossing Signals</title>
<p>
Expand Down Expand Up @@ -112,15 +108,10 @@
</subsection>
<subsection>
<title>Solution</title>
<!-- TODO -->
<!-- <p>
Attempt to construct a library-like solution that could be generalized/fine-tuned ...
</p> -->
<subsection>
<title>Blueprints: States, Inputs and Outputs</title>
<p>
<!-- .. TODO .. -->
<sage>
<sage auto-evaluate="yes">
<input>
# BLUEPRINTS
from dataclasses import dataclass
Expand Down Expand Up @@ -229,8 +220,7 @@
<subsection>
<title>State Machine Engine</title>
<p>
<!-- .. The runner .. -->
<sage>
<sage auto-evaluate="yes">
<input>
class StateMachine:
"""Generic StateMachine Engine to display and run a state-machine."""
Expand Down Expand Up @@ -297,7 +287,6 @@
<subsection>
<title>Model Definition and Instantiation</title>
<p>
<!-- .. TODO .. -->
<sage>
<input>
# DEFINE STATE-MACHINE: states, inputs/outputs, and transitions
Expand Down Expand Up @@ -431,7 +420,6 @@
<subsection>
<title>Visualize the Finite State Machine Model</title>
<p>
<!-- .. TODO .. -->
<sage>
<input>
from sage.graphs.digraph import DiGraph
Expand Down Expand Up @@ -496,7 +484,6 @@
<subsection>
<title>Running the State Machine (Simulation)</title>
<p>
<!-- .. TODO .. -->
<sage>
<input>
# Run the SM for a test scenario
Expand Down
45 changes: 35 additions & 10 deletions source/finite-state-machines/sec-modeling-finite-state-machines.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
visualize the state machine, and simulate its execution in SageMath.</p>
</introduction>
<subsection>
<title>1. Define States and Transitions</title>
<title>Define States, Transitions and Outputs</title>
<p> The first step is to define the states and transitions in the state machine. We can
represent these using lists and dictionaries.
</p>
Expand Down Expand Up @@ -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')
</input>
<output></output>
</sage>
<p>
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.</p>
</subsection>
<subsection>
<title>2. Create a State Transition Graph</title>
<p> In SageMath, we can use the <c>DiGraph</c> 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.</p>
<title>Create Graph Model of State Machine</title>
<p> In SageMath, we can use the <c>DiGraph</c> 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.</p>
<sage>
<input>
# Import DiGraph from SageMath
Expand All @@ -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__()
</input>
<output></output>
</sage>
</subsection>
<subsection>
<title>3. Display the State Machine</title>
<title>Display the State Machine</title>
<p>
The <c>SM.show()</c> command renders a graphical representation of the state machine.
Each vertex in the graph represents a state, and each directed edge represents a
Expand All @@ -100,7 +125,7 @@
</sage>
</subsection>
<subsection>
<title>4. Run the State Machine</title>
<title>Run the State Machine</title>
<p>
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.</p>
Expand Down
2 changes: 1 addition & 1 deletion source/frontmatter.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

<contributor xml:id="ab">
<personname>Allaoua Boughrira (Contrib.)</personname>
<department>Math Department*</department>
<department>Mathematics</department>
<institution>Wright College</institution>
<email>[email protected]</email>
</contributor>
Expand Down

0 comments on commit 58436da

Please sign in to comment.