The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings EMULSION public releases
Summary Activity Surveys SCM Listes Sympa

SCM Repository

2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
6   <head>
7     <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
8     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9     <title>emulsion.model.state_machines &#8212; EMULSION (Epidemiological Multi-Level Simulation framework)</title>
10     <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
11     <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
12     <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
13     <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
14     <script type="text/javascript" src="../../../_static/jquery.js"></script>
15     <script type="text/javascript" src="../../../_static/underscore.js"></script>
16     <script type="text/javascript" src="../../../_static/doctools.js"></script>
17     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
18     <link rel="shortcut icon" href="../../../_static/STEMAH-favicon.png"/>
19     <link rel="index" title="Index" href="../../../genindex.html" />
20     <link rel="search" title="Search" href="../../../search.html" />
21    
22   <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
23   
24   
25   <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
27   </head><body>
28   
30     <div class="document">
31       <div class="documentwrapper">
32         <div class="bodywrapper">
33           <div class="body" role="main">
34             
35   <h1>Source code for emulsion.model.state_machines</h1><div class="highlight"><pre>
36 <span></span><span class="sd">&quot;&quot;&quot;</span>
37 <span class="sd">.. module:: emulsion.model.state_machines</span>
39 <span class="sd">.. moduleauthor:: Sébastien Picault &lt;sebastien.picault@inra.fr&gt;</span>
41 <span class="sd">&quot;&quot;&quot;</span>
43 <span class="c1"># [HEADER]</span>
46 <span class="kn">from</span>   <span class="nn">functools</span>               <span class="k">import</span> <span class="n">partial</span>
48 <span class="kn">import</span> <span class="nn">numpy</span>                   <span class="k">as</span>     <span class="nn">np</span>
49 <span class="kn">from</span>   <span class="nn">sympy</span>                   <span class="k">import</span> <span class="n">sympify</span>
51 <span class="kn">from</span>   <span class="nn">sortedcontainers</span>        <span class="k">import</span> <span class="n">SortedSet</span>
53 <span class="kn">import</span> <span class="nn">emulsion.tools.graph</span>    <span class="k">as</span>     <span class="nn">enx</span>
54 <span class="kn">from</span>   <span class="nn">emulsion.agent.action</span>   <span class="k">import</span> <span class="n">AbstractAction</span>
55 <span class="kn">from</span>   <span class="nn">emulsion.tools.state</span>    <span class="k">import</span> <span class="n">StateVarDict</span><span class="p">,</span> <span class="n">EmulsionEnum</span>
57 <span class="kn">from</span>   <span class="nn">emulsion.model.functions</span> <span class="k">import</span> <span class="n">ACTION_SYMBOL</span><span class="p">,</span> <span class="n">WHEN_SYMBOL</span><span class="p">,</span> <span class="n">ESCAPE_SYMBOL</span><span class="p">,</span>\
58     <span class="n">COND_SYMBOL</span><span class="p">,</span> <span class="n">CROSS_SYMBOL</span><span class="p">,</span> <span class="n">EDGE_KEYWORDS</span><span class="p">,</span> <span class="n">CLOCK_SYMBOL</span><span class="p">,</span>\
59     <span class="n">make_when_condition</span><span class="p">,</span> <span class="n">make_duration_condition</span><span class="p">,</span> <span class="n">make_duration_init_action</span>
61 <span class="kn">from</span>   <span class="nn">emulsion.model.exceptions</span>     <span class="k">import</span> <span class="n">SemanticException</span>
64 <span class="c1">#   _____ _        _       __  __            _     _</span>
65 <span class="c1">#  / ____| |      | |     |  \/  |          | |   (_)</span>
66 <span class="c1"># | (___ | |_ __ _| |_ ___| \  / | __ _  ___| |__  _ _ __   ___</span>
67 <span class="c1">#  \___ \| __/ _` | __/ _ \ |\/| |/ _` |/ __| &#39;_ \| | &#39;_ \ / _ \</span>
68 <span class="c1">#  ____) | || (_| | ||  __/ |  | | (_| | (__| | | | | | | |  __/</span>
69 <span class="c1"># |_____/ \__\__,_|\__\___|_|  |_|\__,_|\___|_| |_|_|_| |_|\___|</span>
73 <div class="viewcode-block" id="StateMachine"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine">[docs]</a><span class="k">class</span> <span class="nc">StateMachine</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
74     <span class="sd">&quot;&quot;&quot;Class in charge of the description of biological or economical</span>
75 <span class="sd">    processes, modeled as Finite State Machines. The formalism</span>
76 <span class="sd">    implemented here is based on UML state machine diagrams, with</span>
77 <span class="sd">    adaptations to biology.</span>
79 <span class="sd">    &quot;&quot;&quot;</span>
80     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">machine_name</span><span class="p">,</span> <span class="n">description</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
81         <span class="sd">&quot;&quot;&quot;Build a State Machine within the specified model, based on</span>
82 <span class="sd">        the specified description (dictionary).</span>
84 <span class="sd">        &quot;&quot;&quot;</span>
85         <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
86         <span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span> <span class="o">=</span> <span class="n">machine_name</span>
87         <span class="bp">self</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">description</span><span class="p">)</span>
89     <span class="k">def</span> <span class="nf">_reset_all</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
90         <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span> <span class="o">=</span> <span class="p">{}</span>
91         <span class="bp">self</span><span class="o">.</span><span class="n">_description</span> <span class="o">=</span> <span class="p">{}</span>
92         <span class="bp">self</span><span class="o">.</span><span class="n">states</span> <span class="o">=</span> <span class="kc">None</span>
93         <span class="bp">self</span><span class="o">.</span><span class="n">graph</span> <span class="o">=</span> <span class="n">enx</span><span class="o">.</span><span class="n">MultiDiGraph</span><span class="p">()</span>
94         <span class="bp">self</span><span class="o">.</span><span class="n">stateprops</span> <span class="o">=</span> <span class="n">StateVarDict</span><span class="p">()</span>
95         <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span> <span class="o">=</span> <span class="p">{}</span>
96 <span class="c1">#        self.edge_actions = {}</span>
98 <div class="viewcode-block" id="StateMachine.parse"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.parse">[docs]</a>    <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">description</span><span class="p">):</span>
99         <span class="sd">&quot;&quot;&quot;Build the State Machine from the specified dictionary</span>
100 <span class="sd">        (expected to come from a YAML configuration file).</span>
102 <span class="sd">        &quot;&quot;&quot;</span>
103         <span class="bp">self</span><span class="o">.</span><span class="n">_reset_all</span><span class="p">()</span>
104         <span class="c1"># keep an exhaustive description</span>
105         <span class="bp">self</span><span class="o">.</span><span class="n">_description</span> <span class="o">=</span> <span class="n">description</span>
106         <span class="c1"># build the enumeration of the states</span>
107         <span class="bp">self</span><span class="o">.</span><span class="n">build_states</span><span class="p">()</span>
108         <span class="c1"># build the graph based on the states and the transitions between them</span>
109         <span class="bp">self</span><span class="o">.</span><span class="n">build_graph</span><span class="p">()</span>
110         <span class="c1"># build actions associated with the state machine (states or edges)</span>
111         <span class="bp">self</span><span class="o">.</span><span class="n">build_actions</span><span class="p">()</span></div>
113 <div class="viewcode-block" id="StateMachine.get_property"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.get_property">[docs]</a>    <span class="k">def</span> <span class="nf">get_property</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_name</span><span class="p">,</span> <span class="n">property_name</span><span class="p">):</span>
114         <span class="sd">&quot;&quot;&quot;Return the property associated to the specified state.&quot;&quot;&quot;</span>
115         <span class="k">if</span> <span class="n">state_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stateprops</span> <span class="ow">or</span>\
116            <span class="n">property_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stateprops</span><span class="p">[</span><span class="n">state_name</span><span class="p">]:</span>
117             <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">state_name</span><span class="p">][</span><span class="n">property_name</span><span class="p">]</span>\
118                 <span class="k">if</span> <span class="n">property_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">state_name</span><span class="p">]</span>\
119                    <span class="k">else</span> <span class="kc">None</span>
120         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stateprops</span><span class="p">[</span><span class="n">state_name</span><span class="p">][</span><span class="n">property_name</span><span class="p">]</span></div>
122 <div class="viewcode-block" id="StateMachine.build_states"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.build_states">[docs]</a>    <span class="k">def</span> <span class="nf">build_states</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
123         <span class="sd">&quot;&quot;&quot;Parse the description of the state machine and extract the</span>
124 <span class="sd">        existing states. States are described as list items, endowed</span>
125 <span class="sd">        with key-value properties. It is recommended to define only</span>
126 <span class="sd">        one state per list item (especially to ensure that states are</span>
127 <span class="sd">        always stored in the same order in all executions).</span>
129 <span class="sd">        Example of YAML specification:</span>
130 <span class="sd">        ------------------------------</span>
131 <span class="sd">        states:</span>
132 <span class="sd">          - S:</span>
133 <span class="sd">              name: Susceptible</span>
134 <span class="sd">              desc: &#39;non-shedder cows without antibodies&#39;</span>
135 <span class="sd">          - I+:</span>
136 <span class="sd">              name: Infectious plus</span>
137 <span class="sd">              desc: &#39;shedder cows with antibodies&#39;</span>
138 <span class="sd">              fillcolor: orange</span>
139 <span class="sd">              on_stay:</span>
140 <span class="sd">                - increase: total_E</span>
141 <span class="sd">                  rate: Q1</span>
143 <span class="sd">        &quot;&quot;&quot;</span>
144         <span class="n">states</span> <span class="o">=</span> <span class="p">[]</span>
145         <span class="n">default_state</span> <span class="o">=</span> <span class="kc">None</span>
146         <span class="c1"># retrieve information for each state</span>
147         <span class="k">for</span> <span class="n">statedict</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">[</span><span class="s1">&#39;states&#39;</span><span class="p">]:</span>
148             <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">statedict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
149                 <span class="n">states</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
150                 <span class="c1"># provide a default fillcolor</span>
151                 <span class="k">if</span> <span class="s1">&#39;fillcolor&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
152                     <span class="n">value</span><span class="p">[</span><span class="s1">&#39;fillcolor&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;lightgray&#39;</span>
153                 <span class="c1"># if properties are provided, add the corresponding</span>
154                 <span class="c1"># expression to the model</span>
155                 <span class="k">if</span> <span class="s1">&#39;properties&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
156                     <span class="n">value</span><span class="p">[</span><span class="s1">&#39;properties&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
157                 <span class="c1"># store special property: &quot;autoremove: yes&quot;</span>
158                 <span class="n">value</span><span class="p">[</span><span class="s1">&#39;properties&#39;</span><span class="p">][</span><span class="s1">&#39;autoremove&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span><span class="p">[</span><span class="s1">&#39;autoremove&#39;</span><span class="p">]</span>\
159                                                     <span class="k">if</span> <span class="s1">&#39;autoremove&#39;</span> <span class="ow">in</span> <span class="n">value</span> <span class="k">else</span> <span class="kc">False</span>
160                 <span class="c1"># store special property: &quot;default: yes&quot;</span>
161                 <span class="c1"># if several states are marked &quot;default&quot;, take the first one</span>
162                 <span class="n">value</span><span class="p">[</span><span class="s1">&#39;properties&#39;</span><span class="p">][</span><span class="s1">&#39;default&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
163                 <span class="k">if</span> <span class="p">(</span><span class="s1">&#39;default&#39;</span> <span class="ow">in</span> <span class="n">value</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="s1">&#39;default&#39;</span><span class="p">])</span> <span class="ow">and</span> <span class="p">(</span><span class="n">default_state</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">):</span>
164                     <span class="n">value</span><span class="p">[</span><span class="s1">&#39;properties&#39;</span><span class="p">][</span><span class="s1">&#39;default&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
165                     <span class="n">default_state</span> <span class="o">=</span> <span class="n">name</span>
166                 <span class="bp">self</span><span class="o">.</span><span class="n">stateprops</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
167                                          <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="p">[</span><span class="s1">&#39;properties&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
168                 <span class="c1"># store other information</span>
169                 <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
170                 <span class="c1"># and retrieve available actions if any</span>
171                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;on_enter&#39;</span><span class="p">,</span> <span class="s1">&#39;on_stay&#39;</span><span class="p">,</span> <span class="s1">&#39;on_exit&#39;</span><span class="p">]:</span>
172                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
173                         <span class="bp">self</span><span class="o">.</span><span class="n">_add_state_actions</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">keyword</span><span class="p">,</span> <span class="n">value</span><span class="p">[</span><span class="n">keyword</span><span class="p">])</span>
174         <span class="c1"># build the enumeration of the states</span>
175         <span class="bp">self</span><span class="o">.</span><span class="n">states</span> <span class="o">=</span> <span class="n">EmulsionEnum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="o">.</span><span class="n">capitalize</span><span class="p">(),</span>
176                                    <span class="n">states</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="vm">__name__</span><span class="p">)</span>
177         <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">state_machine</span> <span class="o">=</span> <span class="bp">self</span>
178         <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">autoremove</span> <span class="o">=</span> <span class="kc">False</span>
180         <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="p">:</span>
181             <span class="k">if</span> <span class="n">state</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">states</span><span class="p">:</span>
182                 <span class="n">other_machine</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">states</span><span class="p">[</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
183                 <span class="k">raise</span> <span class="n">SemanticException</span><span class="p">(</span>
184                     <span class="s1">&#39;Conflict: State </span><span class="si">%s</span><span class="s1"> found in statemachines </span><span class="si">%s</span><span class="s1"> and </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
185                     <span class="p">(</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">other_machine</span><span class="p">,</span> <span class="n">state</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
186             <span class="k">if</span> <span class="n">state</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">:</span>
187                 <span class="k">raise</span> <span class="n">SemanticException</span><span class="p">(</span>
188                     <span class="s1">&#39;Conflict: State </span><span class="si">%s</span><span class="s1"> of statemachines </span><span class="si">%s</span><span class="s1"> found in parameters&#39;</span>
189                     <span class="o">%</span> <span class="p">(</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">state</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
190             <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">states</span><span class="p">[</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span>
191             <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">stateprops</span><span class="p">[</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;autoremove&#39;</span><span class="p">]:</span>
192                 <span class="n">state</span><span class="o">.</span><span class="n">autoremove</span> <span class="o">=</span> <span class="kc">True</span>
194         <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">is_default</span> <span class="o">=</span> <span class="kc">False</span>
195         <span class="k">if</span> <span class="n">default_state</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
196             <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="p">[</span><span class="n">default_state</span><span class="p">]</span><span class="o">.</span><span class="n">is_default</span> <span class="o">=</span> <span class="kc">True</span>
197             <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">default</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="p">[</span><span class="n">default_state</span><span class="p">]</span>
198             <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">available</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">default</span><span class="p">,)</span>
199         <span class="k">else</span><span class="p">:</span>
200             <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">default</span> <span class="o">=</span> <span class="kc">None</span>
201             <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">available</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">s</span><span class="o">.</span><span class="n">autoremove</span><span class="p">)</span>
202         <span class="c1"># print(self.states, self.states.default, self.states.available)</span>
204         <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_values</span><span class="p">[</span><span class="s1">&#39;_random_&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_random_state</span>
205         <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">default</span><span class="p">:</span>
206             <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_values</span><span class="p">[</span><span class="s1">&#39;_default_&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_default_state</span>
207         <span class="k">else</span><span class="p">:</span>
208             <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_values</span><span class="p">[</span><span class="s1">&#39;_default_&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_random_state</span></div>
211 <div class="viewcode-block" id="StateMachine.get_random_state"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.get_random_state">[docs]</a>    <span class="k">def</span> <span class="nf">get_random_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">caller</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
212         <span class="sd">&quot;&quot;&quot;Return a random state for this state machine.&quot;&quot;&quot;</span>
213         <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="n">state</span> <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">state</span><span class="o">.</span><span class="n">autoremove</span><span class="p">])</span></div>
215 <div class="viewcode-block" id="StateMachine.get_default_state"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.get_default_state">[docs]</a>    <span class="k">def</span> <span class="nf">get_default_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">caller</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
216         <span class="sd">&quot;&quot;&quot;Return a random state for this state machine.&quot;&quot;&quot;</span>
217         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="o">.</span><span class="n">default</span></div>
219     <span class="nd">@property</span>
220     <span class="k">def</span> <span class="nf">state_colors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
221         <span class="sd">&quot;&quot;&quot;Return a dictionary of state names associated with fill colors.&quot;&quot;&quot;</span>
222         <span class="k">return</span> <span class="p">{</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">state</span><span class="o">.</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;fillcolor&#39;</span><span class="p">]</span>
223                 <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span>
224                 <span class="k">if</span> <span class="ow">not</span> <span class="n">state</span><span class="o">.</span><span class="n">autoremove</span><span class="p">}</span>
227 <div class="viewcode-block" id="StateMachine.build_graph"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.build_graph">[docs]</a>    <span class="k">def</span> <span class="nf">build_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
228         <span class="sd">&quot;&quot;&quot;Parse the description of the state machine and extract the</span>
229 <span class="sd">        graph of the transitions between the states. Since a</span>
230 <span class="sd">        MultiDiGraph is used, each pair of nodes can be bound by</span>
231 <span class="sd">        several transitions if needed (beware the associated</span>
232 <span class="sd">        semantics).</span>
234 <span class="sd">        Example of YAML specification:</span>
235 <span class="sd">        ------------------------------</span>
236 <span class="sd">        transitions:</span>
237 <span class="sd">          - {from: S, to: I-, proba: p, cond: not_vaccinated}</span>
238 <span class="sd">          - {from: I-, to: S, proba: m}</span>
239 <span class="sd">          - {from: I-, to: I+m, proba: &#39;q*plp&#39;}</span>
240 <span class="sd">          - {from: I-, to: I+, proba: &#39;q*(1-plp)&#39;}</span>
242 <span class="sd">        &quot;&quot;&quot;</span>
243         <span class="c1"># add a node for each state</span>
244         <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="p">:</span>
245             <span class="n">name</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">name</span>
246             <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;tooltip&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">describe_state</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
247             <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">])</span>
248         <span class="c1"># build edges between states according to specified transitions</span>
249         <span class="k">if</span> <span class="s1">&#39;transitions&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">:</span>
250             <span class="bp">self</span><span class="o">.</span><span class="n">_parse_edges</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">[</span><span class="s1">&#39;transitions&#39;</span><span class="p">],</span>
251                               <span class="n">type_id</span><span class="o">=</span><span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">)</span>
252         <span class="k">if</span> <span class="s1">&#39;productions&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">:</span>
253             <span class="bp">self</span><span class="o">.</span><span class="n">_parse_edges</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">[</span><span class="s1">&#39;productions&#39;</span><span class="p">],</span>
254                               <span class="n">type_id</span><span class="o">=</span><span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">PRODUCTION</span><span class="p">)</span></div>
256     <span class="k">def</span> <span class="nf">_parse_edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">edges</span><span class="p">,</span> <span class="n">type_id</span><span class="o">=</span><span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">):</span>
257         <span class="sd">&quot;&quot;&quot;Parse the description of edges, with the difference</span>
258 <span class="sd">        transitions/productions</span>
260 <span class="sd">        &quot;&quot;&quot;</span>
261         <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="n">edges</span><span class="p">:</span>
262             <span class="n">from_</span> <span class="o">=</span> <span class="n">edge</span><span class="p">[</span><span class="s1">&#39;from&#39;</span><span class="p">]</span>
263             <span class="n">to_</span> <span class="o">=</span> <span class="n">edge</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span>
264             <span class="n">others</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">edge</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
265                       <span class="k">if</span> <span class="n">k</span> <span class="o">!=</span> <span class="s1">&#39;from&#39;</span> <span class="ow">and</span> <span class="n">k</span> <span class="o">!=</span> <span class="s1">&#39;to&#39;</span><span class="p">}</span>
266             <span class="k">for</span> <span class="n">kwd</span> <span class="ow">in</span> <span class="n">EDGE_KEYWORDS</span><span class="p">:</span>
267                 <span class="k">if</span> <span class="n">kwd</span> <span class="ow">in</span> <span class="n">others</span><span class="p">:</span>
268                     <span class="c1"># parm = pretty(sympify(others[kwd], locals=self.model._namespace))</span>
269                     <span class="n">parm</span> <span class="o">=</span> <span class="n">others</span><span class="p">[</span><span class="n">kwd</span><span class="p">]</span>
270                     <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">kwd</span><span class="p">,</span> <span class="n">parm</span><span class="p">)</span>
271             <span class="c1"># label = &#39;, &#39;.join([pretty(sympify(x, locals=self.model._namespace))</span>
272             <span class="c1">#                    for x in others.values()])</span>
273                     <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">parm</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">:</span>
274                         <span class="n">others</span><span class="p">[</span><span class="s1">&#39;labeltooltip&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">describe_parameter</span><span class="p">(</span><span class="n">parm</span><span class="p">)</span>
275                     <span class="k">else</span><span class="p">:</span>
276                         <span class="n">others</span><span class="p">[</span><span class="s1">&#39;labeltooltip&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">label</span>
277             <span class="c1"># others[&#39;labeltooltip&#39;] = &#39;, &#39;.join([self.model.describe_parameter(x)</span>
278             <span class="c1">#                                     for x in others.values()</span>
279             <span class="c1">#                                     if str(x) in self.model.parameters])</span>
280             <span class="c1"># handle conditions if any on the edge</span>
281             <span class="n">cond</span><span class="p">,</span> <span class="n">escape</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">False</span>
282             <span class="k">if</span> <span class="s1">&#39;cond&#39;</span> <span class="ow">in</span> <span class="n">others</span><span class="p">:</span>
283                 <span class="n">cond</span> <span class="o">=</span> <span class="n">others</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">]</span>
284                 <span class="n">others</span><span class="p">[</span><span class="s1">&#39;truecond&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">others</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">]</span>
285             <span class="k">if</span> <span class="p">(</span><span class="s1">&#39;escape&#39;</span> <span class="ow">in</span> <span class="n">others</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">type_id</span> <span class="o">==</span> <span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">):</span>
286                 <span class="n">cond</span> <span class="o">=</span> <span class="n">others</span><span class="p">[</span><span class="s1">&#39;escape&#39;</span><span class="p">]</span>
287                 <span class="n">escape</span> <span class="o">=</span> <span class="kc">True</span>
288             <span class="k">if</span> <span class="n">cond</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
289                 <span class="c1">### WARNING the operation below is not completely</span>
290                 <span class="c1">### safe... it is done to replace conditions of the form</span>
291                 <span class="c1">### &#39;x == y&#39; by &#39;Eq(x, y)&#39;, but it is a simple</span>
292                 <span class="c1">### substitution instead of parsing the syntax</span>
293                 <span class="c1">### tree... Thus it is *highly* recommended to express</span>
294                 <span class="c1">### conditions directly with Eq(x, y)</span>
295                 <span class="k">if</span> <span class="s1">&#39;==&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">cond</span><span class="p">):</span>
296                     <span class="n">cond</span> <span class="o">=</span> <span class="s1">&#39;Eq(</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">cond</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;==&#39;</span><span class="p">)))</span>
297                     <span class="c1"># others[&#39;label&#39;] = &#39;, &#39;.join(others.values())</span>
298             <span class="c1"># if duration specified for this state, handle it as an</span>
299             <span class="c1"># additional condition</span>
300             <span class="k">if</span> <span class="p">(</span><span class="s1">&#39;duration&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">from_</span><span class="p">])</span> <span class="ow">and</span> <span class="p">(</span><span class="n">type_id</span> <span class="o">==</span> <span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">):</span>
301                 <span class="n">duration_cond</span> <span class="o">=</span> <span class="n">make_duration_condition</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">)</span>
302                 <span class="k">if</span> <span class="n">cond</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
303                     <span class="n">cond</span> <span class="o">=</span> <span class="n">duration_cond</span>
304                 <span class="k">elif</span> <span class="n">escape</span><span class="p">:</span>
305                     <span class="n">cond</span> <span class="o">=</span> <span class="s1">&#39;AND(Not(</span><span class="si">{}</span><span class="s1">),</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">duration_cond</span><span class="p">,</span> <span class="n">cond</span><span class="p">)</span>
306                 <span class="k">else</span><span class="p">:</span>
307                     <span class="n">cond</span> <span class="o">=</span> <span class="s1">&#39;AND(</span><span class="si">{}</span><span class="s1">,</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">duration_cond</span><span class="p">,</span> <span class="n">cond</span><span class="p">)</span>
308                     <span class="c1"># print(cond)</span>
309                 <span class="n">others</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cond</span>
310             <span class="k">if</span> <span class="n">cond</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
311                 <span class="c1">## DEBUG:                print(cond, self.model._namespace)</span>
312                 <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">conditions</span><span class="p">[</span><span class="n">cond</span><span class="p">]</span> <span class="o">=</span> <span class="n">sympify</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span>
313                                                       <span class="nb">locals</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_namespace</span><span class="p">)</span>
314             <span class="c1"># handle &#39;when&#39; clause if any on the edge</span>
315             <span class="bp">self</span><span class="o">.</span><span class="n">_parse_when</span><span class="p">(</span><span class="n">others</span><span class="p">)</span>
316             <span class="c1"># handle &#39;duration&#39;, &#39;escape&#39; and &#39;condition&#39; clauses if</span>
317             <span class="c1"># any on the edge</span>
318             <span class="k">if</span> <span class="n">type_id</span> <span class="o">==</span> <span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">:</span>
319                 <span class="bp">self</span><span class="o">.</span><span class="n">_parse_conditions_durations</span><span class="p">(</span><span class="n">from_</span><span class="p">,</span> <span class="n">others</span><span class="p">)</span>
320             <span class="c1"># parse actions on cross if any</span>
321             <span class="k">if</span> <span class="p">(</span><span class="s1">&#39;on_cross&#39;</span> <span class="ow">in</span> <span class="n">others</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">type_id</span> <span class="o">==</span> <span class="n">enx</span><span class="o">.</span><span class="n">EdgeTypes</span><span class="o">.</span><span class="n">TRANSITION</span><span class="p">):</span>
322                 <span class="n">l_actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_action_list</span><span class="p">(</span><span class="n">others</span><span class="p">[</span><span class="s1">&#39;on_cross&#39;</span><span class="p">])</span>
323                 <span class="n">others</span><span class="p">[</span><span class="s1">&#39;actions&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">l_actions</span>
324             <span class="n">others</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">label</span>
325             <span class="n">others</span><span class="p">[</span><span class="s1">&#39;type_id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">type_id</span>
326             <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">from_</span><span class="p">,</span> <span class="n">to_</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">)</span>
327             <span class="c1"># register rate/proba/amount expressions in the model</span>
328             <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">EDGE_KEYWORDS</span><span class="p">:</span>
329                 <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">others</span><span class="p">:</span>
330                     <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">others</span><span class="p">[</span><span class="n">keyword</span><span class="p">])</span>
333     <span class="k">def</span> <span class="nf">_parse_when</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">edge_desc</span><span class="p">):</span>
334         <span class="sd">&quot;&quot;&quot;Parse the edge description in search for a &#39;when&#39;</span>
335 <span class="sd">        clause. This special condition is aimed at globally assessing</span>
336 <span class="sd">        a time period within the whole simulation.</span>
338 <span class="sd">        &quot;&quot;&quot;</span>
339         <span class="k">if</span> <span class="s1">&#39;when&#39;</span> <span class="ow">in</span> <span class="n">edge_desc</span><span class="p">:</span>
340             <span class="n">expression</span> <span class="o">=</span> <span class="n">sympify</span><span class="p">(</span><span class="n">edge_desc</span><span class="p">[</span><span class="s1">&#39;when&#39;</span><span class="p">],</span>
341                                  <span class="nb">locals</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_event_namespace</span><span class="p">)</span>
342             <span class="n">edge_desc</span><span class="p">[</span><span class="s1">&#39;when&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span>
343             <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_values</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">expression</span><span class="p">)]</span> <span class="o">=</span> <span class="n">make_when_condition</span><span class="p">(</span>
344                 <span class="n">expression</span><span class="p">,</span> <span class="n">modules</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">modules</span><span class="p">)</span>
346     <span class="k">def</span> <span class="nf">_parse_conditions_durations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_</span><span class="p">,</span> <span class="n">edge_desc</span><span class="p">):</span>
347         <span class="sd">&quot;&quot;&quot;Parse the edge description in search for durations,</span>
348 <span class="sd">        escapement and conditions specifications. Durations</span>
349 <span class="sd">        (&#39;duration&#39; clause )are handled as an additional condition</span>
350 <span class="sd">        (agents entering the state are given a &#39;time to live&#39; in the</span>
351 <span class="sd">        state, then they are not allowed to leave the state until</span>
352 <span class="sd">        their stay reaches that value). Escapements (&#39;escape&#39; clause)</span>
353 <span class="sd">        are also translated as a condition, allowing the agent to</span>
354 <span class="sd">        leave the state when the expression is true, only while the</span>
355 <span class="sd">        stay duration is below its nominal value.</span>
357 <span class="sd">        &quot;&quot;&quot;</span>
358         <span class="n">cond</span><span class="p">,</span> <span class="n">escape</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">False</span>
359         <span class="k">if</span> <span class="s1">&#39;cond&#39;</span> <span class="ow">in</span> <span class="n">edge_desc</span><span class="p">:</span>
360             <span class="n">cond</span> <span class="o">=</span> <span class="n">edge_desc</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">]</span>
361         <span class="k">if</span> <span class="s1">&#39;escape&#39;</span> <span class="ow">in</span> <span class="n">edge_desc</span><span class="p">:</span>
362             <span class="n">cond</span> <span class="o">=</span> <span class="n">edge_desc</span><span class="p">[</span><span class="s1">&#39;escape&#39;</span><span class="p">]</span>
363             <span class="n">escape</span> <span class="o">=</span> <span class="kc">True</span>
364         <span class="k">if</span> <span class="n">cond</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
365             <span class="c1">### WARNING the operation below is not completely</span>
366             <span class="c1">### safe... it is done to replace conditions of the form</span>
367             <span class="c1">### &#39;x == y&#39; by &#39;Eq(x, y)&#39;, but it is a simple</span>
368             <span class="c1">### substitution instead of parsing the syntax</span>
369             <span class="c1">### tree... Thus it is *highly* recommended to express</span>
370             <span class="c1">### conditions directly with Eq(x, y)</span>
371             <span class="k">if</span> <span class="s1">&#39;==&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">cond</span><span class="p">):</span>
372                 <span class="n">cond</span> <span class="o">=</span> <span class="s1">&#39;Eq(</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">cond</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;==&#39;</span><span class="p">)))</span>
373                 <span class="c1"># edge_desc[&#39;label&#39;] = &#39;, &#39;.join(edge_desc.values()) if</span>
374         <span class="c1"># duration specified for this state, handle it as an</span>
375         <span class="c1"># additional condition</span>
376         <span class="k">if</span> <span class="s1">&#39;duration&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">from_</span><span class="p">]:</span>
377             <span class="n">duration_cond</span> <span class="o">=</span> <span class="n">make_duration_condition</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">)</span>
378             <span class="k">if</span> <span class="n">cond</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
379                 <span class="n">cond</span> <span class="o">=</span> <span class="n">duration_cond</span>
380             <span class="k">elif</span> <span class="n">escape</span><span class="p">:</span>
381                 <span class="n">cond</span> <span class="o">=</span> <span class="s1">&#39;AND(Not(</span><span class="si">{}</span><span class="s1">),</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">duration_cond</span><span class="p">,</span> <span class="n">cond</span><span class="p">)</span>
382             <span class="k">else</span><span class="p">:</span>
383                 <span class="n">cond</span> <span class="o">=</span> <span class="s1">&#39;AND(</span><span class="si">{}</span><span class="s1">,</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">duration_cond</span><span class="p">,</span> <span class="n">cond</span><span class="p">)</span>
384             <span class="n">edge_desc</span><span class="p">[</span><span class="s1">&#39;cond&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cond</span>
385         <span class="k">if</span> <span class="n">cond</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
386             <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">conditions</span><span class="p">[</span><span class="n">cond</span><span class="p">]</span> <span class="o">=</span> <span class="n">sympify</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="nb">locals</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_namespace</span><span class="p">)</span>
388 <div class="viewcode-block" id="StateMachine.build_actions"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.build_actions">[docs]</a>    <span class="k">def</span> <span class="nf">build_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
389         <span class="sd">&quot;&quot;&quot;Parse the description of the state machine and extract the</span>
390 <span class="sd">        actions that agents running this state machine must have.</span>
392 <span class="sd">        Example of YAML specification:</span>
393 <span class="sd">        ------------------------------</span>
394 <span class="sd">        actions:</span>
395 <span class="sd">          say_hello:</span>
396 <span class="sd">            desc: action performed when entering the S state</span>
398 <span class="sd">        &quot;&quot;&quot;</span>
399         <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
400             <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;on_enter&#39;</span><span class="p">,</span> <span class="s1">&#39;on_stay&#39;</span><span class="p">,</span> <span class="s1">&#39;on_exit&#39;</span><span class="p">]:</span>
401                 <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
402                     <span class="bp">self</span><span class="o">.</span><span class="n">_add_state_actions</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">keyword</span><span class="p">,</span> <span class="n">value</span><span class="p">[</span><span class="n">keyword</span><span class="p">])</span>
403             <span class="k">if</span> <span class="s1">&#39;duration&#39;</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
404                 <span class="n">val</span> <span class="o">=</span> <span class="n">value</span><span class="p">[</span><span class="s1">&#39;duration&#39;</span><span class="p">]</span>
405                 <span class="bp">self</span><span class="o">.</span><span class="n">_add_state_duration_actions</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span></div>
407 <div class="viewcode-block" id="StateMachine.get_value"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.get_value">[docs]</a>    <span class="k">def</span> <span class="nf">get_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
408         <span class="sd">&quot;&quot;&quot;Return the value associated with the specified name.&quot;&quot;&quot;</span>
409         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">name</span><span class="p">)</span></div>
412     <span class="k">def</span> <span class="nf">_add_state_duration_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_name</span><span class="p">,</span> <span class="n">duration_value</span><span class="p">):</span>
413         <span class="sd">&quot;&quot;&quot;Add implicit actions to manage stay duration in the specified state</span>
414 <span class="sd">        name. The `duration_value` can be either a parameter, a</span>
415 <span class="sd">        &#39;statevar&#39; or a distribution.</span>
417 <span class="sd">        &quot;&quot;&quot;</span>
418         <span class="c1"># initialize the actions associated to the state if none</span>
419         <span class="k">if</span> <span class="n">state_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">:</span>
420             <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">state_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
421         <span class="c1"># retrieve the list of actions on enter for this state, if any</span>
422         <span class="n">lenter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">state_name</span><span class="p">][</span><span class="s1">&#39;on_enter&#39;</span><span class="p">]</span>\
423                    <span class="k">if</span> <span class="s1">&#39;on_enter&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">state_name</span><span class="p">]</span> <span class="k">else</span> <span class="p">[]</span>
424         <span class="c1"># build a partial function based on the current state machine name</span>
425         <span class="n">enter_action</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">make_duration_init_action</span><span class="p">,</span>
426                                <span class="n">machine_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">)</span>
427         <span class="c1"># set the name of the action</span>
428         <span class="n">enter_action</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="s1">&#39;init_duration&#39;</span>
429         <span class="c1"># set the action parameters (the expression associated to the duration)</span>
430         <span class="n">enter_params</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">duration_value</span><span class="p">)]</span>
431         <span class="c1"># instantiate the action</span>
432         <span class="n">init_action</span> <span class="o">=</span> <span class="n">AbstractAction</span><span class="o">.</span><span class="n">build_action</span><span class="p">(</span><span class="s1">&#39;duration&#39;</span><span class="p">,</span>
433                                                   <span class="n">function</span><span class="o">=</span><span class="n">enter_action</span><span class="p">,</span>
434                                                   <span class="n">l_params</span><span class="o">=</span><span class="n">enter_params</span><span class="p">,</span>
435                                                   <span class="n">state_machine</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
436         <span class="c1"># and insert it at the beginning of the list of actions</span>
437         <span class="n">lenter</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">init_action</span><span class="p">)</span>
438         <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_init_action</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">machine_name</span><span class="p">,</span>
439                                    <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="p">[</span><span class="n">state_name</span><span class="p">],</span>
440                                    <span class="n">init_action</span><span class="p">)</span>
441         <span class="c1"># lstay = self.state_actions[name][&#39;on_stay&#39;]\</span>
442         <span class="c1">#           if &#39;on_stay&#39; in self.state_actions[name] else []</span>
443         <span class="c1"># stay_action = partial(make_TTL_increase_action,</span>
444         <span class="c1">#                       machine_name=self.machine_name)</span>
445         <span class="c1"># stay_action.__name__ = &#39;+_time_spent&#39;</span>
446         <span class="c1"># lstay.insert(0, AbstractAction.build_action(&#39;duration&#39;,</span>
447         <span class="c1">#                                             function=stay_action,</span>
448         <span class="c1">#                                             state_machine=self))</span>
449         <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">state_name</span><span class="p">][</span><span class="s1">&#39;on_enter&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lenter</span>
450         <span class="c1"># self.state_actions[name][&#39;on_stay&#39;] = lstay</span>
452     <span class="k">def</span> <span class="nf">_add_state_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">event</span><span class="p">,</span> <span class="n">actions</span><span class="p">):</span>
453         <span class="sd">&quot;&quot;&quot;Add the specified actions for the state with the given</span>
454 <span class="sd">        name, associated with the event (e.g. &#39;on_stay&#39;, &#39;on_enter&#39;,</span>
455 <span class="sd">        &#39;on_exit&#39;). Expressions contained in the parameters lists or</span>
456 <span class="sd">        dicts are automatically expanded.</span>
458 <span class="sd">        &quot;&quot;&quot;</span>
459         <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">:</span>
460             <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
461         <span class="n">l_actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_action_list</span><span class="p">(</span><span class="n">actions</span><span class="p">)</span>
462         <span class="bp">self</span><span class="o">.</span><span class="n">state_actions</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="n">event</span><span class="p">]</span> <span class="o">=</span> <span class="n">l_actions</span>
464     <span class="k">def</span> <span class="nf">_parse_action_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">actions</span><span class="p">):</span>
465         <span class="sd">&quot;&quot;&quot;Parse the list of actions associated with a state.&quot;&quot;&quot;</span>
466         <span class="n">l_actions</span> <span class="o">=</span> <span class="p">[]</span>
467         <span class="k">for</span> <span class="n">d_action</span> <span class="ow">in</span> <span class="n">actions</span><span class="p">:</span>
468             <span class="k">if</span> <span class="s1">&#39;action&#39;</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">:</span>
469                 <span class="n">action</span> <span class="o">=</span> <span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]</span>
470                 <span class="n">l_params</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
471                             <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;l_params&#39;</span><span class="p">]]</span>\
472                                 <span class="k">if</span> <span class="s1">&#39;l_params&#39;</span> <span class="ow">in</span> <span class="n">d_action</span>\
473                                 <span class="k">else</span> <span class="p">[]</span>
474                 <span class="c1"># if &#39;d_params&#39; in d_action:</span>
475                 <span class="c1">#     print(d_action[&#39;d_params&#39;])</span>
476                 <span class="n">d_params</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
477                             <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;d_params&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>\
478                                 <span class="k">if</span> <span class="s1">&#39;d_params&#39;</span> <span class="ow">in</span> <span class="n">d_action</span>\
479                                 <span class="k">else</span> <span class="p">{}</span>
480                 <span class="n">l_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
481                     <span class="n">AbstractAction</span><span class="o">.</span><span class="n">build_action</span><span class="p">(</span><span class="s1">&#39;action&#39;</span><span class="p">,</span>
482                                                 <span class="n">method</span><span class="o">=</span><span class="n">action</span><span class="p">,</span>
483                                                 <span class="n">l_params</span><span class="o">=</span><span class="n">l_params</span><span class="p">,</span>
484                                                 <span class="n">d_params</span><span class="o">=</span><span class="n">d_params</span><span class="p">,</span>
485                                                 <span class="n">state_machine</span><span class="o">=</span><span class="bp">self</span><span class="p">))</span>
486             <span class="k">else</span><span class="p">:</span> <span class="c1">#TODO: dispatch through AbstractAction (factory),</span>
487                   <span class="c1">#make subclasses responsible for parameter parsing</span>
488                 <span class="n">understood</span> <span class="o">=</span> <span class="kc">False</span>
489                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;increase&#39;</span><span class="p">,</span> <span class="s1">&#39;decrease&#39;</span><span class="p">,</span>
490                                 <span class="s1">&#39;increase_stoch&#39;</span><span class="p">,</span> <span class="s1">&#39;decrease_stoch&#39;</span><span class="p">]:</span>
491                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">:</span>
492                         <span class="c1"># assume that increase statevar with rate</span>
493                         <span class="n">l_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
494                             <span class="n">AbstractAction</span><span class="o">.</span><span class="n">build_action</span><span class="p">(</span>
495                                 <span class="n">keyword</span><span class="p">,</span>
496                                 <span class="n">statevar_name</span><span class="o">=</span><span class="n">d_action</span><span class="p">[</span><span class="n">keyword</span><span class="p">],</span>
497                                 <span class="n">parameter</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;rate&#39;</span><span class="p">]),</span>
498                                 <span class="n">delta_t</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">delta_t</span><span class="p">,</span>
499                                 <span class="n">state_machine</span><span class="o">=</span><span class="bp">self</span>
500                             <span class="p">)</span>
501                         <span class="p">)</span>
502                         <span class="n">understood</span> <span class="o">=</span> <span class="kc">True</span>
503                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;set_var&#39;</span><span class="p">]:</span>
504                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">:</span>
505                         <span class="c1"># assume that increase statevar with rate</span>
506                         <span class="n">l_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
507                             <span class="n">AbstractAction</span><span class="o">.</span><span class="n">build_action</span><span class="p">(</span>
508                                 <span class="n">keyword</span><span class="p">,</span>
509                                 <span class="n">statevar_name</span><span class="o">=</span><span class="n">d_action</span><span class="p">[</span><span class="n">keyword</span><span class="p">],</span>
510                                 <span class="n">parameter</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]),</span>
511                                 <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span>
512                             <span class="p">)</span>
513                         <span class="p">)</span>
514                         <span class="n">understood</span> <span class="o">=</span> <span class="kc">True</span>
515                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;become&#39;</span><span class="p">,</span> <span class="s1">&#39;clone&#39;</span><span class="p">,</span> <span class="s1">&#39;produce_offspring&#39;</span><span class="p">]:</span>
516                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">:</span>
517                         <span class="n">amount</span> <span class="o">=</span> <span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;amount&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="s1">&#39;amount&#39;</span> <span class="ow">in</span> <span class="n">d_action</span> <span class="k">else</span> <span class="kc">None</span>
518                         <span class="n">probas</span> <span class="o">=</span> <span class="n">d_action</span><span class="p">[</span><span class="s1">&#39;proba&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="s1">&#39;proba&#39;</span> <span class="ow">in</span> <span class="n">d_action</span> <span class="k">else</span> <span class="kc">None</span>
519                         <span class="n">l_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
520                             <span class="n">AbstractAction</span><span class="o">.</span><span class="n">build_action</span><span class="p">(</span>
521                                 <span class="n">keyword</span><span class="p">,</span>
522                                 <span class="n">prototypes</span><span class="o">=</span><span class="n">d_action</span><span class="p">[</span><span class="n">keyword</span><span class="p">],</span>
523                                 <span class="n">amount</span> <span class="o">=</span> <span class="n">amount</span><span class="p">,</span>
524                                 <span class="n">probas</span> <span class="o">=</span> <span class="n">probas</span><span class="p">,</span>
525                                 <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
526                             <span class="p">)</span>
527                         <span class="p">)</span>
528                         <span class="n">understood</span> <span class="o">=</span> <span class="kc">True</span>
529                 <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;message&#39;</span><span class="p">]:</span>
530                     <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">d_action</span><span class="p">:</span>
531                         <span class="n">l_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
532                             <span class="n">AbstractAction</span><span class="o">.</span><span class="n">build_action</span><span class="p">(</span>
533                                 <span class="n">keyword</span><span class="p">,</span>
534                                 <span class="n">parameter</span><span class="o">=</span><span class="n">d_action</span><span class="p">[</span><span class="n">keyword</span><span class="p">]</span>
535                             <span class="p">)</span>
536                         <span class="p">)</span>
537                         <span class="n">understood</span> <span class="o">=</span> <span class="kc">True</span>
538                 <span class="k">if</span> <span class="ow">not</span> <span class="n">understood</span><span class="p">:</span>
539                     <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;ERROR !!!!&#39;</span><span class="p">)</span> <span class="c1"># but there is certainly a fatal error !</span>
540                     <span class="nb">print</span><span class="p">(</span><span class="n">d_action</span><span class="p">)</span>
541         <span class="k">return</span> <span class="n">l_actions</span>
546     <span class="c1">#----------------------------------------------------------------</span>
547     <span class="c1"># Output facilities</span>
549 <div class="viewcode-block" id="StateMachine.describe_state"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.describe_state">[docs]</a>    <span class="k">def</span> <span class="nf">describe_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
550         <span class="sd">&quot;&quot;&quot;Return the description of the state with the specified</span>
551 <span class="sd">        name.</span>
553 <span class="sd">        &quot;&quot;&quot;</span>
554         <span class="n">desc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
555         <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> (</span><span class="si">{}</span><span class="s2">):</span><span class="se">\n\t</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">desc</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span> <span class="n">desc</span><span class="p">[</span><span class="s1">&#39;desc&#39;</span><span class="p">])</span></div>
557 <div class="viewcode-block" id="StateMachine.write_dot"><a class="viewcode-back" href="../../../emulsion.model.html#emulsion.model.state_machines.StateMachine.write_dot">[docs]</a>    <span class="k">def</span> <span class="nf">write_dot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">view_actions</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
558         <span class="sd">&quot;&quot;&quot;Write the graph of the current state machine in the</span>
559 <span class="sd">        specified filename, according to the dot/graphviz format.</span>
561 <span class="sd">        &quot;&quot;&quot;</span>
563         <span class="n">rankdir</span> <span class="o">=</span> <span class="s2">&quot;LR&quot;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">edges</span><span class="p">()</span> <span class="k">else</span> <span class="s2">&quot;TB&quot;</span>
564         <span class="n">output</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;digraph {</span>
565 <span class="s1">          charset=&quot;utf-8&quot;</span>
566 <span class="s1">        &#39;&#39;&#39;</span>
567         <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39;&#39;&#39;</span><span class="se">\t</span><span class="s1">rankdir=</span><span class="si">{}</span><span class="s1">;</span>
568 <span class="s1">        &#39;&#39;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">rankdir</span><span class="p">)</span>
569         <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39;&#39;&#39;</span>
570 <span class="s1">        </span><span class="se">\t</span><span class="s1">node[fontsize=16, fontname=Arial, shape=box, style=&quot;filled,rounded&quot;];</span>
571 <span class="s1">        </span><span class="se">\t</span><span class="s1">edge[minlen=1.5, fontname=Times, penwidth=1.5, tailtooltip=&quot;&quot;, headtooltip=&quot;&quot;];</span>
573 <span class="s1">        &#39;&#39;&#39;</span>
574         <span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">states</span><span class="p">:</span>
575             <span class="n">name</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">name</span>
576             <span class="n">name_lab</span> <span class="o">=</span> <span class="n">name</span>
577             <span class="k">if</span> <span class="s1">&#39;duration&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">]:</span>
578                 <span class="n">name_lab</span> <span class="o">+=</span> <span class="s1">&#39;&amp;nbsp;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">CLOCK_SYMBOL</span><span class="p">)</span>
579             <span class="n">actions</span> <span class="o">=</span> <span class="s1">&#39;shape=&quot;Mrecord&quot;, label=&quot;</span><span class="si">{}</span><span class="s1">&quot;, &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name_lab</span><span class="p">)</span>
580             <span class="n">nodestyle</span> <span class="o">=</span> <span class="s2">&quot;filled,rounded&quot;</span>
581             <span class="k">if</span> <span class="n">state</span><span class="o">.</span><span class="n">is_default</span><span class="p">:</span>
582                 <span class="n">nodestyle</span> <span class="o">+=</span> <span class="s2">&quot;,bold&quot;</span>
583             <span class="k">if</span> <span class="n">state</span><span class="o">.</span><span class="n">autoremove</span><span class="p">:</span>
584                 <span class="n">nodestyle</span> <span class="o">+=</span> <span class="s2">&quot;,dotted&quot;</span>
585             <span class="k">if</span> <span class="n">view_actions</span><span class="p">:</span>
586                 <span class="n">onenter</span> <span class="o">=</span> <span class="n">ACTION_SYMBOL</span><span class="o">+</span><span class="s1">&#39;|&#39;</span>\
587                           <span class="k">if</span> <span class="s1">&#39;on_enter&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
588                 <span class="n">onstay</span> <span class="o">=</span> <span class="s1">&#39;|&#39;</span><span class="o">+</span><span class="n">ACTION_SYMBOL</span>\
589                          <span class="k">if</span> <span class="s1">&#39;on_stay&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
590                 <span class="n">onexit</span> <span class="o">=</span> <span class="s1">&#39;|&#39;</span><span class="o">+</span><span class="n">ACTION_SYMBOL</span>\
591                          <span class="k">if</span> <span class="s1">&#39;on_exit&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
592                 <span class="k">if</span> <span class="n">onenter</span> <span class="ow">or</span> <span class="n">onstay</span> <span class="ow">or</span> <span class="n">onexit</span><span class="p">:</span>
593                     <span class="n">actions</span> <span class="o">=</span> <span class="s1">&#39;shape=&quot;Mrecord&quot;, label=&quot;{</span><span class="si">%s</span><span class="s1">{\ </span><span class="si">%s</span><span class="s1">\ </span><span class="si">%s</span><span class="s1">}</span><span class="si">%s</span><span class="s1">}&quot;, &#39;</span> <span class="o">%</span> <span class="p">(</span>
594                         <span class="n">onenter</span><span class="p">,</span> <span class="n">name_lab</span><span class="p">,</span> <span class="n">onstay</span><span class="p">,</span> <span class="n">onexit</span><span class="p">)</span>
595             <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&quot;</span><span class="si">{}</span><span class="s1">&quot; [</span><span class="si">{}</span><span class="s1">tooltip=&quot;</span><span class="si">{}</span><span class="s1">&quot;, fillcolor=</span><span class="si">{}</span><span class="s1">, style=&quot;</span><span class="si">{}</span><span class="s1">&quot;] ;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
596                 <span class="n">name</span><span class="p">,</span> <span class="n">actions</span><span class="p">,</span>
597                 <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;tooltip&#39;</span><span class="p">],</span>
598                 <span class="c1"># &#39;\n\tON ENTER: {}&#39;.format(self.state_actions[name][&#39;on_enter&#39;])\</span>
599                 <span class="c1"># if onenter else &#39;&#39; +\</span>
600                 <span class="c1"># &#39;\n\tON STAY: {}&#39;.format(self.state_actions[name][&#39;on_stay&#39;])\</span>
601                 <span class="c1"># if onstay else &#39;&#39; +\</span>
602                 <span class="c1"># &#39;\n\tON EXIT: {}&#39;.format(self.state_actions[name][&#39;on_exit&#39;])\</span>
603                 <span class="c1"># if onexit else &#39;&#39;,</span>
604                 <span class="bp">self</span><span class="o">.</span><span class="n">_statedesc</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;fillcolor&#39;</span><span class="p">],</span>
605                 <span class="n">nodestyle</span><span class="p">)</span>
606         <span class="k">for</span> <span class="n">from_</span><span class="p">,</span> <span class="n">to_</span> <span class="ow">in</span> <span class="n">SortedSet</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">edges</span><span class="p">()):</span>
607             <span class="k">for</span> <span class="n">desc</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">edge</span><span class="p">[</span><span class="n">from_</span><span class="p">][</span><span class="n">to_</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
608                 <span class="n">edgetip</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
609                 <span class="n">tail</span> <span class="o">=</span> <span class="s1">&#39;none&#39;</span>
610                 <span class="k">if</span> <span class="s1">&#39;when&#39;</span> <span class="ow">in</span> <span class="n">desc</span><span class="p">:</span>
611                     <span class="n">tail</span> <span class="o">+=</span> <span class="n">WHEN_SYMBOL</span>
612                     <span class="n">edgetip</span> <span class="o">+=</span> <span class="s1">&#39;WHEN: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">desc</span><span class="p">[</span><span class="s1">&#39;when&#39;</span><span class="p">])</span>
613                 <span class="k">if</span> <span class="s1">&#39;escape&#39;</span> <span class="ow">in</span> <span class="n">desc</span><span class="p">:</span>
614                     <span class="n">tail</span> <span class="o">+=</span> <span class="n">ESCAPE_SYMBOL</span>
615                     <span class="n">edgetip</span> <span class="o">+=</span> <span class="s1">&#39;ESCAPE: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">desc</span><span class="p">[</span><span class="s1">&#39;escape&#39;</span><span class="p">])</span>
616                 <span class="k">if</span> <span class="s1">&#39;truecond&#39;</span> <span class="ow">in</span> <span class="n">desc</span><span class="p">:</span>
617                     <span class="n">tail</span> <span class="o">+=</span> <span class="n">COND_SYMBOL</span>
618                     <span class="n">edgetip</span> <span class="o">+=</span> <span class="s1">&#39;COND: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">desc</span><span class="p">[</span><span class="s1">&#39;truecond&#39;</span><span class="p">])</span>
619                 <span class="n">head</span> <span class="o">=</span> <span class="s1">&#39;normalnone&#39;</span>
620                 <span class="k">if</span> <span class="s1">&#39;on_cross&#39;</span> <span class="ow">in</span> <span class="n">desc</span><span class="p">:</span>
621                     <span class="n">head</span> <span class="o">+=</span> <span class="n">CROSS_SYMBOL</span>
622                     <span class="c1"># edgetip += &#39;ON CROSS: {}\\n&#39;.format(desc[&#39;on_cross&#39;])</span>
623                 <span class="n">output</span> <span class="o">+=</span> <span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&quot;</span><span class="si">{}</span><span class="s1">&quot; -&gt; &quot;</span><span class="si">{}</span><span class="s1">&quot; [label=&quot;</span><span class="si">{}</span><span class="s1">&quot;, labeltooltip=&quot;</span><span class="si">{}</span><span class="s1">&quot;, &#39;</span>
624                            <span class="s1">&#39;arrowtail=&quot;</span><span class="si">{}</span><span class="s1">&quot;, arrowhead=&quot;</span><span class="si">{}</span><span class="s1">&quot;, dir=both, &#39;</span>
625                            <span class="s1">&#39;tooltip=&quot;</span><span class="si">{}</span><span class="s1">&quot;, minlen=3, style=&quot;</span><span class="si">{}</span><span class="s1">&quot;];</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
626                                <span class="n">from_</span><span class="p">,</span> <span class="n">to_</span><span class="p">,</span> <span class="n">desc</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">],</span> <span class="n">desc</span><span class="p">[</span><span class="s1">&#39;labeltooltip&#39;</span><span class="p">],</span>
627                                <span class="n">tail</span><span class="p">,</span> <span class="n">head</span><span class="p">,</span> <span class="n">edgetip</span><span class="p">,</span> <span class="n">desc</span><span class="p">[</span><span class="s1">&#39;type_id&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">linestyle</span><span class="p">)</span>
628         <span class="n">output</span> <span class="o">+=</span> <span class="s1">&#39;}&#39;</span>
629         <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
630             <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">output</span><span class="p">)</span></div></div>
631 </pre></div>
633           </div>
634         </div>
635       </div>
636       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
637         <div class="sphinxsidebarwrapper">
638 <h1 class="logo"><a href="../../../index.html">EMULSION</a></h1>
642 <p class="blurb">Epidemiological Multi-Level Simulation Framework</p>
649 <h3>Navigation</h3>
650 <ul>
651 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Install.html">Installation</a></li>
652 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Getting_started.html">Getting started with EMULSION</a></li>
653 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_principles.html">Modelling principles</a></li>
654 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_language_basics.html">Modelling language (basics)</a></li>
655 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_language_advanced.html">Modelling language (advanced)</a></li>
656 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Feature_examples.html">Feature examples</a></li>
657 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Information.html">Information</a></li>
658 <li class="toctree-l1"><a class="reference internal" href="../../../pages/License.html">License</a></li>
659 <li class="toctree-l1"><a class="reference internal" href="../../../emulsion_for_modellers.html">High-level functions for model designers</a></li>
660 <li class="toctree-l1"><a class="reference internal" href="../../../emulsion.html">emulsion package</a></li>
661 </ul>
663 <div class="relations">
664 <h3>Related Topics</h3>
665 <ul>
666   <li><a href="../../../index.html">Documentation overview</a><ul>
667   <li><a href="../../index.html">Module code</a><ul>
668   </ul></li>
669   </ul></li>
670 </ul>
671 </div>
672 <div id="searchbox" style="display: none" role="search">
673   <h3>Quick search</h3>
674     <div class="searchformwrapper">
675     <form class="search" action="../../../search.html" method="get">
676       <input type="text" name="q" />
677       <input type="submit" value="Go" />
678       <input type="hidden" name="check_keywords" value="yes" />
679       <input type="hidden" name="area" value="default" />
680     </form>
681     </div>
682 </div>
683 <script type="text/javascript">$('#searchbox').show(0);</script>
684         </div>
685       </div>
686       <div class="clearer"></div>
687     </div>
688     <div class="footer">
689       &copy;2016, INRA and Univ. Lille.
690       
691       |
692       Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.5</a>
693       &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
694       
695     </div>
697     
699     
700   </body>
701 </html>