state_machine/_docs_/index.html

366 lines
25 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to state_machines documentation! &#8212; state_machine documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-state_machine">
<span id="welcome-to-state-machine-s-documentation"></span><h1>Welcome to state_machines documentation!<a class="headerlink" href="#module-state_machine" title="Permalink to this headline"></a></h1>
<div class="section" id="state-machine-state-machine">
<h2>state_machine (State Machine)<a class="headerlink" href="#state-machine-state-machine" title="Permalink to this headline"></a></h2>
<p><strong>Author:</strong></p>
<ul class="simple">
<li>Dirk Alders &lt;<a class="reference external" href="mailto:sudo-dirk&#37;&#52;&#48;mount-mockery&#46;de">sudo-dirk<span>&#64;</span>mount-mockery<span>&#46;</span>de</a>&gt;</li>
</ul>
<p><strong>Description:</strong></p>
<blockquote>
<div>This Module helps implementing state machines.</div></blockquote>
<p><strong>Submodules:</strong></p>
<ul class="simple">
<li><a class="reference internal" href="#state_machine.state_machine" title="state_machine.state_machine"><code class="xref py py-class docutils literal"><span class="pre">state_machine.state_machine</span></code></a></li>
</ul>
<p><strong>Unittest:</strong></p>
<blockquote>
<div>See also the <a class="reference download internal" href="_downloads/unittest.pdf" download=""><code class="xref download docutils literal"><span class="pre">unittest</span></code></a> documentation.</div></blockquote>
<p><strong>Module Documentation:</strong></p>
<dl class="class">
<dt id="state_machine.state_machine">
<em class="property">class </em><code class="descclassname">state_machine.</code><code class="descname">state_machine</code><span class="sig-paren">(</span><em>default_state</em>, <em>log_lvl</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>default_state</strong> The default state which is set on initialisation.</li>
<li><strong>log_lvl</strong> The log level, this Module logs to (see Loging-Levels of Module <code class="xref py py-mod docutils literal"><span class="pre">logging</span></code>)</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Additional keyword parameters well be stored as varibles of the instance (e.g. to give variables or methods for transition condition calculation).</p>
</div>
<p>A state machine class can be created by deriving it from this class. The transitions are defined by overriding the variable <cite>TRANSITIONS</cite>.
This Variable is a dictionary, where the key is the start-state and the content is a tuple or list of transitions. Each transition is a tuple or list
including the following information: (condition-method (str), transition-time (number), target_state (str)).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The condition-method needs to be implemented as part of the new class.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It is usefull to define the states as variables of this class.</p>
</div>
<p><strong>Example:</strong></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: UTF-8 -*-</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">report</span>
<span class="kn">import</span> <span class="nn">state_machine</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;__example__&#39;</span><span class="p">)</span>
<span class="n">report</span><span class="o">.</span><span class="n">consoleLoggingConfigure</span><span class="p">(</span><span class="n">loggers</span><span class="o">=</span><span class="p">[</span><span class="n">state_machine</span><span class="o">.</span><span class="n">logger_name</span><span class="p">,</span> <span class="s1">&#39;__example__&#39;</span><span class="p">,</span> <span class="p">],</span> <span class="n">level</span><span class="o">=</span><span class="s1">&#39;DEBUG&#39;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">trafic_lights</span><span class="p">(</span><span class="n">state_machine</span><span class="o">.</span><span class="n">state_machine</span><span class="p">):</span>
<span class="n">LOG_PREFIX</span> <span class="o">=</span> <span class="s1">&#39;TraficLights:&#39;</span>
<span class="n">STATE_RED</span> <span class="o">=</span> <span class="s1">&#39;state_red&#39;</span>
<span class="n">STATE_GREEN</span> <span class="o">=</span> <span class="s1">&#39;state_green&#39;</span>
<span class="n">CONDITION_TRUE</span> <span class="o">=</span> <span class="s1">&#39;condition_true&#39;</span>
<span class="n">CONDITION_PEDASTRIAN_REQUEST</span> <span class="o">=</span> <span class="s1">&#39;condition_pedastrian_request&#39;</span>
<span class="n">TRANSITIONS</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">STATE_RED</span><span class="p">:</span> <span class="p">(</span>
<span class="p">(</span><span class="n">CONDITION_PEDASTRIAN_REQUEST</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">STATE_GREEN</span><span class="p">),</span>
<span class="p">),</span>
<span class="n">STATE_GREEN</span><span class="p">:</span> <span class="p">(</span>
<span class="p">(</span><span class="n">CONDITION_TRUE</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">STATE_RED</span><span class="p">),</span>
<span class="p">)</span>
<span class="p">}</span>
<span class="k">def</span> <span class="nf">condition_true</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">set_padestrian_request</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">logger</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__log_lvl__</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> Pedestrian gave state change request.&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">LOG_PREFIX</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pedastrian_request</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">condition_pedastrian_request</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pedastrian_request</span>
<span class="n">sm</span> <span class="o">=</span> <span class="n">trafic_lights</span><span class="p">(</span><span class="n">trafic_lights</span><span class="o">.</span><span class="n">STATE_RED</span><span class="p">,</span> <span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">,</span> <span class="n">pedastrian_request</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">sm</span><span class="o">.</span><span class="n">register_state_change_callback</span><span class="p">(</span><span class="n">sm</span><span class="o">.</span><span class="n">STATE_GREEN</span><span class="p">,</span> <span class="n">sm</span><span class="o">.</span><span class="n">CONDITION_PEDASTRIAN_REQUEST</span><span class="p">,</span> <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">,</span> <span class="s1">&#39;Callback information: Traffic light had been changed to green caused by pedastrian request&#39;</span><span class="p">)</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">sm</span><span class="o">.</span><span class="n">previous_state_was</span><span class="p">(</span><span class="n">sm</span><span class="o">.</span><span class="n">STATE_GREEN</span><span class="p">):</span>
<span class="k">if</span> <span class="n">sm</span><span class="o">.</span><span class="n">this_state_is</span><span class="p">(</span><span class="n">sm</span><span class="o">.</span><span class="n">STATE_RED</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sm</span><span class="o">.</span><span class="n">this_state_duration</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mf">0.2</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">sm</span><span class="o">.</span><span class="n">condition_pedastrian_request</span><span class="p">():</span>
<span class="n">sm</span><span class="o">.</span><span class="n">set_padestrian_request</span><span class="p">()</span>
<span class="n">sm</span><span class="o">.</span><span class="n">work</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="mi">2019</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mi">26</span> <span class="mi">13</span><span class="p">:</span><span class="mi">40</span><span class="p">:</span><span class="mi">44</span><span class="p">,</span><span class="mi">154</span><span class="p">:</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">TraficLights</span><span class="p">:</span> <span class="n">State</span> <span class="n">change</span> <span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">):</span> <span class="kc">None</span> <span class="o">-&gt;</span> <span class="s1">&#39;state_red&#39;</span>
<span class="mi">2019</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mi">26</span> <span class="mi">13</span><span class="p">:</span><span class="mi">40</span><span class="p">:</span><span class="mi">44</span><span class="p">,</span><span class="mi">354</span><span class="p">:</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">TraficLights</span><span class="p">:</span> <span class="n">Pedestrian</span> <span class="n">gave</span> <span class="n">state</span> <span class="n">change</span> <span class="n">request</span><span class="o">.</span>
<span class="mi">2019</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mi">26</span> <span class="mi">13</span><span class="p">:</span><span class="mi">40</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">354</span><span class="p">:</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">TraficLights</span><span class="p">:</span> <span class="n">State</span> <span class="n">change</span> <span class="p">(</span><span class="s1">&#39;condition_pedastrian_request&#39;</span><span class="p">):</span> <span class="s1">&#39;state_red&#39;</span> <span class="o">-&gt;</span> <span class="s1">&#39;state_green&#39;</span>
<span class="mi">2019</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mi">26</span> <span class="mi">13</span><span class="p">:</span><span class="mi">40</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">354</span><span class="p">:</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">Callback</span> <span class="n">information</span><span class="p">:</span> <span class="n">Traffic</span> <span class="n">light</span> <span class="n">had</span> <span class="n">been</span> <span class="n">changed</span> <span class="n">to</span> <span class="n">green</span> <span class="n">caused</span> <span class="n">by</span> <span class="n">pedastrian</span> <span class="n">request</span>
<span class="mi">2019</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mi">26</span> <span class="mi">13</span><span class="p">:</span><span class="mi">40</span><span class="p">:</span><span class="mi">48</span><span class="p">,</span><span class="mi">355</span><span class="p">:</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">TraficLights</span><span class="p">:</span> <span class="n">State</span> <span class="n">change</span> <span class="p">(</span><span class="s1">&#39;condition_true&#39;</span><span class="p">):</span> <span class="s1">&#39;state_green&#39;</span> <span class="o">-&gt;</span> <span class="s1">&#39;state_red&#39;</span>
</pre></div>
</div>
<dl class="method">
<dt id="state_machine.state_machine.last_transition_condition">
<code class="descname">last_transition_condition</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.last_transition_condition" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The last transition condition.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">str</td>
</tr>
</tbody>
</table>
<p>This method returns the last transition condition.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.last_transition_condition_was">
<code class="descname">last_transition_condition_was</code><span class="sig-paren">(</span><em>condition</em><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.last_transition_condition_was" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>condition</strong> (<em>str</em>) The condition to be checked</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">True if the given condition was the last transition condition, else False.</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">bool</td>
</tr>
</tbody>
</table>
<p>This methods returns the boolean information if the last transition condition is equivalent to the given condition.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.previous_state">
<code class="descname">previous_state</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.previous_state" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The previous state.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">str</td>
</tr>
</tbody>
</table>
<p>This method returns the previous state of the state machine.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.previous_state_duration">
<code class="descname">previous_state_duration</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.previous_state_duration" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The time how long the previous state was active.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">float</td>
</tr>
</tbody>
</table>
<p>This method returns the time how long the previous state was active.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.previous_state_was">
<code class="descname">previous_state_was</code><span class="sig-paren">(</span><em>state</em><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.previous_state_was" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>state</strong> (<em>str</em>) The state to be checked</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">True if the given state was previously active, else False.</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">bool</td>
</tr>
</tbody>
</table>
<p>This methods returns the boolean information if the state machine was previously in the given state.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.register_state_change_callback">
<code class="descname">register_state_change_callback</code><span class="sig-paren">(</span><em>state</em>, <em>condition</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.register_state_change_callback" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>state</strong> (<em>str</em>) The target state. The callback will be executed, if the state machine changes to this state. None means all states.</li>
<li><strong>condition</strong> (<em>str</em>) The transition condition. The callback will be executed, if this condition is responsible for the state change. None means all conditions.</li>
<li><strong>callback</strong> The callback to be executed.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Additional arguments and keyword parameters are supported. These arguments and parameters will be used as arguments and parameters for the callback execution.</p>
</div>
<p>This methods allows to register callbacks which will be executed on state changes.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.this_state">
<code class="descname">this_state</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.this_state" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The current state.</td>
</tr>
</tbody>
</table>
<p>This method returns the current state of the state machine.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.this_state_duration">
<code class="descname">this_state_duration</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.this_state_duration" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The time how long the current state is active.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">float</td>
</tr>
</tbody>
</table>
<p>This method returns the time how long the current state is active.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.this_state_is">
<code class="descname">this_state_is</code><span class="sig-paren">(</span><em>state</em><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.this_state_is" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>state</strong> (<em>str</em>) The state to be checked</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">True if the given state is currently active, else False.</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">bool</td>
</tr>
</tbody>
</table>
<p>This methods returns the boolean information if the state machine is currently in the given state.</p>
</dd></dl>
<dl class="method">
<dt id="state_machine.state_machine.work">
<code class="descname">work</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#state_machine.state_machine.work" title="Permalink to this definition"></a></dt>
<dd><p>This Method needs to be executed cyclicly to enable the state machine.</p>
</dd></dl>
</dd></dl>
</div>
<div class="toctree-wrapper compound">
</div>
</div>
<div class="section" id="indices-and-tables">
<h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline"></a></h1>
<ul class="simple">
<li><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></li>
<li><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></li>
<li><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="#">Documentation overview</a><ul>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2019, Dirk Alders.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.7</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/index.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>