<spanid="welcome-to-state-machine-s-documentation"></span><h1>Welcome to state_machine’s documentation!<aclass="headerlink"href="#module-state_machine"title="Permalink to this headline">¶</a></h1>
<emclass="property">class </em><codeclass="descclassname">state_machine.</code><codeclass="descname">state_machine</code><spanclass="sig-paren">(</span><em>default_state</em>, <em>log_lvl</em>, <em>**kwargs</em><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine"title="Permalink to this definition">¶</a></dt>
<li><strong>log_lvl</strong>– The log level, this Module logs to (see Loging-Levels of Module <codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">logging</span></code>)</li>
<pclass="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>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">The condition-method needs to be implemented as part of the new class.</p>
</div>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">It is usefull to define the states as variables of this class.</p>
<spanclass="n">logger</span><spanclass="o">.</span><spanclass="n">log</span><spanclass="p">(</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">__log_lvl__</span><spanclass="p">,</span><spanclass="s1">'</span><spanclass="si">%s</span><spanclass="s1"> Pedestrian gave state change request.'</span><spanclass="p">,</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">LOG_PREFIX</span><spanclass="p">)</span>
<spanclass="n">sm</span><spanclass="o">.</span><spanclass="n">register_state_change_callback</span><spanclass="p">(</span><spanclass="n">sm</span><spanclass="o">.</span><spanclass="n">STATE_GREEN</span><spanclass="p">,</span><spanclass="n">sm</span><spanclass="o">.</span><spanclass="n">CONDITION_PEDASTRIAN_REQUEST</span><spanclass="p">,</span><spanclass="n">logger</span><spanclass="o">.</span><spanclass="n">info</span><spanclass="p">,</span><spanclass="s1">'Callback information: Traffic light had been changed to green caused by pedastrian request'</span><spanclass="p">)</span>
<codeclass="descname">last_transition_condition</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.last_transition_condition"title="Permalink to this definition">¶</a></dt>
<codeclass="descname">last_transition_condition_was</code><spanclass="sig-paren">(</span><em>condition</em><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.last_transition_condition_was"title="Permalink to this definition">¶</a></dt>
<trclass="field-odd field"><thclass="field-name">Parameters:</th><tdclass="field-body"><strong>condition</strong> (<em>str</em>) – The condition to be checked</td>
</tr>
<trclass="field-even field"><thclass="field-name">Returns:</th><tdclass="field-body">True if the given condition was the last transition condition, else False.</td>
<codeclass="descname">previous_state</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.previous_state"title="Permalink to this definition">¶</a></dt>
<codeclass="descname">previous_state_duration</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.previous_state_duration"title="Permalink to this definition">¶</a></dt>
<codeclass="descname">previous_state_was</code><spanclass="sig-paren">(</span><em>state</em><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.previous_state_was"title="Permalink to this definition">¶</a></dt>
<trclass="field-odd field"><thclass="field-name">Parameters:</th><tdclass="field-body"><strong>state</strong> (<em>str</em>) – The state to be checked</td>
</tr>
<trclass="field-even field"><thclass="field-name">Returns:</th><tdclass="field-body">True if the given state was previously active, else False.</td>
<codeclass="descname">register_state_change_callback</code><spanclass="sig-paren">(</span><em>state</em>, <em>condition</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.register_state_change_callback"title="Permalink to this definition">¶</a></dt>
<trclass="field-odd field"><thclass="field-name">Parameters:</th><tdclass="field-body"><ulclass="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>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="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>
<dlclass="method">
<dtid="state_machine.state_machine.this_state">
<codeclass="descname">this_state</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.this_state"title="Permalink to this definition">¶</a></dt>
<codeclass="descname">this_state_duration</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.this_state_duration"title="Permalink to this definition">¶</a></dt>
<codeclass="descname">this_state_is</code><spanclass="sig-paren">(</span><em>state</em><spanclass="sig-paren">)</span><aclass="headerlink"href="#state_machine.state_machine.this_state_is"title="Permalink to this definition">¶</a></dt>
<trclass="field-odd field"><thclass="field-name">Parameters:</th><tdclass="field-body"><strong>state</strong> (<em>str</em>) – The state to be checked</td>
</tr>
<trclass="field-even field"><thclass="field-name">Returns:</th><tdclass="field-body">True if the given state is currently active, else False.</td>
<p>This methods returns the boolean information if the state machine is currently in the given state.</p>
</dd></dl>
<dlclass="method">
<dtid="state_machine.state_machine.work">
<codeclass="descname">work</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="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>
<divclass="toctree-wrapper compound">
</div>
</div>
<divclass="section"id="indices-and-tables">
<h1>Indices and tables<aclass="headerlink"href="#indices-and-tables"title="Permalink to this headline">¶</a></h1>